
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
Revision 108:2ec12f10ebf4, committed 2015-05-13
- Comitter:
- xinlei
- Date:
- Wed May 13 13:54:17 2015 +0000
- Parent:
- 107:f1ee3e1eb126
- Child:
- 109:b7a403dbceb6
- Commit message:
- ReportThread now merges pending state before report.
Changed in this revision
--- a/MbedAgent.cpp Wed May 13 13:03:20 2015 +0000 +++ b/MbedAgent.cpp Wed May 13 13:54:17 2015 +0000 @@ -1,6 +1,6 @@ #include "MbedAgent.h" +#include "watchdog.h" #include "logging.h" -#include "watchdog.h" MbedAgent::MbedAgent(DeviceInfo& deviceInfo): _client(), tpl(), _bootstrap(_client, deviceInfo), @@ -98,15 +98,15 @@ { ReportThread reportThread(pool); _operationSupport.executePendingOperations(); -// PollThread pollThread(pool); -// pollThread.setChannel(deviceID); + PollThread pollThread(pool); + pollThread.setChannel(deviceID); Watchdog wdt; wdt.kick(60.0); // set a 60.0 seconds watchdog while (true) { int l = 0; for (size_t i = 0; i < N; ++i) { - if (reporters[i] == NULL) { +// if (reporters[i] == NULL) { int l2 = reporters[i]->read(buf2+l, sizeof(buf2)-l, status, DISPLAY_LEN); if (l2) { // Refresh LCD display needed LCDDisplay::inst().setThirdLine(status); @@ -117,7 +117,7 @@ } lcdThirdLineBlank = !l2; l += l2; - } +// } } if (l) { l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2);
--- a/operation/Opeartion.cpp Wed May 13 13:03:20 2015 +0000 +++ b/operation/Opeartion.cpp Wed May 13 13:54:17 2015 +0000 @@ -5,7 +5,7 @@ const char *strSuccessful = "SUCCESSFUL"; const char *strFailed = "FAILED"; -const char* strOperationState(OperationState state) +const char* strOperationState(const OperationState state) { switch (state) { case OPERATION_EXECUTING: return strExecuting;
--- a/operation/ReportThread.cpp Wed May 13 13:03:20 2015 +0000 +++ b/operation/ReportThread.cpp Wed May 13 13:54:17 2015 +0000 @@ -7,29 +7,36 @@ { sock.setBlocking(3000); while (true) { + dict.clear(); osEvent e = ipool.get(); if (e.status == osEventMail) { Operation *op = (Operation*)e.value.p; - long id = op->identifier; - OperationState state = op->state; + dict.set(op->identifier, op->state); ipool.free(op); - int l = snprintf(buf2, sizeof(buf2), fmt2, id, strOperationState(state)); - for (unsigned short i = 0; i < 10; ++i) { + while (!dict.full()) { osEvent e = ipool.get(200); if (e.status == osEventMail) { op = (Operation*)e.value.p; - id = op->identifier; - OperationState state = op->state; + dict.set(op->identifier, op->state); ipool.free(op); - l += snprintf(buf2+l, sizeof(buf2)-l, fmt2, id, strOperationState(state)); } else { break; } } + int l = 0; + for (unsigned short i = 0; i < dict.size(); ++i) { + const long id = dict[i].identifier; + const OperationState state = dict[i].state; + l += snprintf(buf2+l, sizeof(buf2)-l, fmt2, id, strOperationState(state)); + } l = snprintf(buf, sizeof(buf), fmtSmartRest, uri, l, buf2); - l = sock.sendOnly(buf, l); - if (l < 0) { - aError("Report: <%ld, %s>\n", id, strOperationState(state)); + for (unsigned i = 0; i < 3; ++i) { + int l2 = sock.sendOnly(buf, l); + if (l2 < 0) { + aError("Report: op state\n"); + Thread::wait(3000); + } else + break; } } }
--- a/operation/ReportThread.h Wed May 13 13:03:20 2015 +0000 +++ b/operation/ReportThread.h Wed May 13 13:54:17 2015 +0000 @@ -1,13 +1,47 @@ #ifndef REPORTTHREAD_H #define REPORTTHREAD_H -#include "SmartRestSocket.h" #include "Operation.h" #include "SmartRestConf.h" +#include "SmartRestSocket.h" +#define OPERATION_DICT_SIZE 10 + +class OperationDict +{ +public: + OperationDict(): count(0) {} + const Operation& operator [](unsigned short i) const { return opl[i]; } + Operation *set(long id, OperationState state) { + unsigned short i = 0; + for (; i < count; ++i) { + if (opl[i].identifier == id) + break; + } + if (i < count) { + opl[i].identifier = id; + opl[i].state = state; + return &opl[i]; + } else if (count < OPERATION_DICT_SIZE) { + opl[i].identifier = id; + opl[i].state = state; + ++count; + return &opl[i]; + } else + return NULL; + + } + void clear() { count = 0; } + bool full() const { return count >= OPERATION_DICT_SIZE; } + unsigned short size() const { return count; } + virtual ~OperationDict() {} +private: + unsigned short count; + Operation opl[OPERATION_DICT_SIZE]; +}; class ReportThread { public: - ReportThread(OperationPool& pool) : ipool(pool), sock(), + ReportThread(OperationPool& pool) : ipool(pool), dict(), sock(), thread(ReportThread::threadWrapper, this) { strncpy(uri, "/s", sizeof(uri)); } @@ -17,6 +51,7 @@ private: char uri[4]; OperationPool& ipool; + OperationDict dict; char buf[SMARTREST_SIZE]; char buf2[SMARTREST_BODY_SIZE]; SmartRestSocket sock;