huaiyu wei / ennoTest2
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sitewhere.c Source File

sitewhere.c

00001 #include "sitewhere.h"
00002 #include "double_conversion.h"
00003 
00004 // Signals end of stream.
00005 uint8_t zero = 0;
00006 
00007 unsigned int sw_register(char* hardwareId, char* specificationToken, uint8_t* buffer, size_t length, char* originator) {
00008     pb_ostream_t stream = pb_ostream_from_buffer(buffer, length);
00009 
00010     SiteWhere_Header header = { };
00011     header.command = SiteWhere_Command_REGISTER;
00012     if (originator != NULL) {
00013         header.has_originator = true;
00014         strcpy(header.originator, originator);
00015     }
00016     if (!pb_encode_delimited(&stream, SiteWhere_Header_fields, &header)) {
00017         return 0;
00018     }
00019 
00020     SiteWhere_RegisterDevice registerDevice = { };
00021     strcpy(registerDevice.hardwareId, hardwareId);
00022     strcpy(registerDevice.specificationToken, specificationToken);
00023     if (!pb_encode_delimited(&stream, SiteWhere_RegisterDevice_fields, &registerDevice)) {
00024         return 0;
00025     }
00026 
00027     return stream.bytes_written;
00028 }
00029 
00030 unsigned int sw_acknowledge(char* hardwareId, char* message, uint8_t* buffer, size_t length, char* originator) {
00031     pb_ostream_t stream = pb_ostream_from_buffer(buffer, length);
00032 
00033     SiteWhere_Header header = { };
00034     header.command = SiteWhere_Command_ACKNOWLEDGE;
00035     if (originator != NULL) {
00036         header.has_originator = true;
00037         strcpy(header.originator, originator);
00038     }
00039     if (!pb_encode_delimited(&stream, SiteWhere_Header_fields, &header)) {
00040         return 0;
00041     }
00042 
00043     SiteWhere_Acknowledge ack = { };
00044     strcpy(ack.hardwareId, hardwareId);
00045     if (message != NULL) {
00046         ack.has_message = true;
00047         strcpy(ack.message, message);
00048     }
00049     if (!pb_encode_delimited(&stream, SiteWhere_Acknowledge_fields, &ack)) {
00050         return 0;
00051     }
00052 
00053     return stream.bytes_written;
00054 }
00055 
00056 unsigned int sw_measurement(char* hardwareId, char* name, float value, int64_t eventDate,
00057         uint8_t* buffer, size_t length, char* originator) {
00058     pb_ostream_t stream = pb_ostream_from_buffer(buffer, length);
00059 
00060     SiteWhere_Header header = { };
00061     header.command = SiteWhere_Command_DEVICEMEASUREMENT;
00062     if (originator != NULL) {
00063         header.has_originator = true;
00064         strcpy(header.originator, originator);
00065     }
00066     if (!pb_encode_delimited(&stream, SiteWhere_Header_fields, &header)) {
00067         return 0;
00068     }
00069 
00070     SiteWhere_DeviceMeasurements measurements = { };
00071     strcpy(measurements.hardwareId, hardwareId);
00072 
00073     SiteWhere_Measurement measurement = { };
00074     strcpy(measurement.measurementId, name);
00075     measurement.measurementValue = float_to_double(value);      //there is a problem
00076     //measurement.measurementValue = value; 
00077     
00078     measurements.measurement[0] = measurement;
00079     measurements.measurement_count = 1;
00080 
00081     if (eventDate != NULL) {
00082         measurements.has_eventDate = true;
00083         measurements.eventDate = eventDate;
00084     }
00085     if (!pb_encode_delimited(&stream, SiteWhere_DeviceMeasurements_fields, &measurements)) {
00086         return 0;
00087     }
00088 
00089     return stream.bytes_written;
00090 }
00091 
00092 unsigned int sw_location(char* hardwareId, float lat, float lon, float ele, int64_t eventDate,
00093         uint8_t* buffer, size_t length, char* originator) {
00094     pb_ostream_t stream = pb_ostream_from_buffer(buffer, length);
00095 
00096     SiteWhere_Header header = { };
00097     header.command = SiteWhere_Command_DEVICELOCATION;
00098     if (originator != NULL) {
00099         header.has_originator = true;
00100         strcpy(header.originator, originator);
00101     }
00102     if (!pb_encode_delimited(&stream, SiteWhere_Header_fields, &header)) {
00103         return 0;
00104     }
00105 
00106     SiteWhere_DeviceLocation location = { };
00107     strcpy(location.hardwareId, hardwareId);
00108     
00109     location.latitude = float_to_double(lat);                    //there is a problem
00110     location.longitude = float_to_double(lon);
00111     location.elevation = float_to_double(ele);
00112     //location.latitude = lat;
00113     //location.longitude = lon;
00114     //location.elevation = ele;
00115 
00116     location.has_elevation = true;
00117 
00118     if (eventDate != NULL) {
00119         location.has_eventDate = true;
00120         location.eventDate = eventDate;
00121     }
00122     if (!pb_encode_delimited(&stream, SiteWhere_DeviceLocation_fields, &location)) {
00123         return 0;
00124     }
00125 
00126     return stream.bytes_written;
00127 }
00128 
00129 unsigned int sw_alert(char* hardwareId, char* alertType, char* alertMessage, int64_t eventDate,
00130         uint8_t* buffer, size_t length, char* originator) {
00131     pb_ostream_t stream = pb_ostream_from_buffer(buffer, length);
00132 
00133     SiteWhere_Header header = { };
00134     header.command = SiteWhere_Command_DEVICEALERT;
00135     if (originator != NULL) {
00136         header.has_originator = true;
00137         strcpy(header.originator, originator);
00138     }
00139     if (!pb_encode_delimited(&stream, SiteWhere_Header_fields, &header)) {
00140         return 0;
00141     }
00142 
00143     SiteWhere_DeviceAlert alert = { };
00144     strcpy(alert.hardwareId, hardwareId);
00145     strcpy(alert.alertType, alertType);
00146     strcpy(alert.alertMessage, alertMessage);
00147     if (eventDate != NULL) {
00148         alert.has_eventDate = true;
00149         alert.eventDate = eventDate;
00150     }
00151     if (!pb_encode_delimited(&stream, SiteWhere_DeviceAlert_fields, &alert)) {
00152         return 0;
00153     }
00154 
00155     return stream.bytes_written;
00156 }