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:
55:63a616a99c9a
Parent:
53:7c23208feca1
Child:
56:f5611e58d7f9
--- a/emmaCode.cpp	Tue Sep 15 12:22:39 2015 +0000
+++ b/emmaCode.cpp	Wed Sep 16 09:05:42 2015 +0000
@@ -1865,6 +1865,99 @@
             DBG.printf("no nodes from server\r\n");    
         }
         
+        //get dType from server
+        sprintf(p,"/%s/api/controller/devicetypes",appNAME.c_str());
+        sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
+        rest.post(p,s);
+        wait(2);
+        str = rxBuf;
+        if(str.rfind("[{\"dtype\"") != std::string::npos) {
+            DBG.printf("get dTypes from server\r\n");
+            str.erase(str.begin(),str.begin()+str.rfind("[{\"dtype\""));
+            str.erase(str.begin()+str.rfind("}]")+2,str.end());
+            //DBG.printf("strCrop:%s\r\n",str.c_str());
+            
+            MbedJSONValue jsonValue;
+            parse(jsonValue,str.c_str());
+            char *parameter[2] = {"dtype","command"};
+            
+            DBG.printf("get %d dTypes from server\r\n",jsonValue.size());
+            
+            //check whether dType valid
+            bool validDType = true;
+            for(int i=0; i<jsonValue.size(); i++) {
+                for(int j=0; j<2; j++) {
+                    validDType = validDType && jsonValue[i].hasMember(parameter[j]);
+                }
+            }    
+            DBG.printf("dType validity:%d\r\n",validDType);
+            
+            if(validDType) {
+                for(int i=0; i<jsonValue.size(); i++) {
+                    string dTypeValue = jsonValue[i][parameter[0]].get<std::string>();
+                    int cmdLen = jsonValue[i][parameter[1]].size();
+                    
+                    //create dType directory
+                    sprintf(p,"/sd/nodeCode/%s",dTypeValue.c_str());
+                    mkdir(p,0777);
+                    
+                    //check whether each of dType's cmd and "ok.txt" are found
+                    bool cmdComplete = true;
+                    for(int j=0; j<cmdLen; j++) {
+                        string cmdKey = jsonValue[i][parameter[1]][j].get<std::string>();
+                        cmdComplete = cmdComplete && isExistNodeCmd(dTypeValue,cmdKey);    
+                    }
+                    cmdComplete = cmdComplete && isExistNodeCmd(dTypeValue,"ok");
+                    DBG.printf("isCmdComplete:%d\r\n",cmdComplete);
+                    
+                    
+                    //get cmdCode based on dType and cmdKey if cmd are not complete
+                    if(!cmdComplete) {
+                        for(int j=0; j<cmdLen; j++) {
+                            rxBuf.clear();
+                            string cmdKey = jsonValue[i][parameter[1]][j].get<std::string>();
+                            sprintf(p,"/%s/api/controller/code/%s/%s",appNAME.c_str(),dTypeValue.c_str(),cmdKey.c_str());
+                            sprintf(s,"{\"uid\":\"%s\",\"hmac\":\"%s\"}",emmaUID.c_str(),hmac.c_str());
+                            rest.post(p,s);
+                            wait(2);
+                            str = rxBuf;
+                            if(str.rfind("[{\"code\"") != std::string::npos) {
+                                DBG.printf("get cmdCode from server\r\n");
+                                str.erase(str.begin(),str.begin()+str.rfind("[{\"code\"")+1);
+                                str.erase(str.begin()+str.rfind("}]")+1,str.end());
+                                //DBG.printf("strCrop:%s\r\n",str.c_str());
+                            
+                                MbedJSONValue jsonValueA;
+                                parse(jsonValueA,str.c_str());
+                            
+                                if(jsonValueA.hasMember("code")) {
+                                    string cmdCodeValue = jsonValueA["code"].get<std::string>();
+                                
+                                    //write cmdCode to SD card
+                                    bool wrRslt = writeNodeCmd(dTypeValue,cmdKey,cmdCodeValue);
+                                    DBG.printf("write cmdKey[%s]:%d\r\n",cmdKey.c_str(),wrRslt);
+                                }
+                            }
+                        }
+                    
+                        //write "ok.txt" to SD card -> indicator that codes are complete and ok
+                        bool wrRslt = writeNodeCmd(dTypeValue,"ok","(ok)");
+                        DBG.printf("write \"ok\":%d\r\n",wrRslt);
+                    }
+                }
+            }
+        } else {
+            DBG.printf("no dType from server\r\n");    
+        }
+        
+        //check if cmd of each dType are found and "ok.txt" is found - X
+        
+        //get cmdCode based on dType and cmd - X
+        
+        //write cmdCode to SD card - X
+        
+        //write "ok.txt" to SD card - X
+        
         /*
         //working
         DBG.printf("emma: setup mqtt client\r\n");
@@ -3848,9 +3941,9 @@
         } else {
             strS = "";    
         }
+        fclose(fp);
+        free(fp);
     }
-    fclose(fp);
-    free(fp);
     return strS;    
 }
 
@@ -3866,13 +3959,11 @@
         free(fp);
         return true;
     }
-    fclose(fp);
-    free(fp);
     return false;
 }
 /*end emma settings*/
 
-/*start emma node*/
+/*start emma nodes*/
 string readNodeIP(string macAddr) {
     FILE *fp;
     signed char c;
@@ -3900,20 +3991,20 @@
         } else {
             strS = "";    
         }
+        fclose(fp);
+        free(fp);
     }
-    fclose(fp);
-    free(fp);
     return strS;
 }
 
-string readNodeCmd(string dType, string cmd) {
+string readNodeCmd(string dType, string cmdKey) {
     FILE *fp;
     signed char c;
     int i=0;
     char s[2048];
-    string strS;
+    string strS;    //default value must be empty
     
-    sprintf(s,"/sd/nodeCode/%s/%s.txt",dType.c_str(),cmd.c_str());
+    sprintf(s,"/sd/nodeCode/%s/%s.txt",dType.c_str(),cmdKey.c_str());
     
     fp = fopen(s,"r");
     memset(s,0,sizeof(s));
@@ -3933,12 +4024,41 @@
         } else {
             strS = "";    
         }
+        fclose(fp);
+        free(fp);
     }
-    fclose(fp);
-    free(fp);
     return strS;    
 }
 
+bool writeNodeCmd(string dType, string cmdKey, string cmdCode) {
+    FILE *fp;
+    char s[255];
+    
+    sprintf(s,"/sd/nodeCode/%s/%s.txt",dType.c_str(),cmdKey.c_str());
+    fp = fopen(s,"w");
+    if(fp != NULL) {
+        fprintf(fp,cmdCode.c_str());
+        fclose(fp);
+        free(fp);
+        return true;
+    }
+    return false;
+}
+
+bool isExistNodeCmd(string dType, string cmdKey) {
+    FILE *fp;
+    char s[255];
+    
+    sprintf(s,"/sd/nodeCode/%s/%s.txt",dType.c_str(),cmdKey.c_str());
+    fp = fopen(s,"r");
+    if(fp != NULL) {
+        fclose(fp);
+        free(fp);
+        return true;
+    }
+    return false;
+}
+
 string *readNodeList(void) {
     static string nd[10];   //max node
     DIR *d;
@@ -3952,10 +4072,10 @@
             q = p->d_name;
             nd[i] = q;
             i++;    
-        }    
+        }
+        closedir(d);
+        free(d);    
     }
-    closedir(d);
-    free(d);
     return nd;
 }
 
@@ -3986,7 +4106,7 @@
     }
     return temp;
 }
-/*end emma node*/
+/*end emma nodes*/
 
 /*start emma private function*/
 void isEthAvailable(void) {
@@ -4189,8 +4309,6 @@
         free(fp);
         return true;
     }
-    fclose(fp);
-    free(fp);
     return false;
 }
 
@@ -4206,8 +4324,6 @@
         free(fp);
         return true;
     }
-    fclose(fp);
-    free(fp);
     return false;
 }
 
@@ -4225,8 +4341,6 @@
         free(fp);
         return true;
     }
-    fclose(fp);
-    free(fp);
     return false;    
 }
 
@@ -4244,8 +4358,6 @@
         free(fp);
         return true;
     }
-    fclose(fp);
-    free(fp);
     return false;    
 }
 /*end emma private function*/
\ No newline at end of file