Skip to content

Commit 14beb82

Browse files
committed
Used a bitmap rather than uint8_t and sync EnergyTimeUtils files from the EVSE_Add_Get_Set_Clear_Targets_Support branch
1 parent 1c5e33d commit 14beb82

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

examples/energy-management-app/energy-management-common/include/EnergyTimeUtils.h

+16-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
#pragma once
2020

21+
#include <app-common/zap-generated/cluster-enums.h>
2122
#include <app/util/config.h>
2223
#include <cstring>
2324
#include <lib/core/CHIPError.h>
25+
#include <lib/support/BitMask.h>
2426
#include <protocols/Protocols.h>
2527
#include <time.h>
2628

@@ -45,16 +47,26 @@ CHIP_ERROR GetEpochTS(uint32_t & chipEpoch);
4547
*
4648
* @param unixEpoch (as time_t)
4749
*
48-
* @return bitmap value for day of week as defined by EnergyEvse::TargetDayOfWeekBitmap
50+
* @return bitmap value for day of week as defined by EnergyEvse::TargetDayOfWeekBitmap. Note
51+
* only one bit will be set for the day of the week.
4952
*/
50-
uint8_t GetLocalDayOfWeekFromUnixEpoch(time_t unixEpoch);
53+
BitMask<EnergyEvse::TargetDayOfWeekBitmap> GetLocalDayOfWeekFromUnixEpoch(time_t unixEpoch);
5154

5255
/**
5356
* @brief Helper function to get current timestamp and work out the day of week based on localtime
5457
*
55-
* @param reference to hold the day of week as a bitmap as defined by EnergyEvse::TargetDayOfWeekBitmap
58+
* @param reference to hold the day of week as a bitmap as defined by EnergyEvse::TargetDayOfWeekBitmap.
59+
* Note only one bit will be set for the current day.
5660
*/
57-
CHIP_ERROR GetLocalDayOfWeekNow(uint8_t & dayOfWeekMap);
61+
CHIP_ERROR GetLocalDayOfWeekNow(BitMask<EnergyEvse::TargetDayOfWeekBitmap> & dayOfWeekMap);
62+
63+
/**
64+
* @brief Helper function to get current timestamp and work out the current number of minutes
65+
* past midnight based on localtime
66+
*
67+
* @param reference to hold the number of minutes past midnight
68+
*/
69+
CHIP_ERROR GetMinutesPastMidnight(uint16_t & minutesPastMidnight);
5870

5971
} // namespace DeviceEnergyManagement
6072
} // namespace Clusters

examples/energy-management-app/energy-management-common/src/EnergyTimeUtils.cpp

+33-4
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ CHIP_ERROR GetEpochTS(uint32_t & chipEpoch)
8080
*
8181
* @param unixEpoch (as time_t)
8282
*
83-
* @return bitmap value for day of week as defined by EnergyEvse::TargetDayOfWeekBitmap
83+
* @return bitmap value for day of week as defined by EnergyEvse::TargetDayOfWeekBitmap. Note
84+
* only one bit will be set for the day of the week.
8485
*/
85-
uint8_t GetLocalDayOfWeekFromUnixEpoch(time_t unixEpoch)
86+
BitMask<EnergyEvse::TargetDayOfWeekBitmap> GetLocalDayOfWeekFromUnixEpoch(time_t unixEpoch)
8687
{
8788
// Define a timezone structure and initialize it to the local timezone
8889
// This will capture any daylight saving time changes
@@ -101,9 +102,10 @@ uint8_t GetLocalDayOfWeekFromUnixEpoch(time_t unixEpoch)
101102
/**
102103
* @brief Helper function to get current timestamp and work out the day of week based on localtime
103104
*
104-
* @param reference to hold the day of week as a bitmap as defined by EnergyEvse::TargetDayOfWeekBitmap
105+
* @return bitmap value for day of week as defined by EnergyEvse::TargetDayOfWeekBitmap. Note
106+
* only one bit will be set for the current day.
105107
*/
106-
CHIP_ERROR GetLocalDayOfWeekNow(uint8_t & dayOfWeekMap)
108+
CHIP_ERROR GetLocalDayOfWeekNow(BitMask<EnergyEvse::TargetDayOfWeekBitmap> & dayOfWeekMap)
107109
{
108110
System::Clock::Milliseconds64 cTMs;
109111
CHIP_ERROR err = chip::System::SystemClock().GetClock_RealTimeMS(cTMs);
@@ -118,6 +120,33 @@ CHIP_ERROR GetLocalDayOfWeekNow(uint8_t & dayOfWeekMap)
118120
return CHIP_NO_ERROR;
119121
}
120122

123+
/**
124+
* @brief Helper function to get current timestamp and work out the current number of minutes
125+
* past midnight based on localtime
126+
*
127+
* @param reference to hold the number of minutes past midnight
128+
*/
129+
CHIP_ERROR GetMinutesPastMidnight(uint16_t & minutesPastMidnight)
130+
{
131+
chip::System::Clock::Milliseconds64 cTMs;
132+
CHIP_ERROR err = chip::System::SystemClock().GetClock_RealTimeMS(cTMs);
133+
if (err != CHIP_NO_ERROR)
134+
{
135+
ChipLogError(Zcl, "EVSE: unable to get current time to check user schedules error=%" CHIP_ERROR_FORMAT, err.Format());
136+
return err;
137+
}
138+
time_t unixEpoch = std::chrono::duration_cast<chip::System::Clock::Seconds32>(cTMs).count();
139+
140+
// Define a timezone structure and initialize it to the local timezone
141+
// This will capture any daylight saving time changes
142+
struct tm local_time;
143+
localtime_r(&unixEpoch, &local_time);
144+
145+
minutesPastMidnight = static_cast<uint16_t>((local_time.tm_hour * 60) + local_time.tm_min);
146+
147+
return err;
148+
}
149+
121150
} // namespace DeviceEnergyManagement
122151
} // namespace Clusters
123152
} // namespace app

0 commit comments

Comments
 (0)