Official reference client implementation for Cumulocity SmartREST on u-blox C027.

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Vincent Wochnik

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Location.cpp Source File

Location.cpp

00001 #include <stdio.h>
00002 #include "Location.h"
00003 #include "SmartRestConf.h"
00004 #include "logging.h"
00005 
00006 // Percentage cut-off for avoiding sending similar sensor readings.
00007 #define THRESHOLD_PERCENT_LOC 0.05
00008 // Timeout for forcing a sending even if readings are similar [seconds].
00009 #define TIME_LIMIT_LOC 900
00010 
00011 static inline bool valueChanged(float *V0, float *V1)
00012 {
00013         if (abs(V0[0]-V1[0]) <= abs(V0[0])*THRESHOLD_PERCENT_LOC &&
00014             abs(V0[1]-V1[1]) <= abs(V0[1])*THRESHOLD_PERCENT_LOC &&
00015             abs(V0[2]-V1[2]) <= abs(V0[2])*THRESHOLD_PERCENT_LOC)
00016                 return false;
00017         else
00018                 return true;
00019 }
00020 
00021 int Location::read(char *buf, size_t maxLen, char *status, size_t num)
00022 {
00023         static const char *fmt = "108,%ld,%.2f,%.6f,%.6f\r\n109,%ld,%.2f,%.6f,%.6f";
00024         GPSTracker::Position pos;
00025         if (!gpsTracker.position(&pos)) {
00026                 return 0;
00027         }
00028         float data[3] = { pos.altitude, pos.latitude, pos.longitude };
00029 
00030         float t_interval = timer.read();
00031         if (!valueChanged(oldValues, data) && t_interval < TIME_LIMIT_LOC) {
00032                 return 0;
00033         }
00034 
00035         size_t l = snprintf(buf, maxLen, fmt, deviceID, data[0], data[1],
00036                             data[2], deviceID, data[0], data[1], data[2]);
00037         if (status) {
00038                 snprintf(status, num, "Send GPS %.1f,%.1f,%.1f",
00039                          data[0], data[1], data[2]);
00040         }
00041         oldValues[0] = data[0];
00042         oldValues[1] = data[1];
00043         oldValues[2] = data[2];
00044         timer.reset();
00045         return l;
00046 }