Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: osisoft-omf.cpp
- Revision:
- 0:6156b29d3c91
- Child:
- 1:1c31b413ba0c
diff -r 000000000000 -r 6156b29d3c91 osisoft-omf.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/osisoft-omf.cpp Mon Jan 22 17:11:34 2018 +0000
@@ -0,0 +1,141 @@
+//Copyright 2017 OSIsoft, LLC
+//
+//Licensed under the Apache License, Version 2.0 (the "License");
+//you may not use this file except in compliance with the License.
+//You may obtain a copy of the License at
+//
+//<http://www.apache.org/licenses/LICENSE-2.0>
+//
+//Unless required by applicable law or agreed to in writing, software
+//distributed under the License is distributed on an "AS IS" BASIS,
+//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//See the License for the specific language governing permissions and
+//limitations under the License.
+
+#include "osisoft-omf.h"
+#include "mbed.h"
+#include "https_request.h"
+#include "ntp-client/NTPClient.h"
+#include "omf-config.h"
+
+// ---------------------------------------------------------------------------------------------------
+
+// The clock is set usign NTP; if that fails, the clock defaults to the below time
+const int DEFAULT_HARD_CODED_UTC_TIME = 1513175347;
+
+// ---------------------------------------------------------------------------------------------------
+
+// ************************************************************************
+// Helper function: prints out an HTTP response
+// ************************************************************************
+
+void dump_response(HttpResponse* res)
+{
+ printf("\n----- HTTPS POST response -----\n");
+ // Print the status code
+ mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
+ // Print the headers
+ mbedtls_printf("Response headers:\n");
+ for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
+ mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
+ }
+ // Print the body
+ mbedtls_printf("Response body (%d bytes):\n\n%s", res->get_body_length(), res->get_body_as_string().c_str());
+}
+
+// ************************************************************************
+// Helper function that casts floats into strings
+// ************************************************************************
+
+string OMFLib_float_to_string(float f) {
+ char buffer[20];
+ int n = sprintf (buffer, "%f", f);
+ return string(buffer);
+}
+
+// ************************************************************************
+// Helper function that sends an actual web request
+// ************************************************************************
+
+//void OMFLib_sendMessageToEndpoint(NetworkInterface* network, const char* action, const char* message_type, const char* body) { // Old: doesn't re-use sockets
+void OMFLib_sendMessageToEndpoint(TLSSocket* socket, const char* action, const char* message_type, const char* body) {
+ printf("\n----- HTTPS POST request -----\n");
+
+ // Create the new request
+ //HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, TARGET_URL); // Old: doesn't re-use sockets
+ HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, TARGET_URL);
+
+ // Turn on debugging - this hides TLS connection information
+ post_req->set_debug(true);
+
+ // Add headers: content type and authentication
+ post_req->set_header("Content-Type", "application/json");
+
+ // 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", "JSON");
+ post_req->set_header("omfversion", "1.0");
+
+ // Send the body
+ printf("Now sending request...");
+ printf("\nOutgoing message headers:\n\tmessagetype: %s\n\taction: %s", message_type, action);
+ printf("\nOutgoing message body:\n\t%s\n", 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", post_req->get_error());
+ printf("Socket connection status after error: %d\n", socket->connected());
+ //return 1;
+ } else {
+ // Print the response
+ dump_response(post_res);
+ }
+
+ // Free up the request object
+ delete post_req;
+}
+
+// ************************************************************************
+// Gets the current time in the appropriate OMF format
+// ************************************************************************
+
+string OMFLib_getCurrentTimeString() {
+ // Declar vars
+ char timestampBuffer[80];
+ time_t now;
+ struct tm ts;
+
+ // Get 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);
+
+ // Return the result
+ return string(timestampBuffer);
+}
+
+// ************************************************************************
+// Sets the clock via NTP via the nwtwork
+// ************************************************************************
+
+void OMFLib_syncClockViaNTP(NetworkInterface* network) {
+ // Hard-code a start time... see https://www.epochconverter.com/
+ set_time(DEFAULT_HARD_CODED_UTC_TIME);
+
+ printf("\n----- Setting internal clock -----\n");
+ // See https://github.com/ARMmbed/ntp-client-example/blob/master/main.cpp
+
+ NTPClient ntp(network);
+ time_t timestamp = ntp.get_timestamp();
+ if (timestamp > 0) {
+ set_time(timestamp);
+ printf("Clock set via NTP to UTC %s", ctime(×tamp));
+ } else {
+ printf("NTP time sync failed; clock set to %i UTC seconds.", DEFAULT_HARD_CODED_UTC_TIME);
+ }
+}
\ No newline at end of file
