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