First version of webapi for event log

This commit is contained in:
Thomas Basler 2022-07-06 22:17:26 +02:00
parent b9bb753906
commit f7eb54fa41
6 changed files with 94 additions and 3 deletions

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "WebApi_dtu.h" #include "WebApi_dtu.h"
#include "WebApi_eventlog.h"
#include "WebApi_firmware.h" #include "WebApi_firmware.h"
#include "WebApi_inverter.h" #include "WebApi_inverter.h"
#include "WebApi_mqtt.h" #include "WebApi_mqtt.h"
@ -23,6 +24,7 @@ private:
AsyncEventSource _events; AsyncEventSource _events;
WebApiDtuClass _webApiDtu; WebApiDtuClass _webApiDtu;
WebApiEventlogClass _webApiEventlog;
WebApiFirmwareClass _webApiFirmware; WebApiFirmwareClass _webApiFirmware;
WebApiInverterClass _webApiInverter; WebApiInverterClass _webApiInverter;
WebApiMqttClass _webApiMqtt; WebApiMqttClass _webApiMqtt;

14
include/WebApi_eventlog.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <ESPAsyncWebServer.h>
class WebApiEventlogClass {
public:
void init(AsyncWebServer* server);
void loop();
private:
void onEventlogStatus(AsyncWebServerRequest* request);
AsyncWebServer* _server;
};

View File

@ -18,11 +18,15 @@ uint8_t AlarmLogParser::getEntryCount()
return (_alarmLogLength - 2) / ALARM_LOG_ENTRY_SIZE; return (_alarmLogLength - 2) / ALARM_LOG_ENTRY_SIZE;
} }
void AlarmLogParser::getLogEntry(uint8_t entryId, AlarmLogEntry* entry) void AlarmLogParser::getLogEntry(uint8_t entryId, AlarmLogEntry_t* entry)
{ {
uint8_t entryStartOffset = 2 + entryId * ALARM_LOG_ENTRY_SIZE; uint8_t entryStartOffset = 2 + entryId * ALARM_LOG_ENTRY_SIZE;
int timezoneOffset = getTimezoneOffset();
entry->MessageId = _payloadAlarmLog[entryStartOffset + 1]; entry->MessageId = _payloadAlarmLog[entryStartOffset + 1];
entry->StartTime = ((uint16_t)_payloadAlarmLog[entryStartOffset + 4] << 8) | ((uint16_t)_payloadAlarmLog[entryStartOffset + 5]) + timezoneOffset;
entry->EndTime = ((uint16_t)_payloadAlarmLog[entryStartOffset + 6] << 8) | ((uint16_t)_payloadAlarmLog[entryStartOffset + 7]) + timezoneOffset;
switch (entry->MessageId) { switch (entry->MessageId) {
case 1: case 1:
@ -233,4 +237,21 @@ void AlarmLogParser::getLogEntry(uint8_t entryId, AlarmLogEntry* entry)
entry->Message = String(F("Unknown")); entry->Message = String(F("Unknown"));
break; break;
} }
}
int AlarmLogParser::getTimezoneOffset()
{
// see: https://stackoverflow.com/questions/13804095/get-the-time-zone-gmt-offset-in-c/44063597#44063597
time_t gmt, rawtime = time(NULL);
struct tm *ptm;
struct tm gbuf;
ptm = gmtime_r(&rawtime, &gbuf);
// Request that mktime() looksup dst in timezone database
ptm->tm_isdst = -1;
gmt = mktime(ptm);
return (int)difftime(rawtime, gmt);
} }

View File

@ -5,7 +5,7 @@
#define ALARM_LOG_ENTRY_COUNT 15 #define ALARM_LOG_ENTRY_COUNT 15
#define ALARM_LOG_ENTRY_SIZE 12 #define ALARM_LOG_ENTRY_SIZE 12
struct AlarmLogEntry { struct AlarmLogEntry_t {
uint16_t MessageId; uint16_t MessageId;
String Message; String Message;
time_t StartTime; time_t StartTime;
@ -18,9 +18,11 @@ public:
void appendFragment(uint8_t offset, uint8_t* payload, uint8_t len); void appendFragment(uint8_t offset, uint8_t* payload, uint8_t len);
uint8_t getEntryCount(); uint8_t getEntryCount();
void getLogEntry(uint8_t entryId, AlarmLogEntry* entry); void getLogEntry(uint8_t entryId, AlarmLogEntry_t* entry);
private: private:
static int getTimezoneOffset();
uint8_t _payloadAlarmLog[ALARM_LOG_ENTRY_SIZE * ALARM_LOG_ENTRY_COUNT]; uint8_t _payloadAlarmLog[ALARM_LOG_ENTRY_SIZE * ALARM_LOG_ENTRY_COUNT];
uint8_t _alarmLogLength; uint8_t _alarmLogLength;
}; };

View File

@ -20,6 +20,7 @@ void WebApiClass::init()
_ws.onEvent(std::bind(&WebApiClass::onWebsocketEvent, this, _1, _2, _3, _4, _5, _6)); _ws.onEvent(std::bind(&WebApiClass::onWebsocketEvent, this, _1, _2, _3, _4, _5, _6));
_webApiDtu.init(&_server); _webApiDtu.init(&_server);
_webApiEventlog.init(&_server);
_webApiFirmware.init(&_server); _webApiFirmware.init(&_server);
_webApiInverter.init(&_server); _webApiInverter.init(&_server);
_webApiMqtt.init(&_server); _webApiMqtt.init(&_server);
@ -36,6 +37,7 @@ void WebApiClass::init()
void WebApiClass::loop() void WebApiClass::loop()
{ {
_webApiDtu.loop(); _webApiDtu.loop();
_webApiEventlog.loop();
_webApiFirmware.loop(); _webApiFirmware.loop();
_webApiInverter.loop(); _webApiInverter.loop();
_webApiMqtt.loop(); _webApiMqtt.loop();

50
src/WebApi_eventlog.cpp Normal file
View File

@ -0,0 +1,50 @@
#include "WebApi_eventlog.h"
#include "ArduinoJson.h"
#include "AsyncJson.h"
#include "Hoymiles.h"
void WebApiEventlogClass::init(AsyncWebServer* server)
{
using namespace std::placeholders;
_server = server;
_server->on("/api/eventlog/status", HTTP_GET, std::bind(&WebApiEventlogClass::onEventlogStatus, this, _1));
}
void WebApiEventlogClass::loop()
{
}
void WebApiEventlogClass::onEventlogStatus(AsyncWebServerRequest* request)
{
AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
auto inv = Hoymiles.getInverterByPos(i);
// Inverter Serial is read as HEX
char buffer[sizeof(uint64_t) * 8 + 1];
sprintf(buffer, "%0lx%08lx",
((uint32_t)((inv->serial() >> 32) & 0xFFFFFFFF)),
((uint32_t)(inv->serial() & 0xFFFFFFFF)));
uint8_t logEntryCount = inv->EventLog()->getEntryCount();
root[buffer]["count"] = logEntryCount;
for (uint8_t logEntry = 0; logEntry < logEntryCount; logEntry++) {
AlarmLogEntry_t entry;
inv->EventLog()->getLogEntry(logEntry, &entry);
root[buffer][String(logEntry)][F("message_id")] = entry.MessageId;
root[buffer][String(logEntry)][F("message")] = entry.Message;
root[buffer][String(logEntry)][F("start_time")] = entry.StartTime;
root[buffer][String(logEntry)][F("end_time")] = entry.EndTime;
}
}
response->setLength();
request->send(response);
}