Separate library that holds helper functions for the main OMF code.
osisoft-omf.cpp@1:1c31b413ba0c, 2018-02-02 (annotated)
- Committer:
- danielelopez
- Date:
- Fri Feb 02 17:54:05 2018 +0000
- Revision:
- 1:1c31b413ba0c
- Parent:
- 0:6156b29d3c91
- Child:
- 5:0e4f98d77171
Updated to add additional constants and function for not reusing sockets
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
danielelopez | 0:6156b29d3c91 | 1 | //Copyright 2017 OSIsoft, LLC |
danielelopez | 0:6156b29d3c91 | 2 | // |
danielelopez | 0:6156b29d3c91 | 3 | //Licensed under the Apache License, Version 2.0 (the "License"); |
danielelopez | 0:6156b29d3c91 | 4 | //you may not use this file except in compliance with the License. |
danielelopez | 0:6156b29d3c91 | 5 | //You may obtain a copy of the License at |
danielelopez | 0:6156b29d3c91 | 6 | // |
danielelopez | 0:6156b29d3c91 | 7 | //<http://www.apache.org/licenses/LICENSE-2.0> |
danielelopez | 0:6156b29d3c91 | 8 | // |
danielelopez | 0:6156b29d3c91 | 9 | //Unless required by applicable law or agreed to in writing, software |
danielelopez | 0:6156b29d3c91 | 10 | //distributed under the License is distributed on an "AS IS" BASIS, |
danielelopez | 0:6156b29d3c91 | 11 | //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
danielelopez | 0:6156b29d3c91 | 12 | //See the License for the specific language governing permissions and |
danielelopez | 0:6156b29d3c91 | 13 | //limitations under the License. |
danielelopez | 0:6156b29d3c91 | 14 | |
danielelopez | 0:6156b29d3c91 | 15 | #include "osisoft-omf.h" |
danielelopez | 0:6156b29d3c91 | 16 | #include "mbed.h" |
danielelopez | 0:6156b29d3c91 | 17 | #include "https_request.h" |
danielelopez | 0:6156b29d3c91 | 18 | #include "ntp-client/NTPClient.h" |
danielelopez | 0:6156b29d3c91 | 19 | #include "omf-config.h" |
danielelopez | 1:1c31b413ba0c | 20 | #include "SSLCertificates.h" |
danielelopez | 0:6156b29d3c91 | 21 | |
danielelopez | 0:6156b29d3c91 | 22 | // --------------------------------------------------------------------------------------------------- |
danielelopez | 0:6156b29d3c91 | 23 | |
danielelopez | 0:6156b29d3c91 | 24 | // The clock is set usign NTP; if that fails, the clock defaults to the below time |
danielelopez | 0:6156b29d3c91 | 25 | const int DEFAULT_HARD_CODED_UTC_TIME = 1513175347; |
danielelopez | 0:6156b29d3c91 | 26 | |
danielelopez | 0:6156b29d3c91 | 27 | // --------------------------------------------------------------------------------------------------- |
danielelopez | 0:6156b29d3c91 | 28 | |
danielelopez | 0:6156b29d3c91 | 29 | // ************************************************************************ |
danielelopez | 0:6156b29d3c91 | 30 | // Helper function: prints out an HTTP response |
danielelopez | 0:6156b29d3c91 | 31 | // ************************************************************************ |
danielelopez | 0:6156b29d3c91 | 32 | |
danielelopez | 1:1c31b413ba0c | 33 | void OMFLib_dump_response(HttpResponse* res) |
danielelopez | 0:6156b29d3c91 | 34 | { |
danielelopez | 1:1c31b413ba0c | 35 | printf("\n\r----- HTTPS POST response -----\n\r"); |
danielelopez | 0:6156b29d3c91 | 36 | // Print the status code |
danielelopez | 1:1c31b413ba0c | 37 | mbedtls_printf("Status: %d - %s\n\r", res->get_status_code(), res->get_status_message().c_str()); |
danielelopez | 0:6156b29d3c91 | 38 | // Print the headers |
danielelopez | 1:1c31b413ba0c | 39 | mbedtls_printf("Response headers:\n\r"); |
danielelopez | 0:6156b29d3c91 | 40 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
danielelopez | 1:1c31b413ba0c | 41 | mbedtls_printf("\t%s: %s\n\r", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
danielelopez | 0:6156b29d3c91 | 42 | } |
danielelopez | 0:6156b29d3c91 | 43 | // Print the body |
danielelopez | 0:6156b29d3c91 | 44 | mbedtls_printf("Response body (%d bytes):\n\n%s", res->get_body_length(), res->get_body_as_string().c_str()); |
danielelopez | 0:6156b29d3c91 | 45 | } |
danielelopez | 0:6156b29d3c91 | 46 | |
danielelopez | 0:6156b29d3c91 | 47 | // ************************************************************************ |
danielelopez | 0:6156b29d3c91 | 48 | // Helper function that casts floats into strings |
danielelopez | 0:6156b29d3c91 | 49 | // ************************************************************************ |
danielelopez | 0:6156b29d3c91 | 50 | |
danielelopez | 0:6156b29d3c91 | 51 | string OMFLib_float_to_string(float f) { |
danielelopez | 0:6156b29d3c91 | 52 | char buffer[20]; |
danielelopez | 0:6156b29d3c91 | 53 | int n = sprintf (buffer, "%f", f); |
danielelopez | 0:6156b29d3c91 | 54 | return string(buffer); |
danielelopez | 0:6156b29d3c91 | 55 | } |
danielelopez | 0:6156b29d3c91 | 56 | |
danielelopez | 0:6156b29d3c91 | 57 | // ************************************************************************ |
danielelopez | 0:6156b29d3c91 | 58 | // Helper function that sends an actual web request |
danielelopez | 0:6156b29d3c91 | 59 | // ************************************************************************ |
danielelopez | 0:6156b29d3c91 | 60 | |
danielelopez | 0:6156b29d3c91 | 61 | void OMFLib_sendMessageToEndpoint(TLSSocket* socket, const char* action, const char* message_type, const char* body) { |
danielelopez | 1:1c31b413ba0c | 62 | printf("\n\r----- HTTPS POST request -----\n\r"); |
danielelopez | 0:6156b29d3c91 | 63 | |
danielelopez | 0:6156b29d3c91 | 64 | // Create the new request |
danielelopez | 0:6156b29d3c91 | 65 | //HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, TARGET_URL); // Old: doesn't re-use sockets |
danielelopez | 0:6156b29d3c91 | 66 | HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, TARGET_URL); |
danielelopez | 0:6156b29d3c91 | 67 | |
danielelopez | 0:6156b29d3c91 | 68 | // Turn on debugging - this hides TLS connection information |
danielelopez | 0:6156b29d3c91 | 69 | post_req->set_debug(true); |
danielelopez | 0:6156b29d3c91 | 70 | |
danielelopez | 0:6156b29d3c91 | 71 | // Add headers: content type and authentication |
danielelopez | 0:6156b29d3c91 | 72 | post_req->set_header("Content-Type", "application/json"); |
danielelopez | 0:6156b29d3c91 | 73 | |
danielelopez | 0:6156b29d3c91 | 74 | // Set OMF headers |
danielelopez | 0:6156b29d3c91 | 75 | post_req->set_header("producertoken", PRODUCER_TOKEN); |
danielelopez | 0:6156b29d3c91 | 76 | post_req->set_header("messagetype", message_type); |
danielelopez | 0:6156b29d3c91 | 77 | post_req->set_header("action", action); |
danielelopez | 0:6156b29d3c91 | 78 | post_req->set_header("messageformat", "JSON"); |
danielelopez | 0:6156b29d3c91 | 79 | post_req->set_header("omfversion", "1.0"); |
danielelopez | 0:6156b29d3c91 | 80 | |
danielelopez | 0:6156b29d3c91 | 81 | // Send the body |
danielelopez | 0:6156b29d3c91 | 82 | printf("Now sending request..."); |
danielelopez | 1:1c31b413ba0c | 83 | printf("\n\rOutgoing message headers:\n\tmessagetype: %s\n\taction: %s", message_type, action); |
danielelopez | 1:1c31b413ba0c | 84 | printf("\n\rOutgoing message body:\n\t%s\n\r", body); |
danielelopez | 0:6156b29d3c91 | 85 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
danielelopez | 0:6156b29d3c91 | 86 | |
danielelopez | 0:6156b29d3c91 | 87 | // Check the response for an error |
danielelopez | 0:6156b29d3c91 | 88 | if (!post_res) { |
danielelopez | 1:1c31b413ba0c | 89 | printf("HttpRequest failed (error code %d)\n\r", post_req->get_error()); |
danielelopez | 1:1c31b413ba0c | 90 | printf("Socket connection status after error: %d\n\r", socket->connected()); |
danielelopez | 0:6156b29d3c91 | 91 | //return 1; |
danielelopez | 0:6156b29d3c91 | 92 | } else { |
danielelopez | 0:6156b29d3c91 | 93 | // Print the response |
danielelopez | 1:1c31b413ba0c | 94 | OMFLib_dump_response(post_res); |
danielelopez | 1:1c31b413ba0c | 95 | } |
danielelopez | 1:1c31b413ba0c | 96 | |
danielelopez | 1:1c31b413ba0c | 97 | // Free up the request object |
danielelopez | 1:1c31b413ba0c | 98 | delete post_req; |
danielelopez | 1:1c31b413ba0c | 99 | } |
danielelopez | 1:1c31b413ba0c | 100 | |
danielelopez | 1:1c31b413ba0c | 101 | // ************************************************************************ |
danielelopez | 1:1c31b413ba0c | 102 | // Helper function that sends an actual web request; does not reuse sockets |
danielelopez | 1:1c31b413ba0c | 103 | // ************************************************************************ |
danielelopez | 1:1c31b413ba0c | 104 | |
danielelopez | 1:1c31b413ba0c | 105 | void OMFLib_sendMessageToEndpoint_NoSocketReuse(NetworkInterface* network, const char* action, const char* message_type, const char* body) { |
danielelopez | 1:1c31b413ba0c | 106 | printf("\n\r----- HTTPS POST request -----\n\r"); |
danielelopez | 1:1c31b413ba0c | 107 | |
danielelopez | 1:1c31b413ba0c | 108 | // Create the new request |
danielelopez | 1:1c31b413ba0c | 109 | HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, TARGET_URL); |
danielelopez | 1:1c31b413ba0c | 110 | |
danielelopez | 1:1c31b413ba0c | 111 | // Turn on debugging - this hides TLS connection information |
danielelopez | 1:1c31b413ba0c | 112 | post_req->set_debug(true); |
danielelopez | 1:1c31b413ba0c | 113 | |
danielelopez | 1:1c31b413ba0c | 114 | // Add headers: content type |
danielelopez | 1:1c31b413ba0c | 115 | post_req->set_header("Content-Type", CONTENT_TYPE); |
danielelopez | 1:1c31b413ba0c | 116 | |
danielelopez | 1:1c31b413ba0c | 117 | // Set OMF headers |
danielelopez | 1:1c31b413ba0c | 118 | post_req->set_header("producertoken", PRODUCER_TOKEN); |
danielelopez | 1:1c31b413ba0c | 119 | post_req->set_header("messagetype", message_type); |
danielelopez | 1:1c31b413ba0c | 120 | post_req->set_header("action", action); |
danielelopez | 1:1c31b413ba0c | 121 | post_req->set_header("messageformat", OMF_MESSAGE_FORMAT); |
danielelopez | 1:1c31b413ba0c | 122 | post_req->set_header("omfversion", OMF_VERSION); |
danielelopez | 1:1c31b413ba0c | 123 | |
danielelopez | 1:1c31b413ba0c | 124 | // Send the body |
danielelopez | 1:1c31b413ba0c | 125 | printf("Now sending request... "); |
danielelopez | 1:1c31b413ba0c | 126 | //printf("\n\rOutgoing message headers:\n\tmessagetype: %s\n\taction: %s", message_type, action); |
danielelopez | 1:1c31b413ba0c | 127 | //printf("\n\rOutgoing message body:\n\t%s\n\r", body); |
danielelopez | 1:1c31b413ba0c | 128 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
danielelopez | 1:1c31b413ba0c | 129 | |
danielelopez | 1:1c31b413ba0c | 130 | // Check the response for an error |
danielelopez | 1:1c31b413ba0c | 131 | if (!post_res) { |
danielelopez | 1:1c31b413ba0c | 132 | printf("HttpRequest failed (error code %d)\n\r", post_req->get_error()); |
danielelopez | 1:1c31b413ba0c | 133 | //return 1; |
danielelopez | 1:1c31b413ba0c | 134 | } else { |
danielelopez | 1:1c31b413ba0c | 135 | // Print the response |
danielelopez | 1:1c31b413ba0c | 136 | printf("Success!\n\r"); |
danielelopez | 1:1c31b413ba0c | 137 | //OMFLib_dump_response(post_res); |
danielelopez | 0:6156b29d3c91 | 138 | } |
danielelopez | 0:6156b29d3c91 | 139 | |
danielelopez | 0:6156b29d3c91 | 140 | // Free up the request object |
danielelopez | 0:6156b29d3c91 | 141 | delete post_req; |
danielelopez | 0:6156b29d3c91 | 142 | } |
danielelopez | 0:6156b29d3c91 | 143 | |
danielelopez | 0:6156b29d3c91 | 144 | // ************************************************************************ |
danielelopez | 0:6156b29d3c91 | 145 | // Gets the current time in the appropriate OMF format |
danielelopez | 0:6156b29d3c91 | 146 | // ************************************************************************ |
danielelopez | 0:6156b29d3c91 | 147 | |
danielelopez | 0:6156b29d3c91 | 148 | string OMFLib_getCurrentTimeString() { |
danielelopez | 0:6156b29d3c91 | 149 | // Declar vars |
danielelopez | 0:6156b29d3c91 | 150 | char timestampBuffer[80]; |
danielelopez | 0:6156b29d3c91 | 151 | time_t now; |
danielelopez | 0:6156b29d3c91 | 152 | struct tm ts; |
danielelopez | 0:6156b29d3c91 | 153 | |
danielelopez | 0:6156b29d3c91 | 154 | // Get the current time |
danielelopez | 0:6156b29d3c91 | 155 | time(&now); |
danielelopez | 0:6156b29d3c91 | 156 | |
danielelopez | 0:6156b29d3c91 | 157 | // Cast the current time into the correct format |
danielelopez | 0:6156b29d3c91 | 158 | ts = *localtime(&now); |
danielelopez | 0:6156b29d3c91 | 159 | strftime(timestampBuffer, sizeof(timestampBuffer), "%Y-%m-%dT%H:%M:%SZ", &ts); |
danielelopez | 0:6156b29d3c91 | 160 | |
danielelopez | 0:6156b29d3c91 | 161 | // Return the result |
danielelopez | 0:6156b29d3c91 | 162 | return string(timestampBuffer); |
danielelopez | 0:6156b29d3c91 | 163 | } |
danielelopez | 0:6156b29d3c91 | 164 | |
danielelopez | 0:6156b29d3c91 | 165 | // ************************************************************************ |
danielelopez | 0:6156b29d3c91 | 166 | // Sets the clock via NTP via the nwtwork |
danielelopez | 0:6156b29d3c91 | 167 | // ************************************************************************ |
danielelopez | 0:6156b29d3c91 | 168 | |
danielelopez | 0:6156b29d3c91 | 169 | void OMFLib_syncClockViaNTP(NetworkInterface* network) { |
danielelopez | 0:6156b29d3c91 | 170 | // Hard-code a start time... see https://www.epochconverter.com/ |
danielelopez | 0:6156b29d3c91 | 171 | set_time(DEFAULT_HARD_CODED_UTC_TIME); |
danielelopez | 0:6156b29d3c91 | 172 | |
danielelopez | 1:1c31b413ba0c | 173 | printf("\n\r----- Setting internal clock -----\n\r"); |
danielelopez | 0:6156b29d3c91 | 174 | // See https://github.com/ARMmbed/ntp-client-example/blob/master/main.cpp |
danielelopez | 0:6156b29d3c91 | 175 | |
danielelopez | 0:6156b29d3c91 | 176 | NTPClient ntp(network); |
danielelopez | 0:6156b29d3c91 | 177 | time_t timestamp = ntp.get_timestamp(); |
danielelopez | 0:6156b29d3c91 | 178 | if (timestamp > 0) { |
danielelopez | 0:6156b29d3c91 | 179 | set_time(timestamp); |
danielelopez | 0:6156b29d3c91 | 180 | printf("Clock set via NTP to UTC %s", ctime(×tamp)); |
danielelopez | 0:6156b29d3c91 | 181 | } else { |
danielelopez | 0:6156b29d3c91 | 182 | printf("NTP time sync failed; clock set to %i UTC seconds.", DEFAULT_HARD_CODED_UTC_TIME); |
danielelopez | 0:6156b29d3c91 | 183 | } |
danielelopez | 0:6156b29d3c91 | 184 | } |