forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebApi_eventlog.cpp
57 lines (44 loc) · 1.71 KB
/
WebApi_eventlog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Thomas Basler and others
*/
#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;
JsonArray eventsArray = root[buffer].createNestedArray(F("events"));
for (uint8_t logEntry = 0; logEntry < logEntryCount; logEntry++) {
JsonObject eventsObject = eventsArray.createNestedObject();
AlarmLogEntry_t entry;
inv->EventLog()->getLogEntry(logEntry, &entry);
eventsObject[F("message_id")] = entry.MessageId;
eventsObject[F("message")] = entry.Message;
eventsObject[F("start_time")] = entry.StartTime;
eventsObject[F("end_time")] = entry.EndTime;
}
}
response->setLength();
request->send(response);
}