emma controller code in production board v1
Dependencies: ADE7758_v1 Crypto DHT11 MQTT MbedJSONValueEmma SDFileSystem TFT_ILI9341 SWSPI SetRTC TFT_fonts Touch W5500Interface mbed-rtos mbed-src SoftSerial
Fork of emma_controller_energy by
Diff: emmaCode.cpp
- Revision:
- 23:fb369e171b7d
- Parent:
- 22:e18d361bf688
- Child:
- 24:5d58515ba510
--- a/emmaCode.cpp Thu Jul 23 05:21:10 2015 +0000 +++ b/emmaCode.cpp Thu Jul 23 07:15:51 2015 +0000 @@ -893,6 +893,7 @@ int trial=0; //string hmac; string hmacTime; + string hmacCmd; string str; time_t seconds; Timer t; @@ -1312,7 +1313,7 @@ sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\",\"time\":\"%s\",\"temp\":%d,\"hum\":%d,\"gas\":%d}", emmaUID.c_str(),hmacTime.c_str(),q,dTemp,dHum,dGas); - DBG.printf("dataEnvironment:\r\n%s\r\n",s); + //DBG.printf("dataEnvironment:\r\n%s\r\n",s); rest.post("/emma/api/controller/environment",s); wait(2); str = rxBuf; @@ -1497,21 +1498,33 @@ wait(2); //send execution result //DBG.printf("send execution result\r\n"); - sprintf(s,"{\"uid\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"result\":\"%s\",\"name\":\"%s\"}", - emmaUID.c_str(), commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),execResult.c_str(),commandName.c_str()); + + //calculate hmacCmd + for(int j=0; j<sizeof(p); j++) { + p[j]=0; } + sprintf(p,"emma-%s-%s",emmaUID.c_str(),commandCmd.c_str()); + hmacCmd = calculateMD5(p); + + sprintf(s,"{\"uid\":\"%s\",\"nType\":\"%s\",\"nAddr\":\"%s\",\"dType\":\"%s\",\"cmd\":\"%s\",\"name\":\"%s\",\"result\":\"%s\",\"hmac\":\"%s\"}", + emmaUID.c_str(), commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),commandName.c_str(),execResult.c_str(),hmacCmd.c_str()); trial=0; while(1) { if(trial>=2) { //two times trial - DBG.printf("send execution result failed\r\n"); + DBG.printf("failed to send execution result\r\n"); break; } - rest.post("/emma/api/controller/test",s); + rest.post("/emma/api/controller/result",s); wait(2); - if(rxBuf.find("\"status\":\"OK\"") != std::string::npos) { - DBG.printf("send execution result success\r\n"); - break; + str = rxBuf; + if(str.rfind("/result") != std::string::npos) { + str.erase(str.begin(),str.begin()+str.rfind("/result")); + if(str.find("\"status\":\"success\"") != std::string::npos) { + DBG.printf("success to send execution result\r\n"); + break; + } } + checkRxBuffer(); trial++; } }