From a5e208755e9655ce98fa639d2eb7f9c2db1f17d1 Mon Sep 17 00:00:00 2001 From: acidvegas Date: Thu, 6 Jun 2024 16:50:21 -0400 Subject: [PATCH] Fixed QUIT messages not displaying, started mainlining gotify code --- src/{apps/gotify.cpp => Gotify.cpp} | 37 +++++++++++++++----------- src/Gotify.h | 17 ++++++++++++ src/main.ino | 41 ++++------------------------- 3 files changed, 43 insertions(+), 52 deletions(-) rename src/{apps/gotify.cpp => Gotify.cpp} (57%) create mode 100644 src/Gotify.h diff --git a/src/apps/gotify.cpp b/src/Gotify.cpp similarity index 57% rename from src/apps/gotify.cpp rename to src/Gotify.cpp index 6be3cf0..34cade9 100644 --- a/src/apps/gotify.cpp +++ b/src/Gotify.cpp @@ -1,42 +1,47 @@ -#include +#include "Gotify.h" -using namespace websockets; WebsocketsClient gotifyClient; const char* gotify_server = "ws://your.gotify.server.com/stream"; // Use ws:// or wss:// based on your server configuration const char* gotify_token = "your_gotify_app_token"; -// Reconnection parameters unsigned long lastAttemptTime = 0; const unsigned long reconnectInterval = 60000; // 1 minute + void onMessageCallback(WebsocketsMessage message) { Serial.println("Gotify Notification:"); Serial.println(message.data()); + playNotificationSound(); } + void connectToGotify() { String url = String(gotify_server) + "?token=" + gotify_token; gotifyClient.onMessage(onMessageCallback); gotifyClient.connect(url); - if (gotifyClient.available()) { + if (gotifyClient.available()) Serial.println("Connected to Gotify WebSocket"); - } else { + else Serial.println("Failed to connect to Gotify WebSocket"); - } } -void checkGotifyWebSocket() { - if (!gotifyClient.available()) { - unsigned long currentTime = millis(); - if (currentTime - lastAttemptTime > reconnectInterval) { - Serial.println("Attempting to reconnect to Gotify WebSocket..."); - lastAttemptTime = currentTime; - connectToGotify(); + +void loopGotifyWebSocket() { + while (true) { + if (!gotifyClient.available()) { + unsigned long currentTime = millis(); + if (currentTime - lastAttemptTime > reconnectInterval) { + Serial.println("Attempting to reconnect to Gotify WebSocket..."); + lastAttemptTime = currentTime; + connectToGotify(); + } + } else { + gotifyClient.poll(); } - } else { - gotifyClient.poll(); + + delay(10); } -} \ No newline at end of file +} diff --git a/src/Gotify.h b/src/Gotify.h new file mode 100644 index 0000000..efc99cb --- /dev/null +++ b/src/Gotify.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +#include "Speaker.h" + +using namespace websockets; + +extern WebsocketsClient gotifyClient; +extern const char* gotify_server; +extern const char* gotify_token; +extern unsigned long lastAttemptTime; +extern const unsigned long reconnectInterval; + +void onMessageCallback(WebsocketsMessage message); +void connectToGotify(); +void loopGotifyWebSocket(); \ No newline at end of file diff --git a/src/main.ino b/src/main.ino index 8c2982c..ebf3bfc 100644 --- a/src/main.ino +++ b/src/main.ino @@ -48,9 +48,6 @@ std::map nickColors; std::vector lines; // Possible rename to bufferLines ? std::vector mentions; std::vector wifiNetworks; - -// Global variables to cache preferences and buffers - String inputBuffer = ""; // Leftover crack variables (will be removed when preferences are done) @@ -232,7 +229,7 @@ void loop() { if (readyToJoinChannel && millis() >= joinChannelTime) { tft.fillScreen(TFT_BLACK); updateStatusBar(); - sendIRC("JOIN " + String(channel)); + sendIRC("JOIN " + String(irc_channel)); readyToJoinChannel = false; } @@ -244,9 +241,8 @@ void loop() { } // Handle inactivity timeout - if (screenOn && millis() - lastActivityTime > INACTIVITY_TIMEOUT) { + if (screenOn && millis() - lastActivityTime > INACTIVITY_TIMEOUT) turnOffScreen(); - } } } } @@ -648,10 +644,10 @@ uint32_t generateRandomColor() { uint16_t getColorFromPercentage(int percentage) { - if (percentage > 75) return TFT_GREEN; + if (percentage > 75) return TFT_GREEN; else if (percentage > 50) return TFT_YELLOW; else if (percentage > 25) return TFT_ORANGE; - else return TFT_RED; + else return TFT_RED; } @@ -1169,7 +1165,7 @@ void parseAndDisplay(String line) { } else if (command == "PART" && line.indexOf(channel) != -1) { String senderNick = line.substring(1, line.indexOf('!')); addLine(senderNick, " has EMO-QUIT " + String(channel), "part"); - } else if (command == "QUIT" && line.indexOf(channel) != -1) { + } else if (command == "QUIT") { String senderNick = line.substring(1, line.indexOf('!')); addLine(senderNick, "", "quit"); } else if (command == "NICK") { @@ -1287,7 +1283,6 @@ void updateStatusBar() { uint16_t darkerGrey = tft.color565(25, 25, 25); tft.fillRect(0, 0, SCREEN_WIDTH, STATUS_BAR_HEIGHT, darkerGrey); - // Display the time struct tm timeinfo; char timeStr[9]; @@ -1415,30 +1410,4 @@ void printDeviceInfo() { Serial.println(" Local IP: " + wifiLocalIP); Serial.println(" Gateway IP: " + wifiGatewayIP); } - - // Display on TFT - tft.fillScreen(TFT_BLACK); - int line = 0; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print("Chip ID: "); tft.setTextColor(TFT_WHITE); tft.println(String(chipId, HEX)); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print("MAC Address: "); tft.setTextColor(TFT_WHITE); tft.println(macAddress); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print("Chip Info: "); tft.setTextColor(TFT_WHITE); tft.println(chipInfo); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_CYAN); tft.print("Battery: "); tft.setTextColor(TFT_WHITE); tft.println(""); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print(" Pin Value: "); tft.setTextColor(TFT_WHITE); tft.println(analogRead(34)); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print(" Avg Pin Val: "); tft.setTextColor(TFT_WHITE); tft.println(BL.pinRead()); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print(" Volts: "); tft.setTextColor(TFT_WHITE); tft.println(BL.getBatteryVolts()); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print(" Charge Level:"); tft.setTextColor(TFT_WHITE); tft.println(BL.getBatteryChargeLevel() + "%"); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_CYAN); tft.print("Memory: "); tft.setTextColor(TFT_WHITE); tft.println(""); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print(" Flash: "); tft.setTextColor(TFT_WHITE); tft.println(flashInfo); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print(" PSRAM: "); tft.setTextColor(TFT_WHITE); tft.println(psramInfo); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print(" Heap: "); tft.setTextColor(TFT_WHITE); tft.println(heapInfo); line++; - if (WiFi.status() == WL_CONNECTED) { - tft.setCursor(0, line * 16); tft.setTextColor(TFT_CYAN); tft.print("WiFi Info: "); tft.setTextColor(TFT_WHITE); tft.println(""); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print(" SSID: "); tft.setTextColor(TFT_WHITE); tft.println(wifiSSID); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print(" Channel: "); tft.setTextColor(TFT_WHITE); tft.println(wifiChannel); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print(" Signal: "); tft.setTextColor(TFT_WHITE); tft.println(wifiSignal); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print(" Local IP: "); tft.setTextColor(TFT_WHITE); tft.println(wifiLocalIP); line++; - tft.setCursor(0, line * 16); tft.setTextColor(TFT_YELLOW); tft.print(" Gateway IP: "); tft.setTextColor(TFT_WHITE); tft.println(wifiGatewayIP); line++; - } else { - tft.setCursor(0, line * 16); tft.setTextColor(TFT_CYAN); tft.print("WiFi Info: "); tft.setTextColor(TFT_WHITE); tft.println("Not connected"); line++; - } } \ No newline at end of file