Fixed QUIT messages not displaying, started mainlining gotify code

This commit is contained in:
Dionysus 2024-06-06 16:50:21 -04:00
parent 995529af02
commit a5e208755e
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE
3 changed files with 43 additions and 52 deletions

View File

@ -1,42 +1,47 @@
#include <ArduinoWebsockets.h>
#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);
}
}
}

17
src/Gotify.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include <ArduinoWebsockets.h>
#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();

View File

@ -48,9 +48,6 @@ std::map<String, uint32_t> nickColors;
std::vector<String> lines; // Possible rename to bufferLines ?
std::vector<bool> mentions;
std::vector<WiFiNetwork> 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++;
}
}