portable version of the cumulocity demo

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AnalogMeasurement.cpp Source File

AnalogMeasurement.cpp

00001 #include "AnalogMeasurement.h"
00002 #include "ComposedRecord.h"
00003 #include "CharValue.h"
00004 #include "IntegerValue.h"
00005 #include "FloatValue.h"
00006 
00007 AnalogMeasurement::AnalogMeasurement(SmartRest& client, SmartRestTemplate& tpl, long& deviceId, AnalogIn& analog1, AnalogIn& analog2) :
00008     _client(client),
00009     _tpl(tpl),
00010     _deviceId(deviceId),
00011     _analog1(analog1),
00012     _analog2(analog2)
00013 {
00014     _init = false;
00015 }
00016 
00017 bool AnalogMeasurement::init()
00018 {
00019     if (_init)
00020         return false;
00021     
00022     // Insert measurement
00023     // USAGE: 107,<DEVICE/ID>,<ANALOG1>,<ANALOG2>
00024     if (!_tpl.add("10,107,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_AnalogMeasurement\"\",\"\"c8y_AnalogMeasurement\"\":{\"\"A1\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"A\"\"},\"\"A2\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"A\"\"}}}\"\r\n"))
00025         return false;
00026 
00027     _init = true;
00028     return true;
00029 }
00030 
00031 bool AnalogMeasurement::run()
00032 {
00033     ComposedRecord record;
00034     IntegerValue msgId(107);
00035     IntegerValue devId(_deviceId);
00036     FloatValue analog1(_analog1.read(), 1);
00037     FloatValue analog2(_analog2.read(), 1);
00038     if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(analog1)) || (!record.add(analog2)))
00039         return false;
00040 
00041     if (_client.send(record) != SMARTREST_SUCCESS) {
00042         puts("Signal measurement failed.");
00043         _client.stop();
00044         return false;
00045     }
00046     _client.stop();
00047     return true;
00048 }