Compare commits

..

No commits in common. "2a5876ef7731bea3c473ea251f766759a7e32e8d" and "6ff3352c576bd03752ad7190298104905eae99ab" have entirely different histories.

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 "config.h" #include "example_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,10 +12,12 @@ 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 = "Test"; String problemDescriptions;
int problemCount = 0; int problemCount = 0;
int severityCounts[5] = {0}; int severityCounts[5] = {0};
bool newProblemsDetected = false; bool newProblemsDetected = false;
@ -53,11 +55,10 @@ 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(2048); // Increased to avoid memory issues DynamicJsonDocument doc(1024);
doc["jsonrpc"] = "2.0"; doc["jsonrpc"] = "2.0";
doc["method"] = "problem.get"; doc["method"] = "problem.get";
doc["id"] = 1; doc["id"] = 1;
@ -78,53 +79,47 @@ void fetchActiveProblems() {
String response = http.getString(); String response = http.getString();
// Parse response // Parse response
DynamicJsonDocument responseDoc(8192); // Increased size to fit more data DynamicJsonDocument responseDoc(4096);
DeserializationError error = deserializeJson(responseDoc, response); deserializeJson(responseDoc, response);
if (!error) {
JsonArray result = responseDoc["result"].as<JsonArray>(); JsonArray result = responseDoc["result"].as<JsonArray>();
int newProblemCount = 0;
// Clear descriptions and severity counts memset(severityCounts, 0, sizeof(severityCounts)); // Reset severities
memset(severityCounts, 0, sizeof(severityCounts));
problemDescriptions = ""; // Reset before appending
totalProblems = result.size(); // Set total problems count totalProblems = result.size(); // Set total problems count
int newProblemCount = 0;
for (JsonObject problem : result) { for (JsonObject problem : result) {
String eventId = problem["eventid"].as<String>(); String eventId = problem["eventid"].as<String>();
int severity = problem["severity"].as<int>(); int severity = problem["severity"].as<int>();
String description = problem["name"].as<String>(); String description = problem["name"].as<String>();
// Check for new problems by comparing event IDs // Check for new problems by comparing event IDs as integers
if (eventId.toInt() > lastEventId.toInt()) { if (eventId.toInt() > lastEventId.toInt()) {
newProblemsDetected = true; newProblemsDetected = true;
} }
// Add new problem descriptions to the array
if (newProblemCount < 10) { if (newProblemCount < 10) {
problems[newProblemCount] = description; problems[newProblemCount] = description;
} }
// Count severities
if (severity >= 0 && severity < 5) { if (severity >= 0 && severity < 5) {
severityCounts[severity]++; severityCounts[severity]++;
} }
problemDescriptions += description + " "; // Append description with space for better display
newProblemCount++; newProblemCount++;
problemDescriptions += description;
} }
problemCount = newProblemCount; problemCount = newProblemCount;
// Update lastEventId after processing the most recent event
if (problemCount > 0) { if (problemCount > 0) {
lastEventId = result[0]["eventid"].as<String>(); lastEventId = result[0]["eventid"].as<String>();
} }
} else { } else {
beep(1000); beep(2000);
}
} else {
display.clearDisplay();
display.setCursor(0,30);
display.print("Error querying zabbix API");
} }
http.end(); http.end();
@ -158,9 +153,10 @@ 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, 48); display.setCursor(scrollPos, 54);
display.print(problemDescriptions); display.print(problemDescriptions);
display.drawLine(0,46,127,46,1); display.setCursor(0, 44);
display.setTextSize(1);
display.display(); display.display();
} }