
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
measurement/SignalQualityMeasurement.cpp@71:063c45e99578, 2015-02-16 (annotated)
- Committer:
- xinlei
- Date:
- Mon Feb 16 09:17:30 2015 +0000
- Revision:
- 71:063c45e99578
- Parent:
- 56:4d6e34f1589d
- Child:
- 72:c5709ae7b193
Various minor bug fixes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 41:804f6a0bda26 | 1 | #include "SignalQualityMeasurement.h" |
Cumulocity | 41:804f6a0bda26 | 2 | #include "ComposedRecord.h" |
Cumulocity | 41:804f6a0bda26 | 3 | #include "CharValue.h" |
Cumulocity | 41:804f6a0bda26 | 4 | #include "IntegerValue.h" |
Cumulocity | 41:804f6a0bda26 | 5 | #include "FloatValue.h" |
Cumulocity | 41:804f6a0bda26 | 6 | |
xinlei | 71:063c45e99578 | 7 | #define THRESHOLD_PERCENT_SIG 0.05 // Percentage cut-off for avoiding sending similar signal sensor data. |
xinlei | 71:063c45e99578 | 8 | #define TIME_LIMIT_SIG 900 // Time interval for forcing a sending even if signal sensor readings are constantly similar. |
xinlei | 71:063c45e99578 | 9 | |
vwochnik | 55:a0f7295ed6b6 | 10 | SignalQualityMeasurement::SignalQualityMeasurement(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, DeviceInfo& deviceInfo) : |
Cumulocity | 41:804f6a0bda26 | 11 | _client(client), |
Cumulocity | 41:804f6a0bda26 | 12 | _tpl(tpl), |
Cumulocity | 41:804f6a0bda26 | 13 | _deviceId(deviceId), |
Cumulocity | 41:804f6a0bda26 | 14 | _deviceInfo(deviceInfo) |
Cumulocity | 41:804f6a0bda26 | 15 | { |
Cumulocity | 41:804f6a0bda26 | 16 | _init = false; |
xinlei | 71:063c45e99578 | 17 | oldValues[0] = 0; |
xinlei | 71:063c45e99578 | 18 | oldValues[1] = 0; |
xinlei | 71:063c45e99578 | 19 | sendingTimer.start(); |
Cumulocity | 41:804f6a0bda26 | 20 | } |
Cumulocity | 41:804f6a0bda26 | 21 | |
Cumulocity | 41:804f6a0bda26 | 22 | bool SignalQualityMeasurement::init() |
Cumulocity | 41:804f6a0bda26 | 23 | { |
Cumulocity | 41:804f6a0bda26 | 24 | if (_init) |
Cumulocity | 41:804f6a0bda26 | 25 | return false; |
Cumulocity | 41:804f6a0bda26 | 26 | |
Cumulocity | 41:804f6a0bda26 | 27 | // Insert measurement |
Cumulocity | 41:804f6a0bda26 | 28 | // USAGE: 104,<DEVICE/ID>,<RSSI>,<BER> |
Cumulocity | 41:804f6a0bda26 | 29 | if (!_tpl.add("10,104,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER UNSIGNED,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_SignalStrength\"\",\"\"c8y_SignalStrength\"\":{\"\"rssi\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"dBm\"\"},\"\"ber\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"%\"\"}}}\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 30 | return false; |
Cumulocity | 41:804f6a0bda26 | 31 | |
Cumulocity | 41:804f6a0bda26 | 32 | _init = true; |
Cumulocity | 41:804f6a0bda26 | 33 | return true; |
Cumulocity | 41:804f6a0bda26 | 34 | } |
Cumulocity | 41:804f6a0bda26 | 35 | |
Cumulocity | 41:804f6a0bda26 | 36 | bool SignalQualityMeasurement::run() |
Cumulocity | 41:804f6a0bda26 | 37 | { |
Cumulocity | 41:804f6a0bda26 | 38 | DeviceInfo::SignalQuality *signalQuality; |
Cumulocity | 41:804f6a0bda26 | 39 | |
Cumulocity | 41:804f6a0bda26 | 40 | if ((signalQuality = _deviceInfo.signalQuality()) == NULL) |
Cumulocity | 41:804f6a0bda26 | 41 | return false; |
Cumulocity | 41:804f6a0bda26 | 42 | |
xinlei | 71:063c45e99578 | 43 | float data[2] = {0, 0}; |
xinlei | 71:063c45e99578 | 44 | data[0] = signalQuality->rssi; |
xinlei | 71:063c45e99578 | 45 | data[1] = signalQuality->ber; |
xinlei | 71:063c45e99578 | 46 | if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_SIG && |
xinlei | 71:063c45e99578 | 47 | abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_SIG) { |
xinlei | 71:063c45e99578 | 48 | if (sendingTimer.read() < TIME_LIMIT_SIG) { |
xinlei | 71:063c45e99578 | 49 | printf("Similar signal readings found, no sending!\r\n"); |
xinlei | 71:063c45e99578 | 50 | return true; |
xinlei | 71:063c45e99578 | 51 | } else { |
xinlei | 71:063c45e99578 | 52 | printf("Sending timer of signal sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); |
xinlei | 71:063c45e99578 | 53 | } |
xinlei | 71:063c45e99578 | 54 | } |
xinlei | 71:063c45e99578 | 55 | |
Cumulocity | 41:804f6a0bda26 | 56 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 57 | IntegerValue msgId(104); |
Cumulocity | 41:804f6a0bda26 | 58 | IntegerValue devId(_deviceId); |
Cumulocity | 41:804f6a0bda26 | 59 | IntegerValue rssi(signalQuality->rssi); |
Cumulocity | 41:804f6a0bda26 | 60 | IntegerValue ber(signalQuality->ber); |
Cumulocity | 41:804f6a0bda26 | 61 | if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(rssi)) || (!record.add(ber))) |
Cumulocity | 41:804f6a0bda26 | 62 | return false; |
Cumulocity | 41:804f6a0bda26 | 63 | |
Cumulocity | 41:804f6a0bda26 | 64 | if (_client.send(record) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 65 | puts("Signal measurement failed."); |
Cumulocity | 41:804f6a0bda26 | 66 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 67 | return false; |
Cumulocity | 41:804f6a0bda26 | 68 | } |
xinlei | 71:063c45e99578 | 69 | |
Cumulocity | 41:804f6a0bda26 | 70 | _client.stop(); |
xinlei | 71:063c45e99578 | 71 | oldValues[0] = data[0]; |
xinlei | 71:063c45e99578 | 72 | oldValues[1] = data[1]; |
xinlei | 71:063c45e99578 | 73 | sendingTimer.reset(); |
xinlei | 71:063c45e99578 | 74 | printf("Signal quality measurement sent.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 75 | return true; |
Cumulocity | 41:804f6a0bda26 | 76 | } |