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 Emma

Revision:
10:1dc69e0ce7fd
Parent:
9:a58c04da4476
Child:
11:2311b2d5157d
--- a/emmaCode.cpp	Mon Jul 13 04:35:56 2015 +0000
+++ b/emmaCode.cpp	Mon Jul 13 06:25:58 2015 +0000
@@ -631,8 +631,99 @@
     //hmac = calculateMD5(s);
     //DBG.printf("hmac:%s\r\n",hmac.c_str());
     
-    //operation wifi
-    if(wifiConnected) {
+    if(ethConnected) {
+        DBG.printf("emmaModeOperation - eth\r\n");
+        //DBG.printf("IP Address:%s\r\n",ipstack.getEth().getIPAddress());
+        //DBG.printf("MAC Address:%s\r\n",ipstack.getEth().getMACAddress());
+        ethMQTTAttemptConnect(&client, &ipstack);
+        //t.start();
+        DBG.printf("start\r\n");
+        while(true) {
+            //if(!ipstack.getEth().linkstatus()) {
+            //    NVIC_SystemReset();  
+            //}
+            //if(t.read_ms() > 5000) {
+            //    if(publish(&client,&ipstack) != 0)
+            //        ethMQTTAttemptConnect(&client, &ipstack);
+            //    t.reset();
+            //}
+        
+            //check for new command
+            if(newCommand) {
+                DBG.printf("newCommand\r\n");
+                //DBG.printf("globalCommand: %s\r\n",globalCommand.c_str());
+                //string qw(globCmd);
+            
+                //if(cmd.find("[") != std::string::npos && cmd.find("]") != std::string::npos) {
+                //    cmd.erase(cmd.begin(),cmd.begin()+cmd.find("[")+1);
+                //    cmd.erase(cmd.begin()+cmd.find("]"),cmd.end());
+                    //globalCommand = cmd;
+                    //newCommand = true;
+                //}
+                /*
+                MbedJSONValue jsonValue;
+                parse(jsonValue,globalCommand.c_str());
+                char *parameter[5] = {"id","nType","nAddr","dType","cmd"};
+                
+                //check if command is valid
+                bool validCommand = true;
+                for(int i=0; i<5; i++) {
+                    validCommand = validCommand && jsonValue.hasMember(parameter[i]);
+                }
+                DBG.printf("command validity:%d\r\n",validCommand);
+                
+                if(validCommand) {
+                    string commandId = jsonValue[parameter[0]].get<std::string>();
+                    string commandNType = jsonValue[parameter[1]].get<std::string>();
+                    string commandNAddr = jsonValue[parameter[2]].get<std::string>();
+                    string commandDType = jsonValue[parameter[3]].get<std::string>();
+                    string commandCmd = jsonValue[parameter[4]].get<std::string>();
+                    
+                    if(commandNType == "0") {       //switch on panel controller
+                        DBG.printf("command for switch\r\n");
+                    }
+                    else if(commandNType == "1") {  //node with mac address
+                        DBG.printf("command for node\r\n");
+                        //get node ip address based on node mac address
+                        string nodeIP = readNodeIP(commandNAddr);
+                        DBG.printf("nodeIP: %s\r\n",nodeIP.c_str());
+                        
+                        //get cmd string based on device type and command number
+                        string nodeCmd = readNodeCmd(commandDType,commandCmd);
+                        DBG.printf("nodeCmd: %s\r\n",nodeCmd.c_str());
+                        
+                        //execute command
+                        int trial=0;
+                        string execStatus="failed";
+                    
+                        while(1) {
+                            sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str());
+                            str = s;
+                            string rcv = ethGET(nodeIP,REMOTE_TCP_PORT,str);
+                            DBG.printf("response:%s\r\n",rcv.c_str());
+                            str = rcv;
+                            if(str.find("OK") != std::string::npos) {
+                                DBG.printf("cmd executed\r\n");
+                                execStatus = "success";
+                                break;    
+                            }
+                            if(trial>0) {   //two times trial
+                                DBG.printf("cmd is not executed\r\n");
+                                break;    
+                            }
+                            trial++;
+                            wait(3);
+                        }
+                    
+                        //send execution status
+                    }
+                }
+                */
+                newCommand = false;    
+            } 
+            client.yield(100);  //allow MQTT client to receive message
+        }
+    } else if(wifiConnected) {
         DBG.printf("emmaModeOperation - wifi\r\n");
     
         //do not delete code below - indicator that esp need to MODE=B and esp.ready() to work
@@ -956,101 +1047,6 @@
             osDelay(5000);    
         }
     }
-    //operation eth
-/*
-    DBG.printf("IP Address:%s\r\n",ipstack.getEth().getIPAddress());
-    DBG.printf("MAC Address:%s\r\n",ipstack.getEth().getMACAddress());
-    ethMQTTAttemptConnect(&client, &ipstack);
-    t.start();
-    DBG.printf("start\r\n");
-    while(true) {
-        if(!ipstack.getEth().linkstatus()) {
-            NVIC_SystemReset();  
-        }
-        //if(t.read_ms() > 5000) {
-        //    if(publish(&client,&ipstack) != 0)
-        //        ethMQTTAttemptConnect(&client, &ipstack);
-        //    t.reset();
-        //}
-        
-        //check for new command
-        if(newCommand) {
-            DBG.printf("newCommand\r\n");
-            DBG.printf("globalCommand: %s\r\n",globalCommand.c_str());
-            //string qw(globCmd);
-            
-            //if(cmd.find("[") != std::string::npos && cmd.find("]") != std::string::npos) {
-            //    cmd.erase(cmd.begin(),cmd.begin()+cmd.find("[")+1);
-            //    cmd.erase(cmd.begin()+cmd.find("]"),cmd.end());
-                //globalCommand = cmd;
-                //newCommand = true;
-            //}
-            
-            MbedJSONValue jsonValue;
-            parse(jsonValue,globalCommand.c_str());
-            char *parameter[5] = {"id","nType","nAddr","dType","cmd"};
-                
-            //check if command is valid
-            bool validCommand = true;
-            for(int i=0; i<5; i++) {
-                validCommand = validCommand && jsonValue.hasMember(parameter[i]);
-            }
-            DBG.printf("is command valid:%d\r\n",validCommand);
-                
-            if(validCommand) {
-                string commandId = jsonValue[parameter[0]].get<std::string>();
-                string commandNType = jsonValue[parameter[1]].get<std::string>();
-                string commandNAddr = jsonValue[parameter[2]].get<std::string>();
-                string commandDType = jsonValue[parameter[3]].get<std::string>();
-                string commandCmd = jsonValue[parameter[4]].get<std::string>();
-                    
-                if(commandNType == "0") {       //switch on panel controller
-                    DBG.printf("command for switch\r\n");
-                }
-                else if(commandNType == "1") {  //node with mac address
-                    DBG.printf("command for node\r\n");
-                    //get node ip address based on node mac address
-                    string nodeIP = readNodeIP(commandNAddr);
-                    DBG.printf("nodeIP: %s\r\n",nodeIP.c_str());
-                        
-                    //get cmd string based on device type and command number
-                    string nodeCmd = readNodeCmd(commandDType,commandCmd);
-                    DBG.printf("nodeCmd: %s\r\n",nodeCmd.c_str());
-                        
-                    //execute command
-                    int trial=0;
-                    string execStatus="failed";
-                    
-                    while(1) {
-                        sprintf(s,"<?xml version=\"1.0\" encoding=\"utf-8\"?><app_cmd cmd=\"5\" /><app_data code=\"%s\"/>\r\n",nodeCmd.c_str());
-                        str = s;
-                        string rcv = ethGET(nodeIP,REMOTE_TCP_PORT,str);
-                        DBG.printf("response:%s\r\n",rcv.c_str());
-                        str = rcv;
-                        if(str.find("OK") != std::string::npos) {
-                            DBG.printf("cmd executed\r\n");
-                            execStatus = "success";
-                            break;    
-                        }
-                        if(trial>0) {   //two times trial
-                            DBG.printf("cmd is not executed\r\n");
-                            break;    
-                        }
-                        trial++;
-                        wait(3);
-                    }
-                    
-                    //send execution status
-                }
-            }
-            
-            newCommand = false;    
-        } 
-        client.yield(100);  //allow MQTT client to receive message
-    }
-*/    
-    
-    //----------------------------------------------------------------------------------    
 }
 void emmaModeFirmwareDownload(void) {
     bool emmaGetFirmwareParam = false;
@@ -1395,23 +1391,26 @@
     //DBG.printf("Payload: %s\r\n", r);
     //string cmd = r;
     //globalCommand = cmd;
-    globalCommand = sp;
+    
+    //globalCommand = sp;
+    
     //globCmd = (char*)message.payload;
     //string globCmd((char*)message.payload);
     //std::string *sp = static_cast<std::char*>(message.payload);
     //string sp((char*)message.payload);
-    DBG.printf("String: %s\r\n",sp.c_str());
+    
+    //DBG.printf("String: %s\r\n",sp.c_str());
+    
     //globCmd = sp;
     
-    newCommand = true;
-    /*
-    if(cmd.find("[") != std::string::npos && cmd.find("]") != std::string::npos) {
-        cmd.erase(cmd.begin(),cmd.begin()+cmd.find("[")+1);
-        cmd.erase(cmd.begin()+cmd.find("]"),cmd.end());
-        globalCommand = cmd;
+    //newCommand = true;
+    if(sp.find("[") != std::string::npos && sp.find("]") != std::string::npos) {
+        sp.erase(sp.begin(),sp.begin()+sp.find("[")+1);
+        sp.erase(sp.begin()+sp.find("]"),sp.end());
+        globalCommand = sp;
         newCommand = true;
     }
-    */
+    sp.clear();
 }
 
 int ethMQTTConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) {