From c145e6aee8f212f72dbb87a00283ae537c5805dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomislav=20Kopi=C4=87?= Date: Wed, 27 Nov 2024 18:49:39 +0100 Subject: [PATCH] Get API to work --- src/main.cpp | 85 +++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 40 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index efc6779..b0e1156 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,7 @@ #include #include #include -#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(); - 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(); - for (JsonObject problem : result) { - String eventId = problem["eventid"].as(); - int severity = problem["severity"].as(); - String description = problem["name"].as(); + // 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(); + int severity = problem["severity"].as(); + String description = problem["name"].as(); + + // 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(); } - - 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(); - } - } 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(); }