![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
MbedAgent.cpp@124:979ab0d075de, 2015-06-01 (annotated)
- Committer:
- xinlei
- Date:
- Mon Jun 01 11:24:17 2015 +0000
- Revision:
- 124:979ab0d075de
- Parent:
- 121:68217ccb8cd1
- Child:
- 125:e1fcf720c63a
Poti: sample interval extended to 10 ms.; Polling: pointer null checking in handshake
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 41:804f6a0bda26 | 1 | #include "MbedAgent.h" |
xinlei | 108:2ec12f10ebf4 | 2 | #include "watchdog.h" |
xinlei | 72:c5709ae7b193 | 3 | #include "logging.h" |
Cumulocity | 41:804f6a0bda26 | 4 | |
xinlei | 98:e369fc75c000 | 5 | MbedAgent::MbedAgent(DeviceInfo& deviceInfo): |
xinlei | 116:5de54f09f754 | 6 | client(), tpl(), _bootstrap(deviceInfo), _integration(client, tpl, deviceInfo), |
xinlei | 116:5de54f09f754 | 7 | lcdThirdLineBlank(true), signal(deviceInfo), temp(), poti(), gps(), |
xinlei | 116:5de54f09f754 | 8 | acc(), sock(), pool(), _operationSupport(tpl, pool) |
Cumulocity | 41:804f6a0bda26 | 9 | { |
xinlei | 107:f1ee3e1eb126 | 10 | reporters[0] = &ConfigSync::inst(); |
xinlei | 99:47ea098f8a47 | 11 | reporters[1] = &temp; |
xinlei | 106:fc5f25f0e0d5 | 12 | reporters[2] = &signal; |
xinlei | 99:47ea098f8a47 | 13 | reporters[3] = &gps; |
xinlei | 99:47ea098f8a47 | 14 | reporters[4] = &acc; |
xinlei | 106:fc5f25f0e0d5 | 15 | reporters[5] = &poti; |
Cumulocity | 41:804f6a0bda26 | 16 | } |
Cumulocity | 41:804f6a0bda26 | 17 | |
Cumulocity | 41:804f6a0bda26 | 18 | bool MbedAgent::init() |
Cumulocity | 41:804f6a0bda26 | 19 | { |
xinlei | 71:063c45e99578 | 20 | bool flag = true; |
xinlei | 100:dbcd3bc51758 | 21 | |
xinlei | 100:dbcd3bc51758 | 22 | // Insert measurement Acceleration |
xinlei | 100:dbcd3bc51758 | 23 | // USAGE: 106,<DEVICE/ID>,<X>,<Y>,<Z> |
xinlei | 100:dbcd3bc51758 | 24 | if (!tpl.add("10,106,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER NUMBER NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_MotionMeasurement\"\",\"\"c8y_MotionMeasurement\"\":{\"\"x\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"m/s^2\"\"},\"\"y\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"m/s^2\"\"},\"\"z\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"m/s^2\"\"}}}\"\r\n")) |
xinlei | 100:dbcd3bc51758 | 25 | flag = false; |
xinlei | 100:dbcd3bc51758 | 26 | |
xinlei | 100:dbcd3bc51758 | 27 | // Insert measurement Potentiometer |
xinlei | 100:dbcd3bc51758 | 28 | // USAGE: 107,<DEVICE/ID>,<ANALOG1>,<ANALOG2> |
xinlei | 100:dbcd3bc51758 | 29 | 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")) |
xinlei | 100:dbcd3bc51758 | 30 | flag = false; |
xinlei | 100:dbcd3bc51758 | 31 | |
xinlei | 100:dbcd3bc51758 | 32 | // Update device position |
xinlei | 100:dbcd3bc51758 | 33 | // USAGE: 108,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE> |
xinlei | 100:dbcd3bc51758 | 34 | if (!tpl.add("10,108,PUT,/inventory/managedObjects/%%,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,UNSIGNED NUMBER NUMBER NUMBER,\"{\"\"c8y_Position\"\":{\"\"alt\"\":%%,\"\"lat\"\":%%,\"\"lng\"\":%%},\"\"c8y_MotionTracking\"\":{\"\"active\"\":true}}\"\r\n")) |
xinlei | 100:dbcd3bc51758 | 35 | flag = false; |
xinlei | 100:dbcd3bc51758 | 36 | |
xinlei | 100:dbcd3bc51758 | 37 | // Insert measurement Location |
xinlei | 100:dbcd3bc51758 | 38 | // USAGE: 109,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE> |
xinlei | 100:dbcd3bc51758 | 39 | if (!tpl.add("10,109,POST,/event/events,application/vnd.com.nsn.cumulocity.event+json,application/vnd.com.nsn.cumulocity.event+json,%%,NOW UNSIGNED NUMBER NUMBER NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_LocationUpdate\"\",\"\"text\"\":\"\"Mbed location update\"\",\"\"c8y_Position\"\":{\"\"alt\"\":%%,\"\"lat\"\":%%,\"\"lng\"\":%%}}\"\r\n")) |
xinlei | 100:dbcd3bc51758 | 40 | flag = false; |
xinlei | 100:dbcd3bc51758 | 41 | |
xinlei | 100:dbcd3bc51758 | 42 | // Insert measurement Signal Quality |
xinlei | 100:dbcd3bc51758 | 43 | // USAGE: 104,<DEVICE/ID>,<RSSI>,<BER> |
xinlei | 100:dbcd3bc51758 | 44 | 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")) |
xinlei | 100:dbcd3bc51758 | 45 | flag = false; |
xinlei | 100:dbcd3bc51758 | 46 | |
xinlei | 100:dbcd3bc51758 | 47 | // Insert measurement Temperature |
xinlei | 100:dbcd3bc51758 | 48 | // USAGE: 105,<DEVICE/ID>,<TEMPERATURE> |
xinlei | 100:dbcd3bc51758 | 49 | if (!tpl.add("10,105,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_TemperatureMeasurement\"\",\"\"c8y_TemperatureMeasurement\"\":{\"\"T\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"C\"\"}}}\"\r\n")) |
xinlei | 100:dbcd3bc51758 | 50 | flag = false; |
xinlei | 100:dbcd3bc51758 | 51 | |
xinlei | 100:dbcd3bc51758 | 52 | // Update Configuration |
xinlei | 100:dbcd3bc51758 | 53 | // Usage: 130,<DEVICE/ID>,<CONFIG/STRING>,<RESPONSIBILITY> |
xinlei | 100:dbcd3bc51758 | 54 | if (!tpl.add("10,130,PUT,/inventory/managedObjects/%%,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,UNSIGNED STRING NUMBER,\"{\"\"c8y_Configuration\"\":{\"\"config\"\":\"\"%%\"\"},\"\"c8y_RequiredAvailability\"\":{ \"\"responseInterval\"\":%%}}\"\r\n")) |
xinlei | 100:dbcd3bc51758 | 55 | flag = false; |
xinlei | 100:dbcd3bc51758 | 56 | |
xinlei | 71:063c45e99578 | 57 | if (!_integration.init()) { |
xinlei | 95:5dfdc8568e9f | 58 | LCDDisplay::inst().setLines("Integrate init fail"); |
xinlei | 71:063c45e99578 | 59 | flag = false; |
xinlei | 71:063c45e99578 | 60 | } |
xinlei | 71:063c45e99578 | 61 | if (!_operationSupport.init()) { |
xinlei | 95:5dfdc8568e9f | 62 | LCDDisplay::inst().setLines("Operation init fail"); |
xinlei | 71:063c45e99578 | 63 | flag = false; |
xinlei | 71:063c45e99578 | 64 | } |
xinlei | 100:dbcd3bc51758 | 65 | for (size_t i = 0; i < N; ++i) { |
xinlei | 100:dbcd3bc51758 | 66 | if (!reporters[i]->init()) { |
xinlei | 100:dbcd3bc51758 | 67 | aError("Init %s", reporters[i]->name()); |
xinlei | 100:dbcd3bc51758 | 68 | } |
xinlei | 100:dbcd3bc51758 | 69 | } |
xinlei | 100:dbcd3bc51758 | 70 | return flag; |
Cumulocity | 41:804f6a0bda26 | 71 | } |
Cumulocity | 41:804f6a0bda26 | 72 | |
xinlei | 92:0acd11870c6a | 73 | int MbedAgent::run() |
Cumulocity | 41:804f6a0bda26 | 74 | { |
Cumulocity | 41:804f6a0bda26 | 75 | // device bootstrapping process |
xinlei | 112:3872569be2af | 76 | if (!_bootstrap.bootstrap()) { |
xinlei | 116:5de54f09f754 | 77 | LCDDisplay::inst().setLines("Bootstrap Failure"); |
xinlei | 92:0acd11870c6a | 78 | return -1; |
xinlei | 105:c61f0d62b625 | 79 | } |
xinlei | 116:5de54f09f754 | 80 | |
xinlei | 98:e369fc75c000 | 81 | Thread::wait(2000); |
xinlei | 98:e369fc75c000 | 82 | LCDDisplay::inst().setLines("Connect to Cloud", srHost); |
xinlei | 116:5de54f09f754 | 83 | setX_ID(UBLOX_SMARTREST_VERSION); |
Cumulocity | 41:804f6a0bda26 | 84 | if (!_integration.integrate()) { |
xinlei | 105:c61f0d62b625 | 85 | LCDDisplay::inst().setLines("Integrate failure"); |
xinlei | 92:0acd11870c6a | 86 | return -2; |
Cumulocity | 41:804f6a0bda26 | 87 | } |
xinlei | 116:5de54f09f754 | 88 | setX_ID(client.getIdentifier()); |
xinlei | 100:dbcd3bc51758 | 89 | aInfo("Set X-ID: %s\n", srX_ID); |
xinlei | 93:61d44636f020 | 90 | |
xinlei | 92:0acd11870c6a | 91 | return 0; |
Cumulocity | 41:804f6a0bda26 | 92 | } |
Cumulocity | 41:804f6a0bda26 | 93 | |
Cumulocity | 41:804f6a0bda26 | 94 | void MbedAgent::loop() |
Cumulocity | 41:804f6a0bda26 | 95 | { |
xinlei | 100:dbcd3bc51758 | 96 | ReportThread reportThread(pool); |
xinlei | 106:fc5f25f0e0d5 | 97 | _operationSupport.executePendingOperations(); |
xinlei | 108:2ec12f10ebf4 | 98 | PollThread pollThread(pool); |
xinlei | 108:2ec12f10ebf4 | 99 | pollThread.setChannel(deviceID); |
xinlei | 93:61d44636f020 | 100 | |
xinlei | 76:b07effe83fb8 | 101 | Watchdog wdt; |
xinlei | 100:dbcd3bc51758 | 102 | wdt.kick(60.0); // set a 60.0 seconds watchdog |
Cumulocity | 41:804f6a0bda26 | 103 | while (true) { |
xinlei | 106:fc5f25f0e0d5 | 104 | int l = 0; |
xinlei | 100:dbcd3bc51758 | 105 | for (size_t i = 0; i < N; ++i) { |
xinlei | 124:979ab0d075de | 106 | if (reporters[i] == &ConfigSync::inst()) { |
xinlei | 106:fc5f25f0e0d5 | 107 | int l2 = reporters[i]->read(buf2+l, sizeof(buf2)-l, status, DISPLAY_LEN); |
xinlei | 106:fc5f25f0e0d5 | 108 | if (l2) { // Refresh LCD display needed |
xinlei | 100:dbcd3bc51758 | 109 | LCDDisplay::inst().setThirdLine(status); |
xinlei | 120:d4d44550e087 | 110 | Thread::wait(400); |
xinlei | 106:fc5f25f0e0d5 | 111 | } else if (!lcdThirdLineBlank && !l2) { // Clear LCD display needed |
xinlei | 100:dbcd3bc51758 | 112 | LCDDisplay::inst().setThirdLine(""); |
xinlei | 106:fc5f25f0e0d5 | 113 | Thread::wait(100); |
xinlei | 100:dbcd3bc51758 | 114 | } |
xinlei | 106:fc5f25f0e0d5 | 115 | lcdThirdLineBlank = !l2; |
xinlei | 106:fc5f25f0e0d5 | 116 | l += l2; |
xinlei | 124:979ab0d075de | 117 | } |
xinlei | 106:fc5f25f0e0d5 | 118 | } |
xinlei | 106:fc5f25f0e0d5 | 119 | if (l) { |
xinlei | 106:fc5f25f0e0d5 | 120 | l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2); |
xinlei | 121:68217ccb8cd1 | 121 | sock.setBlocking(3000); |
xinlei | 116:5de54f09f754 | 122 | l = sock.sendOnly(buf, l); |
xinlei | 106:fc5f25f0e0d5 | 123 | if (l < 0) |
xinlei | 106:fc5f25f0e0d5 | 124 | aWarning("%s\n", status); |
xinlei | 100:dbcd3bc51758 | 125 | } |
xinlei | 76:b07effe83fb8 | 126 | wdt.kick(); // reset watchdog timer |
Cumulocity | 41:804f6a0bda26 | 127 | } |
Cumulocity | 41:804f6a0bda26 | 128 | } |