mirror of
https://github.com/xoseperez/espurna.git
synced 2026-03-23 00:27:06 +01:00
Clean up code, stick to conventions in settings naming
This commit is contained in:
@@ -1,117 +0,0 @@
|
||||
/*
|
||||
|
||||
A SCHEDULER MODULE
|
||||
|
||||
Copyright (C) 2017 by faina09
|
||||
|
||||
*/
|
||||
|
||||
#if SCHEDULER_SUPPORT
|
||||
|
||||
#include <NtpClientLib.h>
|
||||
|
||||
void _schWebSocketOnSend(JsonObject &root){
|
||||
root["maxScheduled"] = MAX_SCHEDULED;
|
||||
JsonArray &sch = root.createNestedArray("schedule");
|
||||
for (byte i = 0; i < MAX_SCHEDULED; i++) {
|
||||
if (!hasSetting("sch_switch", i))
|
||||
break;
|
||||
JsonObject &scheduler = sch.createNestedObject();
|
||||
scheduler["sch_switch"] = getSetting("sch_switch", i, "");
|
||||
scheduler["sch_operation"] = getSetting("sch_operation", i, "");
|
||||
scheduler["sch_hour"] = getSetting("sch_hour", i, "");
|
||||
scheduler["sch_minute"] = getSetting("sch_minute", i, "");
|
||||
scheduler["sch_weekdays"] = getSetting("sch_weekdays", i, "");
|
||||
}
|
||||
}
|
||||
|
||||
void schSetup(){
|
||||
// Update websocket clients
|
||||
#if WEB_SUPPORT
|
||||
wsOnSendRegister(_schWebSocketOnSend);
|
||||
#endif
|
||||
|
||||
int i;
|
||||
for (i = 0; i < MAX_SCHEDULED; i++) {
|
||||
if (getSetting("sch_switch" + String(i)).length() == 0)
|
||||
break;
|
||||
String sch_weekdays = getSetting("sch_weekdays" + String(i));
|
||||
int sch_switch = getSetting("sch_switch" + String(i)).toInt();
|
||||
int sch_operation = getSetting("sch_operation" + String(i)).toInt();
|
||||
int sch_hour = getSetting("sch_hour" + String(i)).toInt();
|
||||
int sch_minute = getSetting("sch_minute" + String(i)).toInt();
|
||||
DEBUG_MSG_P(PSTR("[SCH] Turn switch #%d %s at %02d:%02d on %s\n"), sch_switch, sch_operation ? "ON" : "OFF", sch_hour, sch_minute, (char *)sch_weekdays.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void schLoop(){
|
||||
// Check if we should compare scheduled and actual times
|
||||
static unsigned long last_update = 0;
|
||||
static int sec = 0;
|
||||
if ((millis() - last_update > ((SCH_UPDATE_SEC + 60 - sec)*1000)) || (last_update == 0)) {
|
||||
last_update = millis();
|
||||
if (!ntpConnected()){
|
||||
time_t t = now();
|
||||
sec = second(t);
|
||||
DEBUG_MSG_P(PSTR("[SCH] no NTP, time now=%02d:%02d:%02d\n"),hour(t),minute(t),second(t));
|
||||
}
|
||||
else {
|
||||
// compare at next minute and SCH_UPDATE_SEC seconds
|
||||
sec = NTP.getTimeDateString().substring(6, 8).toInt();
|
||||
}
|
||||
int i;
|
||||
for (i = 0; i < MAX_SCHEDULED; i++) {
|
||||
if (getSetting("sch_switch" + String(i)).length() == 0)
|
||||
break;
|
||||
String sch_weekdays = getSetting("sch_weekdays" + String(i));
|
||||
if (isThisWday(sch_weekdays)) {
|
||||
int sch_switch = getSetting("sch_switch" + String(i)).toInt();
|
||||
int sch_operation = getSetting("sch_operation" + String(i)).toInt();
|
||||
int sch_hour = getSetting("sch_hour" + String(i)).toInt();
|
||||
int sch_minute = getSetting("sch_minute" + String(i)).toInt();
|
||||
//DEBUG_MSG_P(PSTR("[SCH] Today it will turn switch #%d %d @ %02d:%02d\n"), sch_switch, sch_operation, sch_hour, sch_minute);
|
||||
int minToTrigger = diffTime(sch_hour, sch_minute);
|
||||
if (minToTrigger == 0) {
|
||||
relayStatus(sch_switch, sch_operation);
|
||||
DEBUG_MSG_P(PSTR("[SCH] TRIGGERED!! switch #%d is %s\n"), sch_switch, sch_operation ? "ON" : "OFF");
|
||||
}
|
||||
if (minToTrigger < 0) {
|
||||
//DEBUG_MSG_P(PSTR("[SCH] Time now: %s\n"), (char *)ntpDateTime().c_str()); // aaaa/mm/dd hh:mm:ss
|
||||
DEBUG_MSG_P(PSTR("[SCH] Time now: %s, %d minutes to trigger %02d:%02d switch #%d %s\n"),
|
||||
(char *)ntpDateTime().c_str(), -minToTrigger, sch_hour, sch_minute, sch_switch, sch_operation ? "ON" : "OFF");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool isThisWday(String weekdays){
|
||||
//Sunday = 1, Monday = 2, ...
|
||||
int w = weekday(now());
|
||||
//DEBUG_MSG_P(PSTR("[SCH] ntp weekday: %d\n"), w);
|
||||
char * pch;
|
||||
char * p = (char *)weekdays.c_str();
|
||||
while ((pch = strtok_r(p, ",", &p)) != NULL) {
|
||||
//DEBUG_MSG_P(PSTR("[SCH] w found: %d\n"), atoi(pch));
|
||||
if (atoi(pch) == w) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int diffTime(int schhour, int schminute){
|
||||
if (!ntpConnected()){
|
||||
time_t t = now();
|
||||
DEBUG_MSG_P(PSTR("[SCH] no NTP time = %02d:%02d:%02d\n"),hour(t),minute(t),second(t));
|
||||
return (hour(t) - schhour) * 60 + minute(t) - schminute;
|
||||
}
|
||||
else {
|
||||
String value = NTP.getTimeDateString();
|
||||
int hour = value.substring(0, 2).toInt();
|
||||
int minute = value.substring(3, 5).toInt();
|
||||
//DEBUG_MSG_P(PSTR("[SCH] ntp time: %02d:%02d\n"), hour, minute);
|
||||
//DEBUG_MSG_P(PSTR("[SCH] cmp time: %02d:%02d\n"), schhour, schminute);
|
||||
return (hour - schhour) * 60 + minute - schminute;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // SCHEDULER_SUPPORT
|
||||
Binary file not shown.
155
code/espurna/scheduler.ino
Normal file
155
code/espurna/scheduler.ino
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
|
||||
A SCHEDULER MODULE
|
||||
|
||||
Copyright (C) 2017 by faina09
|
||||
|
||||
*/
|
||||
|
||||
#if SCHEDULER_SUPPORT
|
||||
|
||||
#include <NtpClientLib.h>
|
||||
|
||||
#if WEB_SUPPORT
|
||||
|
||||
void _schWebSocketOnSend(JsonObject &root){
|
||||
|
||||
root["maxScheduled"] = MAX_SCHEDULED;
|
||||
JsonArray &sch = root.createNestedArray("schedule");
|
||||
for (byte i = 0; i < MAX_SCHEDULED; i++) {
|
||||
if (!hasSetting("schSwitch", i)) break;
|
||||
JsonObject &scheduler = sch.createNestedObject();
|
||||
scheduler["schSwitch"] = getSetting("schSwitch", i, "");
|
||||
scheduler["schAction"] = getSetting("schAction", i, "");
|
||||
scheduler["schHour"] = getSetting("schHour", i, "");
|
||||
scheduler["schMinute"] = getSetting("schMinute", i, "");
|
||||
scheduler["schWDs"] = getSetting("schWDs", i, "");
|
||||
}
|
||||
}
|
||||
|
||||
void _schConfigure() {
|
||||
|
||||
bool delete_flag = false;
|
||||
|
||||
for (unsigned char i = 0; i < MAX_SCHEDULED; i++) {
|
||||
|
||||
int sch_switch = getSetting("schSwitch", i, 0xFF).toInt();
|
||||
if (sch_switch == 0xFF) delete_flag = true;
|
||||
|
||||
if (delete_flag) {
|
||||
|
||||
delSetting("schSwitch", i);
|
||||
delSetting("schAction", i);
|
||||
delSetting("schHour", i);
|
||||
delSetting("schMinute", i);
|
||||
delSetting("schWDs", i);
|
||||
|
||||
} else {
|
||||
|
||||
#if DEBUG_SUPPORT
|
||||
|
||||
int sch_operation = getSetting("schAction", i, 0).toInt();
|
||||
int sch_hour = getSetting("schHour", i, 0).toInt();
|
||||
int sch_minute = getSetting("schMinute", i, 0).toInt();
|
||||
String sch_weekdays = getSetting("schWDs", i, "");
|
||||
DEBUG_MSG_P(PSTR("[SCH] Turn switch #%d %s at %02d:%02d on %s\n"), sch_switch, sch_operation ? "ON" : "OFF", sch_hour, sch_minute, (char *)sch_weekdays.c_str());
|
||||
|
||||
#endif // DEBUG_SUPPORT
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // WEB_SUPPORT
|
||||
|
||||
bool _isThisWeekday(String weekdays){
|
||||
|
||||
//Sunday = 1, Monday = 2, ...
|
||||
int w = weekday(now());
|
||||
//DEBUG_MSG_P(PSTR("[SCH] NTP weekday: %d\n"), w);
|
||||
|
||||
char * pch;
|
||||
char * p = (char *) weekdays.c_str();
|
||||
while ((pch = strtok_r(p, ",", &p)) != NULL) {
|
||||
//DEBUG_MSG_P(PSTR("[SCH] w found: %d\n"), atoi(pch));
|
||||
if (atoi(pch) == w) return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
int _diffTime(unsigned char schedule_hour, unsigned char schedule_minute){
|
||||
|
||||
unsigned char now_hour;
|
||||
unsigned char now_minute;
|
||||
|
||||
if (ntpConnected()) {
|
||||
String value = NTP.getTimeDateString();
|
||||
now_hour = value.substring(0, 2).toInt();
|
||||
now_minute = value.substring(3, 5).toInt();
|
||||
} else {
|
||||
time_t t = now();
|
||||
now_hour = hour(t);
|
||||
now_minute = minute(t);
|
||||
}
|
||||
|
||||
return (schedule_hour - now_hour) * 60 + schedule_minute - now_minute;
|
||||
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
void schSetup() {
|
||||
|
||||
_schConfigure();
|
||||
|
||||
// Update websocket clients
|
||||
#if WEB_SUPPORT
|
||||
wsOnSendRegister(_schWebSocketOnSend);
|
||||
wsOnAfterParseRegister(_schConfigure);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
void schLoop(){
|
||||
|
||||
static unsigned long last_update = 0;
|
||||
static int update_time = 0;
|
||||
|
||||
// Check if we should compare scheduled and actual times
|
||||
if ((millis() - last_update > update_time) || (last_update == 0)) {
|
||||
last_update = millis();
|
||||
|
||||
// Calculate next update time
|
||||
unsigned char current_second = ntpConnected() ?
|
||||
NTP.getTimeDateString().substring(6, 8).toInt() :
|
||||
second(now())
|
||||
;
|
||||
update_time = (SCH_UPDATE_SEC + 60 - current_second) * 1000;
|
||||
|
||||
for (unsigned char i = 0; i < MAX_SCHEDULED; i++) {
|
||||
|
||||
int sch_switch = getSetting("schSwitch", i, 0xFF).toInt();
|
||||
if (sch_switch == 0xFF) break;
|
||||
|
||||
String sch_weekdays = getSetting("schWDs", i, "");
|
||||
if (_isThisWeekday(sch_weekdays)) {
|
||||
|
||||
int sch_hour = getSetting("schHour", i, 0).toInt();
|
||||
int sch_minute = getSetting("schMinute", i, 0).toInt();
|
||||
int minutes_to_trigger = _diffTime(sch_hour, sch_minute);
|
||||
if (minutes_to_trigger == 0) {
|
||||
int sch_operation = getSetting("schAction", i, 0).toInt();
|
||||
relayStatus(sch_switch, sch_operation);
|
||||
DEBUG_MSG_P(PSTR("[SCH] Schedule #%d TRIGGERED!!\n"), sch_switch);
|
||||
} else if (minutes_to_trigger > 0) {
|
||||
DEBUG_MSG_P(PSTR("[SCH] %d minutes to trigger schedule #%d\n"), minutes_to_trigger, sch_switch);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // SCHEDULER_SUPPORT
|
||||
@@ -101,7 +101,7 @@ function validateForm(form) {
|
||||
// These fields will always be a list of values
|
||||
var is_group = [
|
||||
"ssid", "pass", "gw", "mask", "ip", "dns",
|
||||
"sch_switch","sch_operation","sch_hour","sch_minute","sch_weekdays",
|
||||
"schSwitch","schAction","schHour","schMinute","schWDs",
|
||||
"relayBoot", "relayPulse", "relayTime",
|
||||
"mqttGroup", "mqttGroupInv",
|
||||
"dczRelayIdx", "dczMagnitude",
|
||||
@@ -146,6 +146,9 @@ function getData(form) {
|
||||
}
|
||||
});
|
||||
|
||||
// Post process
|
||||
if ("schSwitch" in data) data["schSwitch"].push(0xFF);
|
||||
|
||||
return data;
|
||||
|
||||
}
|
||||
@@ -479,16 +482,16 @@ function moreSchedule() {
|
||||
var parent = $(this).parents(".pure-g");
|
||||
$("div.more", parent).toggle();
|
||||
}
|
||||
|
||||
function addSchedule() {
|
||||
|
||||
function addSchedule() {
|
||||
var numSchedules = $("#schedules > div").length;
|
||||
if (numSchedules >= maxSchedules) {
|
||||
alert("Max number of schedules reached");
|
||||
return;
|
||||
}
|
||||
}
|
||||
var tabindex = 200 + numSchedules * 10;
|
||||
var template = $("#scheduleTemplate").children();
|
||||
var line = $(template).clone();
|
||||
var line = $(template).clone();
|
||||
$(line).find("input").each(function() {
|
||||
$(this).attr("tabindex", tabindex++);
|
||||
});
|
||||
@@ -1118,7 +1121,7 @@ $(function() {
|
||||
$(".button-add-schedule").on('click', function() {
|
||||
$("div.more", addSchedule()).toggle();
|
||||
});
|
||||
|
||||
|
||||
$(document).on('change', 'input', hasChanged);
|
||||
$(document).on('change', 'select', hasChanged);
|
||||
|
||||
|
||||
@@ -1047,34 +1047,37 @@
|
||||
</div>
|
||||
|
||||
<div id="scheduleTemplate" class="template">
|
||||
|
||||
<div class="pure-g">
|
||||
<label class="pure-u-sm-4-24 pure-u-1-3" for="sch_switch">Turn Switch No</label>
|
||||
|
||||
<label class="pure-u-sm-4-24 pure-u-1-3">Turn Switch No</label>
|
||||
<div class="pure-u-sm-3-24 pure-u-1-3"><input name="schSwitch" type="number" min="0" max="10" step="1" value="0" tabindex="0" /></div>
|
||||
|
||||
<div class="pure-u-sm-3-24 pure-u-1-3">
|
||||
<input name="sch_switch" type="number" min="0" max="10" step="1" value="0" tabindex="0" />
|
||||
</div>
|
||||
<div class="pure-u-sm-3-24 pure-u-1-3">
|
||||
<select name="sch_operation" tabindex="0">
|
||||
<option value="0">OFF</option>
|
||||
<option value="1">ON</option>
|
||||
</select>
|
||||
<select name="schAction" tabindex="0">
|
||||
<option value="0">OFF</option>
|
||||
<option value="1">ON</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<label class="pure-u-sm-5-24 pure-u-1-3">at Time HH MM</label>
|
||||
<div class="pure-u-sm-3-24 pure-u-1-3">
|
||||
<input name="sch_hour" type="number" min="0" max="23" step="1" tabindex="0" value="0" />
|
||||
</div>
|
||||
<div class="pure-u-sm-3-24 pure-u-1-3">
|
||||
<input name="sch_minute" type="number" min="0" max="59" step="1" tabindex="0" value="0" />
|
||||
</div>
|
||||
<div class="pure-u-sm-3-24 pure-u-1-3"><input name="schHour" type="number" min="0" max="23" step="1" tabindex="0" value="0" /></div>
|
||||
<div class="pure-u-sm-3-24 pure-u-1-3"><input name="schMinute" type="number" min="0" max="59" step="1" tabindex="0" value="0" /></div>
|
||||
|
||||
<div class="pure-u break"></div>
|
||||
|
||||
<label class="pure-u-sm-1-4 pure-u-1">on Weekdays:</label>
|
||||
<div class="pure-u-sm-3-4 pure-u-1">
|
||||
<div class="pure-u-1 weekDays-selector">
|
||||
<input type="inbox" name="sch_weekdays" value="1,2,3,4,5,6,7" class="weekday" tabindex="0" />
|
||||
<input type="inbox" name="schWDs" value="1,2,3,4,5,6,7" class="weekday" tabindex="0" />
|
||||
</div>
|
||||
<div class="pure-u-1 hint">1 = Sunday, 2 = Monday, ...</div>
|
||||
</div>
|
||||
|
||||
<div class="pure-u-md-1-6 pure-u-1-4"><button type="button" class="pure-button button-del-schedule pure-u-5-6 pure-u-md-5-6">Del</button></div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<div id="relayTemplate" class="template">
|
||||
|
||||
Reference in New Issue
Block a user