Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Fri May 15 14:07:51 2015 +0000
Revision:
112:3872569be2af
Parent:
108:2ec12f10ebf4
Child:
114:c54e9731b9de
data smoothing for pots readings

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 112:3872569be2af 77 if (!_bootstrap.bootstrap()) {
xinlei 92:0acd11870c6a 78 return -1;
xinlei 105:c61f0d62b625 79 }
xinlei 98:e369fc75c000 80 Thread::wait(2000);
Cumulocity 41:804f6a0bda26 81
xinlei 98:e369fc75c000 82 LCDDisplay::inst().setLines("Connect to Cloud", srHost);
Cumulocity 41:804f6a0bda26 83 if (!_integration.integrate()) {
xinlei 105:c61f0d62b625 84 LCDDisplay::inst().setLines("Integrate failure");
xinlei 92:0acd11870c6a 85 return -2;
Cumulocity 41:804f6a0bda26 86 }
xinlei 98:e369fc75c000 87 setX_ID(_client.getIdentifier());
xinlei 100:dbcd3bc51758 88 aInfo("Set X-ID: %s\n", srX_ID);
xinlei 93:61d44636f020 89
xinlei 92:0acd11870c6a 90 return 0;
Cumulocity 41:804f6a0bda26 91 }
Cumulocity 41:804f6a0bda26 92
Cumulocity 41:804f6a0bda26 93 void MbedAgent::loop()
Cumulocity 41:804f6a0bda26 94 {
xinlei 100:dbcd3bc51758 95 ReportThread reportThread(pool);
xinlei 106:fc5f25f0e0d5 96 _operationSupport.executePendingOperations();
xinlei 108:2ec12f10ebf4 97 PollThread pollThread(pool);
xinlei 108:2ec12f10ebf4 98 pollThread.setChannel(deviceID);
xinlei 93:61d44636f020 99
xinlei 76:b07effe83fb8 100 Watchdog wdt;
xinlei 100:dbcd3bc51758 101 wdt.kick(60.0); // set a 60.0 seconds watchdog
Cumulocity 41:804f6a0bda26 102 while (true) {
xinlei 106:fc5f25f0e0d5 103 int l = 0;
xinlei 100:dbcd3bc51758 104 for (size_t i = 0; i < N; ++i) {
xinlei 112:3872569be2af 105 // if (reporters[i] == &ConfigSync::inst()) {
xinlei 106:fc5f25f0e0d5 106 int l2 = reporters[i]->read(buf2+l, sizeof(buf2)-l, status, DISPLAY_LEN);
xinlei 106:fc5f25f0e0d5 107 if (l2) { // Refresh LCD display needed
xinlei 100:dbcd3bc51758 108 LCDDisplay::inst().setThirdLine(status);
xinlei 106:fc5f25f0e0d5 109 Thread::wait(400);
xinlei 106:fc5f25f0e0d5 110 } else if (!lcdThirdLineBlank && !l2) { // Clear LCD display needed
xinlei 100:dbcd3bc51758 111 LCDDisplay::inst().setThirdLine("");
xinlei 106:fc5f25f0e0d5 112 Thread::wait(100);
xinlei 100:dbcd3bc51758 113 }
xinlei 106:fc5f25f0e0d5 114 lcdThirdLineBlank = !l2;
xinlei 106:fc5f25f0e0d5 115 l += l2;
xinlei 108:2ec12f10ebf4 116 // }
xinlei 106:fc5f25f0e0d5 117 }
xinlei 106:fc5f25f0e0d5 118 if (l) {
xinlei 106:fc5f25f0e0d5 119 l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2);
xinlei 112:3872569be2af 120 // l = sock.sendOnly(buf, l);
xinlei 112:3872569be2af 121 l = sock.sendAndReceive(buf, l, sizeof(buf));
xinlei 106:fc5f25f0e0d5 122 if (l < 0)
xinlei 106:fc5f25f0e0d5 123 aWarning("%s\n", status);
xinlei 100:dbcd3bc51758 124 }
xinlei 76:b07effe83fb8 125 wdt.kick(); // reset watchdog timer
Cumulocity 41:804f6a0bda26 126 }
Cumulocity 41:804f6a0bda26 127 }