From e7198073afc8e11a2a959728ed456da7c55e4221 Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Wed, 24 May 2023 19:21:44 +0200 Subject: [PATCH] Feature: Allow configuration of sunset type --- include/Configuration.h | 1 + include/defaults.h | 1 + src/Configuration.cpp | 2 ++ src/SunPosition.cpp | 21 +++++++++++++++++++-- src/WebApi_ntp.cpp | 4 +++- webapp/src/locales/de.json | 10 ++++++++-- webapp/src/locales/en.json | 10 ++++++++-- webapp/src/locales/fr.json | 10 ++++++++-- webapp/src/types/NtpConfig.ts | 1 + webapp/src/views/NtpAdminView.vue | 23 +++++++++++++++++++++++ 10 files changed, 74 insertions(+), 9 deletions(-) diff --git a/include/Configuration.h b/include/Configuration.h index cf1beca..85987c4 100644 --- a/include/Configuration.h +++ b/include/Configuration.h @@ -66,6 +66,7 @@ struct CONFIG_T { char Ntp_TimezoneDescr[NTP_MAX_TIMEZONEDESCR_STRLEN + 1]; double Ntp_Longitude; double Ntp_Latitude; + uint8_t Ntp_SunsetType; bool Mqtt_Enabled; uint Mqtt_Port; diff --git a/include/defaults.h b/include/defaults.h index 9046aaf..7420dd1 100644 --- a/include/defaults.h +++ b/include/defaults.h @@ -25,6 +25,7 @@ #define NTP_TIMEZONEDESCR "Europe/Berlin" #define NTP_LONGITUDE 10.4515f #define NTP_LATITUDE 51.1657f +#define NTP_SUNSETTYPE 1 #define MQTT_ENABLED false #define MQTT_HOST "" diff --git a/src/Configuration.cpp b/src/Configuration.cpp index f64a57c..c2cfabf 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -46,6 +46,7 @@ bool ConfigurationClass::write() ntp["timezone_descr"] = config.Ntp_TimezoneDescr; ntp["latitude"] = config.Ntp_Latitude; ntp["longitude"] = config.Ntp_Longitude; + ntp["sunsettype"] = config.Ntp_SunsetType; JsonObject mqtt = doc.createNestedObject("mqtt"); mqtt["enabled"] = config.Mqtt_Enabled; @@ -189,6 +190,7 @@ bool ConfigurationClass::read() strlcpy(config.Ntp_TimezoneDescr, ntp["timezone_descr"] | NTP_TIMEZONEDESCR, sizeof(config.Ntp_TimezoneDescr)); config.Ntp_Latitude = ntp["latitude"] | NTP_LATITUDE; config.Ntp_Longitude = ntp["longitude"] | NTP_LONGITUDE; + config.Ntp_SunsetType = ntp["sunsettype"] | NTP_SUNSETTYPE; JsonObject mqtt = doc["mqtt"]; config.Mqtt_Enabled = mqtt["enabled"] | MQTT_ENABLED; diff --git a/src/SunPosition.cpp b/src/SunPosition.cpp index 1485eaa..1cfe1d0 100644 --- a/src/SunPosition.cpp +++ b/src/SunPosition.cpp @@ -45,8 +45,25 @@ void SunPositionClass::updateSunData() } _sun.setCurrentDate(1900 + timeinfo.tm_year, timeinfo.tm_mon + 1, timeinfo.tm_mday); - _sunriseMinutes = static_cast(_sun.calcCustomSunrise(SunSet::SUNSET_NAUTICAL)); - _sunsetMinutes = static_cast(_sun.calcCustomSunset(SunSet::SUNSET_NAUTICAL)); + + double sunset_type; + switch (config.Ntp_SunsetType) { + case 0: + sunset_type = SunSet::SUNSET_OFFICIAL; + break; + case 2: + sunset_type = SunSet::SUNSET_CIVIL; + break; + case 3: + sunset_type = SunSet::SUNSET_ASTONOMICAL; + break; + default: + sunset_type = SunSet::SUNSET_NAUTICAL; + break; + } + + _sunriseMinutes = static_cast(_sun.calcCustomSunrise(sunset_type)); + _sunsetMinutes = static_cast(_sun.calcCustomSunset(sunset_type)); uint minutesPastMidnight = timeinfo.tm_hour * 60 + timeinfo.tm_min; _isDayPeriod = (minutesPastMidnight >= _sunriseMinutes) && (minutesPastMidnight < _sunsetMinutes); diff --git a/src/WebApi_ntp.cpp b/src/WebApi_ntp.cpp index e592712..4fa1d10 100644 --- a/src/WebApi_ntp.cpp +++ b/src/WebApi_ntp.cpp @@ -81,6 +81,7 @@ void WebApiNtpClass::onNtpAdminGet(AsyncWebServerRequest* request) root["ntp_timezone_descr"] = config.Ntp_TimezoneDescr; root["longitude"] = config.Ntp_Longitude; root["latitude"] = config.Ntp_Latitude; + root["sunsettype"] = config.Ntp_SunsetType; response->setLength(); request->send(response); @@ -125,7 +126,7 @@ void WebApiNtpClass::onNtpAdminPost(AsyncWebServerRequest* request) return; } - if (!(root.containsKey("ntp_server") && root.containsKey("ntp_timezone") && root.containsKey("longitude") && root.containsKey("latitude"))) { + if (!(root.containsKey("ntp_server") && root.containsKey("ntp_timezone") && root.containsKey("longitude") && root.containsKey("latitude") && root.containsKey("sunsettype"))) { retMsg["message"] = "Values are missing!"; retMsg["code"] = WebApiError::GenericValueMissing; response->setLength(); @@ -166,6 +167,7 @@ void WebApiNtpClass::onNtpAdminPost(AsyncWebServerRequest* request) strlcpy(config.Ntp_TimezoneDescr, root["ntp_timezone_descr"].as().c_str(), sizeof(config.Ntp_TimezoneDescr)); config.Ntp_Latitude = root["latitude"].as(); config.Ntp_Longitude = root["longitude"].as(); + config.Ntp_SunsetType = root["sunsettype"].as(); Configuration.write(); retMsg["type"] = "success"; diff --git a/webapp/src/locales/de.json b/webapp/src/locales/de.json index 74c01e6..79641e9 100644 --- a/webapp/src/locales/de.json +++ b/webapp/src/locales/de.json @@ -243,8 +243,8 @@ "Synced": "synchronisiert", "NotSynced": "nicht synchronisiert", "LocalTime": "Lokale Uhrzeit", - "Sunrise": "Nautische Morgendämmerung", - "Sunset": "Nautische Abenddämmerung", + "Sunrise": "Morgendämmerung", + "Sunset": "Abenddämmerung", "Mode": "Modus", "Day": "Tag", "Night": "Nacht" @@ -359,6 +359,12 @@ "LocationConfiguration": "Standortkonfiguration", "Longitude": "Längengrad:", "Latitude": "Breitengrad:", + "SunSetType": "Dämmerungstyp:", + "SunSetTypeHint": "Beeinflusst die Tag/Nacht Berechnung. Es kann bis zu einer Minute dauern bis der neue Typ angewendet wurde.", + "OFFICIAL": "Standard Dämmerung (90.8°)", + "NAUTICAL": "Nautische Dämmerung (102°)", + "CIVIL": "Bürgerliche Dämmerung (96°)", + "ASTONOMICAL": "Astronomische Dämmerung (108°)", "Save": "@:dtuadmin.Save", "ManualTimeSynchronization": "Manuelle Zeitsynchronization", "CurrentOpenDtuTime": "Aktuelle OpenDTU-Zeit:", diff --git a/webapp/src/locales/en.json b/webapp/src/locales/en.json index bbbbf91..429b5a7 100644 --- a/webapp/src/locales/en.json +++ b/webapp/src/locales/en.json @@ -243,8 +243,8 @@ "Synced": "synced", "NotSynced": "not synced", "LocalTime": "Local Time", - "Sunrise": "Nautical Sunrise", - "Sunset": "Nautical Sunset", + "Sunrise": "Sunrise", + "Sunset": "Sunset", "Mode": "Mode", "Day": "Day", "Night": "Night" @@ -359,6 +359,12 @@ "LocationConfiguration": "Location Configuration", "Longitude": "Longitude", "Latitude": "Latitude", + "SunSetType": "Sunset type", + "SunSetTypeHint": "Affects the day/night calculation. It can take up to one minute until the new type will be applied.", + "OFFICIAL": "Standard dawn (90.8°)", + "NAUTICAL": "Nautical dawn (102°)", + "CIVIL": "Civil dawn (96°)", + "ASTONOMICAL": "Astronomical dawn (108°)", "Save": "@:dtuadmin.Save", "ManualTimeSynchronization": "Manual Time Synchronization", "CurrentOpenDtuTime": "Current OpenDTU Time:", diff --git a/webapp/src/locales/fr.json b/webapp/src/locales/fr.json index e6bcd5e..f5cce1a 100644 --- a/webapp/src/locales/fr.json +++ b/webapp/src/locales/fr.json @@ -243,8 +243,8 @@ "Synced": "synchronisée", "NotSynced": "pas synchronisée", "LocalTime": "Heure locale", - "Sunrise": "Nautical Sunrise", - "Sunset": "Nautical Sunset", + "Sunrise": "Sunrise", + "Sunset": "Sunset", "Mode": "Mode", "Day": "Day", "Night": "Night" @@ -359,6 +359,12 @@ "LocationConfiguration": "Géolocalisation", "Longitude": "Longitude", "Latitude": "Latitude", + "SunSetType": "Sunset type", + "SunSetTypeHint": "Affects the day/night calculation. It can take up to one minute until the new type will be applied.", + "OFFICIAL": "Standard dawn (90.8°)", + "NAUTICAL": "Nautical dawn (102°)", + "CIVIL": "Civil dawn (96°)", + "ASTONOMICAL": "Astronomical dawn (108°)", "Save": "@:dtuadmin.Save", "ManualTimeSynchronization": "Synchronisation manuelle de l'heure", "CurrentOpenDtuTime": "Heure actuelle de l'OpenDTU", diff --git a/webapp/src/types/NtpConfig.ts b/webapp/src/types/NtpConfig.ts index 172ffa0..a87c806 100644 --- a/webapp/src/types/NtpConfig.ts +++ b/webapp/src/types/NtpConfig.ts @@ -4,4 +4,5 @@ export interface NtpConfig { ntp_timezone_descr: string; latitude: number; longitude: number; + sunsettype: number; } \ No newline at end of file diff --git a/webapp/src/views/NtpAdminView.vue b/webapp/src/views/NtpAdminView.vue index 97f9b2a..69e696c 100644 --- a/webapp/src/views/NtpAdminView.vue +++ b/webapp/src/views/NtpAdminView.vue @@ -36,6 +36,21 @@ + + +
+ +
+ +
+
@@ -67,6 +82,7 @@ import InputElement from '@/components/InputElement.vue'; import type { NtpConfig } from "@/types/NtpConfig"; import { authHeader, handleResponse } from '@/utils/authentication'; import { defineComponent } from 'vue'; +import { BIconInfoCircle } from 'bootstrap-icons-vue'; export default defineComponent({ components: { @@ -74,6 +90,7 @@ export default defineComponent({ BootstrapAlert, CardElement, InputElement, + BIconInfoCircle, }, data() { return { @@ -88,6 +105,12 @@ export default defineComponent({ alertMessage: "", alertType: "info", showAlert: false, + sunsetTypeList: [ + { key: 0, value: 'OFFICIAL' }, + { key: 1, value: 'NAUTICAL' }, + { key: 2, value: 'CIVIL' }, + { key: 3, value: 'ASTONOMICAL' }, + ], }; }, watch: {