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:
9:a58c04da4476
Parent:
8:51a0ca9079ca
Child:
10:1dc69e0ce7fd
--- a/emmaCode.cpp	Sat Jul 11 09:38:12 2015 +0000
+++ b/emmaCode.cpp	Mon Jul 13 04:35:56 2015 +0000
@@ -68,6 +68,7 @@
 bool gprsAvailable = false;
 bool ethConnected = false;
 bool wifiConnected = false;
+bool gprsConnected = false;
 bool useProxy = false;
 bool newCommand = false;
 string globalCommand;
@@ -80,8 +81,8 @@
     
     //read settings
     //readSetting("emmaUID");         //sd card need to be read once before working correctly
-    //emmaUID = readSetting("emmaUID");
-    emmaUID = "066eff575349896767073038";
+    emmaUID = readSetting("emmaUID");
+    //emmaUID = "066eff575349896767073038";
     DBG.printf("emmaUID:%s\r\n",emmaUID.c_str());
     //calculate hmac
     for(int i=0; i<sizeof(s); i++) {
@@ -89,20 +90,20 @@
     sprintf(s,"emma-%s",emmaUID.c_str());   
     hmac = calculateMD5(s);
     DBG.printf("hmac:%s\r\n",hmac.c_str());
-    //platformDOMAIN = readSetting("platformDOMAIN");
-    platformDOMAIN = "testdulu";
+    platformDOMAIN = readSetting("platformDOMAIN");
+    //platformDOMAIN = "testdulu";
     DBG.printf("platformDOMAIN:%s\r\n",platformDOMAIN.c_str());
-    //platformKEY = readSetting("platformKEY");
-    platformKEY = "5980e444-81dd-47ba-8222-6a40bc94fdce";
+    platformKEY = readSetting("platformKEY");
+    //platformKEY = "5980e444-81dd-47ba-8222-6a40bc94fdce";
     DBG.printf("platformKEY:%s\r\n",platformKEY.c_str());
-    //platformSECRET = readSetting("platformSECRET");
-    platformSECRET = "3ca8ec0239fda2b6d12ba1580c91a052";
+    platformSECRET = readSetting("platformSECRET");
+    //platformSECRET = "3ca8ec0239fda2b6d12ba1580c91a052";
     DBG.printf("platformSECRET:%s\r\n",platformSECRET.c_str());
-    //proxySERVER = readSetting("proxySERVER");
+    proxySERVER = readSetting("proxySERVER");
     DBG.printf("proxySERVER:%s\r\n",proxySERVER.c_str());
-    //proxyPORT = readSetting("proxyPORT");
+    proxyPORT = readSetting("proxyPORT");
     DBG.printf("proxyPORT:%s\r\n",proxyPORT.c_str());
-    //proxyAUTH = readSetting("proxyAUTH");
+    proxyAUTH = readSetting("proxyAUTH");
     DBG.printf("proxyAUTH:%s\r\n",proxyAUTH.c_str());
     
     //check proxy
@@ -117,7 +118,7 @@
     
     //check available interface
     isEthAvailable();           //check whether cable is connected
-    wifiAvailable = true;       //we assume wifi will always ready to use
+    wifiAvailable = true;       //we assume wifi will always available
     DBG.printf("eth:%d\r\n",ethAvailable);
     DBG.printf("wifi:%d\r\n",wifiAvailable);
     DBG.printf("gprs:%d\r\n",gprsAvailable);
@@ -306,7 +307,7 @@
             }
         }
     } else {
-        DBG.printf("no eth or wifi available\r\n");
+        DBG.printf("no eth or wifi is available\r\n");
     }
 }
 void emmaModeRegister(void) {
@@ -338,7 +339,126 @@
     //hmac = calculateMD5(s);
     //DBG.printf("hmac:%s\r\n",hmac.c_str());
     
-    if(wifiConnected) {
+    if(ethConnected) {
+        DBG.printf("emmaModeRegister - eth\r\n");
+        
+        //set connHost, connPort, connData
+        if(useProxy) {
+            DBG.printf("use proxy\r\n");
+            connHost = proxySERVER;
+            sscanf(proxyPORT.c_str(),"%d",&connPort);
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            sprintf(s,"GET http://%s:%d/emma/api/controller/register?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,emmaUID.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
+            connData = s;
+        } else {
+            DBG.printf("no proxy\r\n");
+            connHost = EMMA_SERVER_HOST;
+            connPort = EMMA_SERVER_PORT;
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            sprintf(s,"GET /emma/api/controller/register?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",emmaUID.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
+            connData = s;
+        }
+        
+        //register
+        while(!emmaGetRegKey) {
+            str = "";
+            str = ethGET(connHost,connPort,connData);
+            DBG.printf("rsp reg:%s\r\n",str.c_str());
+
+            //check and save platform setting
+            if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
+                str.erase(str.begin(),str.begin()+str.find("[")+1);
+                str.erase(str.begin()+str.find("]"),str.end());
+        
+                MbedJSONValue jsonValue;
+                parse(jsonValue,str.c_str());
+    
+                char *parameter[4] = {"platformDOMAIN","platformKEY","platformSECRET","registrationKey"};
+    
+                //save platform parameter
+                writeSetting(parameter[0],"()");    //sd card need to be initialized
+                for(int i=0; i<3; i++) {
+                    if(jsonValue.hasMember(parameter[i])) {
+                        string val = jsonValue[parameter[i]].get<std::string>();
+                        int st = writeSetting(parameter[i],val.c_str());
+                        if(st) {
+                            DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
+                        } else {
+                            DBG.printf("%s is not saved\r\n",parameter[i]);
+                        }
+                    }
+                }
+        
+                //get registrationKey
+                if(jsonValue.hasMember(parameter[3])) {
+                    string val = jsonValue[parameter[3]].get<std::string>();
+                    if(val.find("(") != std::string::npos && val.find(")") != std::string::npos) {
+                        val.erase(val.begin(),val.begin()+val.find("(")+1);
+                        val.erase(val.begin()+val.find(")"),val.end());
+                        regKey = val;
+                        DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str());
+                        emmaGetRegKey = true;
+                    }
+                }
+            }
+        }
+        
+        //calculate hmac
+        for(int i=0; i<sizeof(r); i++) {
+            r[i]=0; }
+        sprintf(r,"emma-%s-%s",emmaUID.c_str(),regKey.c_str());
+        hmac = calculateMD5(r);
+        DBG.printf("hmac:%s\r\n",hmac.c_str());
+        
+        //set connData
+        if(useProxy) {
+            DBG.printf("use proxy\r\n");
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            sprintf(s,"GET http://%s:%d/emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,emmaUID.c_str(),regKey.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
+            connData = s;
+        } else {
+            DBG.printf("no proxy\r\n");
+            for(int i=0; i<sizeof(s); i++) {
+                s[i]=0; }
+            sprintf(s,"GET /emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",emmaUID.c_str(),regKey.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
+            connData = s;
+        }
+        
+        //verify registration
+        while(!emmaRegistered && loop < 12){
+            str.clear();
+            str = ethGET(connHost,connPort,connData);
+            DBG.printf("rsp vrf:%s\r\n",str.c_str());
+                
+            //check verification
+            if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
+                str.erase(str.begin(),str.begin()+str.find("[")+1);
+                str.erase(str.begin()+str.find("]"),str.end());
+                    
+                MbedJSONValue jsonValue;
+                parse(jsonValue,str.c_str());
+                if(jsonValue.hasMember("user")) {
+                    string val = jsonValue["user"].get<std::string>();
+                    DBG.printf("%s is registered\r\n",val.c_str());
+                    emmaRegistered = true;
+                }
+            }
+            wait(5);
+            loop++;
+        }
+            
+        //check whether registration success
+        if(emmaRegistered) {
+            DBG.printf("registration successful\r\n");
+        } else {
+            DBG.printf("registration unsuccessful\r\n");
+        }
+        while(1);
+        
+    } else if(wifiConnected) {
         DBG.printf("emmaModeRegister - wifi\r\n");
         
         _ESP.printf("MODE=B");
@@ -478,130 +598,11 @@
             while(1);
         }
         
-    } else if(ethConnected) {
-        DBG.printf("emmaModeRegister - eth\r\n");
-        
-        //set connHost, connPort, connData
-        if(useProxy) {
-            DBG.printf("use proxy\r\n");
-            connHost = proxySERVER;
-            sscanf(proxyPORT.c_str(),"%d",&connPort);
-            for(int i=0; i<sizeof(s); i++) {
-                s[i]=0; }
-            sprintf(s,"GET http://%s:%d/emma/api/controller/register?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,emmaUID.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
-            connData = s;
-        } else {
-            DBG.printf("no proxy\r\n");
-            connHost = EMMA_SERVER_HOST;
-            connPort = EMMA_SERVER_PORT;
-            for(int i=0; i<sizeof(s); i++) {
-                s[i]=0; }
-            sprintf(s,"GET /emma/api/controller/register?uid=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",emmaUID.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
-            connData = s;
-        }
-        
-        //register
-        while(!emmaGetRegKey) {
-            str = "";
-            str = ethGET(connHost,connPort,connData);
-            DBG.printf("rsp reg:%s\r\n",str.c_str());
-
-            //check and save platform setting
-            if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
-                str.erase(str.begin(),str.begin()+str.find("[")+1);
-                str.erase(str.begin()+str.find("]"),str.end());
-        
-                MbedJSONValue jsonValue;
-                parse(jsonValue,str.c_str());
-    
-                char *parameter[4] = {"platformDOMAIN","platformKEY","platformSECRET","registrationKey"};
-    
-                //save platform parameter
-                writeSetting(parameter[0],"()");    //sd card need to be initialized
-                for(int i=0; i<3; i++) {
-                    if(jsonValue.hasMember(parameter[i])) {
-                        string val = jsonValue[parameter[i]].get<std::string>();
-                        int st = writeSetting(parameter[i],val.c_str());
-                        if(st) {
-                            DBG.printf("%s: %s is saved\r\n",parameter[i],val.c_str());
-                        } else {
-                            DBG.printf("%s is not saved\r\n",parameter[i]);
-                        }
-                    }
-                }
-        
-                //get registrationKey
-                if(jsonValue.hasMember(parameter[3])) {
-                    string val = jsonValue[parameter[3]].get<std::string>();
-                    if(val.find("(") != std::string::npos && val.find(")") != std::string::npos) {
-                        val.erase(val.begin(),val.begin()+val.find("(")+1);
-                        val.erase(val.begin()+val.find(")"),val.end());
-                        regKey = val;
-                        DBG.printf("%s: %s\r\n",parameter[3],regKey.c_str());
-                        emmaGetRegKey = true;
-                    }
-                }
-            }
-        }
-        
-        //calculate hmac
-        for(int i=0; i<sizeof(r); i++) {
-            r[i]=0; }
-        sprintf(r,"emma-%s-%s",emmaUID.c_str(),regKey.c_str());
-        hmac = calculateMD5(r);
-        DBG.printf("hmac:%s\r\n",hmac.c_str());
-        
-        //set connData
-        if(useProxy) {
-            DBG.printf("use proxy\r\n");
-            for(int i=0; i<sizeof(s); i++) {
-                s[i]=0; }
-            sprintf(s,"GET http://%s:%d/emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",EMMA_SERVER_HOST,EMMA_SERVER_PORT,emmaUID.c_str(),regKey.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
-            connData = s;
-        } else {
-            DBG.printf("no proxy\r\n");
-            for(int i=0; i<sizeof(s); i++) {
-                s[i]=0; }
-            sprintf(s,"GET /emma/api/controller/verify?uid=%s&registrationKey=%s&hmac=%s HTTP/1.0\nHost: %s\r\n\r\n",emmaUID.c_str(),regKey.c_str(),hmac.c_str(),EMMA_SERVER_HOST);
-            connData = s;
-        }
-        
-        //verify registration
-        while(!emmaRegistered && loop < 12){
-            str = "";
-            str = ethGET(connHost,connPort,connData);
-            DBG.printf("rsp vrf:%s\r\n",str.c_str());
-                
-            //check verification
-            if(str.find("[") != std::string::npos && str.find("]") != std::string::npos) {
-                str.erase(str.begin(),str.begin()+str.find("[")+1);
-                str.erase(str.begin()+str.find("]"),str.end());
-                    
-                MbedJSONValue jsonValue;
-                parse(jsonValue,str.c_str());
-                if(jsonValue.hasMember("user")) {
-                    string val = jsonValue["user"].get<std::string>();
-                    DBG.printf("%s is registered\r\n",val.c_str());
-                    emmaRegistered = true;
-                }
-            }
-            wait(5);
-            loop++;
-        }
-            
-        //check whether registration success
-        if(emmaRegistered) {
-            DBG.printf("registration successful\r\n");
-        } else {
-            DBG.printf("registration unsuccessful\r\n");
-        }
-        while(1);
-        
-    } else if(gprsAvailable) {
+    } else if(gprsConnected) {
         DBG.printf("emmaModeRegister - gprs\r\n");
         
     } else {
-        DBG.printf("no wifi, eth, or gprs found\r\n");
+        DBG.printf("no eth, wifi, or gprs is connected\r\n");
     }
 }