Skip to content

Commit

Permalink
add sunset/sunrise windows self test
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Dec 19, 2023
1 parent b4a331e commit bc580f0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/driver/drv_ntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ static int adrLen;
static int g_ntp_delay = 0;
static bool g_synced;
// time offset (time zone?) in seconds
#define CFG_DEFAULT_TIMEOFFSETSECONDS (-8 * 60 * 60)
static int g_timeOffsetSeconds = CFG_DEFAULT_TIMEOFFSETSECONDS;
//#define CFG_DEFAULT_TIMEOFFSETSECONDS (-8 * 60 * 60)
static int g_timeOffsetSeconds = 0;
// current time
unsigned int g_ntpTime;

Expand Down Expand Up @@ -108,11 +108,12 @@ commandResult_t NTP_SetTimeZoneOfs(const void *context, const char *cmd, const c
/* sunrise/sunset defaults */
#define CFG_DEFAULT_LATITUDE 43.994131
#define CFG_DEFAULT_LONGITUDE -123.095854
#define SUN_DATA_COORD_MULT 1000000

struct SUN_DATA sun_data =
{
.latitude = (int) (CFG_DEFAULT_LATITUDE * 1000000),
.longitude = (int) (CFG_DEFAULT_LONGITUDE * 1000000),
.latitude = (int) (CFG_DEFAULT_LATITUDE * SUN_DATA_COORD_MULT),
.longitude = (int) (CFG_DEFAULT_LONGITUDE * SUN_DATA_COORD_MULT),
};

//Set Latitude and Longitude for sunrise/sunset calc
Expand All @@ -127,11 +128,11 @@ commandResult_t NTP_SetLatlong(const void *context, const char *cmd, const char
return CMD_RES_NOT_ENOUGH_ARGUMENTS;
}
newValue = Tokenizer_GetArg(0);
sun_data.latitude = (int) (atof(newValue) * 1000000);
sun_data.latitude = (int) (atof(newValue) * SUN_DATA_COORD_MULT);
addLogAdv(LOG_INFO, LOG_FEATURE_NTP, "NTP latitude set to %s", newValue);

newValue = Tokenizer_GetArg(1);
sun_data.longitude = (int) (atof(newValue) * 1000000);
sun_data.longitude = (int) (atof(newValue) * SUN_DATA_COORD_MULT);
addLogAdv(LOG_INFO, LOG_FEATURE_NTP, "NTP longitude set to %s", newValue);
return CMD_RES_OK;
}
Expand Down
6 changes: 6 additions & 0 deletions src/driver/drv_ntp_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ static int calc_day_offset(int tm_wday, int weekDayFlags)
}
#endif

void NTP_CalculateSunrise(byte *outHour, byte *outMinute) {
dusk2Dawn(&sun_data, SUNRISE_FLAG, outHour, outMinute, 0);
}
void NTP_CalculateSunset(byte *outHour, byte *outMinute) {
dusk2Dawn(&sun_data, SUNSET_FLAG, outHour, outMinute, 0);
}
void NTP_RunEventsForSecond(unsigned int runTime) {
ntpEvent_t *e;
struct tm *ltm;
Expand Down
3 changes: 3 additions & 0 deletions src/selftest/selftest_cmd_calendar.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ void Test_Commands_Calendar() {
SIM_ClearOBK(0);

CMD_ExecuteCommand("startDriver NTP", 0);
//CMD_ExecuteCommand("ntp_timeZoneOfs 0", 0);
// set 2022, 06, 10, 11:27:34, Friday
NTP_SetSimulatedTime(1654853254);

Expand Down Expand Up @@ -58,6 +59,8 @@ void Test_Commands_Calendar() {
SELFTEST_ASSERT_CHANNEL(3, (312+88));




}


Expand Down
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_RepeatingEvents();
void Test_HTTP_Client();
void Test_DeviceGroups();
void Test_NTP();
void Test_NTP_SunsetSunrise();
void Test_MQTT();
void Test_Tasmota();
void Test_EnergyMeter();
Expand Down
1 change: 1 addition & 0 deletions src/selftest/selftest_ntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void Test_NTP() {
SELFTEST_ASSERT_FLOATCOMPARE(NTP_GetTimesZoneOfsSeconds(), -(12 * 60 * 60 + 5 * 60));



}


Expand Down
44 changes: 44 additions & 0 deletions src/selftest/selftest_ntp_sunsetSunrise.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifdef WINDOWS

#include "selftest_local.h"

void Test_NTP_SunsetSunrise() {
byte hour, minute;

// reset whole device
SIM_ClearOBK(0);

// setup test case - Serbia
CMD_ExecuteCommand("startDriver NTP", 0);
CMD_ExecuteCommand("ntp_timeZoneOfs 1", 0);
CMD_ExecuteCommand("ntp_setLatlong 44.272563 19.900690", 0);
// set Tue, 19 Dec 2023 20:14:52
NTP_SetSimulatedTime(1703016892);
NTP_CalculateSunrise(&hour, &minute);
// Expect sunrise at 7:11
SELFTEST_ASSERT(hour == 7);
SELFTEST_ASSERT(minute == 11);
NTP_CalculateSunset(&hour, &minute);
// Expect sunset at 16:02
SELFTEST_ASSERT(hour == 16);
SELFTEST_ASSERT(minute == 2);


// setup test case - Los Angeles
CMD_ExecuteCommand("ntp_timeZoneOfs -8", 0);
CMD_ExecuteCommand("ntp_setLatlong 34.052235 -118.243683", 0);

// set Tue, 19 Dec 2023 20:14:52
NTP_SetSimulatedTime(1703016892);
NTP_CalculateSunrise(&hour, &minute);
// Expect sunrise at 6:52
SELFTEST_ASSERT(hour == 6);
SELFTEST_ASSERT(minute == 52);
NTP_CalculateSunset(&hour, &minute);
// Expect sunset at 16:462
SELFTEST_ASSERT(hour == 16);
SELFTEST_ASSERT(minute == 45);
}


#endif

0 comments on commit bc580f0

Please sign in to comment.