Código de adaptador personalizado C / C ++ para Freezer Monitor Lite

custom_c_adapter.cpp


#include <string> #include <iostream> #include <stdlib.h> #include <time.h> #include <stdio.h> #include "sdk/esp_sdk.h" #include "sdk/esp_error.h" #include "sdk/esp_credentials.h" #include "sdk/esp_project.h" #include "sdk/esp_publisher.h" using namespace std; EspError * g_error = NULL; EspCredentials * g_creds = NULL; void print_error_and_exit(EspError * streamingError, int line) { printf("An error was encountered on line %dn", line); printf("%s", esp_error_get_message(streamingError)); printf("nExitingn"); //free EspCredentials object if it was created if (g_creds) esp_credentials_free(g_creds, g_error); //stop SDK esp_sdk_stop(g_error); //free EspError object if it was created if (g_error) esp_error_free(g_error); exit(1); } // Execute a command and return result as string. // In this example the command should only generate one number. string exec(char const * cmd) { FILE * pipe = popen(cmd, "r"); if (!pipe) return "ERROR"; char buffer[128]; string result = ""; while (!feof(pipe)) { if (fgets(buffer, 128, pipe) != NULL) result += buffer; } pclose(pipe); return result; } int main() { string val_host = "SAPraspberrypi2-B3"; int val_port = 9230; string val_stream = "isFreezerTemperatureReading"; string val_username = ""; string val_password = ""; int val_repeat = -1; int val_interval = 1; string val_sensorId = "RaspberryPi"; string val_Temperature_Command = "echo 90"; int64_t val_readingDate; //will be generated right before row is sent int64_t val_id = 0; //generated in streaming lite int errorflag = 0; //creates error object. Must be freed after use g_error = esp_error_create(); //esp_sdk_start initializes the sdk, must matched with same number of stop calls errorflag = esp_sdk_start(g_error); if (errorflag != 0) print_error_and_exit(g_error, __LINE__); //sets up credentials with authentication using digest and signature verification //other types are available as well g_creds = esp_credentials_create(ESP_CREDENTIALS_USER_PASSWORD, g_error); if (NULL == g_creds) print_error_and_exit(g_error, __LINE__); esp_credentials_set_user(g_creds, val_username.c_str(), g_error); esp_credentials_set_password(g_creds, val_password.c_str(), g_error); //retrieves standalone project (Streaming Lite) object to connect to EspProject * project = esp_project_get_standalone(val_host.c_str(), val_port, g_creds, NULL, g_error); if (NULL == project) print_error_and_exit(g_error, __LINE__); //connect to project errorflag = esp_project_connect(project, g_error); if (errorflag != 0) print_error_and_exit(g_error, __LINE__); // Once we have retrieved the project we do not need the EspCredentials object // Since we are not going to reuse them, need to free them errorflag = esp_credentials_free(g_creds, g_error); if (errorflag != 0) print_error_and_exit(g_error, __LINE__); g_creds = NULL; //Retrieves the stream object const EspStream * stream = esp_project_get_stream(project, val_stream.c_str(), g_error); if (NULL == stream) print_error_and_exit(g_error, __LINE__); //Creates a publisher for this project. Lifetime is managed by SDK EspPublisher * publisher = esp_project_create_publisher(project, NULL, g_error); if (NULL == publisher) print_error_and_exit(g_error, __LINE__); //Connects to publisher errorflag = esp_publisher_connect(publisher, g_error); if (errorflag != 0) print_error_and_exit(g_error, __LINE__); //retrieve an EspMessageWriter to build a single row EspMessageWriter * message = esp_publisher_get_writer(publisher, stream, g_error); if (NULL == message) print_error_and_exit(g_error, __LINE__); //formats data for the stream the EspMessageWriter writes to EspRelativeRowWriter * row = esp_message_writer_get_relative_rowwriter(message, g_error); if (NULL == row) print_error_and_exit(g_error, __LINE__); do { if (val_repeat != -1) val_repeat--; //starts new row definition. First call when starting a row errorflag = esp_relative_rowwriter_start_row(row, g_error); if (errorflag != 0) print_error_and_exit(g_error, __LINE__); //sets operation for row errorflag = esp_relative_rowwriter_set_operation(row, ESP_STREAM_OP_INSERT, g_error); if (errorflag != 0) print_error_and_exit(g_error, __LINE__); // column 1 a string (SensorID) errorflag = esp_relative_rowwriter_set_string(row, val_sensorId.c_str(), g_error); if (errorflag != 0) print_error_and_exit(g_error, __LINE__); // column 2 a float (Temperature) string temperature_s = exec(val_Temperature_Command.c_str()); errorflag = esp_relative_rowwriter_set_float(row, atof(temperature_s.c_str()), g_error); if (errorflag != 0) print_error_and_exit(g_error, __LINE__); //column 3 a msdate (ReadingDate) val_readingDate = time(0); errorflag = esp_relative_rowwriter_set_msdate(row, val_readingDate * 1000, g_error); if (errorflag != 0) print_error_and_exit(g_error, __LINE__); //column 4 a long (Id) errorflag = esp_relative_rowwriter_set_long(row, val_id, g_error); if (errorflag != 0) print_error_and_exit(g_error, __LINE__); //ends row definition errorflag = esp_relative_rowwriter_end_row(row, g_error); if (errorflag != 0) print_error_and_exit(g_error, __LINE__); //Publishes data currently in EspMessageWriter. On successful completion data in EspMessageWriter is cleared errorflag = esp_publisher_publish(publisher, message, g_error); if (errorflag != 0) print_error_and_exit(g_error, __LINE__); //causes publisher to process all its input queues and commit the data to its log stores errorflag = esp_publisher_commit(publisher, g_error); if (errorflag != 0) print_error_and_exit(g_error, __LINE__); printf("Messages published successfullyn"); // wait for interval.. if ((val_repeat == -1 || val_repeat > 0) && val_interval > 0) sleep(val_interval); } while ((val_repeat == -1 || val_repeat > 0) && val_interval > 0); //stopping the sdk errorflag = esp_sdk_stop(g_error); if (errorflag != 0) print_error_and_exit(g_error, __LINE__); //release error object esp_error_free(g_error); }

Hecho

Inicie sesión para responder la pregunta

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *