Skip to content

Commit bf58d13

Browse files
Set Features over Json
Support Date type in DayStruct
1 parent b6f74f8 commit bf58d13

File tree

3 files changed

+77
-19
lines changed

3 files changed

+77
-19
lines changed

examples/all-clusters-app/all-clusters-common/include/energy-calendar-instance.h

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class CalendarProviderInstance : public CalendarProvider
6767
DataModel::Nullable<Structs::PeakPeriodStruct::Type> mNextPeak;
6868

6969
void JsonToCalendarPeriodStruct(Json::Value & root, Structs::CalendarPeriodStruct::Type & value);
70+
void JsonToDateStruct(Json::Value & root, Structs::DateStruct::Type & value);
7071
void JsonToDayStruct(Json::Value & root, Structs::DayStruct::Type & value);
7172
void JsonToPeakPeriodStruct(Json::Value & root, Structs::PeakPeriodStruct::Type & value);
7273

examples/all-clusters-app/all-clusters-common/src/energy-calendar-instance.cpp

+73-16
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ static void ParceDay(uint32_t date, uint16_t & year, uint8_t & month, uint8_t &
4545
weekDay = static_cast<uint8_t>((dayOfYear -1 + weekDayOffeset) % kDaysPerWeek);
4646
}
4747

48+
namespace chip::app::Clusters::EnergyCalendar {
49+
static void RestartServer(uint32_t features);
50+
}
51+
4852
#if 0
4953
static uint64_t GetCurrentDateTime(void)
5054
{
@@ -93,6 +97,16 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
9397
DataModel::Nullable<uint32_t> eventID;
9498
Structs::PeakPeriodStruct::Type peak;
9599

100+
if (root.isMember("Features"))
101+
{
102+
value = root.get("Features", Json::Value());
103+
if (value.isUInt() && value.asUInt() <= 0x0F)
104+
{
105+
RestartServer(value.asUInt());
106+
return CHIP_NO_ERROR;
107+
}
108+
}
109+
96110
if (root.isMember("CalendarID"))
97111
{
98112
value = root.get("CalendarID", Json::Value());
@@ -230,33 +244,30 @@ void chip::app::Clusters::EnergyCalendar::CalendarProviderInstance::ErrorMessage
230244
va_end(v);
231245
}
232246

233-
//
234-
#define EQUAL_BYTE(a, offset, expected) (((a)>>(offset) & 0xFF) == (uint8_t)(expected))
235-
236247
DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay(uint32_t date)
237248
{
238249
DataModel::Nullable<Structs::DayStruct::Type> result = std::nullopt;
239-
uint32_t dt;
250+
Structs::DateStruct::Type dt;
240251

241252
uint16_t year;
242253
uint8_t month;
243254
uint8_t dayOfMonth;
244-
uint8_t weekDay;
255+
uint8_t dayOfWeek;
245256

246-
ParceDay(date, year, month, dayOfMonth, weekDay);
257+
ParceDay(date, year, month, dayOfMonth, dayOfWeek);
247258

248259
for (auto & day : mSpecialDays)
249260
{
250261
if (day.date.HasValue())
251262
{
252263
dt = day.date.Value();
253-
if (!EQUAL_BYTE(dt, 0, 0xFF) && !EQUAL_BYTE(dt, 0, year - 1900))
264+
if (!dt.year.IsNull() && dt.year.Value() != (uint8_t)(year - 1900))
254265
continue;
255-
if (!EQUAL_BYTE(dt, 8, 0) && !EQUAL_BYTE(dt, 8, month))
266+
if (!dt.month.IsNull() && dt.month.Value() != month)
256267
continue;
257-
if (!EQUAL_BYTE(dt, 16, 0) && !EQUAL_BYTE(dt, 16, dayOfMonth))
268+
if (!dt.day.IsNull() && dt.day.Value() != dayOfMonth)
258269
continue;
259-
if (!EQUAL_BYTE(dt, 24, 0) && !EQUAL_BYTE(dt, 24, weekDay))
270+
if (!dt.dayOfWeek.IsNull() && dt.dayOfWeek.Value() != dayOfWeek)
260271
continue;
261272
return day;
262273
}
@@ -267,7 +278,6 @@ DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay(u
267278
return result;
268279
}
269280

270-
271281
for (auto & period : mCalendarPeriods)
272282
{
273283
if (period.startDate.ValueOr(0) > date)
@@ -280,7 +290,7 @@ DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay(u
280290
{
281291
for (auto & day : period.days)
282292
{
283-
if (day.daysOfWeek.ValueOr(0).Has(static_cast<TransitionDayOfWeekBitmap>(1 << weekDay)))
293+
if (day.daysOfWeek.ValueOr(0).Has(static_cast<TransitionDayOfWeekBitmap>(1 << dayOfWeek)))
284294
{
285295
result = day;
286296
break;
@@ -313,12 +323,41 @@ void CalendarProviderInstance::JsonToCalendarPeriodStruct(Json::Value & root, St
313323
}
314324
}
315325

326+
void CalendarProviderInstance::JsonToDateStruct(Json::Value & root, Structs::DateStruct::Type & value)
327+
{
328+
Json::Value t = root.get("Year", Json::Value());
329+
if (!t.empty() && t.isUInt() && t.asUInt() <= 0xff)
330+
{
331+
value.year.SetNonNull(t.asUInt());
332+
}
333+
334+
t = root.get("Month", Json::Value());
335+
if (!t.empty() && t.isUInt() && t.asUInt() >= 1 && t.asUInt() <= 12)
336+
{
337+
value.month.SetNonNull(t.asUInt());
338+
}
339+
340+
t = root.get("Day", Json::Value());
341+
if (!t.empty() && t.isUInt() && t.asUInt() >= 1 && t.asUInt() <= 31)
342+
{
343+
value.day.SetNonNull(t.asUInt());
344+
}
345+
346+
t = root.get("DayOfWeek", Json::Value());
347+
if (!t.empty() && t.isUInt() && t.asUInt() >= 1 && t.asUInt() <= 37)
348+
{
349+
value.dayOfWeek.SetNonNull(t.asUInt());
350+
}
351+
}
352+
316353
void CalendarProviderInstance::JsonToDayStruct(Json::Value & root, Structs::DayStruct::Type & value)
317354
{
318355
Json::Value t = root.get("Date", Json::Value());
319-
if (!t.empty() && t.isUInt() && t.asUInt() < static_cast<uint32_t>(-1))
356+
if (!t.empty())
320357
{
321-
value.date.SetValue(t.asUInt());
358+
Structs::DateStruct::Type date;
359+
JsonToDateStruct(t, date);
360+
value.date.SetValue(date);
322361
}
323362

324363
t = root.get("DaysOfWeek", Json::Value());
@@ -478,6 +517,8 @@ void CalendarProviderInstance::FreeMemoryCalendarPeriodStructList(DataModel::Lis
478517

479518
static std::unique_ptr<CalendarProviderInstance> gMIDelegate;
480519
static std::unique_ptr<EnergyCalendarServer> gMIInstance;
520+
static BitMask<Feature> gMIFeature = BitMask<Feature, uint32_t>(
521+
Feature::kPricingTier, Feature::kFriendlyCredit, Feature::kAuxiliaryLoad, Feature::kPeakPeriod);
481522

482523
void emberAfEnergyCalendarClusterInitCallback(chip::EndpointId endpointId)
483524
{
@@ -489,8 +530,7 @@ void emberAfEnergyCalendarClusterInitCallback(chip::EndpointId endpointId)
489530
{
490531
gMIDelegate->Init();
491532

492-
gMIInstance = std::make_unique<EnergyCalendarServer>(BitMask<Feature, uint32_t>(
493-
Feature::kPricingTier, Feature::kFriendlyCredit, Feature::kAuxiliaryLoad, Feature::kPeakPeriod));
533+
gMIInstance = std::make_unique<EnergyCalendarServer>(gMIFeature);
494534

495535
gMIInstance->Init();
496536

@@ -506,3 +546,20 @@ CalendarProviderInstance * chip::app::Clusters::EnergyCalendar::GetProvider()
506546
{
507547
return &(*gMIDelegate);
508548
}
549+
550+
void chip::app::Clusters::EnergyCalendar::RestartServer(uint32_t features)
551+
{
552+
gMIFeature = static_cast<BitMask<Feature>>(features);
553+
554+
VerifyOrDie(gMIInstance);
555+
gMIInstance.reset();
556+
557+
gMIInstance = std::make_unique<EnergyCalendarServer>(gMIFeature);
558+
gMIInstance->Init();
559+
560+
CHIP_ERROR err = gMIInstance->AddCalendarProvider(&(*gMIDelegate));
561+
if (err != CHIP_NO_ERROR)
562+
{
563+
ChipLogError(NotSpecified, "Failed to add Calendar provider: %s", err.AsString());
564+
}
565+
}

src/app/clusters/energy-calendar-server/energy-calendar-server.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -303,19 +303,19 @@ bool CalendarProvider::CheckPeriods(DataModel::List<Structs::CalendarPeriodStruc
303303

304304
bool CalendarProvider::CheckSpecialDays(DataModel::List<Structs::DayStruct::Type> & specialDays)
305305
{
306-
uint32_t date = 0;
306+
//uint32_t date = 0;
307307
VerifyOrReturnLogSend(specialDays.size() <= 50, mEndpoint, "Special Days list size must be no more 50");
308308
for (auto & day : specialDays)
309309
{
310310
VerifyOrReturnLogSend(!day.daysOfWeek.HasValue(), mEndpoint, "Day in Special Days cannot have DayOfWeek value");
311311
VerifyOrReturnLogSend(!day.calendarID.HasValue(), mEndpoint, "Day in Special Days cannot have CalendarID value");
312312
VerifyOrReturnLogSend(day.date.HasValue(), mEndpoint, "Day in Special Days must have Date value");
313-
VerifyOrReturnLogSend(day.date.Value() > date, mEndpoint, "Days in Special Days must be order");
313+
//VerifyOrReturnLogSend(day.date.Value() > date, mEndpoint, "Days in Special Days must be order");
314314
if(!CheckDay(day))
315315
{
316316
return false;
317317
}
318-
date = day.date.Value();
318+
//date = day.date.Value();
319319
}
320320

321321
return true;

0 commit comments

Comments
 (0)