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:
- 41:2d023bd80a9c
- Parent:
- 40:77bd44f57ad3
- Child:
- 42:b32e51a374c7
--- a/emmaCode.cpp Thu Aug 20 06:15:32 2015 +0000 +++ b/emmaCode.cpp Thu Aug 20 08:45:02 2015 +0000 @@ -1744,6 +1744,146 @@ parse(jsonValue,globalCommand.c_str()); char *parameter[5] = {"from","nType","nAddr","dType","cmd"}; + DBG.printf("get %d command\r\n",jsonValue.size()); + + //processing each command + bool validCommand; + for(int i=0; i<jsonValue.size(); i++) { + //check whether command is valid + DBG.printf("\r\nprocessing cmd[%d]\r\n",i); + validCommand = true; + for(int j=0; j<5; j++) { + validCommand = validCommand && jsonValue[i].hasMember(parameter[j]); + } + DBG.printf("command validity:%d\r\n",validCommand); + + if(validCommand) { + string commandFrom = jsonValue[i][parameter[0]].get<std::string>(); + string commandNType = jsonValue[i][parameter[1]].get<std::string>(); + string commandNAddr = jsonValue[i][parameter[2]].get<std::string>(); + string commandDType = jsonValue[i][parameter[3]].get<std::string>(); + string commandCmd = jsonValue[i][parameter[4]].get<std::string>(); + + 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"); + //get node ip address based on node mac address + //string nodeIP; + //nodeIp = readNodeIP(commandNAddr); + //nodeIP = "192.168.2.15"; + //DBG.printf("nodeIP: %s\r\n",nodeIP.c_str()); + + //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); + + //get cmd string based on device type and command number + string nodeCmd; + nodeCmd = readNodeCmd(commandDType,commandCmd); + + //execute command + DBG.printf("executing command\r\n"); + sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.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 + //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\",\"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++; + } + } + } + } + //clear text on lcd + TFT.locate(0,160); + TFT.printf(" "); + + newCommand = false; + + /* //check whether command is valid bool validCommand = true; for(int i=0; i<5; i++) { @@ -1792,7 +1932,6 @@ //execute command DBG.printf("executing command\r\n"); sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str()); - DBG.printf("s:%s\r\n",s); trial=0; while(1) { @@ -1881,6 +2020,7 @@ TFT.printf(" "); newCommand = false; + */ } osDelay(5000); @@ -2259,11 +2399,21 @@ //check new command if(rxBuf.rfind("/command") != std::string::npos) { rxBuf.erase(rxBuf.begin(),rxBuf.begin()+rxBuf.rfind("/command")); - if(rxBuf.find("[{") != std::string::npos && rxBuf.find("}]") != std::string::npos) { - rxBuf.erase(rxBuf.begin(),rxBuf.begin()+rxBuf.find("[{")+1); - rxBuf.erase(rxBuf.begin()+rxBuf.find("]"),rxBuf.end()); + if(rxBuf.find("[{") != std::string::npos && rxBuf.rfind("}]") != std::string::npos) { + rxBuf.erase(rxBuf.begin(),rxBuf.begin()+rxBuf.find("[{")); + rxBuf.erase(rxBuf.begin()+rxBuf.rfind("}]")+2,rxBuf.end()); + + //start special handler + while(1){ + if(rxBuf.find("}],") != std::string::npos) { + rxBuf.erase(rxBuf.begin()+rxBuf.find("}],")+1,rxBuf.begin()+rxBuf.find("}],")+2); + } else { + break; + } + } + //end special handler + globalCommand = rxBuf; - //DBG.printf("gC:%s\r\n",globalCommand.c_str()); newCommand = true; } }