From bc2017727bdb589a6bef05c57e2d4602dc839428 Mon Sep 17 00:00:00 2001 From: AlexxBoo Date: Tue, 6 Oct 2020 15:26:54 +0200 Subject: [PATCH] added: settings for customizing time zone and DST --- Fields.h | 54 +++++++++++++++++++++++++++++++++-- TimeZones.h | 49 +++++++++++++++++++++++++++++++ data/js/app.js | 2 ++ esp8266-fastled-webserver.ino | 23 +++++++++++++++ 4 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 TimeZones.h diff --git a/Fields.h b/Fields.h index 95415d15..06ece72c 100644 --- a/Fields.h +++ b/Fields.h @@ -109,6 +109,54 @@ void setClockBackgroundFade(uint8_t value) broadcastInt("clockBackgroundFade", clockBackgroundFade); } +String getTimeZone() { + return String(currentTimeZoneIndex); +} + +String getTimeZones() { + String json = ""; + + for (uint8_t i = 0; i < timeZoneCount; i++) { + json += "\"" + timeZones[i].utcOffset + " – " + timeZones[i].region + "\""; + if (i < timeZoneCount - 1) + json += ","; + } + + return json; +} + +String setTimeZone(uint8_t value) +{ + if (value >= timeZoneCount) + value = timeZoneCount - 1; + + currentTimeZoneIndex = value; + + EEPROM.write(11, currentTimeZoneIndex); + EEPROM.commit(); + + timeClient.setTimeOffset(timeZones[currentTimeZoneIndex].utcOffsetInSeconds + (dst ? 3600 : 0)); + + broadcastInt("timeZone", currentTimeZoneIndex); +} + +String getDst() { + return String(dst); +} + +String setDst(uint8_t value) +{ + dst = value == 0 ? 0 : 1; + + EEPROM.write(12, dst); + EEPROM.commit(); + + timeClient.setTimeOffset(timeZones[currentTimeZoneIndex].utcOffsetInSeconds + (dst ? 3600 : 0)); + + broadcastInt("dst", dst); +} + + String getSolidColor() { return String(solidColor.r) + "," + String(solidColor.g) + "," + String(solidColor.b); } @@ -279,9 +327,11 @@ FieldList fields = { {"autoplay", "Autoplay", BooleanFieldType, 0, 1, getAutoplay}, {"autoplayDuration", "Autoplay Duration", NumberFieldType, 0, 255, getAutoplayDuration}, - {"clock", "Clock", SectionFieldType}, - {"showClock", "Show Clock", BooleanFieldType, 0, 1, getShowClock}, + {"clock", "Clock", SectionFieldType}, + {"showClock", "Show Clock", BooleanFieldType, 0, 1, getShowClock}, {"clockBackgroundFade", "Background Fade", NumberFieldType, 0, 255, getClockBackgroundFade}, + {"timeZone", "Time Zone", SelectFieldType, 0, timeZoneCount, getTimeZone, getTimeZones}, + {"dst", "Daylight Saving", BooleanFieldType, 0, 1, getDst}, {"solidColorSection", "Solid Color", SectionFieldType}, {"solidColor", "Color", ColorFieldType, 0, 255, getSolidColor}, diff --git a/TimeZones.h b/TimeZones.h new file mode 100644 index 00000000..e8381ff9 --- /dev/null +++ b/TimeZones.h @@ -0,0 +1,49 @@ +typedef struct { + long utcOffsetInSeconds; + String utcOffset; + String region; +} TimeZone; +typedef TimeZone TimeZoneList[]; + +const TimeZoneList timeZones = { + {-43200, "UTC−12:00", "Baker Island, Howland Island (both uninhabited)"}, + {-39600, "UTC−11:00", "American Samoa, Niue"}, + {-36000, "UTC−10:00", "Honolulu"}, + {-34200, "UTC−09:30", "Marquesas Islands"}, + {-32400, "UTC−09:00", "Anchorage"}, + {-28800, "UTC−08:00", "Los Angeles, Vancouver, Tijuana"}, + {-25200, "UTC−07:00", "Phoenix, Calgary, Ciudad Juárez"}, + {-21600, "UTC−06:00", "Mexico City, Chicago, Guatemala City, Tegucigalpa, Winnipeg, San José, San Salvador"}, + {-18000, "UTC−05:00", "New York, Toronto, Havana, Lima, Bogotá, Kingston"}, + {-14400, "UTC−04:00", "Santiago, Santo Domingo, Manaus, Caracas, La Paz, Halifax"}, + {-12600, "UTC−03:30", "St. John’s"}, + {-10800, "UTC−03:00", "São Paulo, Buenos Aires, Montevideo"}, + { -7200, "UTC−02:00", "Brazil (Fernando de Noronha), South Georgia and the South Sandwich Islands"}, + { -3600, "UTC−01:00", "Cape Verde"}, + { 0, "UTC±00:00", "London, Dublin, Lisbon, Abidjan, Accra, Dakar"}, + { 3600, "UTC+01:00", "Berlin, Rome, Paris, Madrid, Warsaw, Lagos, Kinshasa, Algiers, Casablanca"}, + { 7200, "UTC+02:00", "Cairo, Johannesburg, Khartoum, Kiev, Bucharest, Athens, Jerusalem, Sofia"}, + { 10800, "UTC+03:00", "Moscow, Istanbul, Riyadh, Baghdad, Addis Ababa, Doha"}, + { 12600, "UTC+03:30", "Tehran"}, + { 14400, "UTC+04:00", "Dubai, Baku, Tbilisi, Yerevan, Samara"}, + { 16200, "UTC+04:30", "Kabul"}, + { 18000, "UTC+05:00", "Karachi, Tashkent, Yekaterinburg"}, + { 19800, "UTC+05:30", "Mumbai, Colombo"}, + { 20700, "UTC+05:45", "Kathmandu"}, + { 21600, "UTC+06:00", "Dhaka, Almaty, Omsk"}, + { 23400, "UTC+06:30", "Yangon"}, + { 25200, "UTC+07:00", "Jakarta, Ho Chi Minh City, Bangkok, Krasnoyarsk"}, + { 28800, "UTC+08:00", "Shanghai, Taipei, Kuala Lumpur, Singapore, Perth, Manila, Makassar, Irkutsk"}, + { 31500, "UTC+08:45", "Australia (Eucla) unofficial"}, + { 32400, "UTC+09:00", "Tokyo, Seoul, Pyongyang, Ambon, Yakutsk"}, + { 34200, "UTC+09:30", "Adelaide"}, + { 36000, "UTC+10:00", "Sydney, Port Moresby, Vladivostok"}, + { 37800, "UTC+10:30", "Lord Howe Island"}, + { 39600, "UTC+11:00", "Nouméa, Magadan"}, + { 43200, "UTC+12:00", "Auckland, Suva, Petropavlovsk-Kamchatsky"}, + { 45900, "UTC+12:45", "Chatham Islands"}, + { 46800, "UTC+13:00", "Kiribati (Phoenix Islands), Tonga, Tokelau"}, + { 50400, "UTC+14:00", "Kiribati (Line Islands)"} +}; + +const uint8_t timeZoneCount = ARRAY_SIZE(timeZones); diff --git a/data/js/app.js b/data/js/app.js index cc5bd5f0..828ed7d5 100644 --- a/data/js/app.js +++ b/data/js/app.js @@ -543,6 +543,8 @@ function postValue(name, value) { "autoplayDuration", "showClock", "clockBackgroundFade", + "timeZone", + "dst", ]; if (oldFieldNames.some((f) => f === name)) { diff --git a/esp8266-fastled-webserver.ino b/esp8266-fastled-webserver.ino index 965d6a58..b12c8b6b 100644 --- a/esp8266-fastled-webserver.ino +++ b/esp8266-fastled-webserver.ino @@ -120,6 +120,8 @@ unsigned long autoPlayTimeout = 0; uint8_t showClock = 0; uint8_t clockBackgroundFade = 240; +uint8_t currentTimeZoneIndex = 0; +uint8_t dst = 0; // daylight saving on/off uint8_t currentPaletteIndex = 0; @@ -286,6 +288,7 @@ PatternAndNameList patterns = { const uint8_t patternCount = ARRAY_SIZE(patterns); +#include "TimeZones.h" #include "Fields.h" void setup() { @@ -511,6 +514,18 @@ void setup() { sendInt(clockBackgroundFade); }); + webServer.on("/timeZone", HTTP_POST, []() { + String value = webServer.arg("value"); + setTimeZone(value.toInt()); + sendInt(currentTimeZoneIndex); + }); + + webServer.on("/dst", HTTP_POST, []() { + String value = webServer.arg("value"); + setDst(value.toInt()); + sendInt(dst); + }); + //list directory webServer.on("/list", HTTP_GET, handleFileList); //load editor @@ -921,6 +936,14 @@ void loadSettings() showClock = EEPROM.read(9); clockBackgroundFade = EEPROM.read(10); + + currentTimeZoneIndex = EEPROM.read(11); + if (currentTimeZoneIndex < 0) + currentTimeZoneIndex = 0; + else if (currentTimeZoneIndex >= timeZoneCount) + currentTimeZoneIndex = timeZoneCount -1; + dst = EEPROM.read(12); + timeClient.setTimeOffset(timeZones[currentTimeZoneIndex].utcOffsetInSeconds + (dst ? 3600 : 0)); } void setPower(uint8_t value)