-
Notifications
You must be signed in to change notification settings - Fork 2
/
setupAssist.cpp
193 lines (179 loc) · 6.69 KB
/
setupAssist.cpp
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
// Assist setup for new app installations
// original provided by gemi254
//
// s60sc 2023
#include "appGlobals.h"
#if (!INCLUDE_CERTS)
const char* git_rootCACertificate = "";
#endif
static fs::FS fp = STORAGE;
static bool wgetFile(const char* filePath) {
// download required data file from github repository and store
bool res = false;
if (fp.exists(filePath)) {
// if file exists but is empty, delete it to allow re-download
File f = fp.open(filePath, FILE_READ);
size_t fSize = f.size();
f.close();
if (!fSize) fp.remove(filePath);
}
if (!fp.exists(filePath)) {
char downloadURL[150];
snprintf(downloadURL, 150, "%s%s", GITHUB_PATH, filePath);
File f = fp.open(filePath, FILE_WRITE);
if (f) {
WiFiClientSecure wclient;
if (remoteServerConnect(wclient, GITHUB_HOST, HTTPS_PORT, git_rootCACertificate, SETASSIST)) {
HTTPClient https;
if (https.begin(wclient, GITHUB_HOST, HTTPS_PORT, downloadURL, true)) {
LOG_INF("Downloading %s from %s", filePath, downloadURL);
int httpCode = https.GET();
int fileSize = 0;
if (httpCode == HTTP_CODE_OK) {
fileSize = https.writeToStream(&f);
if (fileSize <= 0) {
LOG_WRN("Download failed: writeToStream - %s", https.errorToString(fileSize).c_str());
httpCode = 0;
} else LOG_INF("Downloaded %s, size %s", filePath, fmtSize(fileSize));
} else LOG_WRN("Download failed, error: %s", https.errorToString(httpCode).c_str());
https.end();
f.close();
if (httpCode == HTTP_CODE_OK) {
if (!strcmp(filePath, CONFIG_FILE_PATH)) doRestart("Config file downloaded");
res = true;
} else {
LOG_WRN("HTTP Get failed with code: %d", httpCode);
fp.remove(filePath);
}
}
}
remoteServerClose(wclient);
} else LOG_WRN("Open failed: %s", filePath);
} else res = true;
return res;
}
bool checkDataFiles() {
// Download any missing data files
bool res = false;
if (strlen(GITHUB_PATH)) {
res = wgetFile(COMMON_JS_PATH);
if (res) res = wgetFile(INDEX_PAGE_PATH);
if (res) res = appDataFiles();
} else res = true; // no download needed
return res;
}
const char* setupPage_html = R"~(
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Application setup</title>
</head>
<script>
function Config(){
if (!window.confirm('This will reboot the device to activate new settings.')) return false;
fetch('/control?ST_SSID=' + encodeURI(document.getElementById('ST_SSID').value))
.then(r => { console.log(r); return fetch('/control?ST_Pass=' + encodeURI(document.getElementById('ST_Pass').value)) })
.then(r => { console.log(r); return fetch('/control?save=1') })
.then(r => { console.log(r); return fetch('/control?reset=1') })
.then(r => { console.log(r); });
return false;
}
</script>
<body style="font-size:18px">
<br>
<center>
<table border="0">
<tr><th colspan="3">Wifi setup..</th></tr>
<tr><td colspan="3"></td></tr>
<tr>
<td>SSID</td>
<td> </td>
<td><input id="ST_SSID" name="ST_SSID" length=32 placeholder="Router SSID" class="input"></td>
</tr>
<tr>
<td>Password</td>
<td> </td>
<td><input id="ST_Pass" name="ST_Pass" length=64 placeholder="Router password" class="input"></td>
</tr>
<tr><td colspan="3"></td></tr>
<tr><td colspan="3" align="center">
<button type="button" onClick="return Config()">Connect</button> <button type="button" onclick="window.location.reload;">Cancel</button>
</td></tr>
</table>
<br/><br/><a href="/web?OTA.htm"><button>OTA Update</button></a>
</center>
</body>
</html>
)~";
const char* otaPage_html = R"~(
<html>
<head>
<title>OTA</title>
<style>
html body {height: 100%;}
body {
font-family: Helvetica !important;
background: #181818;
color: WhiteSmoke;
font-size: 1rem;;
}
</style>
</head>
<body>
<br>
<h3>Upload data file or bin file to ESP32</h3>
<br>
<a href="javascript:history.back()" style="color: WhiteSmoke;">Go Back</a>
<br><br><br>
<form id="upload_form" enctype="multipart/form-data" method="post">
<input type="file" name="otafile" id="otafile" onchange="otaUploadFile()"><br>
<br>
<progress id="progressOta" value="0" max="100" style="width:200px;"></progress>%
<h3 id="status"></h3>
<p id="loaded_n_total"></p>
</form>
<script>
const defaultPort = window.location.protocol == 'http:' ? 80 : 443;
const webPort = !window.location.port ? defaultPort : window.location.port; // in case alternative ports specified
const webServer = window.location.protocol + '//' + document.location.hostname + ':' + webPort;
const $ = document.querySelector.bind(document);
async function otaUploadFile() {
// notify server to start ota
let file = $("#otafile").files[0];
const response = await fetch('/control?startOTA=' + file.name);
if (response.ok) {
// submit file for uploading
let xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", progressHandler, false);
xhr.addEventListener("load", completeHandler, false);
xhr.addEventListener("error", errorHandler, false);
xhr.addEventListener("abort", abortHandler, false);
xhr.open("POST", webServer + '/upload');
xhr.send(file);
} else alert(response.status + ": " + response.statusText);
}
function progressHandler(event) {
$("#loaded_n_total").innerHTML = "Uploaded " + event.loaded + " of " + event.total + " bytes";
let percent = (event.loaded / event.total) * 100;
$("#progressOta").value = Math.round(percent);
$("#status").innerHTML = Math.round(percent) + "% transferred";
if (event.loaded == event.total) $("#status").innerHTML = 'Uploaded, wait for completion result';
}
function completeHandler(event) {
$("#status").innerHTML = event.target.responseText;
$("#progressOta").value = 0;
}
function errorHandler(event) {
$("#status").innerHTML = "Upload Failed";
$("#progressOta").value = 0;
}
function abortHandler(event) {
$("#status").innerHTML = "Upload Aborted";
$("#progressOta").value = 0;
}
</script>
</body>
</html>
)~";