|
7 | 7 | #define F1_ROUND_OFFSET_LABEL "roundOffset" |
8 | 8 | #define F1_CURRENT_RACE_NOTIFICATION_LABEL "currentRaceNotification" |
9 | 9 |
|
10 | | -class F1Config { |
11 | | - public: |
12 | | - //How the time will be displayed, see here for more info: https://github.com/ropg/ezTime#datetime |
13 | | - String timeFormat = "l, d-M-Y H:i"; // Friday, 17-Mar-2023 00:30 |
14 | | - String timeZone = "Europe/London"; //seems to be something wrong with Europe/Dublin |
15 | | - |
16 | | - // Telegram BOT Token (Get from Botfather) |
17 | | - String botToken = ""; |
| 10 | +class F1Config |
| 11 | +{ |
| 12 | +public: |
| 13 | + // How the time will be displayed, see here for more info: https://github.com/ropg/ezTime#datetime |
| 14 | + String timeFormat = "l, H:i"; // Friday, 00:30 |
| 15 | + String timeZone = "Europe/London"; // seems to be something wrong with Europe/Dublin |
| 16 | + |
| 17 | + // Telegram BOT Token (Get from Botfather) |
| 18 | + String botToken = ""; |
| 19 | + |
| 20 | + // Use @myidbot (IDBot) to find out the chat ID of an individual or a group |
| 21 | + // Also note that you need to click "start" on a bot before it can |
| 22 | + // message you |
| 23 | + String chatId = ""; |
| 24 | + |
| 25 | + int roundOffset = 0; |
| 26 | + |
| 27 | + bool currentRaceNotification = false; |
| 28 | + |
| 29 | + bool isTelegramConfigured() |
| 30 | + { |
| 31 | + return (botToken != "") && (chatId != ""); |
| 32 | + } |
| 33 | + |
| 34 | + bool fetchConfigFile() |
| 35 | + { |
| 36 | + if (SPIFFS.exists(F1_CONFIG_JSON)) |
| 37 | + { |
| 38 | + // file exists, reading and loading |
| 39 | + Serial.println("reading config file"); |
| 40 | + File configFile = SPIFFS.open(F1_CONFIG_JSON, "r"); |
| 41 | + if (configFile) |
| 42 | + { |
| 43 | + Serial.println("opened config file"); |
| 44 | + StaticJsonDocument<1024> json; |
| 45 | + DeserializationError error = deserializeJson(json, configFile); |
| 46 | + serializeJsonPretty(json, Serial); |
| 47 | + if (!error) |
| 48 | + { |
| 49 | + Serial.println("\nparsed json"); |
| 50 | + |
| 51 | + if (json.containsKey(F1_TIME_ZONE_LABEL)) |
| 52 | + { |
| 53 | + timeZone = String(json[F1_TIME_ZONE_LABEL].as<String>()); |
| 54 | + } |
18 | 55 |
|
19 | | - // Use @myidbot (IDBot) to find out the chat ID of an individual or a group |
20 | | - // Also note that you need to click "start" on a bot before it can |
21 | | - // message you |
22 | | - String chatId = ""; |
| 56 | + if (json.containsKey(F1_TIME_FORMAT_LABEL)) |
| 57 | + { |
| 58 | + timeFormat = String(json[F1_TIME_FORMAT_LABEL].as<String>()); |
| 59 | + } |
23 | 60 |
|
24 | | - int roundOffset = 0; |
| 61 | + if (json.containsKey(F1_BOT_TOKEN_LABEL)) |
| 62 | + { |
| 63 | + botToken = String(json[F1_BOT_TOKEN_LABEL].as<String>()); |
| 64 | + } |
25 | 65 |
|
26 | | - bool currentRaceNotification = false; |
| 66 | + if (json.containsKey(F1_CHAT_ID_LABEL)) |
| 67 | + { |
| 68 | + chatId = String(json[F1_CHAT_ID_LABEL].as<String>()); |
| 69 | + } |
27 | 70 |
|
28 | | - bool isTelegramConfigured(){ |
29 | | - return (botToken != "") && (chatId != ""); |
30 | | - } |
| 71 | + if (json.containsKey(F1_ROUND_OFFSET_LABEL)) |
| 72 | + { |
| 73 | + roundOffset = json[F1_ROUND_OFFSET_LABEL].as<int>(); |
| 74 | + } |
31 | 75 |
|
32 | | - bool fetchConfigFile() { |
33 | | - if (SPIFFS.exists(F1_CONFIG_JSON)) { |
34 | | - //file exists, reading and loading |
35 | | - Serial.println("reading config file"); |
36 | | - File configFile = SPIFFS.open(F1_CONFIG_JSON, "r"); |
37 | | - if (configFile) { |
38 | | - Serial.println("opened config file"); |
39 | | - StaticJsonDocument<1024> json; |
40 | | - DeserializationError error = deserializeJson(json, configFile); |
41 | | - serializeJsonPretty(json, Serial); |
42 | | - if (!error) { |
43 | | - Serial.println("\nparsed json"); |
44 | | - |
45 | | - if (json.containsKey(F1_TIME_ZONE_LABEL)) { |
46 | | - timeZone = String(json[F1_TIME_ZONE_LABEL].as<String>()); |
47 | | - } |
48 | | - |
49 | | - if (json.containsKey(F1_TIME_FORMAT_LABEL)) { |
50 | | - timeFormat = String(json[F1_TIME_FORMAT_LABEL].as<String>()); |
51 | | - } |
52 | | - |
53 | | - if (json.containsKey(F1_BOT_TOKEN_LABEL)) { |
54 | | - botToken = String(json[F1_BOT_TOKEN_LABEL].as<String>()); |
55 | | - } |
56 | | - |
57 | | - if (json.containsKey(F1_CHAT_ID_LABEL)) { |
58 | | - chatId = String(json[F1_CHAT_ID_LABEL].as<String>()); |
59 | | - } |
60 | | - |
61 | | - if (json.containsKey(F1_ROUND_OFFSET_LABEL)) { |
62 | | - roundOffset = json[F1_ROUND_OFFSET_LABEL].as<int>(); |
63 | | - } |
64 | | - |
65 | | - if (json.containsKey(F1_CURRENT_RACE_NOTIFICATION_LABEL)) { |
66 | | - currentRaceNotification = json[F1_CURRENT_RACE_NOTIFICATION_LABEL].as<bool>(); |
67 | | - } |
68 | | - |
69 | | - return true; |
70 | | - |
71 | | - } else { |
72 | | - Serial.println("failed to load json config"); |
73 | | - return false; |
| 76 | + if (json.containsKey(F1_CURRENT_RACE_NOTIFICATION_LABEL)) |
| 77 | + { |
| 78 | + currentRaceNotification = json[F1_CURRENT_RACE_NOTIFICATION_LABEL].as<bool>(); |
74 | 79 | } |
| 80 | + |
| 81 | + return true; |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + Serial.println("failed to load json config"); |
| 86 | + return false; |
75 | 87 | } |
76 | 88 | } |
| 89 | + } |
77 | 90 |
|
78 | | - |
79 | | - Serial.println("Config file does not exist"); |
| 91 | + Serial.println("Config file does not exist"); |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + bool saveConfigFile() |
| 96 | + { |
| 97 | + Serial.println(F("Saving config")); |
| 98 | + StaticJsonDocument<1024> json; |
| 99 | + json[F1_TIME_ZONE_LABEL] = timeZone; |
| 100 | + json[F1_TIME_FORMAT_LABEL] = timeFormat; |
| 101 | + json[F1_BOT_TOKEN_LABEL] = botToken; |
| 102 | + json[F1_CHAT_ID_LABEL] = chatId; |
| 103 | + json[F1_ROUND_OFFSET_LABEL] = roundOffset; |
| 104 | + json[F1_CURRENT_RACE_NOTIFICATION_LABEL] = currentRaceNotification; |
| 105 | + |
| 106 | + File configFile = SPIFFS.open(F1_CONFIG_JSON, "w"); |
| 107 | + if (!configFile) |
| 108 | + { |
| 109 | + Serial.println("failed to open config file for writing"); |
80 | 110 | return false; |
81 | | - |
82 | 111 | } |
83 | 112 |
|
84 | | - bool saveConfigFile() { |
85 | | - Serial.println(F("Saving config")); |
86 | | - StaticJsonDocument<1024> json; |
87 | | - json[F1_TIME_ZONE_LABEL] = timeZone; |
88 | | - json[F1_TIME_FORMAT_LABEL] = timeFormat; |
89 | | - json[F1_BOT_TOKEN_LABEL] = botToken; |
90 | | - json[F1_CHAT_ID_LABEL] = chatId; |
91 | | - json[F1_ROUND_OFFSET_LABEL] = roundOffset; |
92 | | - json[F1_CURRENT_RACE_NOTIFICATION_LABEL] = currentRaceNotification; |
93 | | - |
94 | | - File configFile = SPIFFS.open(F1_CONFIG_JSON, "w"); |
95 | | - if (!configFile) { |
96 | | - Serial.println("failed to open config file for writing"); |
97 | | - return false; |
98 | | - } |
99 | | - |
100 | | - serializeJsonPretty(json, Serial); |
101 | | - if (serializeJson(json, configFile) == 0) { |
102 | | - Serial.println(F("Failed to write to file")); |
103 | | - return false; |
104 | | - } |
105 | | - configFile.close(); |
106 | | - return true; |
| 113 | + serializeJsonPretty(json, Serial); |
| 114 | + if (serializeJson(json, configFile) == 0) |
| 115 | + { |
| 116 | + Serial.println(F("Failed to write to file")); |
| 117 | + return false; |
107 | 118 | } |
| 119 | + configFile.close(); |
| 120 | + return true; |
| 121 | + } |
108 | 122 | }; |
0 commit comments