discof746 ethernet+lcd

Files at this revision

API Documentation at this revision

Comitter:
38domo
Date:
Mon Aug 31 17:50:33 2020 +0000
Parent:
0:8b675b2726b7
Commit message:
no modif in grove stream

Changed in this revision

GroveStreams.cpp Show annotated file Show diff for this revision Revisions of this file
GroveStreams.h Show annotated file Show diff for this revision Revisions of this file
diff -r 8b675b2726b7 -r e69854e65c88 GroveStreams.cpp
--- 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();
 }
+
diff -r 8b675b2726b7 -r e69854e65c88 GroveStreams.h
--- a/GroveStreams.h	Thu Jan 05 18:39:43 2017 +0000
+++ b/GroveStreams.h	Mon Aug 31 17:50:33 2020 +0000
@@ -23,6 +23,7 @@
 #include "NetworkAPI/tcp/socket.hpp"
 #include "LcdDiscoF746NgTracer.h"
 
+
 class GroveStreams
 {
 
@@ -30,7 +31,7 @@
     EthernetInterface _eth;
     LcdDiscoF746NgTracer* _pLcd;
 
-    const char* _domain;
+    //const char* _domain;
     uint16_t _port;
     const char* _apiKey;
     const char* _myIPAddress;  //Don't Change. Set below from DHCP. Needed by GroveStreams to verify that a device is not uploading more than once every 10s.
@@ -73,8 +74,9 @@
     * \return 0 on success
     */
     unsigned long send(const char* componentId, const char* samples);
+    unsigned long sendJPA(const char* componentId, const char* samples);
+    unsigned long sendInitDatasJPA(const char* componentId, const char* samples);
     
-
     /** Send sample data to GroveStreams. Attempts to restore dropped ethernet
     *  connections
     *
@@ -105,7 +107,9 @@
     * \return 0 on success
     */
     unsigned long send(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize);
-    
+    unsigned long sendJPA(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize);
+    unsigned long sendInitDatasJPA(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize);
+   
     /** Send sample data to GroveStreams. Does not attempt to restore dropped
     *  ethernet connections.
     *
@@ -122,6 +126,8 @@
     * \return 0 on success
     */
     unsigned long sendNoRetry(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize);
+    unsigned long sendNoRetryJPA(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize);
+    unsigned long sendNoRetryInitDatasJPA(const char* componentId, const char* samples, const char* componentName, const char* compTmplId, char* resultBuffer, size_t resultBufferSize);
 
     /** Starts ethernet.
     * Stops ethernet if it is running before starting it.
@@ -136,4 +142,4 @@
 
 };
 
-#endif /* GROVESTREAMS_H_ */
\ No newline at end of file
+#endif /* GROVESTREAMS_H_ */