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:
53:7c23208feca1
Parent:
52:cbe6d1d1efdc
Child:
55:63a616a99c9a
--- a/emmaCode.cpp	Thu Sep 10 05:41:32 2015 +0000
+++ b/emmaCode.cpp	Thu Sep 10 07:03:08 2015 +0000
@@ -1209,6 +1209,42 @@
         
         //execute last state of switches on board
         
+        //get alert threshold from server
+        connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
+        sprintf(s,"POST /%s/api/controller/threshold 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;
+        str.clear();
+        str = ethREST(restSERVER,connPort,s);
+        
+        if(str.rfind("[{\"vrmsTHL\"") != std::string::npos) {
+            DBG.printf("get threshold from server\r\n");
+            str.erase(str.begin(),str.begin()+str.rfind("[{\"vrmsTHL\"")+1);
+            str.erase(str.begin()+str.rfind("}]")+1,str.end());
+            //DBG.printf("strCrop:%s\r\n",str.c_str());
+            
+            MbedJSONValue jsonValue;
+            parse(jsonValue,str.c_str());
+            char *parameter[4] = {"vrmsTHL","vrmsTHH","wattTHL","wattTHH"};
+            
+            //check whether threshold valid
+            bool validTh = true;
+            for(int i=0; i<4; i++) {
+                validTh = validTh && jsonValue.hasMember(parameter[i]);
+            }    
+            DBG.printf("threshold validity:%d\r\n",validTh);
+            
+            if(validTh) {
+                vrmsTHL = jsonValue[parameter[0]].get<int>();
+                vrmsTHH = jsonValue[parameter[1]].get<int>();
+                wattTHL = jsonValue[parameter[2]].get<int>();
+                wattTHH = jsonValue[parameter[3]].get<int>();
+                DBG.printf("vrmsTHL:%d - vrmsTHH:%d - wattTHL:%d - wattTHH:%d\r\n",vrmsTHL,vrmsTHH,wattTHL,wattTHH);
+            }
+            
+        } else {
+            DBG.printf("no threshold from server\r\n");    
+        }
+        
         //get list of nodes from server
         connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
         sprintf(s,"POST /%s/api/controller/wifinodes HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);
@@ -1728,57 +1764,6 @@
         //check firmware update
 
         //execute last state of switches on board
-    
-        //get list of nodes from server
-        /*
-        //working
-        sprintf(s,"/emma/api/controller/remotes?uid=%s&hmac=%s",emmaUID.c_str(),hmac.c_str());
-        rest.get(s);
-        for(int i=0; i<sizeof(s); i++) {
-            s[i]=0; }
-        rest.getResponse(s,sizeof(s));
-        str = s;
-        if(str.rfind("[{\"mac\"") != std::string::npos) {
-            DBG.printf("get nodes from server\r\n");
-            str.erase(str.begin(),str.begin()+str.rfind("[{\"mac\""));
-            str.erase(str.begin()+str.rfind("}]")+2,str.end());
-            
-            MbedJSONValue jsonValue;
-            parse(jsonValue,str.c_str());
-            char *parameter[2] = {"mac","ip"};
-            
-            TFT.locate(0,40);
-            TFT.printf("                                        ");
-            TFT.locate(0,40);
-            TFT.printf("get %d nodes from server",jsonValue.size());
-            wait(0.5);
-            TFT.locate(0,40);
-            TFT.printf("                                        ");
-            
-            //check whether nodes valid
-            bool validNodes = true;
-            for(int i=0; i<jsonValue.size(); i++) {
-                for(int j=0; j<2; j++) {
-                    validNodes = validNodes && jsonValue[i].hasMember(parameter[j]);
-                }    
-            }
-            DBG.printf("nodes validity:%d\r\n",validNodes);
-            
-            if(validNodes) {
-                for(int i=0; i<jsonValue.size(); i++) {
-                    string macValue = jsonValue[i][parameter[0]].get<std::string>();
-                    string ipValue = jsonValue[i][parameter[1]].get<std::string>();
-                    nodes[i].macAddr = macValue;
-                    nodes[i].ipAddr = ipValue;
-                    DBG.printf("nodes[%d]mac:%s\r\n",i,nodes[i].macAddr.c_str());
-                    DBG.printf("nodes[%d]ip:%s\r\n",i,nodes[i].ipAddr.c_str());
-                }
-            }
-            
-        } else {
-            DBG.printf("no nodes from server\r\n");    
-        }
-        */
         
         _ESP.attach(&rxInterrupt,Serial::RxIrq);
         
@@ -1817,7 +1802,7 @@
             DBG.printf("no threshold from server\r\n");    
         }
         
-        //get list of nodes from server (post)
+        //get list of nodes from server
         sprintf(p,"/%s/api/controller/wifinodes",appNAME.c_str());
         sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
         rest.post(p,s);
@@ -2368,6 +2353,43 @@
         
         //execute last state of switches on board
         
+        //get alert threshold from server
+        connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
+        sprintf(s,"POST /%s/api/controller/threshold 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;
+        str.clear();
+        str = gprsREST(restSERVER,connPort,s);
+        DBG.printf("str:%s\r\n",str.c_str());
+        
+        if(str.rfind("[{\"vrmsTHL\"") != std::string::npos) {
+            DBG.printf("get threshold from server\r\n");
+            str.erase(str.begin(),str.begin()+str.rfind("[{\"vrmsTHL\"")+1);
+            str.erase(str.begin()+str.rfind("}]")+1,str.end());
+            //DBG.printf("strCrop:%s\r\n",str.c_str());
+            
+            MbedJSONValue jsonValue;
+            parse(jsonValue,str.c_str());
+            char *parameter[4] = {"vrmsTHL","vrmsTHH","wattTHL","wattTHH"};
+            
+            //check whether threshold valid
+            bool validTh = true;
+            for(int i=0; i<4; i++) {
+                validTh = validTh && jsonValue.hasMember(parameter[i]);
+            }    
+            DBG.printf("threshold validity:%d\r\n",validTh);
+            
+            if(validTh) {
+                vrmsTHL = jsonValue[parameter[0]].get<int>();
+                vrmsTHH = jsonValue[parameter[1]].get<int>();
+                wattTHL = jsonValue[parameter[2]].get<int>();
+                wattTHH = jsonValue[parameter[3]].get<int>();
+                DBG.printf("vrmsTHL:%d - vrmsTHH:%d - wattTHL:%d - wattTHH:%d\r\n",vrmsTHL,vrmsTHH,wattTHL,wattTHH);
+            }
+            
+        } else {
+            DBG.printf("no threshold from server\r\n");    
+        }
+        
         //get list of nodes from server
         connBodyLen = sprintf(connBody,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
         sprintf(s,"POST /%s/api/controller/wifinodes HTTP/1.0\nHost: %s\nContent-Length:%d\n\n%s\r\n\r\n",appNAME.c_str(),restSERVER.c_str(),connBodyLen,connBody);