Implemented first readonly version of ntp settings
This commit is contained in:
parent
821b9b16d1
commit
1f692f241f
@ -3,12 +3,15 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#define CONFIG_FILENAME "/config.bin"
|
||||
#define CONFIG_VERSION 0x00010000 // 0.1.0
|
||||
#define CONFIG_VERSION 0x00010400 // 0.1.4 // make sure to clean all after change
|
||||
|
||||
#define WIFI_MAX_SSID_STRLEN 31
|
||||
#define WIFI_MAX_PASSWORD_STRLEN 31
|
||||
#define WIFI_MAX_HOSTNAME_STRLEN 31
|
||||
|
||||
#define NTP_MAX_SERVER_STRLEN 31
|
||||
#define NTP_MAX_TIMEZONE_STRLEN 50
|
||||
|
||||
struct CONFIG_T {
|
||||
uint32_t Cfg_Version;
|
||||
uint Cfg_SaveCount;
|
||||
@ -22,6 +25,9 @@ struct CONFIG_T {
|
||||
byte WiFi_Dns2[4];
|
||||
bool WiFi_Dhcp;
|
||||
char WiFi_Hostname[WIFI_MAX_HOSTNAME_STRLEN + 1];
|
||||
|
||||
char Ntp_Server[NTP_MAX_SERVER_STRLEN + 1];
|
||||
char Ntp_Timezone[NTP_MAX_TIMEZONE_STRLEN + 1];
|
||||
};
|
||||
|
||||
class ConfigurationClass {
|
||||
|
||||
14
include/NtpSettings.h
Normal file
14
include/NtpSettings.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
class NtpSettingsClass {
|
||||
public:
|
||||
NtpSettingsClass();
|
||||
void init();
|
||||
|
||||
void setServer();
|
||||
void setTimezone();
|
||||
};
|
||||
|
||||
extern NtpSettingsClass NtpSettings;
|
||||
@ -18,9 +18,12 @@ private:
|
||||
|
||||
void onNetworkStatus(AsyncWebServerRequest* request);
|
||||
void onSystemStatus(AsyncWebServerRequest* request);
|
||||
void onNtpStatus(AsyncWebServerRequest* request);
|
||||
|
||||
void onNetworkAdminGet(AsyncWebServerRequest* request);
|
||||
void onNetworkAdminPost(AsyncWebServerRequest* request);
|
||||
|
||||
void onNtpAdminGet(AsyncWebServerRequest* request);
|
||||
};
|
||||
|
||||
extern WebApiClass WebApi;
|
||||
@ -16,3 +16,6 @@
|
||||
#define WIFI_SSID ""
|
||||
#define WIFI_PASSWORD ""
|
||||
#define WIFI_DHCP true
|
||||
|
||||
#define NTP_SERVER "pool.ntp.org"
|
||||
#define NTP_TIMEZONE "CET-1CEST,M3.5.0,M10.5.0/3"
|
||||
@ -15,6 +15,10 @@ void ConfigurationClass::init()
|
||||
strlcpy(config.WiFi_Password, WIFI_PASSWORD, sizeof(config.WiFi_Password));
|
||||
config.WiFi_Dhcp = WIFI_DHCP;
|
||||
strlcpy(config.WiFi_Hostname, APP_HOSTNAME, sizeof(config.WiFi_Hostname));
|
||||
|
||||
// NTP Settings
|
||||
strlcpy(config.Ntp_Server, NTP_SERVER, sizeof(config.Ntp_Server));
|
||||
strlcpy(config.Ntp_Timezone, NTP_TIMEZONE, sizeof(config.Ntp_Timezone));
|
||||
}
|
||||
|
||||
bool ConfigurationClass::write()
|
||||
@ -48,6 +52,10 @@ bool ConfigurationClass::read()
|
||||
|
||||
void ConfigurationClass::migrate()
|
||||
{
|
||||
if (config.Cfg_Version < 0x00010400) {
|
||||
strlcpy(config.Ntp_Server, NTP_SERVER, sizeof(config.Ntp_Server));
|
||||
strlcpy(config.Ntp_Timezone, NTP_TIMEZONE, sizeof(config.Ntp_Timezone));
|
||||
}
|
||||
config.Cfg_Version = CONFIG_VERSION;
|
||||
write();
|
||||
}
|
||||
|
||||
26
src/NtpSettings.cpp
Normal file
26
src/NtpSettings.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
#include "NtpSettings.h"
|
||||
#include "Configuration.h"
|
||||
#include <time.h>
|
||||
|
||||
NtpSettingsClass::NtpSettingsClass()
|
||||
{
|
||||
}
|
||||
|
||||
void NtpSettingsClass::init()
|
||||
{
|
||||
setServer();
|
||||
setTimezone();
|
||||
}
|
||||
|
||||
void NtpSettingsClass::setServer()
|
||||
{
|
||||
configTime(0, 0, Configuration.get().Ntp_Server);
|
||||
}
|
||||
|
||||
void NtpSettingsClass::setTimezone()
|
||||
{
|
||||
setenv("TZ", Configuration.get().Ntp_Timezone, 1);
|
||||
tzset();
|
||||
}
|
||||
|
||||
NtpSettingsClass NtpSettings;
|
||||
@ -30,6 +30,9 @@ void WebApiClass::init()
|
||||
_server.on("/api/network/config", HTTP_GET, std::bind(&WebApiClass::onNetworkAdminGet, this, _1));
|
||||
_server.on("/api/network/config", HTTP_POST, std::bind(&WebApiClass::onNetworkAdminPost, this, _1));
|
||||
|
||||
_server.on("/api/ntp/status", HTTP_GET, std::bind(&WebApiClass::onNtpStatus, this, _1));
|
||||
_server.on("/api/ntp/config", HTTP_GET, std::bind(&WebApiClass::onNtpAdminGet, this, _1));
|
||||
|
||||
_server.serveStatic("/", LITTLEFS, "/", "max-age=86400").setDefaultFile("index.html");
|
||||
_server.onNotFound(std::bind(&WebApiClass::onNotFound, this, _1));
|
||||
_server.begin();
|
||||
@ -271,4 +274,40 @@ void WebApiClass::onNetworkAdminPost(AsyncWebServerRequest* request)
|
||||
WiFiSettings.applyConfig();
|
||||
}
|
||||
|
||||
void WebApiClass::onNtpStatus(AsyncWebServerRequest* request)
|
||||
{
|
||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||
JsonObject root = response->getRoot();
|
||||
CONFIG_T& config = Configuration.get();
|
||||
|
||||
root[F("ntp_server")] = config.Ntp_Server;
|
||||
root[F("ntp_timezone")] = config.Ntp_Timezone;
|
||||
|
||||
struct tm timeinfo;
|
||||
if (!getLocalTime(&timeinfo)) {
|
||||
root[F("ntp_status")] = false;
|
||||
} else {
|
||||
root[F("ntp_status")] = true;
|
||||
}
|
||||
char timeStringBuff[50];
|
||||
strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %B %d %Y %H:%M:%S", &timeinfo);
|
||||
root[F("ntp_localtime")] = timeStringBuff;
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
void WebApiClass::onNtpAdminGet(AsyncWebServerRequest* request)
|
||||
{
|
||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||
JsonObject root = response->getRoot();
|
||||
CONFIG_T& config = Configuration.get();
|
||||
|
||||
root[F("ntp_server")] = config.Ntp_Server;
|
||||
root[F("ntp_timezone")] = config.Ntp_Timezone;
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
WebApiClass WebApi;
|
||||
@ -1,4 +1,5 @@
|
||||
#include "Configuration.h"
|
||||
#include "NtpSettings.h"
|
||||
#include "WebApi.h"
|
||||
#include "WiFiSettings.h"
|
||||
#include "defaults.h"
|
||||
@ -45,6 +46,11 @@ void setup()
|
||||
Serial.println(F("done"));
|
||||
WiFiSettings.applyConfig();
|
||||
|
||||
// Initialize NTP
|
||||
Serial.print(F("Initialize NTP... "));
|
||||
NtpSettings.init();
|
||||
Serial.println(F("done"));
|
||||
|
||||
// Initialize WebApi
|
||||
Serial.print(F("Initialize WebApi... "));
|
||||
WebApi.init();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user