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 problems[10];
String problemDescriptions = "Test";
String problemDescriptions;
int problemCount = 0;
int severityCounts[5] = {0};
int severityCounts[6] = {0};
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() {
pinMode(PIN_BTN_L, INPUT);
@ -31,9 +41,10 @@ void initSystems() {
Wire.setClock(400000L); // Set I2C to Fast Mode (400 kHz)
display.ssd1306_command(SSD1306_SETCONTRAST);
display.ssd1306_command(200); // Value between 0 and 255
// Initialize the SSD1306 display
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
@ -41,14 +52,12 @@ void initSystems() {
// Clear the display buffer
display.clearDisplay();
display.setTextSize(1);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.display();
}
int totalProblems = 0; // Variable to store the total number of active problems
void fetchActiveProblems() {
WiFiClient client;
HTTPClient http;
@ -90,6 +99,10 @@ void fetchActiveProblems() {
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;
for (JsonObject problem : result) {
String eventId = problem["eventid"].as<String>();
@ -101,10 +114,6 @@ void fetchActiveProblems() {
newProblemsDetected = true;
}
if (newProblemCount < 10) {
problems[newProblemCount] = description;
}
if (severity >= 0 && severity < 5) {
severityCounts[severity]++;
}
@ -121,60 +130,87 @@ void fetchActiveProblems() {
} else {
beep(1000);
}
} else {
display.clearDisplay();
display.setCursor(0,30);
display.print("Error querying zabbix API");
} else {
problemDescriptions="Zabbix API error";
}
http.end();
}
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 = 150; // Delay in milliseconds between scroll updates
void playAlertMelody() {
int toneDuration = 60; // Each tone lasts 150ms
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() {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("N:");
display.print(severityCounts[0]);
display.print("I:");
display.print(severityCounts[1]);
display.print("W:");
display.print(severityCounts[2]);
display.print("A:");
display.print(severityCounts[3]);
display.print("H:");
display.print(severityCounts[4]);
display.print("D:");
display.print(severityCounts[0]);
display.setCursor(0,10);
display.print("Total Problems: ");
display.print(totalProblems); // Display total problems
display.setCursor(0, 0);
display.printf(" I:%d W:%d A:%d H:%d D:%d",
severityCounts[1],
severityCounts[2],
severityCounts[3],
severityCounts[4],
severityCounts[5]);
display.drawLine(0, 45, 127, 45, 1); // Line under the graph
display.drawLine(0, 10, 127, 10, 1); // Line under the graph
// Draw the history graph
int graphHeight = 30; // Height of the graph
int graphTop = 15; // Top position of the graph
int graphBottom = graphTop + graphHeight;
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 (millis() - lastScrollTime > scrollDelay) {
// Update scroll position
scrollPos -= 11; // Move left by 1 pixel each time
scrollPos -= 11;
lastScrollTime = millis();
// If the text has completely scrolled off, reset scroll position to start from the right
if (scrollPos < -stateWidth) {
scrollPos = SCREEN_WIDTH;
}
}
}
// Draw the file name with current scroll position
display.setTextSize(2);
display.setCursor(scrollPos, 48);
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();
}
@ -196,7 +232,7 @@ void loop() {
displayProblems();
if (newProblemsDetected) {
beep(1000);
playAlertMelody();
newProblemsDetected = false;
}
}
}