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