Test

Revision:
81:dded8c042cca
Parent:
80:5e52c5847273
--- a/source/main.cpp	Fri Feb 01 14:08:20 2019 +0000
+++ b/source/main.cpp	Mon Feb 04 12:35:18 2019 +0000
@@ -24,6 +24,12 @@
     double            ToF;       /* Tiempo de vuelo*/
 } BLEdata_t;
 
+typedef struct {
+    string           hazardousDevice;       /* MAC dispositvo peligroso */
+    string            affectedDevice;       /* MAC dispositivo afectado */
+    uint8_t                     type;       /* Tipo de evento           */
+} JSONdata_t;
+
 DigitalOut led3Test(LED3);
 DigitalOut led4BLE(LED4);
 Serial pcSerial(USBTX, USBRX); // Abrimos conexión serial con el puerto USB
@@ -31,13 +37,14 @@
 TOFService* tofService;
 
 EventQueue eventQueue;
-//Queue<EventData_t, 32> Eventqueue; WIP
+Queue<JSONdata_t, 32> JSONqueue;
 Queue<BLEdata_t, 32> BLEqueue;
-MemoryPool<BLEdata_t, 32> BLEmpool;
+Mail<BLEdata_t, 16> BLEMail;
 
 Thread threadLED(osPriorityAboveNormal1, 400);
 Thread threadSerial(osPriorityAboveNormal2, 2500);
 Thread threadBLE(osPriorityRealtime3, 2500);
+Thread threadCalculateEvent(osPriorityRealtime2, 1500);
 
 template<typename arg>
 void printLog(const char * log, arg data) {
@@ -71,6 +78,38 @@
 }
 
 /**
+ * Thread encargado de calcular el tipo de evento a enviar
+ */
+void calculateEvent() {
+    while(true) {
+        printLog("Leo evento de BLEMail\r\n");
+        osEvent evt = BLEMail.get();
+        printLog("Evento leido de BLEMail\r\n");
+        if (evt.status == osEventMail) {
+            BLEdata_t *dataIn = (BLEdata_t*) evt.value.p;
+            printLog("ToF: %s\r\n", dataIn->ToF);
+            printLog("Se obtiene puntero de BLEdata\r\n");
+            
+            JSONdata_t event;
+            printLog("Se declara objeto JSONdata_t\r\n");
+            if(dataIn->ToF > 1.0) {
+                printLog("Evento de tipo 1\r\n");
+                event.hazardousDevice = dataIn->MAC1;
+                event.affectedDevice  = dataIn->MAC2;
+                event.type            = 1; 
+            } else { // Keep Alive
+                event.type            = 0;
+                event.hazardousDevice = dataIn->MAC1;
+            }
+            BLEMail.free(dataIn);
+            printLog("Se va a escribir en la cola de JSON\r\n");
+            JSONqueue.put(&event);
+            printLog("Evento enviado a JSON\r\n");
+        }
+    }
+}
+
+/**
  * Thread encargado de enviar JSONs por el puerto serie
  */
 void sendJsonOverSerial() {
@@ -82,20 +121,18 @@
         picojson::object event;
         picojson::object extraInfo;
         
-        osEvent evt = BLEqueue.get();
+        osEvent evt = JSONqueue.get();
         if (evt.status == osEventMessage) {
-            BLEdata_t *BLEdata = (BLEdata_t*)evt.value.p;
+            JSONdata_t *data = (JSONdata_t*) evt.value.p;
 
-            BLEmpool.free(BLEdata);
-                
-            int type = rand() % 4;
-            event["idEvent"] = picojson::value(BLEdata->MAC1);
-            event["type"] = picojson::value((double) type);
-            str = "E9:ED:F4:AC:BF:8E";
-            extraInfo["hazardousDevice"] = picojson::value(str);
-            str = "D5:62:12:BF:B8:45";
+            int id = rand() % 1000;
+            event["idEvent"] = picojson::value((double) id);
+            event["type"] = picojson::value((double) data->type);
+            //str = "E9:ED:F4:AC:BF:8E";
+            extraInfo["hazardousDevice"] = picojson::value(data->hazardousDevice);
+            //str = "D5:62:12:BF:B8:45";
             
-            if(type != 0) extraInfo["affectedDevice"] = picojson::value(str);
+            if(data->type != 0) extraInfo["affectedDevice"] = picojson::value(data->affectedDevice);
             event["extraInfo"] = picojson::value(extraInfo);
             json["Event"] = picojson::value(event);
             
@@ -122,27 +159,34 @@
 
 void parseRawJSONToQueue(string JSONsource) {
     picojson::value v;
-    string err;
-    BLEdata_t* data = BLEmpool.alloc();
+    string err, MAC1, MAC2;
+    BLEdata_t *data = BLEMail.alloc();;
     
     picojson::parse(v, JSONsource.c_str(), JSONsource.c_str() + strlen(JSONsource.c_str()), &err);
     printLog("res error? %s\r\n", err);
     
+    printLog("ToF %f\r\n", v.get("TOF").get<double>());
+    
+    MAC1 = v.get("MAC1").get<string>();
+    MAC2 = v.get("MAC2").get<string>();
+    
+    printLog("MAC1 en string: %s", MAC1);
+    printLog("MAC2 en string: %s", MAC2);
     data->ToF  = v.get("TOF").get<double>();
-    data->MAC1 = v.get("MAC1").get<string>();
-    data->MAC2 = v.get("MAC2").get<string>();
+    
+    printLog("Se leen los datos del JSON\r\n");
     
     printLog("ToF = %f", data->ToF);
     printLog(" MAC1 = %s", data->MAC1);
     printLog(" MAC2 = %s\r\n", data->MAC2);
     
-    //EventQueue.put(data); WIP
+    BLEMail.put(data);
+    
+    printLog("Se introduce dato en BLEqueue\r\n");
 }
 
 void writeCharCallback(const GattWriteCallbackParams *params) {
     if(params->handle == tofService->getValueHandle()) {
-        //BLEdata_t* data = BLEmpool.alloc();
-        //BLEdata_t data;
         char toChar [TOFService::TOF_CHAR_ARRAY_SIZE];
         printLog("Data received: length = %d, data = 0x", params->len);
         for(int x=0; x < params->len; x++) {
@@ -155,7 +199,6 @@
         printLog("Cadena: %s\n\r", toChar);
         string str(toChar);
         eventQueue.call(parseRawJSONToQueue, str);
-        //BLEqueue.put(data);
     }
 }
 
@@ -211,9 +254,11 @@
     srand(time(NULL));
     threadLED.start(callback(blinkLED3));
     threadBLE.start(callback(BLEServiceManagment));
-    //threadSerial.start(callback(sendJsonOverSerial)); WIP
+    threadSerial.start(callback(sendJsonOverSerial));
+    threadCalculateEvent.start(callback(calculateEvent));
             
     threadLED.join();
     threadBLE.join();
-    //threadSerial.join();
+    threadSerial.join();
+    threadCalculateEvent.join();
 }
\ No newline at end of file