Get API to work

This commit is contained in:
Tomislav Kopić 2024-11-27 18:49:39 +01:00
parent 6ff3352c57
commit c145e6aee8

View File

@ -2,7 +2,7 @@
#include <Adafruit_SSD1306.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include "example_config.h"
#include "config.h"
#include "SmartCube/cubeSound.h"
#include "SmartCube/cubeButtons.h"
#include "SmartCube/cubeWifiManager.h"
@ -12,12 +12,10 @@ cubeWifiManager cubeWifiManager(display);
unsigned long lastRefresh = 0;
const unsigned long refreshInterval = 60000; // 60 seconds
unsigned long lastDisplayUpdate = 0;
const unsigned long displayInterval = 100; // 100 ms
String lastEventId = "";
String problems[10];
String problemDescriptions;
String problemDescriptions = "Test";
int problemCount = 0;
int severityCounts[5] = {0};
bool newProblemsDetected = false;
@ -55,10 +53,11 @@ void fetchActiveProblems() {
WiFiClient client;
HTTPClient http;
http.begin(client, zabbixServer);
http.setTimeout(5000); // 5-second timeout
http.addHeader("Content-Type", "application/json");
// JSON payload
DynamicJsonDocument doc(1024);
DynamicJsonDocument doc(2048); // Increased to avoid memory issues
doc["jsonrpc"] = "2.0";
doc["method"] = "problem.get";
doc["id"] = 1;
@ -79,47 +78,54 @@ void fetchActiveProblems() {
String response = http.getString();
// Parse response
DynamicJsonDocument responseDoc(4096);
deserializeJson(responseDoc, response);
JsonArray result = responseDoc["result"].as<JsonArray>();
int newProblemCount = 0;
memset(severityCounts, 0, sizeof(severityCounts)); // Reset severities
DynamicJsonDocument responseDoc(8192); // Increased size to fit more data
DeserializationError error = deserializeJson(responseDoc, response);
totalProblems = result.size(); // Set total problems count
if (!error) {
JsonArray result = responseDoc["result"].as<JsonArray>();
for (JsonObject problem : result) {
String eventId = problem["eventid"].as<String>();
int severity = problem["severity"].as<int>();
String description = problem["name"].as<String>();
// Clear descriptions and severity counts
memset(severityCounts, 0, sizeof(severityCounts));
problemDescriptions = ""; // Reset before appending
// Check for new problems by comparing event IDs as integers
if (eventId.toInt() > lastEventId.toInt()) {
newProblemsDetected = true;
totalProblems = result.size(); // Set total problems count
int newProblemCount = 0;
for (JsonObject problem : result) {
String eventId = problem["eventid"].as<String>();
int severity = problem["severity"].as<int>();
String description = problem["name"].as<String>();
// Check for new problems by comparing event IDs
if (eventId.toInt() > lastEventId.toInt()) {
newProblemsDetected = true;
}
if (newProblemCount < 10) {
problems[newProblemCount] = description;
}
if (severity >= 0 && severity < 5) {
severityCounts[severity]++;
}
problemDescriptions += description + " "; // Append description with space for better display
newProblemCount++;
}
// Add new problem descriptions to the array
if (newProblemCount < 10) {
problems[newProblemCount] = description;
}
problemCount = newProblemCount;
// Count severities
if (severity >= 0 && severity < 5) {
severityCounts[severity]++;
if (problemCount > 0) {
lastEventId = result[0]["eventid"].as<String>();
}
newProblemCount++;
problemDescriptions += description;
} else {
beep(1000);
}
} else {
display.clearDisplay();
display.setCursor(0,30);
display.print("Error querying zabbix API");
problemCount = newProblemCount;
// Update lastEventId after processing the most recent event
if (problemCount > 0) {
lastEventId = result[0]["eventid"].as<String>();
}
} else {
beep(2000);
}
http.end();
@ -153,10 +159,9 @@ void displayProblems() {
}
// Draw the file name with current scroll position
display.setTextSize(2);
display.setCursor(scrollPos, 54);
display.setCursor(scrollPos, 49);
display.print(problemDescriptions);
display.setCursor(0, 44);
display.setTextSize(1);
display.drawLine(0,47,127,47,1);
display.display();
}