Add hasp_time

This commit is contained in:
fvanroie 2022-04-03 01:39:56 +02:00
parent 13c1c02d66
commit 143abc212d
8 changed files with 196 additions and 83 deletions

View File

@ -33,6 +33,7 @@
#include "sys/svc/hasp_ota.h"
#include "mqtt/hasp_mqtt.h"
#include "sys/net/hasp_network.h" // for network_get_status()
#include "sys/net/hasp_time.h"
#endif
#endif
@ -499,6 +500,13 @@ void dispatch_config(const char* topic, const char* payload, uint8_t source)
else
wifiGetConfig(settings);
}
else if(strcasecmp_P(topic, PSTR("time")) == 0) {
if(update)
timeSetConfig(settings);
else
timeGetConfig(settings);
}
#if HASP_USE_MQTT > 0
else if(strcasecmp_P(topic, PSTR("mqtt")) == 0) {
if(update)

View File

@ -35,34 +35,35 @@ void configSetConfig(JsonObject& settings);
void configGetConfig(JsonDocument& settings);
/* json keys used in the configfile */
const char FP_CONFIG_STARTPAGE[] PROGMEM = "startpage";
const char FP_CONFIG_STARTDIM[] PROGMEM = "startdim";
const char FP_CONFIG_THEME[] PROGMEM = "theme";
const char FP_CONFIG_HUE[] PROGMEM = "hue";
const char FP_CONFIG_ZIFONT[] PROGMEM = "font";
const char FP_CONFIG_PAGES[] PROGMEM = "pages";
const char FP_CONFIG_ENABLE[] PROGMEM = "enable";
const char FP_CONFIG_HOST[] PROGMEM = "host";
const char FP_CONFIG_PORT[] PROGMEM = "port";
const char FP_CONFIG_NAME[] PROGMEM = "name";
const char FP_CONFIG_USER[] PROGMEM = "user";
const char FP_CONFIG_PASS[] PROGMEM = "pass";
const char FP_CONFIG_SSID[] PROGMEM = "ssid";
const char FP_CONFIG_GROUP[] PROGMEM = "group";
const char FP_CONFIG_BAUD[] PROGMEM = "baud";
const char FP_CONFIG_LOG[] PROGMEM = "log";
const char FP_CONFIG_PROTOCOL[] PROGMEM = "proto";
const char FP_GUI_ROTATION[] PROGMEM = "rotate";
const char FP_GUI_INVERT[] PROGMEM = "invert";
const char FP_GUI_TICKPERIOD[] PROGMEM = "tick";
const char FP_GUI_IDLEPERIOD1[] PROGMEM = "idle1";
const char FP_GUI_IDLEPERIOD2[] PROGMEM = "idle2";
const char FP_GUI_CALIBRATION[] PROGMEM = "calibration";
const char FP_GUI_BACKLIGHTPIN[] PROGMEM = "bckl";
const char FP_GUI_POINTER[] PROGMEM = "cursor";
const char FP_DEBUG_TELEPERIOD[] PROGMEM = "tele";
const char FP_DEBUG_ANSI[] PROGMEM = "ansi";
const char FP_GPIO_CONFIG[] PROGMEM = "config";
const char FP_CONFIG_STARTPAGE[] PROGMEM = "startpage";
const char FP_CONFIG_STARTDIM[] PROGMEM = "startdim";
const char FP_CONFIG_THEME[] PROGMEM = "theme";
const char FP_CONFIG_HUE[] PROGMEM = "hue";
const char FP_CONFIG_ZIFONT[] PROGMEM = "font";
const char FP_CONFIG_PAGES[] PROGMEM = "pages";
const char FP_CONFIG_ENABLE[] PROGMEM = "enable";
const char FP_CONFIG_HOST[] PROGMEM = "host";
const char FP_CONFIG_PORT[] PROGMEM = "port";
const char FP_CONFIG_NAME[] PROGMEM = "name";
const char FP_CONFIG_USER[] PROGMEM = "user";
const char FP_CONFIG_PASS[] PROGMEM = "pass";
const char FP_CONFIG_SSID[] PROGMEM = "ssid";
const char FP_CONFIG_GROUP[] PROGMEM = "group";
const char FP_CONFIG_BAUD[] PROGMEM = "baud";
const char FP_CONFIG_LOG[] PROGMEM = "log";
const char FP_CONFIG_PROTOCOL[] PROGMEM = "proto";
const char FP_GUI_ROTATION[] PROGMEM = "rotate";
const char FP_GUI_INVERT[] PROGMEM = "invert";
const char FP_GUI_TICKPERIOD[] PROGMEM = "tick";
const char FP_GUI_IDLEPERIOD1[] PROGMEM = "idle1";
const char FP_GUI_IDLEPERIOD2[] PROGMEM = "idle2";
const char FP_GUI_CALIBRATION[] PROGMEM = "calibration";
const char FP_GUI_BACKLIGHTPIN[] PROGMEM = "bckl";
const char FP_GUI_BACKLIGHTINVERT[] PROGMEM = "bcklinv";
const char FP_GUI_POINTER[] PROGMEM = "cursor";
const char FP_DEBUG_TELEPERIOD[] PROGMEM = "tele";
const char FP_DEBUG_ANSI[] PROGMEM = "ansi";
const char FP_GPIO_CONFIG[] PROGMEM = "config";
const char FP_HASP_CONFIG_FILE[] PROGMEM = "/config.json";
@ -74,6 +75,7 @@ const char FP_MDNS[] PROGMEM = "mdns";
const char FP_HASP[] PROGMEM = "hasp";
const char FP_GUI[] PROGMEM = "gui";
const char FP_DEBUG[] PROGMEM = "debug";
const char FP_TIME[] PROGMEM = "time";
#endif

View File

@ -286,6 +286,9 @@ void debug_get_tag(uint8_t tag, char* buffer)
memcpy_P(buffer, PSTR("GPIO"), 5);
break;
case TAG_TIME:
memcpy_P(buffer, PSTR("TIME"), 5);
break;
case TAG_ETH:
memcpy_P(buffer, PSTR("ETH "), 5);
break;

View File

@ -191,6 +191,7 @@ enum {
TAG_MQTT_PUB = 66,
TAG_MQTT_RCV = 67,
TAG_FTP = 68,
TAG_TIME = 69,
TAG_LVGL = 90,
TAG_LVFS = 91,

View File

@ -6,6 +6,7 @@
#include "hasplib.h"
#include "hasp_oobe.h"
#include "sys/net/hasp_network.h"
#include "sys/net/hasp_time.h"
#include "dev/device.h"
// #include "drv/old/hasp_drv_touch.h"
#include "ArduinoLog.h"
@ -84,6 +85,7 @@ void setup()
#if HASP_USE_WIFI > 0 || HASP_USE_ETHERNET > 0
networkSetup();
timeSetup();
#endif
#if HASP_USE_MDNS > 0

View File

@ -1,12 +1,6 @@
/* MIT License - Copyright (c) 2019-2022 Francis Van Roie
For full license information read the LICENSE file in the project folder */
#include <time.h>
#include <sys/time.h>
#include <Arduino.h>
#include "ArduinoLog.h"
#include "hasp_conf.h"
#include "hal/hasp_hal.h"
#include "hasp_debug.h"
@ -15,42 +9,9 @@
#include "hasp/hasp.h"
#include "sys/svc/hasp_mdns.h"
#if defined(ARDUINO_ARCH_ESP32)
#include "Preferences.h"
#endif
#ifndef MYTZ
#define MYTZ "EST5EDT,M3.2.0/2,M11.1.0"
#endif
#ifndef NTPSERVER1
#define NTPSERVER1 "pool.ntp.org"
#endif
#ifndef NTPSERVER2
#define NTPSERVER2 "time.nist.gov"
#endif
#ifndef NTPSERVER3
#define NTPSERVER3 "time.google.com"
#endif
#if defined(ARDUINO_ARCH_ESP32)
// These strings must be constant and kept in memory
String mytz((char*)0);
String ntp1((char*)0);
String ntp2((char*)0);
String ntp3((char*)0);
#endif
#if HASP_USE_ETHERNET > 0 || HASP_USE_WIFI > 0
void networkStart(void)
{
#if defined(ARDUINO_ARCH_ESP8266)
LOG_WARNING(TAG_MAIN, F("TIMEZONE: %s"), MYTZ);
configTzTime(MYTZ, NTPSERVER1, NTPSERVER2, NTPSERVER3); // literal string
#endif
// haspProgressVal(255); // hide
haspReconnect();
debugStartSyslog();
@ -83,22 +44,6 @@ void networkStop(void)
void networkSetup()
{
#if defined(ARDUINO_ARCH_ESP32)
Preferences preferences;
preferences.begin("time", false);
mytz = preferences.getString("tz", MYTZ);
ntp1 = preferences.getString("ntp1", NTPSERVER1);
ntp2 = preferences.getString("ntp2", NTPSERVER2);
ntp3 = preferences.getString("ntp3", NTPSERVER3);
LOG_WARNING(TAG_MAIN, F("TIMEZONE: %s"), mytz.c_str());
LOG_WARNING(TAG_MAIN, F("NTPSERVER: %s %s %s"), ntp1.c_str(), ntp2.c_str(), ntp3.c_str());
configTzTime(mytz.c_str(), ntp1.c_str(), ntp2.c_str(), ntp3.c_str());
preferences.end();
#endif
#if HASP_USE_ETHERNET > 0
ethernetSetup();
#endif

130
src/sys/net/hasp_time.cpp Normal file
View File

@ -0,0 +1,130 @@
#include <time.h>
#include <sys/time.h>
#include <Arduino.h>
#include "ArduinoLog.h"
#include "hasp_conf.h"
#include "hal/hasp_hal.h"
#include "hasp_debug.h"
#include "hasp_config.h"
#if defined(ARDUINO_ARCH_ESP32)
#include "Preferences.h"
#include "nvs.h"
#include "nvs_flash.h"
#endif
#ifndef MYTZ
#define MYTZ "EST5EDT,M3.2.0/2,M11.1.0"
#endif
#ifndef NTPSERVER1
#define NTPSERVER1 "pool.ntp.org"
#endif
#ifndef NTPSERVER2
#define NTPSERVER2 "time.nist.gov"
#endif
#ifndef NTPSERVER3
#define NTPSERVER3 "time.google.com"
#endif
#if defined(ARDUINO_ARCH_ESP32)
// These strings must be constant and kept in memory
String mytz((char*)0);
String ntp1((char*)0);
String ntp2((char*)0);
String ntp3((char*)0);
#endif
void timeSetup()
{
#if defined(ARDUINO_ARCH_ESP8266)
LOG_WARNING(TAG_TIME, F("TIMEZONE: %s"), MYTZ);
configTzTime(MYTZ, NTPSERVER1, NTPSERVER2, NTPSERVER3); // literal string
#endif
#if defined(ARDUINO_ARCH_ESP32)
Preferences preferences;
preferences.begin("time", true);
mytz = preferences.getString("tz", MYTZ);
ntp1 = preferences.getString("ntp1", NTPSERVER1);
ntp2 = preferences.getString("ntp2", NTPSERVER2);
ntp3 = preferences.getString("ntp3", NTPSERVER3);
LOG_WARNING(TAG_TIME, F("TIMEZONE: %s"), mytz.c_str());
LOG_WARNING(TAG_TIME, F("NTPSERVER: %s %s %s"), ntp1.c_str(), ntp2.c_str(), ntp3.c_str());
configTzTime(mytz.c_str(), ntp1.c_str(), ntp2.c_str(), ntp3.c_str());
preferences.end();
#endif
}
/* ===== Read/Write Configuration ===== */
#if HASP_USE_CONFIG > 0
bool timeGetConfig(const JsonObject& settings)
{
Preferences preferences;
bool changed = false;
preferences.begin("time", true);
settings["tz"] = preferences.getString("tz", MYTZ);
settings["ntp"][0] = preferences.getString("ntp1", NTPSERVER1);
settings["ntp"][1] = preferences.getString("ntp2", NTPSERVER2);
settings["ntp"][2] = preferences.getString("ntp3", NTPSERVER3);
preferences.end();
nvs_iterator_t it = nvs_entry_find("nvs", "time", NVS_TYPE_ANY);
while(it != NULL) {
nvs_entry_info_t info;
nvs_entry_info(it, &info);
it = nvs_entry_next(it);
printf("key '%s', type '%d' \n", info.key, info.type);
};
if(changed) configOutput(settings, TAG_TIME);
return changed;
}
bool nvsUpateString(Preferences& preferences, const char* key, JsonVariant value)
{
bool changed = false;
const char* val = value.as<const char*>();
if(!value.isNull()) { // Json key exists
if(preferences.isKey(key)) { // Nvs key exists
changed = preferences.getString(key, "") != String(val); // Value changed
} else
changed = true; // Nvs key doesnot exist, create it
if(changed) {
size_t len = preferences.putString(key, val);
LOG_VERBOSE(TAG_TIME, F(D_BULLET "Wrote %s => %s (%d bytes)"), key, val, len);
}
}
return changed;
}
bool timeSetConfig(const JsonObject& settings)
{
Preferences preferences;
preferences.begin("time", false);
configOutput(settings, TAG_TIME);
bool changed = false;
char key[16] = "tz";
changed |= nvsUpateString(preferences, key, settings[key]);
changed |= nvsUpateString(preferences, "ntp1", settings["ntp"][0]);
changed |= nvsUpateString(preferences, "ntp2", settings["ntp"][1]);
changed |= nvsUpateString(preferences, "ntp3", settings["ntp"][2]);
preferences.end();
timeSetup();
return changed;
}
#endif

22
src/sys/net/hasp_time.h Normal file
View File

@ -0,0 +1,22 @@
/* MIT License - Copyright (c) 2019-2022 Francis Van Roie
For full license information read the LICENSE file in the project folder */
#ifndef HASP_TIME_H
#define HASP_TIME_H
#include "hasplib.h"
/* ===== Default Event Processors ===== */
void timeSetup();
/* ===== Special Event Processors ===== */
/* ===== Getter and Setter Functions ===== */
/* ===== Read/Write Configuration ===== */
#if HASP_USE_CONFIG > 0
bool timeGetConfig(const JsonObject& settings);
bool timeSetConfig(const JsonObject& settings);
#endif
#endif