Skip to content

Commit 953d870

Browse files
fix: some further improvements of logging and socket-events
1 parent 731beca commit 953d870

File tree

18 files changed

+497
-387
lines changed

18 files changed

+497
-387
lines changed

components/esp32-javascript/esp32-javascript.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,6 @@ static void createConsole(duk_context *ctx)
410410

411411
static duk_ret_t el_suspend(duk_context *ctx)
412412
{
413-
// force garbage collection 2 times see duktape doc
414-
// greatly increases perfomance with external memory
415-
duk_gc(ctx, 0);
416-
duk_gc(ctx, 0);
417-
418413
// feed watchdog
419414
vTaskDelay(1);
420415

@@ -426,6 +421,19 @@ static duk_ret_t el_suspend(duk_context *ctx)
426421
int arrsize = 0;
427422
TickType_t timeout = portMAX_DELAY;
428423

424+
// force garbage collection 2 times see duktape doc
425+
// greatly increases perfomance with external memory
426+
duk_gc(ctx, 0);
427+
if (uxQueueMessagesWaiting(el_event_queue) == 0)
428+
{
429+
// if no event is waiting also compact heap
430+
duk_gc(ctx, DUK_GC_COMPACT);
431+
}
432+
else
433+
{
434+
duk_gc(ctx, 0);
435+
}
436+
429437
while (xQueueReceive(el_event_queue, &events, timeout) == pdTRUE)
430438
{
431439
timeout = 0; // set timeout to 0 to not wait in while loop if there are no more events available

components/esp32-javascript/modules/esp32-javascript/boot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ function connectToWifi() {
207207
else if (evt.status === 2) {
208208
console.debug("WIFI: CONNECTING...");
209209
}
210-
});
210+
}, config_1.config.wifi.bssid);
211211
}
212212
function main() {
213213
var _a, _b;

components/esp32-javascript/modules/esp32-javascript/boot.ts

Lines changed: 71 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -148,75 +148,82 @@ function connectToWifi() {
148148
}
149149

150150
let retries = 0;
151-
wifi.connectWifi(config.wifi.ssid, config.wifi.password, function (evt, ip) {
152-
if (evt.status === 0) {
153-
console.debug("WIFI: DISCONNECTED");
154-
if (!configServerStarted) {
155-
retries++;
156-
}
157-
if (!configServerStarted && retries === 5) {
158-
console.warn("Maximum retries exceeded to connect to wifi.");
159-
if (config?.ota?.offline) {
160-
stopWifi();
161-
loadOfflineScript();
162-
} else {
163-
console.warn("No offline script was found.");
164-
startSoftApMode();
165-
}
166-
}
167-
} else if (evt.status === 1) {
168-
if (!programLoaded) {
169-
console.info("WIFI: CONNECTED [" + ip + "]");
170-
151+
wifi.connectWifi(
152+
config.wifi.ssid,
153+
config.wifi.password,
154+
function (evt, ip) {
155+
if (evt.status === 0) {
156+
console.debug("WIFI: DISCONNECTED");
171157
if (!configServerStarted) {
172-
configServer.startConfigServer();
173-
configServerStarted = true;
158+
retries++;
159+
}
160+
if (!configServerStarted && retries === 5) {
161+
console.warn("Maximum retries exceeded to connect to wifi.");
162+
if (config?.ota?.offline) {
163+
stopWifi();
164+
loadOfflineScript();
165+
} else {
166+
console.warn("No offline script was found.");
167+
startSoftApMode();
168+
}
174169
}
170+
} else if (evt.status === 1) {
171+
if (!programLoaded) {
172+
console.info("WIFI: CONNECTED [" + ip + "]");
175173

176-
retries = 0;
177-
178-
if (config?.ota?.url) {
179-
programLoaded = true;
180-
console.info("Loading program from: " + config.ota.url);
181-
182-
let headers: Headers;
183-
fetch(config.ota.url)
184-
.then(function (r) {
185-
headers = r.headers;
186-
return r.text();
187-
})
188-
.then(function (data) {
189-
if (config?.ota?.offline) {
190-
config.ota.script = data;
191-
saveConfig(config);
192-
console.info("==> Saved offline script length=" + data.length);
193-
} else {
194-
console.info("==> NOT saving offline script");
195-
}
196-
197-
const dateString = headers.get("Date");
198-
if (dateString) {
199-
const now = parseDate(dateString);
200-
setDateTimeInMillis(now.getTime());
201-
setDateTimeZoneOffsetInHours(2);
202-
setBootTime(new Date());
203-
console.debug(`Setting boot time to ${getBootTime()}`);
204-
}
205-
evalScript(data, headers);
206-
})
207-
.catch(function (error) {
208-
console.error(error);
209-
startSoftApMode();
210-
});
211-
} else {
212-
console.error("No OTA (Over-the-air) url specified.");
213-
loadOfflineScript();
174+
if (!configServerStarted) {
175+
configServer.startConfigServer();
176+
configServerStarted = true;
177+
}
178+
179+
retries = 0;
180+
181+
if (config?.ota?.url) {
182+
programLoaded = true;
183+
console.info("Loading program from: " + config.ota.url);
184+
185+
let headers: Headers;
186+
fetch(config.ota.url)
187+
.then(function (r) {
188+
headers = r.headers;
189+
return r.text();
190+
})
191+
.then(function (data) {
192+
if (config?.ota?.offline) {
193+
config.ota.script = data;
194+
saveConfig(config);
195+
console.info(
196+
"==> Saved offline script length=" + data.length
197+
);
198+
} else {
199+
console.info("==> NOT saving offline script");
200+
}
201+
202+
const dateString = headers.get("Date");
203+
if (dateString) {
204+
const now = parseDate(dateString);
205+
setDateTimeInMillis(now.getTime());
206+
setDateTimeZoneOffsetInHours(2);
207+
setBootTime(new Date());
208+
console.debug(`Setting boot time to ${getBootTime()}`);
209+
}
210+
evalScript(data, headers);
211+
})
212+
.catch(function (error) {
213+
console.error(error);
214+
startSoftApMode();
215+
});
216+
} else {
217+
console.error("No OTA (Over-the-air) url specified.");
218+
loadOfflineScript();
219+
}
214220
}
221+
} else if (evt.status === 2) {
222+
console.debug("WIFI: CONNECTING...");
215223
}
216-
} else if (evt.status === 2) {
217-
console.debug("WIFI: CONNECTING...");
218-
}
219-
});
224+
},
225+
config.wifi.bssid
226+
);
220227
}
221228

222229
export function main(): void {

components/esp32-javascript/modules/esp32-javascript/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface Esp32JsConfig {
3030
wifi?: {
3131
ssid?: string;
3232
password?: string;
33+
bssid?: string;
3334
};
3435
ota?: {
3536
url?: string;

components/esp32-javascript/modules/esp32-javascript/configserver.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ var schema = {
7878
type: "string",
7979
title: "Password",
8080
},
81+
bssid: {
82+
type: "string",
83+
title: "BSSID",
84+
},
8185
},
8286
},
8387
ota: {
@@ -170,7 +174,7 @@ function startConfigServer() {
170174
":" +
171175
configManager.config.access.password);
172176
http_1.httpServer(80, false, function (req, res) {
173-
var _a, _b, _c, _d, _e;
177+
var _a, _b, _c, _d, _e, _f;
174178
if (req.headers.get("authorization") !== authString &&
175179
exports.baExceptionPathes.indexOf(req.path) < 0) {
176180
console.debug("401 response");
@@ -196,6 +200,7 @@ function startConfigServer() {
196200
var config_1 = http_1.parseQueryStr(req.body);
197201
storedConfig.wifi.ssid = config_1.ssid;
198202
storedConfig.wifi.password = config_1.password;
203+
storedConfig.wifi.bssid = config_1.bssid;
199204
storedConfig.access.username = config_1.username;
200205
storedConfig.access.password = config_1.userpass;
201206
storedConfig.ota.url = config_1.url;
@@ -211,7 +216,7 @@ function startConfigServer() {
211216
var config = configManager.config;
212217
page(res, "Setup", "" + (successMessage
213218
? "<div class=\"formpad green\">" + successMessage + "</div>"
214-
: "") + (errorMessage ? "<div class=\"formpad red\">" + errorMessage + "</div>" : "") + "<h2>Configuration</h2><h3>Wifi</h3><form action=\"/setup\" method=\"post\">\n <div class=\"formpad\"><label for=\"ssid\" class=\"formlabel\">SSID</label><input type=\"text\" name=\"ssid\" class=\"fill input\" value=\"" + (((_a = config.wifi) === null || _a === void 0 ? void 0 : _a.ssid) || "") + "\" /></div>\n <div class=\"formpad\"><label for=\"password\" class=\"formlabel\">Password</label><input type=\"text\" name=\"password\" class=\"fill input\" value=\"" + (((_b = config.wifi) === null || _b === void 0 ? void 0 : _b.password) || "") + "\" /></div>\n <h3>Basic authentication</h3>\n <div class=\"formpad\"><label for=\"username\" class=\"formlabel\">Username</label><input type=\"text\" name=\"username\" class=\"fill input\" value=\"" + config.access.username + "\" /></div>\n <div class=\"formpad\"><label for=\"userpass\" class=\"formlabel\">Password</label><input type=\"text\" name=\"userpass\" class=\"fill input\" value=\"" + config.access.password + "\" /></div>\n <h3>JavaScript OTA</h3><div class=\"formpad\"><label for=\"url\" class=\"formlabel\">JS file url</label><input type=\"text\" name=\"url\" class=\"fill input\" value=\"" + (((_c = config.ota) === null || _c === void 0 ? void 0 : _c.url) || "") + "\" /></div>\n <div class=\"formpad\"><label for=\"offline\"><input type=\"checkbox\" name=\"offline\" value=\"true\" " + (((_d = config.ota) === null || _d === void 0 ? void 0 : _d.offline) ? "checked" : "") + "/> Offline Mode</label></div>\n <label for=\"script\" class=\"formpad\">Offline Script</label><div class=\"formpad\"><textarea name=\"script\" class=\"full input txt\">" + (((_e = config.ota) === null || _e === void 0 ? void 0 : _e.script) || "") + "</textarea></div>\n <div class=\"formpad\"><input type=\"submit\" value=\"Save\" class=\"formpad input\"/></div></form>\n <h2>Logs</h2>\n <div class=\"formpad\">\n <p>\n Showing last " + filelogging_1.LOG_FILE_NUM_LIMIT + " log files, with each having maximum of " + filelogging_1.LOG_FILE_SIZE_LIMIT / 1024 + " kB data.<br/>\n </p>\n " + getLogFileList()
219+
: "") + (errorMessage ? "<div class=\"formpad red\">" + errorMessage + "</div>" : "") + "<h2>Configuration</h2><h3>Wifi</h3><form action=\"/setup\" method=\"post\">\n <div class=\"formpad\"><label for=\"ssid\" class=\"formlabel\">SSID</label><input type=\"text\" name=\"ssid\" class=\"fill input\" value=\"" + (((_a = config.wifi) === null || _a === void 0 ? void 0 : _a.ssid) || "") + "\" /></div>\n <div class=\"formpad\"><label for=\"password\" class=\"formlabel\">Password</label><input type=\"text\" name=\"password\" class=\"fill input\" value=\"" + (((_b = config.wifi) === null || _b === void 0 ? void 0 : _b.password) || "") + "\" /></div>\n <div class=\"formpad\"><label for=\"bssid\" class=\"formlabel\">BSSID (optional)</label><input type=\"text\" name=\"bssid\" class=\"fill input\" value=\"" + (((_c = config.wifi) === null || _c === void 0 ? void 0 : _c.bssid) || "") + "\" /></div>\n <h3>Basic authentication</h3>\n <div class=\"formpad\"><label for=\"username\" class=\"formlabel\">Username</label><input type=\"text\" name=\"username\" class=\"fill input\" value=\"" + config.access.username + "\" /></div>\n <div class=\"formpad\"><label for=\"userpass\" class=\"formlabel\">Password</label><input type=\"text\" name=\"userpass\" class=\"fill input\" value=\"" + config.access.password + "\" /></div>\n <h3>JavaScript OTA</h3><div class=\"formpad\"><label for=\"url\" class=\"formlabel\">JS file url</label><input type=\"text\" name=\"url\" class=\"fill input\" value=\"" + (((_d = config.ota) === null || _d === void 0 ? void 0 : _d.url) || "") + "\" /></div>\n <div class=\"formpad\"><label for=\"offline\"><input type=\"checkbox\" name=\"offline\" value=\"true\" " + (((_e = config.ota) === null || _e === void 0 ? void 0 : _e.offline) ? "checked" : "") + "/> Offline Mode</label></div>\n <label for=\"script\" class=\"formpad\">Offline Script</label><div class=\"formpad\"><textarea name=\"script\" class=\"full input txt\">" + (((_f = config.ota) === null || _f === void 0 ? void 0 : _f.script) || "") + "</textarea></div>\n <div class=\"formpad\"><input type=\"submit\" value=\"Save\" class=\"formpad input\"/></div></form>\n <h2>Logs</h2>\n <div class=\"formpad\">\n <p>\n Showing last " + filelogging_1.LOG_FILE_NUM_LIMIT + " log files, with each having maximum of " + filelogging_1.LOG_FILE_SIZE_LIMIT / 1024 + " kB data.<br/>\n </p>\n " + getLogFileList()
215220
.map(function (e) {
216221
return e.filename + " (" + (e.size === undefined ? "?" : Math.floor(e.size / 1024)) + " kB) <form action=\"/viewlog\" method=\"post\" class=\"inline-form\"><button class=\"input\" type=\"submit\" name=\"file\" value=\"" + filelogging_1.FILE_LOGGING_DIRECTORY + "/" + e.filename + "\">View</button></form> <form action=\"/deletelog\" method=\"post\" class=\"inline-form\"><button class=\"input\" type=\"submit\" name=\"file\" value=\"" + filelogging_1.FILE_LOGGING_DIRECTORY + "/" + e.filename + "\">Delete</button></form><br />";
217222
})

components/esp32-javascript/modules/esp32-javascript/configserver.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ let schema = {
7676
type: "string",
7777
title: "Password",
7878
},
79+
bssid: {
80+
type: "string",
81+
title: "BSSID",
82+
},
7983
},
8084
},
8185
ota: {
@@ -265,6 +269,7 @@ export function startConfigServer(): void {
265269
const config = parseQueryStr(req.body);
266270
storedConfig.wifi.ssid = config.ssid;
267271
storedConfig.wifi.password = config.password;
272+
storedConfig.wifi.bssid = config.bssid;
268273
storedConfig.access.username = config.username;
269274
storedConfig.access.password = config.userpass;
270275
storedConfig.ota.url = config.url;
@@ -295,6 +300,9 @@ export function startConfigServer(): void {
295300
<div class="formpad"><label for="password" class="formlabel">Password</label><input type="text" name="password" class="fill input" value="${
296301
config.wifi?.password || ""
297302
}" /></div>
303+
<div class="formpad"><label for="bssid" class="formlabel">BSSID (optional)</label><input type="text" name="bssid" class="fill input" value="${
304+
config.wifi?.bssid || ""
305+
}" /></div>
298306
<h3>Basic authentication</h3>
299307
<div class="formpad"><label for="username" class="formlabel">Username</label><input type="text" name="username" class="fill input" value="${
300308
config.access.username

components/esp32-javascript/modules/esp32-javascript/esp32-javascript.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,17 @@ declare function el_suspend(): Esp32JsEventloopEvent[];
3636
declare function main(): void;
3737

3838
interface Esp32JsWifiConfig {
39-
bssid: number[];
39+
bssid: [number, number, number, number, number, number];
4040
}
4141
declare function getWifiConfig(): Esp32JsWifiConfig;
4242
declare const EL_WIFI_EVENT_TYPE: number;
4343
declare const EL_TIMER_EVENT_TYPE: number;
4444
declare const EL_LOG_EVENT_TYPE: number;
45-
declare function el_connectWifi(ssid: string, password: string): void;
45+
declare function el_connectWifi(
46+
ssid: string,
47+
password: string,
48+
bssid?: [number, number, number, number, number, number]
49+
): void;
4650
declare function el_createSoftAp(ssid: string, password: string): void;
4751

4852
declare function writeSocket(

components/esp32-javascript/modules/esp32-javascript/filelogging.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ SOFTWARE.
2626
var stringbuffer_1 = require("./stringbuffer");
2727
exports.FILE_LOGGING_DIRECTORY = "/data/logs";
2828
exports.LOG_FILE_SIZE_LIMIT = 10240;
29-
exports.LOG_FILE_NUM_LIMIT = 10;
29+
exports.LOG_FILE_NUM_LIMIT = 8;
3030
var TDIWEF = "TDIWEF";
3131
var NUMBER_PREFIX = "00000000";
3232
var logFileNumber = -1;
@@ -54,10 +54,12 @@ function cleanupOldLogs() {
5454
}
5555
}
5656
global.el_flushLogBuffer = function () {
57-
var swap = global.logBuffer;
58-
global.logBuffer = new stringbuffer_1.StringBuffer();
5957
var logFile = exports.FILE_LOGGING_DIRECTORY + "/logs-" + getLogFileNumber() + ".txt";
60-
appendFile(logFile, swap.toString());
58+
var ret = appendFile(logFile, global.logBuffer.toString());
59+
if (ret < 0) {
60+
console.error("Could not flush log file. Space exceeded?");
61+
}
62+
global.logBuffer = new stringbuffer_1.StringBuffer();
6163
if (fileSize(logFile) > exports.LOG_FILE_SIZE_LIMIT) {
6264
logFileNumber++;
6365
}

components/esp32-javascript/modules/esp32-javascript/filelogging.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { StringBuffer } from "./stringbuffer";
2525

2626
export const FILE_LOGGING_DIRECTORY = "/data/logs";
2727
export const LOG_FILE_SIZE_LIMIT = 10240;
28-
export const LOG_FILE_NUM_LIMIT = 10;
28+
export const LOG_FILE_NUM_LIMIT = 8;
2929

3030
const TDIWEF = "TDIWEF";
3131
const NUMBER_PREFIX = "00000000";
@@ -57,10 +57,13 @@ function cleanupOldLogs() {
5757
}
5858

5959
global.el_flushLogBuffer = function (): void {
60-
const swap = global.logBuffer;
61-
global.logBuffer = new StringBuffer();
6260
const logFile = `${FILE_LOGGING_DIRECTORY}/logs-${getLogFileNumber()}.txt`;
63-
appendFile(logFile, swap.toString());
61+
const ret = appendFile(logFile, global.logBuffer.toString());
62+
if (ret < 0) {
63+
console.error("Could not flush log file. Space exceeded?");
64+
}
65+
66+
global.logBuffer = new StringBuffer();
6467
if (fileSize(logFile) > LOG_FILE_SIZE_LIMIT) {
6568
logFileNumber++;
6669
}

0 commit comments

Comments
 (0)