Browse Source

Changes in time management

i18n
Xose Pérez 7 years ago
parent
commit
8eb22ff9bc
3 changed files with 18 additions and 35 deletions
  1. +1
    -1
      code/espurna/mqtt.ino
  2. +9
    -14
      code/espurna/ntp.ino
  3. +8
    -20
      code/espurna/scheduler.ino

+ 1
- 1
code/espurna/mqtt.ino View File

@ -487,7 +487,7 @@ void mqttFlush() {
// Add extra propeties
#if NTP_SUPPORT && MQTT_ENQUEUE_DATETIME
if (ntpConnected()) root[MQTT_TOPIC_TIME] = ntpDateTime();
if (ntpSynced()) root[MQTT_TOPIC_TIME] = ntpDateTime();
#endif
#if MQTT_ENQUEUE_MAC
root[MQTT_TOPIC_MAC] = WiFi.macAddress();


+ 9
- 14
code/espurna/ntp.ino View File

@ -22,7 +22,7 @@ Ticker _ntp_delay;
void _ntpWebSocketOnSend(JsonObject& root) {
root["time"] = ntpDateTime();
root["ntpVisible"] = 1;
root["ntpStatus"] = ntpConnected();
root["ntpStatus"] = ntpSynced();
root["ntpServer1"] = getSetting("ntpServer1", NTP_SERVER);
root["ntpServer2"] = getSetting("ntpServer2");
root["ntpServer3"] = getSetting("ntpServer3");
@ -50,21 +50,18 @@ void _ntpConfigure() {
// -----------------------------------------------------------------------------
bool ntpConnected() {
bool ntpSynced() {
return (timeStatus() == timeSet);
}
String ntpDateTime() {
if (!ntpConnected()) return String("Not set");
String value = NTP.getTimeDateString();
int hour = value.substring(0, 2).toInt();
int minute = value.substring(3, 5).toInt();
int second = value.substring(6, 8).toInt();
int day = value.substring(9, 11).toInt();
int month = value.substring(12, 14).toInt();
int year = value.substring(15, 19).toInt();
if (!ntpSynced()) return String();
char buffer[20];
snprintf_P(buffer, sizeof(buffer), PSTR("%04d-%02d-%02d %02d:%02d:%02d"), year, month, day, hour, minute, second);
time_t t = now();
snprintf_P(buffer, sizeof(buffer),
PSTR("%04d-%02d-%02d %02d:%02d:%02d"),
year(t), month(t), day(t), hour(t), minute(t), second(t)
);
return String(buffer);
}
@ -85,9 +82,7 @@ void ntpSetup() {
}
});
wifiRegister([](justwifi_messages_t code, char * parameter) {
if (code == MESSAGE_CONNECTED) _ntpConfigure();
});
_ntpConfigure();
#if WEB_SUPPORT
wsOnSendRegister(_ntpWebSocketOnSend);


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

@ -9,7 +9,7 @@ Adapted by Xose Pérez <xose dot perez at gmail dot com>
#if SCHEDULER_SUPPORT
#include <NtpClientLib.h>
#include <TimeLib.h>
#if WEB_SUPPORT
@ -86,22 +86,10 @@ bool _schIsThisWeekday(String weekdays){
}
int _schMinutesLeft(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);
}
time_t t = now();
unsigned char now_hour = hour(t);
unsigned char now_minute = minute(t);
return (schedule_hour - now_hour) * 60 + schedule_minute - now_minute;
}
// -----------------------------------------------------------------------------
@ -126,15 +114,15 @@ void schLoop() {
static unsigned long last_update = 0;
static int update_time = 0;
// Check time has been sync'ed
if (!ntpSynced()) return;
// 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())
;
unsigned char current_second = second();
update_time = (SCHEDULER_UPDATE_SEC + 60 - current_second) * 1000;
for (unsigned char i = 0; i < SCHEDULER_MAX_SCHEDULES; i++) {


Loading…
Cancel
Save