Skip to content

Commit

Permalink
Merge pull request #7 from witnessmenow/dev
Browse files Browse the repository at this point in the history
Making Telegram support optional
  • Loading branch information
witnessmenow authored Jul 4, 2023
2 parents 1cd0f7f + 250206b commit 2fe1ebf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
26 changes: 18 additions & 8 deletions F1-Notifications/F1-Notifications.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//#define YELLOW_DISPLAY

// 2. Matrix Displays (Like the ESP32 Trinity)
//#define MATRIX_DISPLAY
#define MATRIX_DISPLAY

// If no defines are set, it will default to CYD
#if !defined(YELLOW_DISPLAY) && !defined(MATRIX_DISPLAY)
Expand Down Expand Up @@ -215,17 +215,27 @@ bool notificaitonEventRaised = false;

void sendNotification() {
// Cause it could be set to the image one
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
Serial.println("Sending notifcation");
f1Config.currentRaceNotification = sendNotificationOfNextRace(&bot);
if (!f1Config.currentRaceNotification) {
//Notificaiton failed, raise event again
Serial.println("Notfication failed");
setEvent( sendNotification, getNotifyTime() );
if (f1Config.isTelegramConfigured()) {
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
Serial.println("Sending notifcation");
f1Config.currentRaceNotification = sendNotificationOfNextRace(&bot);
if (!f1Config.currentRaceNotification) {
//Notificaiton failed, raise event again
Serial.println("Notfication failed");
setEvent( sendNotification, getNotifyTime() );
} else {
notificaitonEventRaised = false;
f1Config.saveConfigFile();
}
} else {

Serial.println("Would have sent Notification now, but telegram is not configured");

notificaitonEventRaised = false;
f1Config.currentRaceNotification = true;
f1Config.saveConfigFile();
}

}

bool first = true;
Expand Down
4 changes: 4 additions & 0 deletions F1-Notifications/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class F1Config {

bool currentRaceNotification = false;

bool isTelegramConfigured(){
return (botToken != "") && (chatId != "");
}

bool fetchConfigFile() {
if (SPIFFS.exists(F1_CONFIG_JSON)) {
//file exists, reading and loading
Expand Down
8 changes: 7 additions & 1 deletion F1-Notifications/raceLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ int fetchRaceJson(FileFetcher fileFetcher) {
}

bool saveCurrentRaceToFile(const JsonObject& raceJson) {

if(raceJson.isNull()){
Serial.println("Race data is null, nothing to save");
return false;
}

File currentRaceFile = SPIFFS.open(CURRENT_RACE_FILE_NAME, "w");
if (!currentRaceFile) {
Serial.println("failed to open config file for writing");
Expand Down Expand Up @@ -229,7 +235,7 @@ bool getNextRace(int &offset, bool &notificationSent, F1Display* f1Display, bool
bool newRace = false;
int roundNumber = races[i]["round"];
if (roundNumber != offset) {
if (saveCurrentRaceToFile(races[i].to<JsonObject>())) {
if (saveCurrentRaceToFile(races[i])) {
offset = roundNumber;
notificationSent = false;
Serial.println("New Race");
Expand Down

0 comments on commit 2fe1ebf

Please sign in to comment.