Skip to content

Commit

Permalink
Fractional auto-off delay support
Browse files Browse the repository at this point in the history
Fixes #53
Closes #60
  • Loading branch information
Johan Stenmark authored and rojer committed Apr 22, 2020
1 parent bfc85d3 commit 9b708be
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
28 changes: 17 additions & 11 deletions fs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ <h1 class="" id="sw1_name">SW1 Name</h1>
</div>
<div>
<label for="sw1_auto_off_delay">Auto off delay:</label>
<input type="text" id="sw1_auto_off_delay" placeholder="HH:mm:ss" required pattern="(0*([0-9]|1[0-9]|2[0-3])):(0*([0-9]|[1-4][0-9]|5[0-9])):(0*([0-9]|[1-4][0-9]|5[0-9]))">
<input type="text" id="sw1_auto_off_delay" placeholder="HH:MM:SS.sss" required pattern="(0*([0-9]|1[0-9]|2[0-3])):(0*([0-9]|[1-4][0-9]|5[0-9])):(0*([0-9]|[1-4][0-9]|5[0-9])).([1-9][0-9][0-9]|[0-9][1-9][0-9])">
</div>
<div class="form-control">
<label></label>
Expand Down Expand Up @@ -149,7 +149,7 @@ <h1 class="" id="sw2_name">SW2 Name</h1>
</div>
<div>
<label for="sw2_auto_off_delay">Auto off delay:</label>
<input type="text" id="sw2_auto_off_delay" placeholder="HH:mm:ss" required pattern="(0*([0-9]|1[0-9]|2[0-3])):(0*([0-9]|[1-4][0-9]|5[0-9])):(0*([0-9]|[1-4][0-9]|5[0-9]))">
<input type="text" id="sw2_auto_off_delay" placeholder="HH:MM:SS.sss" required pattern="(0*([0-9]|1[0-9]|2[0-3])):(0*([0-9]|[1-4][0-9]|5[0-9])):(0*([0-9]|[1-4][0-9]|5[0-9])).(([1-9][0-9][0-9]|[0-9][1-9][0-9])">
</div>
<div class="form-control">
<label></label>
Expand Down Expand Up @@ -400,27 +400,33 @@ <h1 class="">Firmware</h1>
var auto_off_delay = el(swType + "_auto_off_delay");
var auto_off_delay_valid = auto_off_delay.checkValidity();
if (!auto_off_delay_valid) {
alert("Auto off delay must follow 24 hour format HH:mm:ss");
alert("Auto off delay must follow 24 hour format HH:MM:SS.sss with a minimum value of 10ms.");
return false;
}
return true;
}

function dateStringToSeconds(dateString) {
var dateStringParts = dateString.split(':');
var seconds = parseInt(dateStringParts[0]) * 3600 + parseInt(dateStringParts[1]) * 60 + parseInt(dateStringParts[2]);
var secondsPart = dateStringParts[2].split('.')[0];
var fractionPart = dateStringParts[2].split('.')[1];
var seconds = parseInt(dateStringParts[0]) * 3600 + parseInt(dateStringParts[1]) * 60 + parseInt(secondsPart) + parseFloat(fractionPart / 1000);
return seconds;
}

function secondsToDateString(seconds) {
if (seconds == 0) return "";
var date = new Date(1970, 0, 1);
date.setSeconds(seconds);
var dateString = twoDigitString(date.getHours()) + ":" + twoDigitString(date.getMinutes()) + ":" + twoDigitString(date.getSeconds());
date.setMilliseconds(seconds * 1000);
var dateString = nDigitString(date.getHours(), 2) + ":" +
nDigitString(date.getMinutes(), 2) + ":" +
nDigitString(date.getSeconds(), 2) + "." +
nDigitString(date.getMilliseconds(), 3);
return dateString;
}

function twoDigitString(num) {
return num.toLocaleString(undefined, {minimumIntegerDigits: 2})
function nDigitString(num, digits) {
return num.toString().padStart(digits, "0");
}

el("sw1_save_btn").onclick = function() {
Expand Down Expand Up @@ -539,9 +545,9 @@ <h1 class="">Firmware</h1>
var mins = parseInt(d / 60);
var secs = d % 60;
return days + ":" +
hours.toString().padStart(2, "0") + ":" +
mins.toString().padStart(2, "0") + ":" +
secs.toString().padStart(2, "0");
nDigitString(hours, 2) + ":" +
nDigitString(mins, 2) + ":" +
nDigitString(secs, 2);
}

</script>
Expand Down
2 changes: 1 addition & 1 deletion mos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ config_schema:
- ["sw.state", "b", false, {"State of the switch"}]
- ["sw.initial_state", "i", 3, {"Initial state on power-on: 0 - off, 1 - on, 2 - restore last state, 3 - matches input if in toggle mode, otherwise off"}]
- ["sw.auto_off", "b", false, {"Whether the switch should automatically turn OFF after turning ON"}]
- ["sw.auto_off_delay", "i", -1, {"Delay for automatically turning OFF, in seconds"}]
- ["sw.auto_off_delay", "d", -1, {"Delay for automatically turning OFF, in seconds"}]

- ["shelly.cfg_version", "i", 0, {"Configuration version"}]
# Deprecated settings, only kept to enable migration.
Expand Down
4 changes: 2 additions & 2 deletions src/shelly_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,15 @@ static void shelly_get_info_handler(struct mg_rpc_request_info *ri,
"{id: %Q, app: %Q, host: %Q, version: %Q, fw_build: %Q, uptime: %d, "
#ifdef MGOS_CONFIG_HAVE_SW1
"sw1: {id: %d, name: %Q, in_mode: %d, initial: %d, "
"state: %B, auto_off: %B, auto_off_delay: %d"
"state: %B, auto_off: %B, auto_off_delay: %.3f"
#ifdef SHELLY_HAVE_PM
", apower: %.3f, aenergy: %.3f"
#endif
"},"
#endif
#ifdef MGOS_CONFIG_HAVE_SW2
"sw2: {id: %d, name: %Q, in_mode: %d, initial: %d, "
"state: %B, auto_off: %B, auto_off_delay: %d"
"state: %B, auto_off: %B, auto_off_delay: %.3f"
#ifdef SHELLY_HAVE_PM
", apower: %.3f, aenergy: %.3f"
#endif
Expand Down

0 comments on commit 9b708be

Please sign in to comment.