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:
- 56:f5611e58d7f9
- Parent:
- 55:63a616a99c9a
- Child:
- 57:4241a1711069
--- a/emmaCode.cpp Wed Sep 16 09:05:42 2015 +0000 +++ b/emmaCode.cpp Wed Sep 16 11:05:38 2015 +0000 @@ -1582,7 +1582,7 @@ DBG.printf("command for switch\r\n"); } else if(commandNType == "1" || commandNType == "2") { //ir&rf remote control or wifi smart plug - DBG.printf("command for remote control\r\n"); + DBG.printf("command for rc or wifi plug\r\n"); //get index of node list based on mac address int idx = NODES_INVALID; @@ -1597,52 +1597,84 @@ if(idx != NODES_INVALID) { DBG.printf("index found at %d\r\n",idx); + string nodeCmd; if(commandNType == "1"){ //get cmd string based on device type and command number - string nodeCmd; nodeCmd = readNodeCmd(commandDType,commandCmd); sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str()); } else if(commandNType == "2") { sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"13\"/><app_data on-off=\"%s\"/>\r\n",commandCmd.c_str()); } - //execute command - DBG.printf("executing command\r\n"); + if((commandNType == "1" && !nodeCmd.empty()) || commandNType == "2") { + //execute command + DBG.printf("executing command\r\n"); - trial=0; - while(1) { - 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; + trial=0; + while(1) { + 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; + } + str = ethREST(nodes[idx].ipAddr,REMOTE_TCP_PORT,s); + wait(2); + if(str.find("200 OK") != 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; + } + str.clear(); + trial++; } - str = ethREST(nodes[idx].ipAddr,REMOTE_TCP_PORT,s); + wait(2); - if(str.find("200 OK") != 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; + //send execution result + sprintf(p,"emma-%s-%s",emmaUID.c_str(),commandCmd.c_str()); + hmacCmd = calculateMD5(p); + + connBodyLen = sprintf(connBody,"{\"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()); + + sprintf(s,"POST /%s/api/controller/result HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody); + connPort = restPORT; + + trial=0; + while(1) { + if(trial>=2) { //two times trial + DBG.printf("failed to send execution result\r\n"); + break; + } + str = ethREST(restSERVER,connPort,s); + wait(2); + if(str.rfind("[{\"status\"") != std::string::npos) { + str.erase(str.begin(),str.begin()+str.rfind("[{\"status\"")); + if(str.find("\"status\":\"success\"") != std::string::npos) { + DBG.printf("success to send execution result\r\n"); + break; + } + } + str.clear(); + trial++; } - str.clear(); - trial++; - } + } } else { TFT.foreground(Red); TFT.locate(0,180); @@ -1654,36 +1686,6 @@ TFT.printf(" "); TFT.foreground(White); } - wait(2); - - //send execution result - sprintf(p,"emma-%s-%s",emmaUID.c_str(),commandCmd.c_str()); - hmacCmd = calculateMD5(p); - - connBodyLen = sprintf(connBody,"{\"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()); - - sprintf(s,"POST /%s/api/controller/result HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody); - connPort = restPORT; - - trial=0; - while(1) { - if(trial>=2) { //two times trial - DBG.printf("failed to send execution result\r\n"); - break; - } - str = ethREST(restSERVER,connPort,s); - wait(2); - if(str.rfind("[{\"status\"") != std::string::npos) { - str.erase(str.begin(),str.begin()+str.rfind("[{\"status\"")); - if(str.find("\"status\":\"success\"") != std::string::npos) { - DBG.printf("success to send execution result\r\n"); - break; - } - } - str.clear(); - trial++; - } } } } @@ -2314,7 +2316,7 @@ DBG.printf("command for switch\r\n"); } else if(commandNType == "1" || commandNType == "2") { //ir&rf remote control or wifi smart plug - DBG.printf("command for remote control\r\n"); + DBG.printf("command for rc or wifi plug\r\n"); //get node ip address based on node mac address //string nodeIP; //nodeIp = readNodeIP(commandNAddr); @@ -2334,52 +2336,85 @@ if(idx != NODES_INVALID) { DBG.printf("index found at %d\r\n",idx); + string nodeCmd; if(commandNType == "1") { //get cmd string based on device type and command number - string nodeCmd; nodeCmd = readNodeCmd(commandDType,commandCmd); sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str()); } else if(commandNType == "2") { sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"13\"/><app_data on-off=\"%s\"/>\r\n",commandCmd.c_str()); } - //execute command - DBG.printf("executing command\r\n"); + if((commandNType == "1" && !nodeCmd.empty()) || commandNType == "2") { + //execute command + DBG.printf("executing command\r\n"); - 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; + 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++; } - 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; + + wait(2); + //send execution result + 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; + } + sprintf(p,"/%s/api/controller/result",appNAME.c_str()); + rest.post(p,s); + //rest.post("/emma/api/controller/result",s); //working + 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(); + rxBuf.clear(); + trial++; } - trial++; - } + } } else { TFT.foreground(Red); TFT.locate(0,180); @@ -2391,37 +2426,6 @@ TFT.printf(" "); TFT.foreground(White); } - - wait(2); - //send execution result - 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; - } - sprintf(p,"/%s/api/controller/result",appNAME.c_str()); - rest.post(p,s); - //rest.post("/emma/api/controller/result",s); //working - 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(); - rxBuf.clear(); - trial++; - } } } } @@ -2835,7 +2839,7 @@ DBG.printf("command for switch\r\n"); } else if(commandNType == "1" || commandNType == "2") { //ir&rf remote control or wifi smart plug - DBG.printf("command for remote control\r\n"); + DBG.printf("command for rc or wifi plug\r\n"); //get index of node list based on mac address int idx = NODES_INVALID; @@ -2850,51 +2854,84 @@ if(idx != NODES_INVALID) { DBG.printf("index found at %d\r\n",idx); + string nodeCmd; if(commandNType == "1"){ //get cmd string based on device type and command number - string nodeCmd; nodeCmd = readNodeCmd(commandDType,commandCmd); sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str()); } else if(commandNType == "2") { sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"13\"/><app_data on-off=\"%s\"/>\r\n",commandCmd.c_str()); } - //execute command - DBG.printf("executing command\r\n"); + if((commandNType == "1" && !nodeCmd.empty()) || commandNType == "2") { + //execute command + DBG.printf("executing command\r\n"); - 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; + 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++; } - 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; + + //send execution result + sprintf(p,"emma-%s-%s",emmaUID.c_str(),commandCmd.c_str()); + hmacCmd = calculateMD5(p); + + connBodyLen = sprintf(connBody,"{\"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()); + + sprintf(s,"POST /%s/api/controller/result HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody); + connPort = restPORT; + + trial=0; + while(1) { + if(trial>=2) { //two times trial + DBG.printf("failed to send execution result\r\n"); + break; + } + str = gprsREST(restSERVER,connPort,s); + wait(2); + if(str.rfind("[{\"status\"") != std::string::npos) { + str.erase(str.begin(),str.begin()+str.rfind("[{\"status\"")); + if(str.find("\"status\":\"success\"") != std::string::npos) { + DBG.printf("success to send execution result\r\n"); + break; + } + } + str.clear(); + trial++; } - trial++; } } else { TFT.foreground(Red); @@ -2907,36 +2944,6 @@ TFT.printf(" "); TFT.foreground(White); } - wait(2); - - //send execution result - sprintf(p,"emma-%s-%s",emmaUID.c_str(),commandCmd.c_str()); - hmacCmd = calculateMD5(p); - - connBodyLen = sprintf(connBody,"{\"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()); - - sprintf(s,"POST /%s/api/controller/result HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody); - connPort = restPORT; - - trial=0; - while(1) { - if(trial>=2) { //two times trial - DBG.printf("failed to send execution result\r\n"); - break; - } - str = gprsREST(restSERVER,connPort,s); - wait(2); - if(str.rfind("[{\"status\"") != std::string::npos) { - str.erase(str.begin(),str.begin()+str.rfind("[{\"status\"")); - if(str.find("\"status\":\"success\"") != std::string::npos) { - DBG.printf("success to send execution result\r\n"); - break; - } - } - str.clear(); - trial++; - } } } }