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:
- 42:b32e51a374c7
- Parent:
- 41:2d023bd80a9c
- Child:
- 43:612547648ed1
--- a/emmaCode.cpp Thu Aug 20 08:45:02 2015 +0000 +++ b/emmaCode.cpp Thu Aug 20 09:52:41 2015 +0000 @@ -1734,7 +1734,7 @@ checkRxBuffer(); if(newCommand) { - DBG.printf("newCommand:\r\n%s\r\n",globalCommand.c_str()); + //DBG.printf("newCommand:\r\n%s\r\n",globalCommand.c_str()); TFT.locate(0,160); TFT.printf(" "); TFT.locate(0,160); @@ -1767,8 +1767,8 @@ if(commandNType == "0") { //switch on panel controller DBG.printf("command for switch\r\n"); } - else if(commandNType == "1") { //remote node - DBG.printf("command for remote\r\n"); + else if(commandNType == "1") { //ir&rf remote control + DBG.printf("command for remote control\r\n"); //get node ip address based on node mac address //string nodeIP; //nodeIp = readNodeIP(commandNAddr); @@ -1871,7 +1871,103 @@ break; } } - checkRxBuffer(); + //checkRxBuffer(); + trial++; + } + } else if(commandNType == "2") { //wifi smart plug + DBG.printf("command for smart plug\r\n"); + + //get index of node list based on mac address + int idx = NODES_INVALID; + for(int i=0; i<NODES_MAX; i++) { + if(!nodes[i].macAddr.compare(commandNAddr)) { + idx = i; + } + } + + //execution process + string execResult = "failed"; + if(idx != NODES_INVALID) { + DBG.printf("index found at %d\r\n",idx); + + //execute command + DBG.printf("executing command\r\n"); + sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"13\"/><app_data on-off=\"%s\"/>\r\n",commandCmd.c_str()); + + trial=0; + while(1) { + rxBuf.clear(); + if(trial>=2) { //two times trial + DBG.printf("cmd is not executed\r\n"); + TFT.foreground(Red); + TFT.locate(0,180); + TFT.printf(" "); + TFT.locate(0,180); + TFT.printf("cmd is not executed"); + wait(1); + TFT.locate(0,180); + TFT.printf(" "); + TFT.foreground(White); + break; + } + nodes[idx].restConn->get("/",s); + wait(2); + if(rxBuf.find("REST: status = 200") != std::string::npos) { + DBG.printf("cmd is executed\r\n"); + TFT.foreground(Green); + TFT.locate(0,180); + TFT.printf(" "); + TFT.locate(0,180); + TFT.printf("cmd is executed"); + wait(1); + TFT.locate(0,180); + TFT.printf(" "); + TFT.foreground(White); + execResult = "success"; + break; + } + trial++; + } + } else { + TFT.foreground(Red); + TFT.locate(0,180); + TFT.printf(" "); + TFT.locate(0,180); + TFT.printf("node is invalid"); + wait(1); + TFT.locate(0,180); + TFT.printf(" "); + TFT.foreground(White); + } + + wait(2); + //send execution result + //DBG.printf("send execution result\r\n"); + + //calculate hmacCmd + 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\",\"from\":\"%s\",\"result\":\"%s\",\"hmac\":\"%s\"}", + emmaUID.c_str(), commandNType.c_str(),commandNAddr.c_str(),commandDType.c_str(),commandCmd.c_str(),commandFrom.c_str(),execResult.c_str(),hmacCmd.c_str()); + + trial=0; + while(1) { + if(trial>=2) { //two times trial + DBG.printf("failed to send execution result\r\n"); + break; + } + rest.post("/emma/api/controller/result",s); + wait(2); + 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++; } }