Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
MbedAgent.cpp
00001 #include "MbedAgent.h" 00002 #include "watchdog.h" 00003 #include "logging.h" 00004 00005 MbedAgent::MbedAgent(DeviceInfo& deviceInfo): 00006 client(), tpl(), _bootstrap(deviceInfo), _integration(client, tpl, deviceInfo), 00007 lcdThirdLineBlank(true), signal(deviceInfo), temp(), poti(), gps(), 00008 acc(), sock(), pool(), _operationSupport(tpl, pool) 00009 { 00010 reporters[0] = &ConfigSync::inst(); 00011 reporters[1] = &temp; 00012 reporters[2] = &signal; 00013 reporters[3] = &gps; 00014 reporters[4] = &acc; 00015 reporters[5] = &poti; 00016 } 00017 00018 bool MbedAgent::init() 00019 { 00020 bool flag = true; 00021 00022 // Insert measurement Acceleration 00023 // USAGE: 106,<DEVICE/ID>,<X>,<Y>,<Z> 00024 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")) 00025 flag = false; 00026 00027 // Insert measurement Potentiometer 00028 // USAGE: 107,<DEVICE/ID>,<ANALOG1>,<ANALOG2> 00029 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")) 00030 flag = false; 00031 00032 // Update device position 00033 // USAGE: 108,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE> 00034 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")) 00035 flag = false; 00036 00037 // Insert measurement Location 00038 // USAGE: 109,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE> 00039 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")) 00040 flag = false; 00041 00042 // Insert measurement Signal Quality 00043 // USAGE: 104,<DEVICE/ID>,<RSSI>,<BER> 00044 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")) 00045 flag = false; 00046 00047 // Insert measurement Temperature 00048 // USAGE: 105,<DEVICE/ID>,<TEMPERATURE> 00049 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")) 00050 flag = false; 00051 00052 // Update Configuration 00053 // Usage: 130,<DEVICE/ID>,<CONFIG/STRING>,<RESPONSIBILITY> 00054 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")) 00055 flag = false; 00056 00057 if (!_integration.init()) { 00058 LCDDisplay::inst().setLines("Integrate init fail"); 00059 flag = false; 00060 } 00061 if (!_operationSupport.init()) { 00062 LCDDisplay::inst().setLines("Operation init fail"); 00063 flag = false; 00064 } 00065 for (size_t i = 0; i < N; ++i) { 00066 if (!reporters[i]->init()) { 00067 aError("Init %s", reporters[i]->name()); 00068 } 00069 } 00070 return flag; 00071 } 00072 00073 int MbedAgent::run() 00074 { 00075 // device bootstrapping process 00076 if (!_bootstrap.bootstrap()) { 00077 LCDDisplay::inst().setLines("Bootstrap Failure"); 00078 return -1; 00079 } 00080 00081 Thread::wait(2000); 00082 LCDDisplay::inst().setLines("Connect to Cloud", srHost); 00083 setX_ID(UBLOX_SMARTREST_VERSION); 00084 if (!_integration.integrate()) { 00085 LCDDisplay::inst().setLines("Integrate failure"); 00086 return -2; 00087 } 00088 setX_ID(client.getIdentifier()); 00089 aInfo("Set X-ID: %s\n", srX_ID); 00090 00091 return 0; 00092 } 00093 00094 void MbedAgent::loop() 00095 { 00096 ReportThread reportThread(pool); 00097 _operationSupport.executePendingOperations(); 00098 PollThread pollThread(pool); 00099 pollThread.setChannel(deviceID); 00100 00101 Watchdog wdt; 00102 wdt.kick(300.0); // set a 60.0 seconds watchdog 00103 while (true) { 00104 int l = 0; 00105 bool flag = false; 00106 for (size_t i = 0; i < N; ++i) { 00107 // if (reporters[i] == &ConfigSync::inst()) { 00108 int l2 = reporters[i]->read(buf2+l, sizeof(buf2)-l, status, DISPLAY_LEN); 00109 if (l2 < 0) { 00110 flag |= reporters[i] == &signal; 00111 } else if (l2 > 0) { // Refresh LCD display needed 00112 l += l2; 00113 LCDDisplay::inst().setThirdLine(status); 00114 lcdThirdLineBlank = false; 00115 Thread::wait(400); 00116 } else if (!lcdThirdLineBlank) { // Clear LCD display needed 00117 LCDDisplay::inst().setThirdLine(""); 00118 lcdThirdLineBlank = true; 00119 Thread::wait(100); 00120 } 00121 // } 00122 } 00123 if (l) { 00124 l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2); 00125 sock.setBlocking(3000); 00126 l = sock.sendOnly(buf, l); 00127 if (l < 0) 00128 aWarning("%s\n", status); 00129 } 00130 if (!flag) 00131 wdt.kick(); // reset watchdog timer 00132 } 00133 }
Generated on Sat Jul 16 2022 07:10:59 by 1.7.2