|
| 1 | +#if defined(ENABLE_DEBUG) && !defined(ENABLE_DEBUG_EVENT_LOG) |
| 2 | +#undef ENABLE_DEBUG |
| 3 | +#endif |
| 4 | + |
| 5 | +#include <LITTLEFS.h> |
| 6 | +#include <ArduinoJson.h> |
| 7 | + |
| 8 | +#include "debug.h" |
| 9 | +#include "emonesp.h" |
| 10 | +#include "event_log.h" |
| 11 | + |
| 12 | +EventLog::EventLog() : |
| 13 | + _min_log_index(0), |
| 14 | + _max_log_index(0) |
| 15 | +{ |
| 16 | +} |
| 17 | + |
| 18 | +EventLog::~EventLog() |
| 19 | +{ |
| 20 | +} |
| 21 | + |
| 22 | +String EventLog::filenameFromIndex(uint32_t index) |
| 23 | +{ |
| 24 | + String filename = EVENTLOG_BASE_DIRECTORY; |
| 25 | + filename += "/" + String(index); |
| 26 | + return filename; |
| 27 | +} |
| 28 | + |
| 29 | +uint32_t EventLog::indexFromFilename(String &path) |
| 30 | +{ |
| 31 | + DBUGVAR(path); |
| 32 | + |
| 33 | + int lastSeparator = path.lastIndexOf('/'); |
| 34 | + String name = lastSeparator >= 0 ? path.substring(lastSeparator + 1) : path; |
| 35 | + DBUGVAR(name); |
| 36 | + |
| 37 | + return atol(name.c_str()); |
| 38 | +} |
| 39 | + |
| 40 | +// Scan our base directory for existing log files and workout the min/max index files |
| 41 | +void EventLog::begin() |
| 42 | +{ |
| 43 | + File eventLog = LittleFS.open(EVENTLOG_BASE_DIRECTORY); |
| 44 | + if(eventLog && eventLog.isDirectory()) |
| 45 | + { |
| 46 | + _min_log_index = UINT32_MAX; |
| 47 | + _max_log_index = 0; |
| 48 | + |
| 49 | + File file = eventLog.openNextFile(); |
| 50 | + while(file) |
| 51 | + { |
| 52 | + if(!file.isDirectory()) |
| 53 | + { |
| 54 | + String name = file.name(); |
| 55 | + long chunk = indexFromFilename(name); |
| 56 | + DBUGVAR(chunk); |
| 57 | + if(chunk > _max_log_index) { |
| 58 | + _max_log_index = chunk; |
| 59 | + DBUGVAR(_max_log_index); |
| 60 | + } |
| 61 | + if(chunk < _min_log_index) { |
| 62 | + _min_log_index = chunk; |
| 63 | + DBUGVAR(_min_log_index); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + file = eventLog.openNextFile(); |
| 68 | + } |
| 69 | + |
| 70 | + if(UINT32_MAX == _min_log_index) { |
| 71 | + _min_log_index = 0; |
| 72 | + } |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + LittleFS.mkdir(EVENTLOG_BASE_DIRECTORY); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void EventLog::log(EventType type, EvseState managerState, uint8_t evseState, uint32_t evseFlags, uint32_t pilot, double energy, uint32_t elapsed, double temperature, double temperatureMax, uint8_t divertMode) |
| 81 | +{ |
| 82 | + time_t now = time(NULL); |
| 83 | + struct tm timeinfo; |
| 84 | + gmtime_r(&now, &timeinfo); |
| 85 | + |
| 86 | + // Check if we have a reasonable time, don't want to be logging events from 1970 |
| 87 | + if(timeinfo.tm_year < (2021 - 1900)) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + String eventFilename = filenameFromIndex(_max_log_index); |
| 92 | + File eventFile = LittleFS.open(eventFilename, FILE_APPEND); |
| 93 | + if(eventFile && eventFile.size() > EVENTLOG_ROTATE_SIZE) |
| 94 | + { |
| 95 | + DBUGLN("Rotating log file"); |
| 96 | + eventFile.close(); |
| 97 | + |
| 98 | + _max_log_index ++; |
| 99 | + eventFilename = filenameFromIndex(_max_log_index); |
| 100 | + eventFile = LittleFS.open(eventFilename, FILE_APPEND); |
| 101 | + |
| 102 | + // _max_log_index is inclusive, so we need to increment it here |
| 103 | + while((_max_log_index + 1) - _min_log_index > EVENTLOG_MAX_ROTATE_COUNT) { |
| 104 | + LittleFS.remove(filenameFromIndex(_min_log_index)); |
| 105 | + _min_log_index++; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if(eventFile) |
| 110 | + { |
| 111 | + StaticJsonDocument<256> line; |
| 112 | + char output[80]; |
| 113 | + strftime(output, 80, "%FT%TZ", &timeinfo); |
| 114 | + |
| 115 | + line["t"] = output; |
| 116 | + line["ty"] = type.toInt(); |
| 117 | + line["ms"] = managerState.toString(); |
| 118 | + line["es"] = evseState; |
| 119 | + line["ef"] = evseFlags; |
| 120 | + line["p"] = pilot; |
| 121 | + line["e"] = energy; |
| 122 | + line["el"] = elapsed; |
| 123 | + line["tp"] = temperature; |
| 124 | + line["tm"] = temperatureMax; |
| 125 | + line["dm"] = divertMode; |
| 126 | + |
| 127 | + serializeJson(line, eventFile); |
| 128 | + eventFile.println(""); |
| 129 | + |
| 130 | + #ifdef ENABLE_DEBUG |
| 131 | + serializeJson(line, DEBUG_PORT); |
| 132 | + DBUGLN(""); |
| 133 | + #endif |
| 134 | + |
| 135 | + eventFile.close(); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +void EventLog::enumerate(uint32_t index, std::function<void(String time, EventType type, EvseState managerState, uint8_t evseState, uint32_t evseFlags, uint32_t pilot, double energy, uint32_t elapsed, double temperature, double temperatureMax, uint8_t divertMode)> callback) |
| 140 | +{ |
| 141 | + String filename = filenameFromIndex(index); |
| 142 | + File eventFile = LittleFS.open(filename); |
| 143 | + if(eventFile) |
| 144 | + { |
| 145 | + while(eventFile.available()) |
| 146 | + { |
| 147 | + String line = eventFile.readStringUntil('\n'); |
| 148 | + if(line.length() > 0) |
| 149 | + { |
| 150 | + StaticJsonDocument<256> json; |
| 151 | + DeserializationError error = deserializeJson(json, line); |
| 152 | + if(error) |
| 153 | + { |
| 154 | + DBUGF("Error parsing line: %s", error.c_str()); |
| 155 | + break; |
| 156 | + } |
| 157 | + |
| 158 | + String time = json["t"]; |
| 159 | + EventType type = EventType::Information; |
| 160 | + type.fromInt(json["ty"]); |
| 161 | + EvseState managerState = EvseState::None; |
| 162 | + managerState.fromString(json["ms"]); |
| 163 | + uint8_t evseState = json["es"]; |
| 164 | + uint32_t evseFlags = json["ef"]; |
| 165 | + uint32_t pilot = json["p"]; |
| 166 | + double energy = json["e"]; |
| 167 | + uint32_t elapsed = json["el"]; |
| 168 | + double temperature = json["tp"]; |
| 169 | + double temperatureMax = json["tm"]; |
| 170 | + uint8_t divertMode = json["dm"]; |
| 171 | + |
| 172 | + callback(time, type, managerState, evseState, evseFlags, pilot, energy, elapsed, temperature, temperatureMax, divertMode); |
| 173 | + } |
| 174 | + } |
| 175 | + eventFile.close(); |
| 176 | + } |
| 177 | +} |
0 commit comments