Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Wed May 13 13:54:17 2015 +0000
Revision:
108:2ec12f10ebf4
Parent:
107:f1ee3e1eb126
Child:
112:3872569be2af
ReportThread now merges pending state before report.

Who changed what in which revision?

UserRevisionLine numberNew 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 99:47ea098f8a47 6 _client(), tpl(), _bootstrap(_client, deviceInfo),
xinlei 100:dbcd3bc51758 7 _integration(_client, tpl, deviceInfo), lcdThirdLineBlank(true),
xinlei 107:f1ee3e1eb126 8 signal(deviceInfo), temp(), poti(), gps(), acc(), sock(),
xinlei 107:f1ee3e1eb126 9 pool(), _operationSupport(tpl, pool)
Cumulocity 41:804f6a0bda26 10 {
xinlei 107:f1ee3e1eb126 11 reporters[0] = &ConfigSync::inst();
xinlei 99:47ea098f8a47 12 reporters[1] = &temp;
xinlei 106:fc5f25f0e0d5 13 reporters[2] = &signal;
xinlei 99:47ea098f8a47 14 reporters[3] = &gps;
xinlei 99:47ea098f8a47 15 reporters[4] = &acc;
xinlei 106:fc5f25f0e0d5 16 reporters[5] = &poti;
Cumulocity 41:804f6a0bda26 17 }
Cumulocity 41:804f6a0bda26 18
Cumulocity 41:804f6a0bda26 19 bool MbedAgent::init()
Cumulocity 41:804f6a0bda26 20 {
xinlei 71:063c45e99578 21 bool flag = true;
xinlei 100:dbcd3bc51758 22
xinlei 100:dbcd3bc51758 23 // Insert measurement Acceleration
xinlei 100:dbcd3bc51758 24 // USAGE: 106,<DEVICE/ID>,<X>,<Y>,<Z>
xinlei 100:dbcd3bc51758 25 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 26 flag = false;
xinlei 100:dbcd3bc51758 27
xinlei 100:dbcd3bc51758 28 // Insert measurement Potentiometer
xinlei 100:dbcd3bc51758 29 // USAGE: 107,<DEVICE/ID>,<ANALOG1>,<ANALOG2>
xinlei 100:dbcd3bc51758 30 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 31 flag = false;
xinlei 100:dbcd3bc51758 32
xinlei 100:dbcd3bc51758 33 // Update device position
xinlei 100:dbcd3bc51758 34 // USAGE: 108,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE>
xinlei 100:dbcd3bc51758 35 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 36 flag = false;
xinlei 100:dbcd3bc51758 37
xinlei 100:dbcd3bc51758 38 // Insert measurement Location
xinlei 100:dbcd3bc51758 39 // USAGE: 109,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE>
xinlei 100:dbcd3bc51758 40 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 41 flag = false;
xinlei 100:dbcd3bc51758 42
xinlei 100:dbcd3bc51758 43 // Insert measurement Signal Quality
xinlei 100:dbcd3bc51758 44 // USAGE: 104,<DEVICE/ID>,<RSSI>,<BER>
xinlei 100:dbcd3bc51758 45 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 46 flag = false;
xinlei 100:dbcd3bc51758 47
xinlei 100:dbcd3bc51758 48 // Insert measurement Temperature
xinlei 100:dbcd3bc51758 49 // USAGE: 105,<DEVICE/ID>,<TEMPERATURE>
xinlei 100:dbcd3bc51758 50 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 51 flag = false;
xinlei 100:dbcd3bc51758 52
xinlei 100:dbcd3bc51758 53 // Update Configuration
xinlei 100:dbcd3bc51758 54 // Usage: 130,<DEVICE/ID>,<CONFIG/STRING>,<RESPONSIBILITY>
xinlei 100:dbcd3bc51758 55 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 56 flag = false;
xinlei 100:dbcd3bc51758 57
xinlei 71:063c45e99578 58 if (!_integration.init()) {
xinlei 95:5dfdc8568e9f 59 LCDDisplay::inst().setLines("Integrate init fail");
xinlei 71:063c45e99578 60 flag = false;
xinlei 71:063c45e99578 61 }
xinlei 71:063c45e99578 62 if (!_operationSupport.init()) {
xinlei 95:5dfdc8568e9f 63 LCDDisplay::inst().setLines("Operation init fail");
xinlei 71:063c45e99578 64 flag = false;
xinlei 71:063c45e99578 65 }
xinlei 100:dbcd3bc51758 66 for (size_t i = 0; i < N; ++i) {
xinlei 100:dbcd3bc51758 67 if (!reporters[i]->init()) {
xinlei 100:dbcd3bc51758 68 aError("Init %s", reporters[i]->name());
xinlei 100:dbcd3bc51758 69 }
xinlei 100:dbcd3bc51758 70 }
xinlei 100:dbcd3bc51758 71 return flag;
Cumulocity 41:804f6a0bda26 72 }
Cumulocity 41:804f6a0bda26 73
xinlei 92:0acd11870c6a 74 int MbedAgent::run()
Cumulocity 41:804f6a0bda26 75 {
Cumulocity 41:804f6a0bda26 76 // device bootstrapping process
xinlei 105:c61f0d62b625 77 if (!_bootstrap.setUpCredentials()) {
xinlei 105:c61f0d62b625 78 LCDDisplay::inst().setLines("Bootstrap error");
xinlei 92:0acd11870c6a 79 return -1;
xinlei 105:c61f0d62b625 80 }
xinlei 93:61d44636f020 81 setAuth(_bootstrap.username(), _bootstrap.password());
xinlei 98:e369fc75c000 82 aInfo("Set auth: %s:%s (%s)\n", srUsername, srPassword, srAuthStr);
vwochnik 52:8f1370084268 83
xinlei 98:e369fc75c000 84 Thread::wait(2000);
Cumulocity 41:804f6a0bda26 85
xinlei 98:e369fc75c000 86 LCDDisplay::inst().setLines("Connect to Cloud", srHost);
Cumulocity 41:804f6a0bda26 87 if (!_integration.integrate()) {
xinlei 105:c61f0d62b625 88 LCDDisplay::inst().setLines("Integrate failure");
xinlei 92:0acd11870c6a 89 return -2;
Cumulocity 41:804f6a0bda26 90 }
xinlei 98:e369fc75c000 91 setX_ID(_client.getIdentifier());
xinlei 100:dbcd3bc51758 92 aInfo("Set X-ID: %s\n", srX_ID);
xinlei 93:61d44636f020 93
xinlei 92:0acd11870c6a 94 return 0;
Cumulocity 41:804f6a0bda26 95 }
Cumulocity 41:804f6a0bda26 96
Cumulocity 41:804f6a0bda26 97 void MbedAgent::loop()
Cumulocity 41:804f6a0bda26 98 {
xinlei 100:dbcd3bc51758 99 ReportThread reportThread(pool);
xinlei 106:fc5f25f0e0d5 100 _operationSupport.executePendingOperations();
xinlei 108:2ec12f10ebf4 101 PollThread pollThread(pool);
xinlei 108:2ec12f10ebf4 102 pollThread.setChannel(deviceID);
xinlei 93:61d44636f020 103
xinlei 76:b07effe83fb8 104 Watchdog wdt;
xinlei 100:dbcd3bc51758 105 wdt.kick(60.0); // set a 60.0 seconds watchdog
Cumulocity 41:804f6a0bda26 106 while (true) {
xinlei 106:fc5f25f0e0d5 107 int l = 0;
xinlei 100:dbcd3bc51758 108 for (size_t i = 0; i < N; ++i) {
xinlei 108:2ec12f10ebf4 109 // if (reporters[i] == NULL) {
xinlei 106:fc5f25f0e0d5 110 int l2 = reporters[i]->read(buf2+l, sizeof(buf2)-l, status, DISPLAY_LEN);
xinlei 106:fc5f25f0e0d5 111 if (l2) { // Refresh LCD display needed
xinlei 100:dbcd3bc51758 112 LCDDisplay::inst().setThirdLine(status);
xinlei 106:fc5f25f0e0d5 113 Thread::wait(400);
xinlei 106:fc5f25f0e0d5 114 } else if (!lcdThirdLineBlank && !l2) { // Clear LCD display needed
xinlei 100:dbcd3bc51758 115 LCDDisplay::inst().setThirdLine("");
xinlei 106:fc5f25f0e0d5 116 Thread::wait(100);
xinlei 100:dbcd3bc51758 117 }
xinlei 106:fc5f25f0e0d5 118 lcdThirdLineBlank = !l2;
xinlei 106:fc5f25f0e0d5 119 l += l2;
xinlei 108:2ec12f10ebf4 120 // }
xinlei 106:fc5f25f0e0d5 121 }
xinlei 106:fc5f25f0e0d5 122 if (l) {
xinlei 106:fc5f25f0e0d5 123 l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2);
xinlei 106:fc5f25f0e0d5 124 l = sock.sendOnly(buf, l);
xinlei 106:fc5f25f0e0d5 125 if (l < 0)
xinlei 106:fc5f25f0e0d5 126 aWarning("%s\n", status);
xinlei 100:dbcd3bc51758 127 }
xinlei 76:b07effe83fb8 128 wdt.kick(); // reset watchdog timer
Cumulocity 41:804f6a0bda26 129 }
Cumulocity 41:804f6a0bda26 130 }