temps_reel/software/raspberry/superviseur-robot/lib/src/monitor.cpp

80 lines
2.4 KiB
C++
Raw Normal View History

2018-08-27 16:39:49 +02:00
#include "monitor.h"
2018-10-25 15:02:48 +02:00
#include "server.h"
2018-08-27 16:39:49 +02:00
using namespace std;
SOCKADDR_IN ssin;
SOCKET sock;
socklen_t recsize = sizeof (ssin);
string serverReceive(int size);
2018-10-25 15:02:48 +02:00
int sendMessage(const char *data, int dataLength);
2018-08-27 16:39:49 +02:00
int receive(char *data);
int send_message_to_monitor(const char* typeMessage, const void * data) {
if ((string) typeMessage == HEADER_STM_IMAGE) {
Jpg * imgC = (Jpg*) data;
2018-10-25 15:02:48 +02:00
sendMessage("IMG", 3);
sendMessage((const char*)imgC->data(), imgC->size());
sendMessage("TRAME", 5);
2018-08-27 16:39:49 +02:00
return 0;
} else if ((string) typeMessage == HEADER_STM_POS) {
char buffer[400];
2018-08-27 16:39:49 +02:00
Position * maPosition = (Position*) data;
sprintf(buffer, "POScenter: %3d;%3d | %.1fTRAME", maPosition->center.x, maPosition->center.y, maPosition->angle);
2018-10-25 15:02:48 +02:00
sendMessage(buffer, strlen(buffer));
2018-08-27 16:39:49 +02:00
return 0;
} else if ((string) typeMessage == HEADER_STM_MES) {
char buffer[50];
sprintf(buffer, "MSG%sTRAME", (const char*) data);
2018-10-25 15:02:48 +02:00
sendMessage(buffer, strlen(buffer));
2018-08-27 16:39:49 +02:00
return 0;
} else if ((string) typeMessage == HEADER_STM_ACK) {
char buffer[50];
sprintf(buffer, "ACK%sTRAME", (const char*) data);
2018-10-25 15:02:48 +02:00
sendMessage(buffer, strlen(buffer));
2018-08-27 16:39:49 +02:00
return 0;
} else if ((string) typeMessage == HEADER_STM_BAT) {
char buffer[50];
sprintf(buffer, "BAT%sTRAME", (const char*) data);
2018-10-25 15:02:48 +02:00
sendMessage(buffer, strlen(buffer));
2018-08-27 16:39:49 +02:00
return 0;
} else if ((string) typeMessage == HEADER_STM_NO_ACK) {
char buffer[50];
sprintf(buffer, "NAK%sTRAME", (const char*) data);
2018-10-25 15:02:48 +02:00
sendMessage(buffer, strlen(buffer));
2018-08-27 16:39:49 +02:00
return 0;
} else if ((string) typeMessage == HEADER_STM_LOST_DMB) {
char buffer[50];
sprintf(buffer, "LCD%sTRAME", (const char*) data);
2018-10-25 15:02:48 +02:00
sendMessage(buffer, strlen(buffer));
2018-08-27 16:39:49 +02:00
return 0;
} else {
return -1;
}
}
int receive_message_from_monitor(char *typeMessage, char *data) {
char buffer[20];
int tBuffer = receive(buffer);
sscanf(buffer, "%3s:%s", typeMessage, data);
return tBuffer;
}
int receive(char *data) {
int result;
2018-10-25 15:02:48 +02:00
result = receiveDataFromServer(data, 20);
2018-08-27 16:39:49 +02:00
//cout <<"Data : " << data << endl;
return result;
}
2018-10-25 15:02:48 +02:00
int sendMessage(const char *data, int dataLength) {
if (sendDataToServer((char*)data, dataLength) != dataLength) {
2018-08-27 16:39:49 +02:00
perror("Send failed : ");
return false;
}
return true;
}