Browse Source

Option to set schedules based on UTC time (#783)

rfm69
Xose Pérez 6 years ago
parent
commit
bb82d49452
6 changed files with 2340 additions and 2318 deletions
  1. BIN
      code/espurna/data/index.html.gz
  2. +10
    -2
      code/espurna/ntp.ino
  3. +20
    -10
      code/espurna/scheduler.ino
  4. +2305
    -2304
      code/espurna/static/index.html.gz.h
  5. +2
    -2
      code/html/custom.js
  6. +3
    -0
      code/html/index.html

BIN
code/espurna/data/index.html.gz View File


+ 10
- 2
code/espurna/ntp.ino View File

@ -88,8 +88,10 @@ void _ntpUpdate() {
wsSend(_ntpWebSocketOnSend); wsSend(_ntpWebSocketOnSend);
#endif #endif
if (strlen(ntpDateTime().c_str())) {
DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str());
if (ntpSynced()) {
time_t t = now();
DEBUG_MSG_P(PSTR("[NTP] UTC Time : %s\n"), (char *) ntpDateTime(ntpLocal2UTC(t)).c_str());
DEBUG_MSG_P(PSTR("[NTP] Local Time: %s\n"), (char *) ntpDateTime(t).c_str());
} }
} }
@ -143,6 +145,12 @@ String ntpDateTime() {
return String(); return String();
} }
time_t ntpLocal2UTC(time_t local) {
int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt();
if (NTP.isSummerTime()) offset += 60;
return local - offset * 60;
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void ntpSetup() { void ntpSetup() {


+ 20
- 10
code/espurna/scheduler.ino View File

@ -35,6 +35,7 @@ void _schWebSocketOnSend(JsonObject &root){
scheduler["schType"] = getSetting("schType", i, 0).toInt(); scheduler["schType"] = getSetting("schType", i, 0).toInt();
scheduler["schHour"] = getSetting("schHour", i, 0).toInt(); scheduler["schHour"] = getSetting("schHour", i, 0).toInt();
scheduler["schMinute"] = getSetting("schMinute", i, 0).toInt(); scheduler["schMinute"] = getSetting("schMinute", i, 0).toInt();
scheduler["schUTC"] = getSetting("schUTC", i, 0).toInt() == 1;
scheduler["schWDs"] = getSetting("schWDs", i, ""); scheduler["schWDs"] = getSetting("schWDs", i, "");
} }
@ -64,22 +65,25 @@ void _schConfigure() {
delSetting("schMinute", i); delSetting("schMinute", i);
delSetting("schWDs", i); delSetting("schWDs", i);
delSetting("schType", i); delSetting("schType", i);
delSetting("schUTC", i);
} else { } else {
#if DEBUG_SUPPORT #if DEBUG_SUPPORT
int sch_enabled = getSetting("schEnabled", i, 1).toInt() == 1;
bool sch_enabled = getSetting("schEnabled", i, 1).toInt() == 1;
int sch_action = getSetting("schAction", i, 0).toInt(); int sch_action = getSetting("schAction", i, 0).toInt();
int sch_hour = getSetting("schHour", i, 0).toInt(); int sch_hour = getSetting("schHour", i, 0).toInt();
int sch_minute = getSetting("schMinute", i, 0).toInt(); int sch_minute = getSetting("schMinute", i, 0).toInt();
bool sch_utc = getSetting("schUTC", i, 0).toInt() == 1;
String sch_weekdays = getSetting("schWDs", i, ""); String sch_weekdays = getSetting("schWDs", i, "");
unsigned char sch_type = getSetting("schType", i, SCHEDULER_TYPE_SWITCH).toInt(); unsigned char sch_type = getSetting("schType", i, SCHEDULER_TYPE_SWITCH).toInt();
DEBUG_MSG_P( DEBUG_MSG_P(
PSTR("[SCH] Schedule #%d: %s #%d to %d at %02d:%02d on %s%s\n"),
PSTR("[SCH] Schedule #%d: %s #%d to %d at %02d:%02d %s on %s%s\n"),
i, SCHEDULER_TYPE_SWITCH == sch_type ? "switch" : "channel", sch_switch, i, SCHEDULER_TYPE_SWITCH == sch_type ? "switch" : "channel", sch_switch,
sch_action, sch_hour, sch_minute, (char *) sch_weekdays.c_str(),
sch_action, sch_hour, sch_minute, sch_utc ? "UTC" : "local time",
(char *) sch_weekdays.c_str(),
sch_enabled ? "" : " (disabled)" sch_enabled ? "" : " (disabled)"
); );
@ -91,11 +95,11 @@ void _schConfigure() {
} }
bool _schIsThisWeekday(String weekdays){
bool _schIsThisWeekday(time_t t, String weekdays){
// Convert from Sunday to Monday as day 1 // Convert from Sunday to Monday as day 1
int w = weekday(now()) - 1;
if (w == 0) w = 7;
int w = weekday(t) - 1;
if (0 == w) w = 7;
char pch; char pch;
char * p = (char *) weekdays.c_str(); char * p = (char *) weekdays.c_str();
@ -107,8 +111,7 @@ bool _schIsThisWeekday(String weekdays){
} }
int _schMinutesLeft(unsigned char schedule_hour, unsigned char schedule_minute){
time_t t = now();
int _schMinutesLeft(time_t t, unsigned char schedule_hour, unsigned char schedule_minute){
unsigned char now_hour = hour(t); unsigned char now_hour = hour(t);
unsigned char now_minute = minute(t); unsigned char now_minute = minute(t);
return (schedule_hour - now_hour) * 60 + schedule_minute - now_minute; return (schedule_hour - now_hour) * 60 + schedule_minute - now_minute;
@ -116,6 +119,9 @@ int _schMinutesLeft(unsigned char schedule_hour, unsigned char schedule_minute){
void _schCheck() { void _schCheck() {
time_t local_time = now();
time_t utc_time = ntpLocal2UTC(local_time);
// Check schedules // Check schedules
for (unsigned char i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) { for (unsigned char i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {
@ -125,12 +131,16 @@ void _schCheck() {
// Skip disabled schedules // Skip disabled schedules
if (getSetting("schEnabled", i, 1).toInt() == 0) continue; if (getSetting("schEnabled", i, 1).toInt() == 0) continue;
// Get the datetime used for the calculation
bool sch_utc = getSetting("schUTC", i, 0).toInt() == 1;
time_t t = sch_utc ? utc_time : local_time;
String sch_weekdays = getSetting("schWDs", i, ""); String sch_weekdays = getSetting("schWDs", i, "");
if (_schIsThisWeekday(sch_weekdays)) {
if (_schIsThisWeekday(t, sch_weekdays)) {
int sch_hour = getSetting("schHour", i, 0).toInt(); int sch_hour = getSetting("schHour", i, 0).toInt();
int sch_minute = getSetting("schMinute", i, 0).toInt(); int sch_minute = getSetting("schMinute", i, 0).toInt();
int minutes_to_trigger = _schMinutesLeft(sch_hour, sch_minute);
int minutes_to_trigger = _schMinutesLeft(t, sch_hour, sch_minute);
if (minutes_to_trigger == 0) { if (minutes_to_trigger == 0) {


+ 2305
- 2304
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 2
- 2
code/html/custom.js View File

@ -179,7 +179,7 @@ function addValue(data, name, value) {
// These fields will always be a list of values // These fields will always be a list of values
var is_group = [ var is_group = [
"ssid", "pass", "gw", "mask", "ip", "dns", "ssid", "pass", "gw", "mask", "ip", "dns",
"schEnabled", "schSwitch","schAction","schType","schHour","schMinute","schWDs",
"schEnabled", "schSwitch","schAction","schType","schHour","schMinute","schWDs","schUTC",
"relayBoot", "relayPulse", "relayTime", "relayBoot", "relayPulse", "relayTime",
"mqttGroup", "mqttGroupInv", "relayOnDisc", "mqttGroup", "mqttGroupInv", "relayOnDisc",
"dczRelayIdx", "dczMagnitude", "dczRelayIdx", "dczMagnitude",
@ -1303,7 +1303,7 @@ function initUrls(root) {
}); });
urls.ws.protocol = "ws"; urls.ws.protocol = "ws";
} }
function connectToURL(url) { function connectToURL(url) {


+ 3
- 0
code/html/index.html View File

@ -1252,6 +1252,9 @@
</div> </div>
<div class="pure-u-0 pure-u-lg-1-3"></div> <div class="pure-u-0 pure-u-lg-1-3"></div>
<label class="pure-u-1 pure-u-lg-1-4">Use UTC time</label>
<div class="pure-u-1 pure-u-lg-3-4"><input type="checkbox" name="schUTC" /></div>
<label class="pure-u-1 pure-u-lg-1-4">And weekday is one of</label> <label class="pure-u-1 pure-u-lg-1-4">And weekday is one of</label>
<div class="pure-u-2-5 pure-u-lg-1-5"> <div class="pure-u-2-5 pure-u-lg-1-5">
<input class="pure-u-23-24 pure-u-lg-23-24" name="schWDs" type="text" maxlength="15" tabindex="0" value="1,2,3,4,5,6,7" /> <input class="pure-u-23-24 pure-u-lg-23-24" name="schWDs" type="text" maxlength="15" tabindex="0" value="1,2,3,4,5,6,7" />


Loading…
Cancel
Save