discof746 ethernet+lcd+ts2

Dependents:   grove_stream_jpa_sd2 grove_stream_jpa_sd2 grove_stream_jpa_sd2-2 grove_stream_jpa_sd2-3

Revision:
1:e69854e65c88
Parent:
0:8b675b2726b7
Child:
2:b77ba7f2611b
--- a/GroveStreams.cpp	Thu Jan 05 18:39:43 2017 +0000
+++ b/GroveStreams.cpp	Mon Aug 31 17:50:33 2020 +0000
@@ -31,10 +31,13 @@
     _eth.disconnect();
 }
 
+extern char gsStreamIPdatas[];
+static char* _domain;
 int GroveStreams::init(const char* apiKey, LcdDiscoF746NgTracer* pLcd)
 {
-    _domain = "grovestreams.com";
-    _port = 80;
+
+    _domain = gsStreamIPdatas;
+    _port = 1880;
     _apiKey = apiKey;
     _myIPAddress = NULL;  //Don't Change. Set below from DHCP. Needed by GroveStreams to verify that a device is not uploading more than once every 10s.
     _pLcd = pLcd;
@@ -65,6 +68,18 @@
     return send(componentId, samples, NULL, NULL, resultBuffer, sizeof resultBuffer);
 }
 
+unsigned long GroveStreams::sendJPA(const char* componentId, const char* samples)
+{
+    char resultBuffer[256]= {0};
+    return sendJPA(componentId, samples, NULL, NULL, resultBuffer, sizeof resultBuffer);
+}
+
+unsigned long GroveStreams::sendInitDatasJPA(const char* componentId, const char* samples)
+{
+    char resultBuffer[256]= {0};
+    return sendInitDatasJPA(componentId, samples, NULL, NULL, resultBuffer, sizeof resultBuffer);
+}
+
 unsigned long GroveStreams::send(const char* componentId, const char* samples, char* resultBuffer, size_t resultBufferSize)
 {
     return send(componentId, samples, NULL, NULL, resultBuffer, resultBufferSize);
@@ -98,6 +113,63 @@
     return result;
 }
 
+unsigned long GroveStreams::sendJPA(const char* componentId, const char* samples, const char* componentName, 
+                                 const char* compTmplId, char* resultBuffer, size_t resultBufferSize)
+{
+    int failedCounter = 0;
+    int result = 1;
+
+    while (failedCounter < 3 && result != 0) {
+        
+        result = sendNoRetryJPA(componentId, samples, componentName, compTmplId, resultBuffer, resultBufferSize);
+        
+        this->printf("send result code: %d", result);
+        
+        if (result != 0) {
+            //Wait and try again
+            failedCounter++;
+            this->printf("send failed %d times", failedCounter);
+            wait(2);
+        }
+
+        if (failedCounter > 3 ) {
+            this->printf("Too many failures. Restarting Ethernet.");
+            startEthernet();
+        }
+    }
+
+    return result;
+}
+   
+
+unsigned long GroveStreams::sendInitDatasJPA(const char* componentId, const char* samples, const char* componentName, 
+                                 const char* compTmplId, char* resultBuffer, size_t resultBufferSize)
+{
+    int failedCounter = 0;
+    int result = 1;
+
+    while (failedCounter < 3 && result != 0) {
+        
+        result = sendNoRetryInitDatasJPA(componentId, samples, componentName, compTmplId, resultBuffer, resultBufferSize);
+        
+        this->printf("send result code: %d", result);
+        
+        if (result != 0) {
+            //Wait and try again
+            failedCounter++;
+            this->printf("send failed %d times", failedCounter);
+            wait(2);
+        }
+
+        if (failedCounter > 3 ) {
+            this->printf("Too many failures. Restarting Ethernet.");
+            startEthernet();
+        }
+    }
+
+    return result;
+}
+
 unsigned long GroveStreams::sendNoRetry(const char* componentId, const char* samples, const char* componentName, 
                                         const char* compTmplId, char* resultBuffer, size_t resultBufferSize)
 {
@@ -124,7 +196,129 @@
             //You may need to increase the size of sbuffer if any other char array sizes have increased
             char sbuffer[512]= {0};
 
-            sprintf(sbuffer, "PUT /api/feed?compId=%s%s%s&api_key=%s%s HTTP/1.1\r\nHost: \r\nConnection: close\r\nX-Forwarded-For:%s\r\n\r\n",
+            sprintf(sbuffer, "PUT /stm32/datas?compId=%s%s%s&api_key=%s%s HTTP/1.1\r\nHost: \r\nConnection: close\r\nX-Forwarded-For:%s\r\n\r\n",
+                    componentId, compTmplIdPart, compNamePart, _apiKey, samples, _myIPAddress);
+
+            this->printf("send sbuffer size: %d", strlen(sbuffer));
+
+            int scount = socket.write(sbuffer, strlen(sbuffer));
+            //this->printf("sent %d bytes: [%.*s]", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
+
+            //Read status line
+            
+            int rcount = socket.read(resultBuffer, resultBufferSize);
+
+            this->printf("HTTP Response size: %d bytes", rcount);
+            this->printf("HTTP Response Status:[%.*s]", strstr(resultBuffer, "\r\n")-resultBuffer, resultBuffer);
+            
+            //Only return the body
+            strcpy(resultBuffer, strstr(resultBuffer, "\r\n\r\n") + strlen("\r\n\r\n"));
+            strcat(resultBuffer, "\0");
+            //this->printf("HTTP Body: [%s]", resultBuffer);
+            
+            socket.close();
+        } else {
+
+            this->printf("Socket Connect Failed: %d", connResult);
+        }
+
+    } else {
+        this->printf("Socket Open Failed: %d", openResult);
+        socket.close();
+        return 1;
+    }
+
+    return 0;
+}
+
+unsigned long GroveStreams::sendNoRetryJPA(const char* componentId, const char* samples, const char* componentName, 
+                                        const char* compTmplId, char* resultBuffer, size_t resultBufferSize)
+{
+    //Assemble the url that is used to pass the temperature readings to GroveStreams and call it
+
+    network::tcp::Socket socket;
+
+    int openResult = socket.open();
+    if (openResult == 0) {
+
+        int connResult = socket.connect(_domain, _port);
+        if(connResult == 0) {
+
+            char compNamePart[100] = {0};
+            if (strlen(componentName) > 0) {
+                sprintf(compNamePart, "&compName=%s",componentName);
+            }
+
+            char compTmplIdPart[100] = {0};
+            if (strlen(compTmplId) > 0) {
+                sprintf(compTmplIdPart, "&compTmplId=%s",compTmplId);
+            }
+
+            //You may need to increase the size of sbuffer if any other char array sizes have increased
+            char sbuffer[512]= {0};
+
+            sprintf(sbuffer, "PUT /stm32/touches?compId=%s%s%s&api_key=%s%s HTTP/1.1\r\nHost: \r\nConnection: close\r\nX-Forwarded-For:%s\r\n\r\n",
+                    componentId, compTmplIdPart, compNamePart, _apiKey, samples, _myIPAddress);
+
+            this->printf("send sbuffer size: %d", strlen(sbuffer));
+
+            int scount = socket.write(sbuffer, strlen(sbuffer));
+            //this->printf("sent %d bytes: [%.*s]", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
+
+            //Read status line
+            
+            int rcount = socket.read(resultBuffer, resultBufferSize);
+
+            this->printf("HTTP Response size: %d bytes", rcount);
+            this->printf("HTTP Response Status:[%.*s]", strstr(resultBuffer, "\r\n")-resultBuffer, resultBuffer);
+            
+            //Only return the body
+            strcpy(resultBuffer, strstr(resultBuffer, "\r\n\r\n") + strlen("\r\n\r\n"));
+            strcat(resultBuffer, "\0");
+            //this->printf("HTTP Body: [%s]", resultBuffer);
+            
+            socket.close();
+        } else {
+
+            this->printf("Socket Connect Failed: %d", connResult);
+        }
+
+    } else {
+        this->printf("Socket Open Failed: %d", openResult);
+        socket.close();
+        return 1;
+    }
+
+    return 0;
+}
+
+unsigned long GroveStreams::sendNoRetryInitDatasJPA(const char* componentId, const char* samples, const char* componentName, 
+                                        const char* compTmplId, char* resultBuffer, size_t resultBufferSize)
+{
+    //Assemble the url that is used to pass the temperature readings to GroveStreams and call it
+
+    network::tcp::Socket socket;
+
+    int openResult = socket.open();
+    if (openResult == 0) {
+        //printf("\nConnect to socket : %s\n",_domain);
+        int connResult = socket.connect(_domain, _port);
+        if(connResult == 0) {
+
+            char compNamePart[100] = {0};
+            if (strlen(componentName) > 0) {
+                sprintf(compNamePart, "&compName=%s",componentName);
+            }
+
+            char compTmplIdPart[100] = {0};
+            if (strlen(compTmplId) > 0) {
+                sprintf(compTmplIdPart, "&compTmplId=%s",compTmplId);
+            }
+
+            //You may need to increase the size of sbuffer if any other char array sizes have increased
+            char sbuffer[512]= {0};
+
+            sprintf(sbuffer, "PUT /stm32/init?compId=%s%s%s&api_key=%s%s HTTP/1.1\r\nHost: \r\nConnection: close\r\nX-Forwarded-For:%s\r\n\r\n",
                     componentId, compTmplIdPart, compNamePart, _apiKey, samples, _myIPAddress);
 
             this->printf("send sbuffer size: %d", strlen(sbuffer));
@@ -158,8 +352,6 @@
 
     return 0;
 }
-
-
 int GroveStreams::startEthernet()
 {
     //Start or restart the Ethernet connection.
@@ -198,3 +390,4 @@
 {
     return _eth.getMACAddress();
 }
+