From 5107961d7315cbcec64208d146cf3fca84f80968 Mon Sep 17 00:00:00 2001 From: openshwprojects Date: Sat, 18 Feb 2023 13:38:11 +0100 Subject: [PATCH] day of week events - p2 --- openBeken_win32_mvsc2017.vcxproj | 3 +- openBeken_win32_mvsc2017.vcxproj.filters | 3 + src/driver/drv_ntp.c | 24 ++++++- src/driver/drv_ntp.h | 2 + src/driver/drv_ntp_events.c | 80 ++++++++++++++++++++++-- src/obk_config.h | 2 + src/selftest/selftest_cmd_calendar.c | 39 ++++++++++++ src/selftest/selftest_local.h | 1 + 8 files changed, 146 insertions(+), 8 deletions(-) create mode 100644 src/selftest/selftest_cmd_calendar.c diff --git a/openBeken_win32_mvsc2017.vcxproj b/openBeken_win32_mvsc2017.vcxproj index 16115ff23..003a80226 100644 --- a/openBeken_win32_mvsc2017.vcxproj +++ b/openBeken_win32_mvsc2017.vcxproj @@ -149,7 +149,7 @@ Disabled .\libs_for_simulator\SDL2-2.24.2\include;.\src\win32\stubs\;.\libs_for_simulator\DevIL-Windows-SDK-1.8.0\include;.\libs_for_simulator\freeglut-MSVC-3.0.0-2.mp\include;.\libs_for_simulator\nativefiledialog\src\include;%(AdditionalIncludeDirectories) - WINDOWS;%(PreprocessorDefinitions) + WINDOWS;%(PreprocessorDefinitions);_USE_32BIT_TIME_T true EnableFastChecks MultiThreadedDebugDLL @@ -525,6 +525,7 @@ + diff --git a/openBeken_win32_mvsc2017.vcxproj.filters b/openBeken_win32_mvsc2017.vcxproj.filters index f7e9a3820..0d771ffce 100644 --- a/openBeken_win32_mvsc2017.vcxproj.filters +++ b/openBeken_win32_mvsc2017.vcxproj.filters @@ -516,6 +516,9 @@ Drv + + SelfTest + diff --git a/src/driver/drv_ntp.c b/src/driver/drv_ntp.c index b5d9f70b4..52a5c0ca9 100644 --- a/src/driver/drv_ntp.c +++ b/src/driver/drv_ntp.c @@ -118,8 +118,20 @@ commandResult_t NTP_Info(const void *context, const char *cmd, const char *args, return CMD_RES_OK; } +#if WINDOWS +bool b_ntp_simulatedTime = false; +void NTP_SetSimulatedTime(unsigned int timeNow) { + g_time = timeNow; + g_time += g_timeOffsetSeconds; + g_synced = true; + b_ntp_simulatedTime = true; +} +#endif void NTP_Init() { +#if WINDOWS + b_ntp_simulatedTime = false; +#endif //cmddetail:{"name":"ntp_timeZoneOfs","args":"[Value]", //cmddetail:"descr":"Sets the time zone offset in hours. Also supports HH:MM syntax if you want to specify value in minutes. For negative values, use -HH:MM syntax, for example -5:30 will shift time by 5 hours and 30 minutes negative.", //cmddetail:"fn":"NTP_SetTimeZoneOfs","file":"driver/drv_ntp.c","requires":"", @@ -136,6 +148,11 @@ void NTP_Init() { //cmddetail:"examples":""} CMD_RegisterCommand("ntp_info", NTP_Info, NULL); +#if ENABLE_CALENDAR_EVENTS + NTP_Init_Events(); +#endif + + addLogAdv(LOG_INFO, LOG_FEATURE_NTP, "NTP driver initialized with server=%s, offset=%d", CFG_GetNTPServer(), g_timeOffsetSeconds); g_synced = false; } @@ -289,16 +306,21 @@ void NTP_SendRequest_BlockingMode() { } - void NTP_OnEverySecond() { g_time++; +#if ENABLE_CALENDAR_EVENTS + NTP_RunEvents(g_time, g_synced); +#endif if(Main_IsConnectedToWiFi()==0) { return; } #if WINDOWS + if (b_ntp_simulatedTime) { + return; + } #elif PLATFORM_BL602 #elif PLATFORM_W600 || PLATFORM_W800 #elif PLATFORM_XR809 diff --git a/src/driver/drv_ntp.h b/src/driver/drv_ntp.h index 6c3a2a017..86828e946 100644 --- a/src/driver/drv_ntp.h +++ b/src/driver/drv_ntp.h @@ -11,6 +11,8 @@ unsigned int NTP_GetCurrentTimeWithoutOffset(); void NTP_AppendInformationToHTTPIndexPage(http_request_t* request); bool NTP_IsTimeSynced(); int NTP_GetTimesZoneOfsSeconds(); +// for Simulator only, on Windows, for unit testing +void NTP_SetSimulatedTime(unsigned int timeNow); #endif /* __DRV_NTP_H__ */ diff --git a/src/driver/drv_ntp_events.c b/src/driver/drv_ntp_events.c index 54727fde9..a1bb52f89 100644 --- a/src/driver/drv_ntp_events.c +++ b/src/driver/drv_ntp_events.c @@ -32,8 +32,13 @@ void NTP_RunEventsForSecond(unsigned int runTime) { ntpEvent_t *e; struct tm *ltm; + // NOTE: on windows, you need _USE_32BIT_TIME_T ltm = localtime((time_t*)&runTime); + if (ltm == 0) { + return; + } + e = ntp_events; while (e) { @@ -80,15 +85,54 @@ void NTP_RunEvents(unsigned int newTime, bool bTimeValid) { } ntp_eventsTime = newTime; } +void NTP_AddClockEvent(int hour, int minute, int second, int weekDayFlags, int id, const char* command) { + ntpEvent_t* newEvent = (ntpEvent_t*)malloc(sizeof(ntpEvent_t)); + if (newEvent == NULL) { + // handle error + return; + } + + newEvent->hour = hour; + newEvent->minute = minute; + newEvent->second = second; + newEvent->weekDayFlags = weekDayFlags; + newEvent->id = id; + newEvent->command = strdup(command); + newEvent->next = ntp_events; + + ntp_events = newEvent; +} +void NTP_RemoveClockEvent(int id) { + ntpEvent_t* curr = ntp_events; + ntpEvent_t* prev = NULL; + + while (curr != NULL) { + if (curr->id == id) { + if (prev == NULL) { + ntp_events = curr->next; + } + else { + prev->next = curr->next; + } + free(curr->command); + free(curr); + return; + } + prev = curr; + curr = curr->next; + } +} // addClockEvent [Time] [WeekDayFlags] [UniqueIDForRemoval] // Bit flag 0 - sunday, bit flag 1 - monday, etc... // Example: do event on 15:30 every weekday, unique ID 123 // addClockEvent 15:30:00 0xff 123 POWER0 ON // Example: do event on 8:00 every sunday // addClockEvent 8:00:00 0x01 234 backlog led_temperature 500; led_dimmer 100; led_enableAll 1; - -commandResult_t NTP_AddClockEvent(const void *context, const char *cmd, const char *args, int cmdFlags) { +commandResult_t CMD_NTP_AddClockEvent(const void *context, const char *cmd, const char *args, int cmdFlags) { int hour, minute, second; + const char *s; + int flags; + int id; Tokenizer_TokenizeString(args, 0); // following check must be done after 'Tokenizer_TokenizeString', @@ -97,18 +141,42 @@ commandResult_t NTP_AddClockEvent(const void *context, const char *cmd, const ch if (Tokenizer_CheckArgsCountAndPrintWarning(cmd, 4)) { return CMD_RES_NOT_ENOUGH_ARGUMENTS; } + s = Tokenizer_GetArg(0); + if (sscanf(s, "%i:%i:%i", &hour, &minute, &second) <= 1) { + return CMD_RES_BAD_ARGUMENT; + } + flags = Tokenizer_GetArgInteger(1); + id = Tokenizer_GetArgInteger(2); + s = Tokenizer_GetArgFrom(3); + + NTP_AddClockEvent(hour, minute, second, flags, id, s); return CMD_RES_OK; } -commandResult_t NTP_RemoveClockEvent(const void *context, const char *cmd, const char *args, int cmdFlags) { +commandResult_t CMD_NTP_RemoveClockEvent(const void* context, const char* cmd, const char* args, int cmdFlags) { + int id; + + // tokenize the args string + Tokenizer_TokenizeString(args, 0); + + // Check the number of arguments + if (Tokenizer_CheckArgsCountAndPrintWarning(cmd, 1)) { + return CMD_RES_NOT_ENOUGH_ARGUMENTS; + } + + // Parse the id parameter + id = Tokenizer_GetArgInteger(0); + + // Remove the clock event with the given id + NTP_RemoveClockEvent(id); return CMD_RES_OK; } -} + void NTP_Init_Events() { - CMD_RegisterCommand("addClockEvent",NTP_AddClockEvent, NULL); - CMD_RegisterCommand("removeClockEvent", NTP_RemoveClockEvent, NULL); + CMD_RegisterCommand("addClockEvent",CMD_NTP_AddClockEvent, NULL); + CMD_RegisterCommand("removeClockEvent", CMD_NTP_RemoveClockEvent, NULL); } diff --git a/src/obk_config.h b/src/obk_config.h index 1241779df..b27d99b32 100644 --- a/src/obk_config.h +++ b/src/obk_config.h @@ -39,6 +39,7 @@ #define ENABLE_DRIVER_CSE7766 1 #define ENABLE_DRIVER_TUYAMCU 1 #define ENABLE_TEST_COMMANDS 1 +#define ENABLE_CALENDAR_EVENTS 1 #elif PLATFORM_BL602 @@ -62,6 +63,7 @@ #define ENABLE_DRIVER_TUYAMCU 1 #define ENABLE_I2C 1 #define ENABLE_TEST_COMMANDS 1 +#define ENABLE_CALENDAR_EVENTS 0 #else diff --git a/src/selftest/selftest_cmd_calendar.c b/src/selftest/selftest_cmd_calendar.c new file mode 100644 index 000000000..f3e5491db --- /dev/null +++ b/src/selftest/selftest_cmd_calendar.c @@ -0,0 +1,39 @@ +#ifdef WINDOWS + +#include "selftest_local.h". + +void Test_Commands_Calendar() { + // reset whole device + SIM_ClearOBK(); + + CMD_ExecuteCommand("startDriver NTP", 0); + // set 2022, 06, 10, 11:27:34, Friday + NTP_SetSimulatedTime(1654853254); + + SELFTEST_ASSERT_CHANNEL(1, 0); + + // every day + CMD_ExecuteCommand("addClockEvent 11:28:00 0xff 123 addChannel 1 54", 0); + // sunday + CMD_ExecuteCommand("addClockEvent 11:28:30 0x01 234 addChannel 2 12", 0); + // friday + CMD_ExecuteCommand("addClockEvent 11:28:30 32 456 addChannel 3 312", 0); + // friday + CMD_ExecuteCommand("addClockEvent 11:29:00 32 654 addChannel 3 88", 0); + Sim_RunSeconds(30, false); + + SELFTEST_ASSERT_CHANNEL(1, 54); + Sim_RunSeconds(30, false); + SELFTEST_ASSERT_CHANNEL(1, 54); + SELFTEST_ASSERT_CHANNEL(2, 0); + SELFTEST_ASSERT_CHANNEL(3, 312); + Sim_RunSeconds(30, false); + SELFTEST_ASSERT_CHANNEL(1, 54); + SELFTEST_ASSERT_CHANNEL(2, 0); + SELFTEST_ASSERT_CHANNEL(3, (312+88)); + + +} + + +#endif diff --git a/src/selftest/selftest_local.h b/src/selftest/selftest_local.h index d8dc24076..ad41510cc 100644 --- a/src/selftest/selftest_local.h +++ b/src/selftest/selftest_local.h @@ -88,6 +88,7 @@ void Test_Role_ToggleAll(); void Test_Demo_SimpleShuttersScript(); void Test_Commands_Generic(); void Test_ChangeHandlers_MQTT(); +void Test_Commands_Calendar(); void Test_GetJSONValue_Setup(const char *text); void Test_FakeHTTPClientPacket_GET(const char *tg);