This commit is contained in:
Tomislav Kopić 2024-11-26 22:10:51 +01:00
parent 2da3ff1019
commit 6ff3352c57
2 changed files with 36 additions and 35 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"string": "cpp"
}
}

View File

@ -17,6 +17,7 @@ const unsigned long displayInterval = 100; // 100 ms
String lastEventId = "";
String problems[10];
String problemDescriptions;
int problemCount = 0;
int severityCounts[5] = {0};
bool newProblemsDetected = false;
@ -48,6 +49,8 @@ void initSystems() {
display.display();
}
int totalProblems = 0; // Variable to store the total number of active problems
void fetchActiveProblems() {
WiFiClient client;
HTTPClient http;
@ -55,7 +58,7 @@ void fetchActiveProblems() {
http.addHeader("Content-Type", "application/json");
// JSON payload
DynamicJsonDocument doc(512);
DynamicJsonDocument doc(1024);
doc["jsonrpc"] = "2.0";
doc["method"] = "problem.get";
doc["id"] = 1;
@ -83,6 +86,8 @@ void fetchActiveProblems() {
int newProblemCount = 0;
memset(severityCounts, 0, sizeof(severityCounts)); // Reset severities
totalProblems = result.size(); // Set total problems count
for (JsonObject problem : result) {
String eventId = problem["eventid"].as<String>();
int severity = problem["severity"].as<int>();
@ -104,6 +109,7 @@ void fetchActiveProblems() {
}
newProblemCount++;
problemDescriptions += description;
}
problemCount = newProblemCount;
@ -119,49 +125,39 @@ void fetchActiveProblems() {
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 displayProblems() {
static int scrollPos = SCREEN_WIDTH;
static unsigned long lastScrollTime = 0;
if (millis() - lastDisplayUpdate < displayInterval) {
return; // Skip rendering if the display interval hasn't elapsed
}
lastDisplayUpdate = millis();
display.clearDisplay();
// Header
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Problems: ");
display.print(problemCount);
display.setCursor(0,0);
display.print("Total Problems: ");
display.print(totalProblems); // Display total problems
// Display severity counts (S0 to S4)
for (int i = 0; i < 5; i++) {
display.setCursor(0, 10 + (i * 10));
display.printf("S%d: %d", i, severityCounts[i]);
}
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
// Scrolling description of the first problem
if (problemCount > 0) {
String problemText = problems[0];
int16_t x1, y1;
uint16_t textWidth, textHeight;
display.getTextBounds(problemText, 0, 55, &x1, &y1, &textWidth, &textHeight);
lastScrollTime = millis();
if (millis() - lastScrollTime > 100) {
scrollPos -= 2;
if (scrollPos < -textWidth) {
// If the text has completely scrolled off, reset scroll position to start from the right
if (scrollPos < -stateWidth) {
scrollPos = SCREEN_WIDTH;
}
lastScrollTime = millis();
}
display.setTextSize(1);
display.setCursor(scrollPos, 55);
display.print(problemText);
}
// Draw the file name with current scroll position
display.setTextSize(2);
display.setCursor(scrollPos, 54);
display.print(problemDescriptions);
display.setCursor(0, 44);
display.setTextSize(1);
display.display();
}
@ -175,7 +171,7 @@ void loop() {
unsigned long currentMillis = millis();
cubeButtonHandler();
if (currentMillis - lastRefresh >= refreshInterval) {
if (!is_display_off && currentMillis - lastRefresh >= refreshInterval) {
lastRefresh = currentMillis;
fetchActiveProblems();
}