Add graph

This commit is contained in:
Tomislav Kopić 2024-11-28 18:36:49 +01:00
parent 7941d32984
commit fb8788420d

View File

@ -15,10 +15,20 @@ const unsigned long refreshInterval = 60000; // 60 seconds
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[6] = {0};
bool newProblemsDetected = false; bool newProblemsDetected = false;
int totalProblems = 0; // Variable to store the total number of active problems
// History buffer for the graph
const int maxHistorySize = 30;
int problemHistory[maxHistorySize] = {0}; // Array to store history
int historyIndex = 0; // Current index for the latest value
int scrollPos = SCREEN_WIDTH; // Global variable to keep track of the scroll position
unsigned long lastScrollTime = 0; // To control the scroll speed
const int scrollDelay = 120; // Delay in milliseconds between scroll updates
void initSystems() { void initSystems() {
pinMode(PIN_BTN_L, INPUT); pinMode(PIN_BTN_L, INPUT);
@ -31,9 +41,10 @@ void initSystems() {
Wire.setClock(400000L); // Set I2C to Fast Mode (400 kHz) Wire.setClock(400000L); // Set I2C to Fast Mode (400 kHz)
display.ssd1306_command(SSD1306_SETCONTRAST); display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(200); // Value between 0 and 255 display.ssd1306_command(200); // Value between 0 and 255
// Initialize the SSD1306 display // Initialize the SSD1306 display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Adjust I2C address if needed if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Adjust I2C address if needed
for(;;); // Don't proceed, loop forever for (;;); // Don't proceed, loop forever
} }
// Rotate the display 180 degrees // Rotate the display 180 degrees
@ -41,14 +52,12 @@ void initSystems() {
// Clear the display buffer // Clear the display buffer
display.clearDisplay(); display.clearDisplay();
display.setTextSize(1); display.setTextSize(1);
display.setTextColor(WHITE); display.setTextColor(WHITE);
display.setCursor(0, 0); display.setCursor(0, 0);
display.display(); display.display();
} }
int totalProblems = 0; // Variable to store the total number of active problems
void fetchActiveProblems() { void fetchActiveProblems() {
WiFiClient client; WiFiClient client;
HTTPClient http; HTTPClient http;
@ -90,6 +99,10 @@ void fetchActiveProblems() {
totalProblems = result.size(); // Set total problems count totalProblems = result.size(); // Set total problems count
// Store the current totalProblems in the history buffer
problemHistory[historyIndex] = totalProblems;
historyIndex = (historyIndex + 1) % maxHistorySize; // Circular buffer
int newProblemCount = 0; int newProblemCount = 0;
for (JsonObject problem : result) { for (JsonObject problem : result) {
String eventId = problem["eventid"].as<String>(); String eventId = problem["eventid"].as<String>();
@ -101,10 +114,6 @@ void fetchActiveProblems() {
newProblemsDetected = true; newProblemsDetected = true;
} }
if (newProblemCount < 10) {
problems[newProblemCount] = description;
}
if (severity >= 0 && severity < 5) { if (severity >= 0 && severity < 5) {
severityCounts[severity]++; severityCounts[severity]++;
} }
@ -121,60 +130,87 @@ void fetchActiveProblems() {
} else { } else {
beep(1000); beep(1000);
} }
} else { } else {
display.clearDisplay(); problemDescriptions="Zabbix API error";
display.setCursor(0,30);
display.print("Error querying zabbix API");
} }
http.end(); http.end();
} }
int scrollPos = SCREEN_WIDTH; // Global variable to keep track of the scroll position void playAlertMelody() {
unsigned long lastScrollTime = 0; // To control the scroll speed int toneDuration = 60; // Each tone lasts 150ms
const int scrollDelay = 150; // Delay in milliseconds between scroll updates int pauseDuration = 20; // Pause between tones
// Play first tone
tone(PIN_BUZZER, 1500); // 1000 Hz
delay(toneDuration);
noTone(PIN_BUZZER);
delay(pauseDuration);
// Play second tone
tone(PIN_BUZZER, 1200); // 1200 Hz
delay(toneDuration);
noTone(PIN_BUZZER);
delay(pauseDuration);
// Play third tone
tone(PIN_BUZZER, 1500); // 1500 Hz
delay(toneDuration);
noTone(PIN_BUZZER);
}
void displayProblems() { void displayProblems() {
display.clearDisplay(); display.clearDisplay();
display.setTextSize(1); display.setTextSize(1);
display.setCursor(0,0); display.setCursor(0, 0);
display.print("N:"); display.printf(" I:%d W:%d A:%d H:%d D:%d",
display.print(severityCounts[0]); severityCounts[1],
display.print("I:"); severityCounts[2],
display.print(severityCounts[1]); severityCounts[3],
display.print("W:"); severityCounts[4],
display.print(severityCounts[2]); severityCounts[5]);
display.print("A:"); display.drawLine(0, 45, 127, 45, 1); // Line under the graph
display.print(severityCounts[3]); display.drawLine(0, 10, 127, 10, 1); // Line under the graph
display.print("H:");
display.print(severityCounts[4]); // Draw the history graph
display.print("D:"); int graphHeight = 30; // Height of the graph
display.print(severityCounts[0]); int graphTop = 15; // Top position of the graph
display.setCursor(0,10); int graphBottom = graphTop + graphHeight;
display.print("Total Problems: ");
display.print(totalProblems); // Display total problems int maxProblems = *std::max_element(problemHistory, problemHistory + maxHistorySize); // Find max for scaling
if (maxProblems == 0) maxProblems = 1; // Avoid division by zero
for (int i = 0; i < maxHistorySize - 1; i++) {
int x1 = (i * SCREEN_WIDTH) / maxHistorySize;
int x2 = ((i + 1) * SCREEN_WIDTH) / maxHistorySize;
int y1 = graphBottom - (problemHistory[(historyIndex + i) % maxHistorySize] * graphHeight) / maxProblems;
int y2 = graphBottom - (problemHistory[(historyIndex + i + 1) % maxHistorySize] * graphHeight) / maxProblems;
display.drawLine(x1, y1, x2, y2, 1); // Draw line between points
}
// Scroll text below the graph
int stateWidth = strlen(problemDescriptions.c_str()) * 12;
int stateWidth = strlen(problemDescriptions.c_str()) * 12; // Approximate width of the status text in pixels
// Scroll the text to the left
if (!is_display_off) { if (!is_display_off) {
if (millis() - lastScrollTime > scrollDelay) { if (millis() - lastScrollTime > scrollDelay) {
// Update scroll position scrollPos -= 11;
scrollPos -= 11; // Move left by 1 pixel each time
lastScrollTime = millis(); lastScrollTime = millis();
// If the text has completely scrolled off, reset scroll position to start from the right
if (scrollPos < -stateWidth) { if (scrollPos < -stateWidth) {
scrollPos = SCREEN_WIDTH; scrollPos = SCREEN_WIDTH;
} }
} }
} }
// Draw the file name with current scroll position
display.setTextSize(2); display.setTextSize(2);
display.setCursor(scrollPos, 48); display.setCursor(scrollPos, 48);
display.print(problemDescriptions); display.print(problemDescriptions);
display.drawLine(0,46,127,46,1); display.setTextSize(1);
display.fillRect(117,35,10,10,0);
display.setCursor(118,36);
display.print(totalProblems);
display.display(); display.display();
} }
@ -196,7 +232,7 @@ void loop() {
displayProblems(); displayProblems();
if (newProblemsDetected) { if (newProblemsDetected) {
beep(1000); playAlertMelody();
newProblemsDetected = false; newProblemsDetected = false;
} }
} }