🧼 Removes ntp
This commit is contained in:
+25299
-26936
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,7 @@ static const char *TAG = "APService";
|
||||
|
||||
APService::APService()
|
||||
: endpoint(APSettings::read, APSettings::update, this),
|
||||
_persistence(APSettings::read, APSettings::update, this, NTP_SETTINGS_FILE) {
|
||||
_persistence(APSettings::read, APSettings::update, this, AP_SETTINGS_FILE) {
|
||||
addUpdateHandler([&](const String &originId) { reconfigureAP(); }, false);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace feature_service {
|
||||
|
||||
void features(JsonObject &root) {
|
||||
root["ntp"] = USE_NTP;
|
||||
root["upload_firmware"] = USE_UPLOAD_FIRMWARE;
|
||||
root["download_firmware"] = USE_DOWNLOAD_FIRMWARE;
|
||||
root["sleep"] = USE_SLEEP;
|
||||
|
||||
@@ -7,11 +7,6 @@
|
||||
|
||||
#define FT_ENABLED(feature) feature
|
||||
|
||||
// ntp feature on by default
|
||||
#ifndef USE_NTP
|
||||
#define USE_NTP 1
|
||||
#endif
|
||||
|
||||
// upload firmware feature off by default
|
||||
#ifndef USE_UPLOAD_FIRMWARE
|
||||
#define USE_UPLOAD_FIRMWARE 0
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
#define AP_SETTINGS_FILE "/config/apSettings.json"
|
||||
#define CAMERA_SETTINGS_FILE "/config/cameraSettings.json"
|
||||
#define FS_CONFIG_DIRECTORY "/config"
|
||||
#define NTP_SETTINGS_FILE "/config/ntpSettings.json"
|
||||
#define DEVICE_CONFIG_FILE "/config/peripheral.json"
|
||||
#define WIFI_SETTINGS_FILE "/config/wifiSettings.json"
|
||||
#define SERVO_SETTINGS_FILE "/config/servoSettings.json"
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
#include <ntp_service.h>
|
||||
|
||||
static const char *TAG = "NPT Service";
|
||||
|
||||
NTPService::NTPService()
|
||||
: endpoint(NTPSettings::read, NTPSettings::update, this),
|
||||
_persistence(NTPSettings::read, NTPSettings::update, this, NTP_SETTINGS_FILE) {
|
||||
addUpdateHandler([&](const String &originId) { configureNTP(); }, false);
|
||||
}
|
||||
|
||||
void NTPService::begin() {
|
||||
WiFi.onEvent(std::bind(&NTPService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
|
||||
WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
|
||||
WiFi.onEvent(std::bind(&NTPService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2),
|
||||
WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
|
||||
_persistence.readFromFS();
|
||||
configureNTP();
|
||||
}
|
||||
|
||||
/*
|
||||
* Formats the time using the format provided.
|
||||
*
|
||||
* Uses a 25 byte buffer, large enough to fit an ISO time string with offset.
|
||||
*/
|
||||
const char *formatTime(const tm *time, const char *format) {
|
||||
static char time_string[25];
|
||||
strftime(time_string, sizeof(time_string), format, time);
|
||||
return time_string;
|
||||
}
|
||||
|
||||
const char *toUTCTimeString(const tm *time) { return formatTime(time, "%FT%TZ"); }
|
||||
|
||||
const char *toLocalTimeString(const tm *time) { return formatTime(time, "%FT%T"); }
|
||||
|
||||
esp_err_t NTPService::getStatus(PsychicRequest *request) {
|
||||
PsychicJsonResponse response = PsychicJsonResponse(request, false);
|
||||
JsonObject root = response.getRoot();
|
||||
|
||||
// grab the current instant in unix seconds
|
||||
time_t now = time(nullptr);
|
||||
|
||||
// only provide enabled/disabled status for now
|
||||
root["status"] = sntp_enabled() ? 1 : 0;
|
||||
|
||||
// the current time in UTC
|
||||
root["utc_time"] = toUTCTimeString(gmtime(&now));
|
||||
|
||||
// local time with offset
|
||||
root["local_time"] = toLocalTimeString(localtime(&now));
|
||||
|
||||
// the sntp server name
|
||||
root["server"] = sntp_getservername(0);
|
||||
|
||||
// device uptime in seconds
|
||||
root["uptime"] = millis() / 1000;
|
||||
|
||||
return response.send();
|
||||
}
|
||||
|
||||
void NTPService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
ESP_LOGI(TAG, "Got IP address, starting NTP Synchronization");
|
||||
configureNTP();
|
||||
}
|
||||
|
||||
void NTPService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
ESP_LOGD(TAG, "WiFi connection dropped, stopping NTP.");
|
||||
configureNTP();
|
||||
}
|
||||
|
||||
void NTPService::configureNTP() {
|
||||
if (WiFi.isConnected() && _state.enabled) {
|
||||
ESP_LOGI(TAG, "Starting NTP...");
|
||||
configTzTime(_state.tzFormat.c_str(), _state.server.c_str());
|
||||
} else {
|
||||
setenv("TZ", _state.tzFormat.c_str(), 1);
|
||||
tzset();
|
||||
sntp_stop();
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t NTPService::handleTime(PsychicRequest *request, JsonVariant &json) {
|
||||
if (!sntp_enabled() && json.is<JsonObject>()) {
|
||||
struct tm tm = {0};
|
||||
String timeLocal = json["local_time"];
|
||||
char *s = strptime(timeLocal.c_str(), "%Y-%m-%dT%H:%M:%S", &tm);
|
||||
if (s != nullptr) {
|
||||
time_t time = mktime(&tm);
|
||||
struct timeval now = {.tv_sec = time};
|
||||
settimeofday(&now, nullptr);
|
||||
return request->reply(200);
|
||||
}
|
||||
}
|
||||
return request->reply(400);
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
#ifndef NTPService_h
|
||||
#define NTPService_h
|
||||
|
||||
#include <filesystem.h>
|
||||
#include <template/stateful_persistence.h>
|
||||
#include <WiFi.h>
|
||||
#include <template/stateful_endpoint.h>
|
||||
#include <settings/ntp_settings.h>
|
||||
|
||||
#include <lwip/apps/sntp.h>
|
||||
#include <time.h>
|
||||
|
||||
class NTPService : public StatefulService<NTPSettings> {
|
||||
public:
|
||||
NTPService();
|
||||
|
||||
void begin();
|
||||
static esp_err_t getStatus(PsychicRequest *request);
|
||||
static esp_err_t handleTime(PsychicRequest *request, JsonVariant &json);
|
||||
|
||||
StatefulHttpEndpoint<NTPSettings> endpoint;
|
||||
|
||||
private:
|
||||
FSPersistence<NTPSettings> _persistence;
|
||||
|
||||
void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||
void configureNTP();
|
||||
};
|
||||
|
||||
#endif // end NTPService_h
|
||||
Reference in New Issue
Block a user