Skip to content

Commit

Permalink
day of week events - p2
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Feb 18, 2023
1 parent c88e139 commit 5107961
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 8 deletions.
3 changes: 2 additions & 1 deletion openBeken_win32_mvsc2017.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<ClCompile>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>.\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)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WINDOWS;%(PreprocessorDefinitions);_USE_32BIT_TIME_T </PreprocessorDefinitions>
<MinimalRebuild>true</MinimalRebuild>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
Expand Down Expand Up @@ -525,6 +525,7 @@
<ClCompile Include="src\selftest\selftest_changeHandlers.c" />
<ClCompile Include="src\selftest\selftest_changeHandlers_mqtt.c" />
<ClCompile Include="src\selftest\selftest_cmd_alias.c" />
<ClCompile Include="src\selftest\selftest_cmd_calendar.c" />
<ClCompile Include="src\selftest\selftest_cmd_channels.c" />
<ClCompile Include="src\selftest\selftest_cmd_generic.c" />
<ClCompile Include="src\selftest\selftest_demo_fanCyclingRelays.c" />
Expand Down
3 changes: 3 additions & 0 deletions openBeken_win32_mvsc2017.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,9 @@
<ClCompile Include="src\driver\drv_ntp_events.c">
<Filter>Drv</Filter>
</ClCompile>
<ClCompile Include="src\selftest\selftest_cmd_calendar.c">
<Filter>SelfTest</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\new_cfg.h" />
Expand Down
24 changes: 23 additions & 1 deletion src/driver/drv_ntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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":"",
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/driver/drv_ntp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */

80 changes: 74 additions & 6 deletions src/driver/drv_ntp_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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',
Expand All @@ -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);
}

2 changes: 2 additions & 0 deletions src/obk_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -62,6 +63,7 @@
#define ENABLE_DRIVER_TUYAMCU 1
#define ENABLE_I2C 1
#define ENABLE_TEST_COMMANDS 1
#define ENABLE_CALENDAR_EVENTS 0

#else

Expand Down
39 changes: 39 additions & 0 deletions src/selftest/selftest_cmd_calendar.c
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/selftest/selftest_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5107961

Please sign in to comment.