Separate library that holds helper functions for the main OMF code.

Revision:
18:c6c9cce5d990
Parent:
17:cc05a00e6ee6
Child:
19:9b357e31f61d
--- a/osisoft-omf.cpp	Mon Jun 11 14:17:30 2018 +0000
+++ b/osisoft-omf.cpp	Mon Jun 25 21:09:08 2018 +0000
@@ -110,62 +110,97 @@
 // Helper function that sends an actual web request; does not reuse sockets
 // ************************************************************************
 
-void OMFLib_sendMessageToEndpoint_NoSocketReuse(NetworkInterface* network, const char* action, const char* message_type, const char* body) {
+#if ENABLE_PRINTOUTS == YES_ENABLE_PRINTOUTS
+    void OMFLib_sendMessageToEndpoint_NoSocketReuse(NetworkInterface* network, const char* action, const char* message_type, const char* body) {
+        
+        printf("\n\r----- HTTPS POST request -----\n\r");
+        
+        // Create the new request
+        HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, TARGET_URL);
     
-    if (ENABLE_PRINTOUTS) printf("\n\r----- HTTPS POST request -----\n\r");
+        // Turn on debugging - this hides TLS connection information
+        //post_req->set_debug(true);
     
-    // Create the new request
-    HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, TARGET_URL);
-
-    // Turn on debugging - this hides TLS connection information
-    //post_req->set_debug(true);
-
-    // Add headers: content type 
-    post_req->set_header("Content-Type", CONTENT_TYPE);
-
-    // Set OMF headers
-    post_req->set_header("producertoken", PRODUCER_TOKEN);
-    post_req->set_header("messagetype", message_type);
-    post_req->set_header("action", action);
-    post_req->set_header("messageformat", OMF_MESSAGE_FORMAT);
-    post_req->set_header("omfversion", OMF_VERSION);
+        // Add headers: content type 
+        post_req->set_header("Content-Type", CONTENT_TYPE);
+    
+        // Set OMF headers
+        post_req->set_header("producertoken", PRODUCER_TOKEN);
+        post_req->set_header("messagetype", message_type);
+        post_req->set_header("action", action);
+        post_req->set_header("messageformat", OMF_MESSAGE_FORMAT);
+        post_req->set_header("omfversion", OMF_VERSION);
+            
+        // Send the body
+        printf("Now sending request... ");
+        //printf("\n\rOutgoing message headers:\n\tmessagetype: %s\n\taction: %s", message_type, action);
+        //printf("\n\rOutgoing message body:\n\t%s\n\r", body);
+        HttpResponse* post_res = post_req->send(body, strlen(body));
         
-    // Send the body
-    if (ENABLE_PRINTOUTS) printf("Now sending request... ");
-    //printf("\n\rOutgoing message headers:\n\tmessagetype: %s\n\taction: %s", message_type, action);
-    //printf("\n\rOutgoing message body:\n\t%s\n\r", body);
-    HttpResponse* post_res = post_req->send(body, strlen(body));
+        // Check the response for an error
+        if (!post_res) {
+            printf("HttpRequest failed (error code %d)\n\r", post_req->get_error());
+            //return 1;
+        } else {
+            // Print the response
+            printf("Success!\n\r");
+            //OMFLib_dump_response(post_res);
+        }
     
-    // Check the response for an error
-    if (!post_res) {
-        if (ENABLE_PRINTOUTS) printf("HttpRequest failed (error code %d)\n\r", post_req->get_error());
-        //return 1;
-    } else {
-        // Print the response
-        if (ENABLE_PRINTOUTS) printf("Success!\n\r");
-        //OMFLib_dump_response(post_res);
+        // Free up the request object
+        delete post_req;
     }
+#endif
 
-    // Free up the request object
-    delete post_req;
-}
+// Option if printouts should be hidden
+#if ENABLE_PRINTOUTS == DO_NOT_ENABLE_PRINTOUTS
+    void OMFLib_sendMessageToEndpoint_NoSocketReuse(NetworkInterface* network, const char* action, const char* message_type, const char* body) {
+
+        // Create the new request
+        HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, TARGET_URL);
+    
+        // Turn on debugging - this hides TLS connection information
+        //post_req->set_debug(true);
+    
+        // Add headers: content type 
+        post_req->set_header("Content-Type", CONTENT_TYPE);
+    
+        // Set OMF headers
+        post_req->set_header("producertoken", PRODUCER_TOKEN);
+        post_req->set_header("messagetype", message_type);
+        post_req->set_header("action", action);
+        post_req->set_header("messageformat", OMF_MESSAGE_FORMAT);
+        post_req->set_header("omfversion", OMF_VERSION);
+            
+        // Send the body
+        HttpResponse* post_res = post_req->send(body, strlen(body));
+    
+        // Free up the request object
+        delete post_req;
+    }
+#endif
 
 // ************************************************************************
 // Gets the current time in the appropriate OMF format
+// See https://os.mbed.com/blog/entry/103/
 // ************************************************************************
-
+const int TIMESTAMP_BUFFER_SIZE = 21;
 string OMFLib_getCurrentTimeString() {
     // Declar vars
-    char timestampBuffer[80];
+    char timestampBuffer[TIMESTAMP_BUFFER_SIZE]; // The format is YYYY-MM-DDThh:mm:ssZ
     time_t now;
-    struct tm ts;
+    //struct tm ts;
     
-    // Get the current time
+    // Set the "now" var to the current time
     time(&now);
     
-    // Cast the current time into the correct format
-    ts = *localtime(&now);
-    strftime(timestampBuffer, sizeof(timestampBuffer), "%Y-%m-%dT%H:%M:%SZ", &ts);
+    // Cast the current time into UTC seconds
+    //ts = *localtime(&now);
+    
+    // Print out the time in seconds as a formatted string to the timestamp buffer
+    //strftime(timestampBuffer, sizeof(timestampBuffer), "%Y-%m-%dT%H:%M:%SZ", &ts);
+    //strftime(timestampBuffer, sizeof(timestampBuffer), "%Y-%m-%dT%H:%M:%SZ", &ts);
+    strftime(timestampBuffer, TIMESTAMP_BUFFER_SIZE, "%Y-%m-%dT%H:%M:%SZ", localtime(&now));
     
     // Return the result
     return string(timestampBuffer);
@@ -190,37 +225,34 @@
     
     // Get the timestamp via NTP
     time_t timestamp = ntp.get_timestamp();
-    if (timestamp > 0) {
-        set_time(timestamp);
-        printf("Clock set via NTP to UTC %s", ctime(&timestamp));
-    } else {
-        printf("NTP time sync failed; clock set to %i UTC seconds.", DEFAULT_HARD_CODED_UTC_TIME);
-    }
+    
+    // Set the current timestamp to the NTP timestamp
+    set_time(timestamp);
+    
+    // Print the result
+    printf("Attempt done; clock set to UTC %s", ctime(&timestamp));
 }
 
 // ************************************************************************
 // Sends the dynamic types, static types, assets, and links messages
-// Uses the SEND_ASSETS_AND_LINKS flag found in config.egressSettings and
+// Uses the SEND_ASSETS_AND_LINKS MACRO found in config.egressSettings and
 // Uses the custom OMF JSON found in config.customOMFJSONStructures.h
 // ************************************************************************
 
-void OMFLib_sendInitialOMFMessages(NetworkInterface* __network_interface) {
-    // Send the DYNAMIC types message, so that these types can be referenced in all later messages
-    printf("\n\r!!!!!!!! Sending DYNAMIC Types message... !!!!!!!!\n\r");
-    //OMFLib_sendMessageToEndpoint(socket, "create", "Type", dynamic_types_message_JSON.c_str());
-    //OMFLib_sendMessageToEndpoint_NoSocketReuse(network, "create", "Type", DYNAMIC_TYPES_MESSAGE_JSON.c_str());
-    OMFLib_sendMessageToEndpoint_NoSocketReuse(__network_interface, "create", "Type", DYNAMIC_TYPES_MESSAGE_JSON.c_str());
-
-    // Send the container message, to instantiate this particular container; we can now directly start sending data to it using its Id
-    printf("\n\r!!!!!!!! Sending Containers message... !!!!!!!!\n\r");
-    //OMFLib_sendMessageToEndpoint(socket, "create", "Container", CONTAINERS_MESSAGE_JSON.c_str());  
-    //OMFLib_sendMessageToEndpoint_NoSocketReuse(network, "create", "Container", CONTAINERS_MESSAGE_JSON.c_str());  
-    OMFLib_sendMessageToEndpoint_NoSocketReuse(__network_interface, "create", "Container", CONTAINERS_MESSAGE_JSON.c_str()); 
+#if SEND_ASSETS_AND_LINKS == YES_SEND_ASSETS_AND_LINKS
+    void OMFLib_sendInitialOMFMessages(NetworkInterface* __network_interface) {
+        // Send the DYNAMIC types message, so that these types can be referenced in all later messages
+        printf("\n\r!!!!!!!! Sending DYNAMIC Types message... !!!!!!!!\n\r");
+        //OMFLib_sendMessageToEndpoint(socket, "create", "Type", dynamic_types_message_JSON.c_str());
+        //OMFLib_sendMessageToEndpoint_NoSocketReuse(network, "create", "Type", DYNAMIC_TYPES_MESSAGE_JSON.c_str());
+        OMFLib_sendMessageToEndpoint_NoSocketReuse(__network_interface, "create", "Type", DYNAMIC_TYPES_MESSAGE_JSON.c_str());
     
-    // Check whether assets and links should be sent!
-    if (SEND_ASSETS_AND_LINKS == true)
-    {
-    
+        // Send the container message, to instantiate this particular container; we can now directly start sending data to it using its Id
+        printf("\n\r!!!!!!!! Sending Containers message... !!!!!!!!\n\r");
+        //OMFLib_sendMessageToEndpoint(socket, "create", "Container", CONTAINERS_MESSAGE_JSON.c_str());  
+        //OMFLib_sendMessageToEndpoint_NoSocketReuse(network, "create", "Container", CONTAINERS_MESSAGE_JSON.c_str());  
+        OMFLib_sendMessageToEndpoint_NoSocketReuse(__network_interface, "create", "Container", CONTAINERS_MESSAGE_JSON.c_str()); 
+        
         // ************************************************************************  
         // Send the STATIC types message, so that these types can be referenced in all later messages
         // ************************************************************************
@@ -248,4 +280,21 @@
         //OMFLib_sendMessageToEndpoint_NoSocketReuse(network, "create", "Data", LINKS_MESSAGE_JSON.c_str());  
         OMFLib_sendMessageToEndpoint_NoSocketReuse(__network_interface, "create", "Data", LINKS_MESSAGE_JSON.c_str());
     }
-}
\ No newline at end of file
+#endif
+
+// If assets and links should be skipped...
+#if SEND_ASSETS_AND_LINKS == DO_NOT_SEND_ASSETS_AND_LINKS
+    void OMFLib_sendInitialOMFMessages(NetworkInterface* __network_interface) {
+        // Send the DYNAMIC types message, so that these types can be referenced in all later messages
+        printf("\n\r!!!!!!!! Sending DYNAMIC Types message... !!!!!!!!\n\r");
+        //OMFLib_sendMessageToEndpoint(socket, "create", "Type", dynamic_types_message_JSON.c_str());
+        //OMFLib_sendMessageToEndpoint_NoSocketReuse(network, "create", "Type", DYNAMIC_TYPES_MESSAGE_JSON.c_str());
+        OMFLib_sendMessageToEndpoint_NoSocketReuse(__network_interface, "create", "Type", DYNAMIC_TYPES_MESSAGE_JSON.c_str());
+    
+        // Send the container message, to instantiate this particular container; we can now directly start sending data to it using its Id
+        printf("\n\r!!!!!!!! Sending Containers message... !!!!!!!!\n\r");
+        //OMFLib_sendMessageToEndpoint(socket, "create", "Container", CONTAINERS_MESSAGE_JSON.c_str());  
+        //OMFLib_sendMessageToEndpoint_NoSocketReuse(network, "create", "Container", CONTAINERS_MESSAGE_JSON.c_str());  
+        OMFLib_sendMessageToEndpoint_NoSocketReuse(__network_interface, "create", "Container", CONTAINERS_MESSAGE_JSON.c_str()); 
+    }
+#endif
\ No newline at end of file