Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed
Fork of ICE by
src/AnalyticsLogger/AnalyticsLogger.cpp@247:a70c6c4923ff, 2016-10-21 (annotated)
- Committer:
- davidjhoward
- Date:
- Fri Oct 21 11:29:05 2016 +0000
- Revision:
- 247:a70c6c4923ff
- Parent:
- 235:ce028fbf054d
make sure last packet sent to cloud has correct sequence number
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmarkel44 | 0:65cfa4873284 | 1 | /****************************************************************************** |
davidjhoward | 58:a4422d19b2ea | 2 | * |
jmarkel44 | 0:65cfa4873284 | 3 | * File: AnalyticsLogger.cpp |
jmarkel44 | 0:65cfa4873284 | 4 | * Desciption: source for the ICE Analytics Logger |
jmarkel44 | 0:65cfa4873284 | 5 | * |
jmarkel44 | 0:65cfa4873284 | 6 | *****************************************************************************/ |
jmarkel44 | 0:65cfa4873284 | 7 | #include "global.h" |
jmarkel44 | 0:65cfa4873284 | 8 | #include <stdio.h> |
davidjhoward | 125:f11cc566d073 | 9 | #include <string> |
davidjhoward | 125:f11cc566d073 | 10 | #include <sstream> |
davidjhoward | 125:f11cc566d073 | 11 | #include <iostream> |
jmarkel44 | 0:65cfa4873284 | 12 | #include "AnalyticsLogger.h" |
davidjhoward | 125:f11cc566d073 | 13 | #include "LoggerApi.h" |
davidjhoward | 58:a4422d19b2ea | 14 | #include "eep.h" |
davidjhoward | 60:96e17fb215a6 | 15 | #include "rtc.h" |
jmarkel44 | 0:65cfa4873284 | 16 | |
jmarkel44 | 0:65cfa4873284 | 17 | /***************************************************************************** |
jmarkel44 | 0:65cfa4873284 | 18 | * Function: AnalyticsLogger |
davidjhoward | 58:a4422d19b2ea | 19 | * Description: entry point for the Analytics Logger |
jmarkel44 | 0:65cfa4873284 | 20 | * |
jmarkel44 | 0:65cfa4873284 | 21 | * @param (IN) args (user-defined arguments) |
jmarkel44 | 0:65cfa4873284 | 22 | * @return none |
jmarkel44 | 0:65cfa4873284 | 23 | *****************************************************************************/ |
jmarkel44 | 0:65cfa4873284 | 24 | void AnalyticsLogger(void const *args) |
jmarkel44 | 0:65cfa4873284 | 25 | { |
davidjhoward | 60:96e17fb215a6 | 26 | struct tm *ts; |
davidjhoward | 60:96e17fb215a6 | 27 | time_t curr_sec; |
davidjhoward | 60:96e17fb215a6 | 28 | int last_min=8; |
davidjhoward | 199:d65ed41d4dd4 | 29 | bool log_sent=false; |
davidjhoward | 199:d65ed41d4dd4 | 30 | |
jmarkel44 | 0:65cfa4873284 | 31 | while ( true ) { |
davidjhoward | 199:d65ed41d4dd4 | 32 | |
davidjhoward | 199:d65ed41d4dd4 | 33 | std::ostringstream log_event; |
davidjhoward | 60:96e17fb215a6 | 34 | |
davidjhoward | 60:96e17fb215a6 | 35 | curr_sec = time(0); |
davidjhoward | 60:96e17fb215a6 | 36 | ts = localtime(&curr_sec); |
davidjhoward | 60:96e17fb215a6 | 37 | // printf("curr_sec=%ld, min=%d (last=%d)\r\n", curr_sec, ts->tm_min, last_min ); |
davidjhoward | 225:11e39a2f3785 | 38 | if( ((ts->tm_min%5) == 0) && (ts->tm_min != last_min) ) { |
davidjhoward | 60:96e17fb215a6 | 39 | last_min = ts->tm_min; |
davidjhoward | 199:d65ed41d4dd4 | 40 | |
davidjhoward | 247:a70c6c4923ff | 41 | int map_count = ModbusRegisterMap.size(); |
davidjhoward | 247:a70c6c4923ff | 42 | int collected_count = 0; |
davidjhoward | 247:a70c6c4923ff | 43 | |
davidjhoward | 125:f11cc566d073 | 44 | log_event << "\"lr\":["; |
davidjhoward | 61:ae6dd6692c7d | 45 | std::map<std::string, ModbusRegister>::iterator iter; |
davidjhoward | 61:ae6dd6692c7d | 46 | for (iter = ModbusRegisterMap.begin(); iter != ModbusRegisterMap.end(); ++iter) { |
davidjhoward | 247:a70c6c4923ff | 47 | |
davidjhoward | 247:a70c6c4923ff | 48 | collected_count = collected_count + 1; |
davidjhoward | 199:d65ed41d4dd4 | 49 | |
davidjhoward | 125:f11cc566d073 | 50 | log_event << "{\"t\":"<< "\"" << iter->first.c_str() << "\"," << "\"v\":"<< "\"" << iter->second.float_value<< "\"},"; |
davidjhoward | 199:d65ed41d4dd4 | 51 | log_sent = false; |
davidjhoward | 199:d65ed41d4dd4 | 52 | if( log_event.str().size() >= 150 ) { |
davidjhoward | 199:d65ed41d4dd4 | 53 | std::string str = log_event.str(); |
davidjhoward | 199:d65ed41d4dd4 | 54 | str.erase( str.size() - 1 ); |
davidjhoward | 247:a70c6c4923ff | 55 | if( collected_count == map_count ) { |
davidjhoward | 247:a70c6c4923ff | 56 | str.append("],\"seq\":\"0\""); |
davidjhoward | 247:a70c6c4923ff | 57 | } else { |
davidjhoward | 247:a70c6c4923ff | 58 | str.append("],\"seq\":\"1\""); |
davidjhoward | 247:a70c6c4923ff | 59 | } |
davidjhoward | 199:d65ed41d4dd4 | 60 | // printf("%s:%d: Logging %s : len=%d\r\n", __func__, __LINE__, str.c_str(), str.length() ); |
davidjhoward | 199:d65ed41d4dd4 | 61 | LiveDataLoggerApi( str.c_str() ); |
davidjhoward | 199:d65ed41d4dd4 | 62 | log_event.str(""); |
davidjhoward | 199:d65ed41d4dd4 | 63 | log_event.clear(); |
davidjhoward | 199:d65ed41d4dd4 | 64 | log_event << "\"lr\":["; |
davidjhoward | 199:d65ed41d4dd4 | 65 | log_sent = true; |
davidjhoward | 199:d65ed41d4dd4 | 66 | } |
davidjhoward | 61:ae6dd6692c7d | 67 | } |
davidjhoward | 199:d65ed41d4dd4 | 68 | if( log_sent == false ) { |
davidjhoward | 199:d65ed41d4dd4 | 69 | std::string str = log_event.str(); |
davidjhoward | 199:d65ed41d4dd4 | 70 | str.erase( str.size() - 1 ); |
davidjhoward | 235:ce028fbf054d | 71 | str.append("],\"seq\":\"0\""); |
davidjhoward | 247:a70c6c4923ff | 72 | if( str.length() > 20 ) { |
davidjhoward | 199:d65ed41d4dd4 | 73 | // printf("%s:%d: Logging %s : len=%d\r\n", __func__, __LINE__, str.c_str(), str.length() ); |
davidjhoward | 199:d65ed41d4dd4 | 74 | LiveDataLoggerApi( str.c_str() ); |
davidjhoward | 199:d65ed41d4dd4 | 75 | } |
davidjhoward | 199:d65ed41d4dd4 | 76 | } |
davidjhoward | 58:a4422d19b2ea | 77 | } |
davidjhoward | 199:d65ed41d4dd4 | 78 | |
davidjhoward | 78:5a2e019036a4 | 79 | Thread::wait(5000); |
jmarkel44 | 0:65cfa4873284 | 80 | } |
jmarkel44 | 0:65cfa4873284 | 81 | } |