-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_server_page_generation.cpp
More file actions
329 lines (288 loc) · 18.2 KB
/
setup_server_page_generation.cpp
File metadata and controls
329 lines (288 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "setup_server.h"
#include <WiFi.h>
#include "calendar_handler.h"
const char* SetupServer::getSetupPage() {
static String page;
String wifiStatus;
String sectionClass;
if (config.wifi_ssid.isEmpty()) {
wifiStatus = "<span class='status-incomplete'>Not Configured</span>";
sectionClass = " incomplete";
} else if (WiFi.status() != WL_CONNECTED) {
wifiStatus = "<span class='status-incomplete'>Configured (" + config.wifi_ssid + ") - Not Connected</span>";
sectionClass = " incomplete";
} else {
wifiStatus = "<span class='status-complete'>Configured (" + config.wifi_ssid + ") - Connected</span>";
sectionClass = " complete";
}
String oauthStatus = oauthHandler.isAuthorized() ?
"<span class='status-complete'>Connected</span>" :
"<span class='status-incomplete'>Not Connected</span>";
String calendarStatus = "<span class='status-complete' id='calendar-status'>Loading calendar name...</span>";
page =
"<!DOCTYPE html>"
"<html>"
"<head>"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
"<title>Bindicator Setup</title>"
"<style>"
"body { font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; background-color: #1e1e1e; color: #ffffff; }"
"@media (max-width: 640px) { body { padding: 10px; } }"
".setup-section { display: flex; flex-direction: column; margin-bottom: 30px; padding: 20px; border: 1px solid #333; border-radius: 5px; position: relative; background-color: #252525; }"
"@media (max-width: 640px) { .setup-section { padding: 15px; margin-bottom: 20px; } }"
"h1 { color: #ffffff; }"
"@media (max-width: 640px) { h1 { font-size: 24px; margin: 10px 0; } }"
"h2 { color: #ffffff; margin-top: 0; display: flex; align-items: center; gap: 10px; }"
"@media (max-width: 640px) { h2 { font-size: 20px; } }"
".step-number { background-color: #444; color: white; width: 24px; height: 24px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 14px; }"
"@media (max-width: 640px) { .step-number { width: 20px; height: 20px; font-size: 12px; } }"
"label { display: block; margin-bottom: 5px; }"
"input[type=\"text\"], input[type=\"password\"], select { width: 100%; box-sizing: border-box; padding: 8px; margin-bottom: 10px; border: 1px solid #444; border-radius: 4px; background-color: #333; color: #ffffff; }"
"@media (max-width: 640px) { input[type=\"text\"], input[type=\"password\"], select { padding: 10px; } }"
"button { background-color: #4CAF50; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; }"
"@media (max-width: 640px) { "
"button { width: 100%; padding: 12px; margin-bottom: 8px; } "
".setup-section div[style*=\"display: flex\"] { flex-direction: column; }"
".setup-section div[style*=\"display: flex\"] button { margin-right: 0; }"
"}"
".setup-complete { margin: 30px auto; padding: 30px; border: 2px solid #4CAF50; border-radius: 10px; background-color: #252525; text-align: center; max-width: 500px; }"
"@media (max-width: 640px) { .setup-complete { padding: 20px; margin: 20px auto; } }"
".setup-complete h2 { justify-content: center; margin-bottom: 20px; }"
".setup-complete p { margin: 10px 0; }"
".launch-button { background-color: #4CAF50; color: white; padding: 15px 30px; border: none; border-radius: 5px; font-size: 18px; cursor: pointer; margin-top: 20px; }"
"@media (max-width: 640px) { .launch-button { padding: 12px 24px; font-size: 16px; width: 100%; } }"
"#upcoming-bins-list { color: #888; font-size: 0.9em; margin: 0; padding-left: 20px; }"
"@media (max-width: 640px) { #upcoming-bins-list { padding-left: 15px; } }"
".status-cell { margin-top: 15px; padding-top: 15px; border-top: 1px solid #444; word-break: break-word; }"
".setup-section::before { content: ''; position: absolute; top: 0; left: 0; width: 4px; height: 100%; border-radius: 5px 0 0 5px; }"
".setup-section.incomplete::before { background-color: #f44336; }"
".setup-section.complete::before { background-color: #4CAF50; }"
".setup-section.disabled { opacity: 0.7; background-color: #1a1a1a; }"
".status { margin-top: 10px; }"
".status-complete { color: #4CAF50; }"
".status-incomplete { color: #f44336; }"
".help-text { font-size: 0.9em; color: #888; margin-top: 5px; }"
"</style>"
"</head>"
"<body>"
"<h1>Bindicator Setup</h1>";
// WiFi Section
page += "<div class='setup-section" + sectionClass + "'>"
"<div class='section-content'>"
"<h2><span class='step-number'>1</span> WiFi Setup</h2>"
"<form action='/save' method='POST'>"
"<label for='ssid'>WiFi Name:</label>"
"<input type='text' id='ssid' name='ssid' value='" + config.wifi_ssid + "'>"
"<label for='password'>WiFi Password:</label>"
"<input type='password' id='password' name='password' value='" + config.wifi_password + "'>"
"<button type='submit'>Save WiFi Settings</button>"
"</form>"
"</div>"
"<div class='status-cell'>Status: " + wifiStatus + "</div>"
"</div>";
// OAuth Section
String oauthContent = config.wifi_ssid.isEmpty() ?
"<p>Please complete WiFi setup first</p>" :
"<p>Click below to connect your Google Calendar:</p><a href='/oauth'><button>Connect Google Calendar</button></a>";
page += "<div class='setup-section" +
String(oauthHandler.isAuthorized() ? " complete" : " incomplete") +
String(config.wifi_ssid.isEmpty() ? " disabled" : "") + "'>" +
"<div class='section-content'>"
"<h2><span class='step-number'>2</span> Google Calendar Setup</h2>" +
oauthContent +
"</div>"
"<div class='status-cell'>Status: " + oauthStatus + "</div>"
"</div>";
// Setup complete
if (WiFi.status() == WL_CONNECTED && oauthHandler.isAuthorized()) {
page += "<div id='setupComplete' class='setup-complete'>"
"<h2>Your Bindicator is Ready!</h2>"
"<p>Setup is complete and your Bindicator is configured.</p>"
"<p><small>If your bin events are not on your main calendar, you can change the calendar in the calendar settings section below.</small></p>"
"<button class='launch-button' onclick='restartDevice()'>Launch Bindicator</button>"
"</div>";
}
// Calendar Section
page += "<div id='calendar-section' class='setup-section" +
String(!oauthHandler.isAuthorized() ? " disabled" : " complete") + "'>" +
"<div class='section-content'>"
"<h2><span class='step-number'>3</span> Calendar Settings</h2>"
"<form action='/save-calendar' method='POST'>"
"<label for='calendar_id' style='margin-bottom: 10px; display: block;'>Select Calendar:</label>"
"<div id='calendar-select-container'>"
"<select id='calendar_id' name='calendar_id' style='display:none;' onchange='updateCalendar(event)'>"
"<option value='" + ConfigManager::getCalendarId() + "' selected>" +
(ConfigManager::getCalendarId() == "primary" ? "Main Calendar" : ConfigManager::getCalendarId()) +
"</option>"
"</select>"
"<div id='calendar-loading'>Loading calendars...</div>"
"</div>"
"<p class='help-text'>Choose which calendar contains your bin collection schedule</p>"
"</form>"
"<div id='upcoming-bins' style='margin-top: 15px; display: none;'>"
"<hr style='border: none; border-top: 1px solid #444; margin: 15px 0;'>"
"<h3 style='color: #ffffff; margin: 0 0 10px 0;'>Upcoming Collections</h3>"
"<ul id='upcoming-bins-list' style='color: #888; font-size: 0.9em; margin: 0; padding-left: 20px;'>"
"<li>Loading...</li>"
"</ul>"
"</div>"
"</div>"
"<div class='status-cell'>Status: " + calendarStatus + "</div>"
"</div>";
// Device Control Section
page += "<div class='setup-section'>"
"<h2 style='color: #ff4444;'>Device Control</h2>"
"<div style='display: flex; gap: 10px;'>"
"<button onclick='restartDevice()' style='background-color: #ff4444;'>Restart Device</button>"
"<button onclick='factoryReset()' style='background-color: #ff4444;'>Factory Reset</button>"
"</div>"
"</div>";
page += "<script>"
"const currentId = '" + ConfigManager::getCalendarId() + "';"
"function restartDevice() {"
"if (confirm('Are you sure you want to restart the device?')) {"
"fetch('/restart', { method: 'POST' })"
".then(() => alert('Device is restarting...'))"
".catch(err => alert('Error: ' + err));"
"}"
"}"
"function factoryReset() {"
"if (confirm('WARNING: This will erase all settings and return the device to factory defaults.\\n\\nAre you sure you want to continue?')) {"
"fetch('/factory-reset', { method: 'POST' })"
".then(() => alert('Device is resetting and will restart...'))"
".catch(err => alert('Error: ' + err));"
"}"
"}"
"</script>"
"<script>"
"fetch('/calendars')"
".then(response => {"
"if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);"
"return response.json();"
"})"
".then(data => {"
"const select = document.querySelector('#calendar_id');"
"select.innerHTML = '';"
"const currentId = '" + ConfigManager::getCalendarId() + "';"
"const primaryOption = document.createElement('option');"
"primaryOption.value = 'primary';"
"primaryOption.text = 'Main Calendar';"
"primaryOption.selected = currentId === 'primary';"
"select.appendChild(primaryOption);"
"data.items.forEach(cal => {"
"if (cal.id !== 'primary') {"
"const option = document.createElement('option');"
"option.value = cal.id;"
"option.text = cal.summary;"
"option.selected = cal.id === currentId;"
"select.appendChild(option);"
"}"
"});"
"const selectedCalendar = data.items.find(cal => cal.id === currentId) || "
"{summary: currentId === 'primary' ? 'Main Calendar' : 'Unknown Calendar'};"
"const statusElement = document.querySelector('#calendar-section .status-cell');"
"statusElement.textContent = 'Status: Configured (' + selectedCalendar.summary + ')';"
"document.querySelector('#calendar-loading').style.display = 'none';"
"select.style.display = 'block';"
// Only fetch upcoming bins after calendar list is loaded
"const upcomingDiv = document.querySelector('#upcoming-bins');"
"upcomingDiv.style.display = 'block';"
"const upcomingList = document.querySelector('#upcoming-bins-list');"
"upcomingList.innerHTML = '<li>Loading calendar schedule...</li>';"
"return fetchWithRetry('/upcoming-bins');"
"})"
".then(data => {"
"if (!data) return;" // Skip if no data from fetchWithRetry
"const upcomingList = document.querySelector('#upcoming-bins-list');"
"upcomingList.innerHTML = '';"
"const events = data.items || [];"
"if (events.length === 0) {"
"upcomingList.innerHTML = '<li>No collections scheduled</li>';"
"} else {"
"events.forEach(event => {"
"const date = new Date(event.start.date || event.start.dateTime);"
"const day = date.toLocaleDateString('en-GB', { weekday: 'long', month: 'long', day: 'numeric' });"
"upcomingList.innerHTML += `<li>${day}: ${event.summary}</li>`;"
"});"
"}"
"})"
".catch(error => {"
"console.error('Error:', error);"
"const loadingElement = document.querySelector('#calendar-loading');"
"if (loadingElement) {"
"loadingElement.textContent = 'Error loading calendars';"
"}"
"const statusElement = document.querySelector('#calendar-section .status-cell');"
"if (statusElement) {"
"statusElement.textContent = 'Status: Error loading calendar';"
"}"
"const upcomingList = document.querySelector('#upcoming-bins-list');"
"if (upcomingList) {"
"upcomingList.innerHTML = '<li>Error loading schedule</li>';"
"}"
"});"
"function updateCalendar(event) {"
"event.preventDefault();"
"const select = document.querySelector('#calendar_id');"
"const calendarId = select.value;"
"const selectedOption = select.options[select.selectedIndex];"
"const statusElement = document.querySelector('#calendar-section .status-cell');"
"statusElement.textContent = 'Status: Saving...';"
"const upcomingDiv = document.querySelector('#upcoming-bins');"
"const upcomingList = document.querySelector('#upcoming-bins-list');"
"upcomingList.innerHTML = '<li>Loading new calendar schedule...</li>';"
"upcomingDiv.style.display = 'block';"
"fetch('/save-calendar', {"
"method: 'POST',"
"headers: {'Content-Type': 'application/x-www-form-urlencoded'},"
"body: 'calendar_id=' + encodeURIComponent(calendarId)"
"})"
".then(response => {"
"if (!response.ok) throw new Error('Failed to save calendar');"
"statusElement.textContent = 'Status: Configured (' + selectedOption.text + ')';"
"return fetchWithRetry('/upcoming-bins');"
"})"
".then(data => {"
"upcomingList.innerHTML = '';"
"const events = data.items || [];"
"if (events.length === 0) {"
"upcomingList.innerHTML = '<li>No collections scheduled</li>';"
"} else {"
"events.forEach(event => {"
"const date = new Date(event.start.date || event.start.dateTime);"
"const day = date.toLocaleDateString('en-GB', { weekday: 'long', month: 'long', day: 'numeric' });"
"upcomingList.innerHTML += `<li>${day}: ${event.summary}</li>`;"
"});"
"}"
"})"
".catch(error => {"
"console.error('Error:', error);"
"statusElement.textContent = 'Error saving calendar';"
"upcomingList.innerHTML = '<li>Error loading schedule</li>';"
"alert('Failed to update calendar settings');"
"});"
"return false;"
"}"
"async function fetchWithRetry(url, maxRetries = 3, delay = 1000) {"
"let lastError;"
"for (let i = 0; i < maxRetries; i++) {"
"try {"
"const response = await fetch(url);"
"if (!response.ok) {"
"throw new Error(`HTTP error! status: ${response.status}`);"
"}"
"return await response.json();"
"} catch (error) {"
"console.log(`Attempt ${i + 1} failed:`, error);"
"lastError = error;"
"if (i === maxRetries - 1) break;"
"await new Promise(resolve => setTimeout(resolve, delay * Math.pow(2, i)));" // Exponential backoff
"}"
"}"
"throw lastError;" // Throw the last error if all retries failed
"}"
"</script>"
"</body>"
"</html>";
return page.c_str();
}