Skip to content

Commit

Permalink
Option to set schedules based on UTC time (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed Apr 12, 2018
1 parent 42b2974 commit bb82d49
Show file tree
Hide file tree
Showing 6 changed files with 2,340 additions and 2,318 deletions.
Binary file modified code/espurna/data/index.html.gz
Binary file not shown.
12 changes: 10 additions & 2 deletions code/espurna/ntp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ void _ntpUpdate() {
wsSend(_ntpWebSocketOnSend);
#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());
}

}
Expand Down Expand Up @@ -143,6 +145,12 @@ String ntpDateTime() {
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() {
Expand Down
30 changes: 20 additions & 10 deletions code/espurna/scheduler.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void _schWebSocketOnSend(JsonObject &root){
scheduler["schType"] = getSetting("schType", i, 0).toInt();
scheduler["schHour"] = getSetting("schHour", i, 0).toInt();
scheduler["schMinute"] = getSetting("schMinute", i, 0).toInt();
scheduler["schUTC"] = getSetting("schUTC", i, 0).toInt() == 1;
scheduler["schWDs"] = getSetting("schWDs", i, "");
}

Expand Down Expand Up @@ -64,22 +65,25 @@ void _schConfigure() {
delSetting("schMinute", i);
delSetting("schWDs", i);
delSetting("schType", i);
delSetting("schUTC", i);

} else {

#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_hour = getSetting("schHour", 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, "");
unsigned char sch_type = getSetting("schType", i, SCHEDULER_TYPE_SWITCH).toInt();

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,
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)"
);

Expand All @@ -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
int w = weekday(now()) - 1;
if (w == 0) w = 7;
int w = weekday(t) - 1;
if (0 == w) w = 7;

char pch;
char * p = (char *) weekdays.c_str();
Expand All @@ -107,15 +111,17 @@ 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_minute = minute(t);
return (schedule_hour - now_hour) * 60 + schedule_minute - now_minute;
}

void _schCheck() {

time_t local_time = now();
time_t utc_time = ntpLocal2UTC(local_time);

// Check schedules
for (unsigned char i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {

Expand All @@ -125,12 +131,16 @@ void _schCheck() {
// Skip disabled schedules
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, "");
if (_schIsThisWeekday(sch_weekdays)) {
if (_schIsThisWeekday(t, sch_weekdays)) {

int sch_hour = getSetting("schHour", 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) {

Expand Down
Loading

0 comments on commit bb82d49

Please sign in to comment.