Skip to content

Commit

Permalink
Add setting to automatically reboot every X hours (1..72, default to …
Browse files Browse the repository at this point in the history
…24). If zero, then never reboot

Update web.cpp
  • Loading branch information
dkerr64 authored and jgstroud committed Mar 9, 2024
1 parent a5ed36d commit 54db210
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 8 deletions.
5 changes: 4 additions & 1 deletion build_web_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
const char type_tiff[] = "image/tiff";
const char type_tif[] = "image/tiff";
const char type_txt[] = "text/plain";
const char type_[] = "text/plain";
const char type_htm[] = "text/html";
const char type_html[] = "text/html";
const char type_css[] = "text/css";
Expand All @@ -133,7 +134,9 @@
)
n = 0
for file, var, crc32 in varnames:
t = file.rpartition(".")[-1]
t = ""
if file.find(".") > 0:
t = file.rpartition(".")[-1]
# Need comma at end of every line except last one...
if n > 0:
wf.write(",")
Expand Down
2 changes: 1 addition & 1 deletion src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ uint32_t read_file_from_flash(const char* filename, uint32_t defaultValue) {

void write_file_to_flash(const char *filename, uint32_t* value) {
File file = LittleFS.open(filename, "w");
RINFO("writing %02X to file %s", *value, filename);
RINFO("writing 0x%02X to file %s", *value, filename);
file.print(*value);
file.close();
}
31 changes: 29 additions & 2 deletions src/web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
// This is used for CSS, JS and IMAGE file types. Set to 30 days !!
#define CACHE_CONTROL (60 * 60 * 24 * 30)

// Reboot the system every X seconds, defaults to disabled
#define REBOOT_SECONDS (0)

#include <string>
#include <tuple>
#include <unordered_map>
Expand Down Expand Up @@ -62,6 +65,10 @@ const char credentials_file[] = "www_credentials";
bool passwordReq = false;
const char www_pw_required_file[] = "www_pw_required_file";

// Control automatic reboot
uint32_t rebootSeconds; // seconds between reboots
const char system_reboot_timer[] = "system_reboot_timer";

// For Server Sent Events (SSE) support
// Just reloading page causes register on new channel. So we need a reasonable number
// to accommodate "extra" until old one is detected as disconnected.
Expand Down Expand Up @@ -150,13 +157,22 @@ void web_loop()
ADD_BOOL_C(json, "garageLightOn", garage_door.light, last_reported_garage_door.light);
ADD_BOOL_C(json, "garageMotion", garage_door.motion, last_reported_garage_door.motion);
ADD_BOOL_C(json, "garageObstructed", garage_door.obstructed, last_reported_garage_door.obstructed);
if (strlen(json) > 2) // Have we added anything to the JSON string?
if (strlen(json) > 2)
{
// Have we added anything to the JSON string?
ADD_INT(json, "upTime", millis());
END_JSON(json);
REMOVE_NL(json);
SSEBroadcastState(json);
}
if ((rebootSeconds != 0) && (rebootSeconds < millis() / 1000))
{
// Reboot the system if we have reached time...
RINFO("Rebooting system as %i seconds expired", rebootSeconds);
server.stop();
sync_and_restart();
return;
}
server.handleClient();
}

Expand Down Expand Up @@ -193,6 +209,9 @@ void setup_web()
passwordReq = (read_file_from_flash(www_pw_required_file) != 0);
RINFO("WWW Password %s required", (passwordReq) ? "is" : "not");

rebootSeconds = read_file_from_flash(system_reboot_timer, REBOOT_SECONDS);
RINFO("System will reboot every %i seconds", rebootSeconds);

RINFO("Registering URI handlers");
// Register URI handlers for URIs that have built-in handlers in this source file.
for (auto uri : builtInUri)
Expand Down Expand Up @@ -399,6 +418,8 @@ void handle_status()
ADD_BOOL(json, "garageObstructed", garage_door.obstructed);
if (all)
ADD_BOOL(json, "passwordRequired", passwordReq);
if (all)
ADD_INT(json, "rebootSeconds", rebootSeconds);

END_JSON(json);
// Only log if all requested (no arguments).
Expand Down Expand Up @@ -505,6 +526,12 @@ void handle_setgdo()
write_file_to_flash(www_pw_required_file, &required);
reboot = true;
}
else if (!strcmp(key, "rebootSeconds"))
{
uint32_t seconds = atoi(value);
write_file_to_flash(system_reboot_timer, &seconds);
reboot = true;
}
else
{
error = true;
Expand Down Expand Up @@ -626,4 +653,4 @@ void SSEBroadcastState(const char *data)
RINFO("SSEBroadcastState - client %s registered on channel %d but not listening", IPaddrstr.c_str(), i);
}
}
}
}
Empty file added src/www/auth
Empty file.
7 changes: 6 additions & 1 deletion src/www/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ async function checkStatus() {
document.getElementById("gdosec1").checked = (serverStatus.GDOSecurityType == 1) ? true : false;
document.getElementById("gdosec2").checked = (serverStatus.GDOSecurityType == 2) ? true : false;
document.getElementById("pwreq").checked = serverStatus.passwordRequired;
document.getElementById("reboothours").value = serverStatus.rebootSeconds / 60 / 60;

// Use Server Send Events to keep status up-to-date, 2 == CLOSED
if (!evtSource || evtSource.readyState == 2) {
Expand Down Expand Up @@ -381,7 +382,11 @@ async function saveSettings() {
console.log("Set GDO security type to: " + gdoSec);
let pwReq = (document.getElementById("pwreq").checked) ? '1' : '0';
console.log("Set GDO Web Password required to: " + pwReq);
await setGDO("gdoSecurity", gdoSec, "passwordRequired", pwReq);
let rebootHours = Math.max(Math.min(parseInt(document.getElementById("reboothours").value), 72), 0);
if (isNaN(rebootHours)) rebootHours = 24;
console.log("Set GDO Reboot Every: " + (rebootHours * 60 * 60) + " seconds");

await setGDO("gdoSecurity", gdoSec, "passwordRequired", pwReq, "rebootSeconds", rebootHours * 60 * 60);
clearInterval(statusUpdater);
countdown(30, "Settings saved, RATGDO device rebooting...&nbsp;");
return;
Expand Down
17 changes: 16 additions & 1 deletion src/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@
<span style="font-size: 0.6em;">(default: admin/password)</span>
</td>
</tr>
<tr>
<td style="text-align: right;">Reboot Every:</td>
<td>&nbsp;
<input type="number" id="reboothours" name="reboothours" value="24" , min="0" , max="72" , step="1">
<label for="reboothours">Hours <span style="font-size: 0.6em;">(never:0, min:1, max:72)</span></label>
</td>
</tr>
<tr>
<td>
<!--
Expand Down Expand Up @@ -280,7 +287,7 @@
document.getElementById("main-icon").style.display = "none";
document.getElementById("settings-page").style.display = "none";
document.getElementById("main-page").style.display = "block";
document.getElementById("settings-icon").style.display = "block"
document.getElementById("settings-icon").style.display = "block";
};
history.replaceState("mainpage", "");
window.addEventListener("popstate", (e) => {
Expand All @@ -292,6 +299,7 @@
}
});

// Check new passwords match when typed in.
newPW = document.getElementById("newPassword");
confirmPW = document.getElementById("confirmPassword");
matchMsg = document.getElementById("matchMsg");
Expand All @@ -311,6 +319,13 @@
matchMsg.innerHTML = "Passwords do not match<br>";
}
};

// Allow only numeric keystrokes into the reboot every X hours field
document.getElementById("reboothours").addEventListener("keypress", (event) => {
if (event.which < 48 || event.which > 57) {
event.preventDefault();
}
});
</script>
</body>

Expand Down
3 changes: 2 additions & 1 deletion src/www/status.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"garageLockState": "Unsecured",
"garageMotion": false,
"garageObstructed": false,
"passwordRequired": true
"passwordRequired": true,
"rebootSeconds": 0
}
3 changes: 2 additions & 1 deletion src/www/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ fieldset {
html,
div,
input[type="file"],
/* fieldset, */
body {
background-color: #E0E0E0;
border-color: #303030;
color: black;
}

input[type="number"],
input[type="password"] {
background-color: #F0F0F0;
border-color: #303030;
Expand All @@ -251,6 +251,7 @@ fieldset {
color: white;
}

input[type="number"],
input[type="password"] {
background-color: #202020;
border-color: #E0E0E0;
Expand Down

0 comments on commit 54db210

Please sign in to comment.