Get API to work
This commit is contained in:
		
							
								
								
									
										85
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										85
									
								
								src/main.cpp
									
									
									
									
									
								
							| @@ -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>(); |     if (!error) { | ||||||
|     int newProblemCount = 0; |       JsonArray result = responseDoc["result"].as<JsonArray>(); | ||||||
|     memset(severityCounts, 0, sizeof(severityCounts)); // Reset severities |  | ||||||
|  |  | ||||||
|     totalProblems = result.size(); // Set total problems count |       // Clear descriptions and severity counts | ||||||
|  |       memset(severityCounts, 0, sizeof(severityCounts)); | ||||||
|  |       problemDescriptions = ""; // Reset before appending | ||||||
|  |  | ||||||
|     for (JsonObject problem : result) { |       totalProblems = result.size(); // Set total problems count | ||||||
|       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 as integers |       int newProblemCount = 0; | ||||||
|       if (eventId.toInt() > lastEventId.toInt()) { |       for (JsonObject problem : result) { | ||||||
|         newProblemsDetected = true; |         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; |       if (problemCount > 0) { | ||||||
|  |         lastEventId = result[0]["eventid"].as<String>(); | ||||||
|       } |       } | ||||||
|  |     } else { | ||||||
|       // Count severities |       beep(1000); | ||||||
|       if (severity >= 0 && severity < 5) { |  | ||||||
|         severityCounts[severity]++; |  | ||||||
|       } |  | ||||||
|  |  | ||||||
|       newProblemCount++; |  | ||||||
|       problemDescriptions += description; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     problemCount = newProblemCount; |  | ||||||
|  |  | ||||||
|     // Update lastEventId after processing the most recent event |  | ||||||
|     if (problemCount > 0) { |  | ||||||
|       lastEventId = result[0]["eventid"].as<String>(); |  | ||||||
|     } |     } | ||||||
|   } else {     |   } else {     | ||||||
|     beep(2000); |     display.clearDisplay(); | ||||||
|  |     display.setCursor(0,30); | ||||||
|  |     display.print("Error querying zabbix API"); | ||||||
|  |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   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(); | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user