
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
measurement/AnalogMeasurement.cpp@88:8ab476939897, 2015-03-16 (annotated)
- Committer:
- xinlei
- Date:
- Mon Mar 16 10:16:48 2015 +0000
- Revision:
- 88:8ab476939897
- Parent:
- 85:b49c4cfecc43
- Child:
- 90:423177e8a401
Bumped version to v2.1rc1.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 47:89ae46d5c466 | 1 | #include "AnalogMeasurement.h" |
Cumulocity | 47:89ae46d5c466 | 2 | #include "ComposedRecord.h" |
Cumulocity | 47:89ae46d5c466 | 3 | #include "CharValue.h" |
Cumulocity | 47:89ae46d5c466 | 4 | #include "IntegerValue.h" |
Cumulocity | 47:89ae46d5c466 | 5 | #include "FloatValue.h" |
xinlei | 72:c5709ae7b193 | 6 | #include "logging.h" |
Cumulocity | 47:89ae46d5c466 | 7 | |
xinlei | 77:f6717e4eccc4 | 8 | #define THRESHOLD_PERCENT_ANA 0.02 // Percentage cut-off for avoiding sending similar analog sensor data. |
xinlei | 73:313975bfec96 | 9 | // Time interval for forcing a sending even if analog sensor readings are constantly similar (in seconds). |
xinlei | 73:313975bfec96 | 10 | #define TIME_LIMIT_ANA 900 |
xinlei | 71:063c45e99578 | 11 | |
xinlei | 73:313975bfec96 | 12 | AnalogMeasurement::AnalogMeasurement(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, AnalogIn& analog1, |
xinlei | 82:ca7430f50b2b | 13 | AnalogIn& analog2, DeviceIO& io, DeviceInfo& deviceInfo, DisplayInfo& displayInfo) : |
Cumulocity | 47:89ae46d5c466 | 14 | _client(client), |
Cumulocity | 47:89ae46d5c466 | 15 | _tpl(tpl), |
Cumulocity | 47:89ae46d5c466 | 16 | _deviceId(deviceId), |
Cumulocity | 47:89ae46d5c466 | 17 | _analog1(analog1), |
xinlei | 73:313975bfec96 | 18 | _analog2(analog2), |
xinlei | 73:313975bfec96 | 19 | _io(io), |
xinlei | 73:313975bfec96 | 20 | _deviceInfo(deviceInfo), |
xinlei | 82:ca7430f50b2b | 21 | _displayInfo(displayInfo) |
Cumulocity | 47:89ae46d5c466 | 22 | { |
Cumulocity | 47:89ae46d5c466 | 23 | _init = false; |
xinlei | 71:063c45e99578 | 24 | oldValues[0] = 0; |
xinlei | 71:063c45e99578 | 25 | oldValues[1] = 0; |
xinlei | 71:063c45e99578 | 26 | sendingTimer.start(); |
Cumulocity | 47:89ae46d5c466 | 27 | } |
Cumulocity | 47:89ae46d5c466 | 28 | |
Cumulocity | 47:89ae46d5c466 | 29 | bool AnalogMeasurement::init() |
Cumulocity | 47:89ae46d5c466 | 30 | { |
Cumulocity | 47:89ae46d5c466 | 31 | if (_init) |
Cumulocity | 47:89ae46d5c466 | 32 | return false; |
Cumulocity | 47:89ae46d5c466 | 33 | |
Cumulocity | 47:89ae46d5c466 | 34 | // Insert measurement |
Cumulocity | 47:89ae46d5c466 | 35 | // USAGE: 107,<DEVICE/ID>,<ANALOG1>,<ANALOG2> |
Cumulocity | 47:89ae46d5c466 | 36 | 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")) |
Cumulocity | 47:89ae46d5c466 | 37 | return false; |
Cumulocity | 47:89ae46d5c466 | 38 | |
Cumulocity | 47:89ae46d5c466 | 39 | _init = true; |
Cumulocity | 47:89ae46d5c466 | 40 | return true; |
Cumulocity | 47:89ae46d5c466 | 41 | } |
Cumulocity | 47:89ae46d5c466 | 42 | |
Cumulocity | 47:89ae46d5c466 | 43 | bool AnalogMeasurement::run() |
Cumulocity | 47:89ae46d5c466 | 44 | { |
xinlei | 84:5dc5a50e4b06 | 45 | extern bool lastSensorReadingSent; |
xinlei | 85:b49c4cfecc43 | 46 | float data[2] = {_analog1.read()*100, _analog2.read()*100}; |
xinlei | 77:f6717e4eccc4 | 47 | |
xinlei | 71:063c45e99578 | 48 | if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_ANA && |
xinlei | 71:063c45e99578 | 49 | abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_ANA) { |
xinlei | 71:063c45e99578 | 50 | if (sendingTimer.read() < TIME_LIMIT_ANA) { |
xinlei | 84:5dc5a50e4b06 | 51 | if (lastSensorReadingSent) { |
xinlei | 84:5dc5a50e4b06 | 52 | _io.lcdPrint(_displayInfo.getFirstLine(), _displayInfo.getSecondLine()); |
xinlei | 84:5dc5a50e4b06 | 53 | lastSensorReadingSent=false; |
xinlei | 84:5dc5a50e4b06 | 54 | } |
xinlei | 73:313975bfec96 | 55 | aDebug("Similar analog readings found, no sending!\r\n"); |
xinlei | 88:8ab476939897 | 56 | // printf("[N] %f, %f\r\n", data[0], data[1]); |
xinlei | 71:063c45e99578 | 57 | return true; |
xinlei | 71:063c45e99578 | 58 | } else { |
xinlei | 77:f6717e4eccc4 | 59 | aDebug("Analog sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); |
xinlei | 71:063c45e99578 | 60 | } |
xinlei | 71:063c45e99578 | 61 | } |
xinlei | 73:313975bfec96 | 62 | |
xinlei | 88:8ab476939897 | 63 | // printf("[Y] %f, %f (%f, %f)\r\n", data[0], data[1], oldValues[0], oldValues[1]); |
xinlei | 73:313975bfec96 | 64 | char status[25] = {0}; |
xinlei | 74:ca3001991fdc | 65 | snprintf(status, 25, "Sending Poti %.1f,%.1f", data[0], data[1]); |
xinlei | 83:3c8ceb12b773 | 66 | _io.lcdPrint(_displayInfo.getFirstLine(), _displayInfo.getSecondLine(), status); |
Cumulocity | 47:89ae46d5c466 | 67 | ComposedRecord record; |
Cumulocity | 47:89ae46d5c466 | 68 | IntegerValue msgId(107); |
Cumulocity | 47:89ae46d5c466 | 69 | IntegerValue devId(_deviceId); |
xinlei | 71:063c45e99578 | 70 | FloatValue analog1(data[0], 1); |
xinlei | 71:063c45e99578 | 71 | FloatValue analog2(data[1], 1); |
Cumulocity | 47:89ae46d5c466 | 72 | if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(analog1)) || (!record.add(analog2))) |
Cumulocity | 47:89ae46d5c466 | 73 | return false; |
Cumulocity | 47:89ae46d5c466 | 74 | |
xinlei | 73:313975bfec96 | 75 | float t_start = sendingTimer.read(); |
Cumulocity | 47:89ae46d5c466 | 76 | if (_client.send(record) != SMARTREST_SUCCESS) { |
xinlei | 76:b07effe83fb8 | 77 | aWarning("Sending analog readings failed."); |
Cumulocity | 47:89ae46d5c466 | 78 | _client.stop(); |
Cumulocity | 47:89ae46d5c466 | 79 | return false; |
Cumulocity | 47:89ae46d5c466 | 80 | } |
xinlei | 73:313975bfec96 | 81 | float t_end = sendingTimer.read(); |
Cumulocity | 47:89ae46d5c466 | 82 | _client.stop(); |
xinlei | 74:ca3001991fdc | 83 | aInfo("Analog readings sent in %.1f.\r\n", t_end-t_start); |
xinlei | 71:063c45e99578 | 84 | oldValues[0] = data[0]; |
xinlei | 71:063c45e99578 | 85 | oldValues[1] = data[1]; |
xinlei | 71:063c45e99578 | 86 | sendingTimer.reset(); |
xinlei | 84:5dc5a50e4b06 | 87 | lastSensorReadingSent=true; |
Cumulocity | 47:89ae46d5c466 | 88 | return true; |
Cumulocity | 47:89ae46d5c466 | 89 | } |