#include #include #include #include "washingMachineState.h" #include "eventsManager.h" #include #define TAG "WMS" WashingMachineState wms; bool stateOn; // Indique si la machine est en route WashingMachineState traiteMessage(char* message){ cJSON *root = cJSON_Parse(message); char* json = cJSON_PrintUnformatted(root); ESP_LOGE(TAG, "%s", json); bool state = cJSON_IsTrue(cJSON_GetObjectItem(root, "state")); double timestamp = cJSON_GetNumberValue(cJSON_GetObjectItem(root, "timestamp")); bool ack = cJSON_HasObjectItem(root,"ack") && cJSON_GetNumberValue(cJSON_GetObjectItem(root,"ack")); //char dateStr[80] ; //timestampToDate(timestamp,dateStr,80); wms.etat = state?LAVEUSE_LAVAGE:LAVEUSE_ARRET; wms.depuis = timestamp; wms.ack = ack; if(wms.etat==LAVEUSE_LAVAGE){ stateOn=true; } if(wms.etat==LAVEUSE_ARRET && stateOn){ //Fin détectée ! stateOn=false; send_event(EVT_FIN_MACHINE, NULL); } cJSON_Delete(root); return wms; } void timestampToDate(double timestamp, char* dateStr, size_t dateStrSize) { time_t ts = timestamp; struct tm *pTime = localtime(&ts); strftime(dateStr, dateStrSize, "%d/%m/%Y %H:%M:%S", pTime); //printf("Date and french time : %s\n", dateStr); } WashingMachineState getEtatMachine(){ return wms; } char *duree_depuis_timestamp_formatee(time_t timestamp) { time_t maintenant = time(NULL); long diff_minutes = (maintenant - timestamp) / 60; if (diff_minutes < 0) diff_minutes = 0; // sécurité si timestamp futur char *resultat = malloc(32); if (!resultat) return NULL; if (diff_minutes < 60) { snprintf(resultat, 32, "%ldmn", diff_minutes); } else { long heures = diff_minutes / 60; long minutes = diff_minutes % 60; snprintf(resultat, 32, "%ldh%02ld", heures, minutes); } return resultat; } void getEtatMachineStr(WashingMachineState wms, char* etat, size_t etatSize) { char* etatStr; switch (wms.etat) { case LAVEUSE_ARRET: /* code */ etatStr="Machine arretée"; break; case LAVEUSE_LAVAGE: /* code */ etatStr="Machine en route"; break; default: etatStr="Etat inconnu"; break; } char dateStr[30]; timestampToDate(wms.depuis,dateStr,30); //Si la machine est en route on ajoute la durée en heures/minutes depuis le départ if(wms.etat==LAVEUSE_LAVAGE){ char *duree = duree_depuis_timestamp_formatee(wms.depuis); if (duree) { printf("Durée : %s\n", duree); snprintf(etat,etatSize,"%s depuis %s\n (%s)", etatStr, duree, dateStr); free(duree); } }else{ snprintf(etat,etatSize,"%s depuis %s", etatStr, dateStr); } ESP_LOGE(TAG,"%s",etat); } void acknoledge(){ wms.ack=true; }