@@ -45,6 +45,10 @@ static void ParceDay(uint32_t date, uint16_t & year, uint8_t & month, uint8_t &
45
45
weekDay = static_cast <uint8_t >((dayOfYear -1 + weekDayOffeset) % kDaysPerWeek );
46
46
}
47
47
48
+ namespace chip ::app::Clusters::EnergyCalendar {
49
+ static void RestartServer (uint32_t features);
50
+ }
51
+
48
52
#if 0
49
53
static uint64_t GetCurrentDateTime(void)
50
54
{
@@ -93,6 +97,16 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
93
97
DataModel::Nullable<uint32_t > eventID;
94
98
Structs::PeakPeriodStruct::Type peak;
95
99
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
+
96
110
if (root.isMember (" CalendarID" ))
97
111
{
98
112
value = root.get (" CalendarID" , Json::Value ());
@@ -230,33 +244,30 @@ void chip::app::Clusters::EnergyCalendar::CalendarProviderInstance::ErrorMessage
230
244
va_end (v);
231
245
}
232
246
233
- //
234
- #define EQUAL_BYTE (a, offset, expected ) (((a)>>(offset) & 0xFF ) == (uint8_t )(expected))
235
-
236
247
DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay (uint32_t date)
237
248
{
238
249
DataModel::Nullable<Structs::DayStruct::Type> result = std::nullopt;
239
- uint32_t dt;
250
+ Structs::DateStruct::Type dt;
240
251
241
252
uint16_t year;
242
253
uint8_t month;
243
254
uint8_t dayOfMonth;
244
- uint8_t weekDay ;
255
+ uint8_t dayOfWeek ;
245
256
246
- ParceDay (date, year, month, dayOfMonth, weekDay );
257
+ ParceDay (date, year, month, dayOfMonth, dayOfWeek );
247
258
248
259
for (auto & day : mSpecialDays )
249
260
{
250
261
if (day.date .HasValue ())
251
262
{
252
263
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 ))
254
265
continue ;
255
- if (!EQUAL_BYTE (dt, 8 , 0 ) && ! EQUAL_BYTE (dt, 8 , month) )
266
+ if (!dt. month . IsNull ( ) && dt. month . Value () != month)
256
267
continue ;
257
- if (!EQUAL_BYTE (dt, 16 , 0 ) && ! EQUAL_BYTE (dt, 16 , dayOfMonth) )
268
+ if (!dt. day . IsNull ( ) && dt. day . Value () != dayOfMonth)
258
269
continue ;
259
- if (!EQUAL_BYTE (dt, 24 , 0 ) && ! EQUAL_BYTE (dt, 24 , weekDay) )
270
+ if (!dt. dayOfWeek . IsNull ( ) && dt. dayOfWeek . Value () != dayOfWeek )
260
271
continue ;
261
272
return day;
262
273
}
@@ -267,7 +278,6 @@ DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay(u
267
278
return result;
268
279
}
269
280
270
-
271
281
for (auto & period : mCalendarPeriods )
272
282
{
273
283
if (period.startDate .ValueOr (0 ) > date)
@@ -280,7 +290,7 @@ DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay(u
280
290
{
281
291
for (auto & day : period.days )
282
292
{
283
- if (day.daysOfWeek .ValueOr (0 ).Has (static_cast <TransitionDayOfWeekBitmap>(1 << weekDay )))
293
+ if (day.daysOfWeek .ValueOr (0 ).Has (static_cast <TransitionDayOfWeekBitmap>(1 << dayOfWeek )))
284
294
{
285
295
result = day;
286
296
break ;
@@ -313,12 +323,41 @@ void CalendarProviderInstance::JsonToCalendarPeriodStruct(Json::Value & root, St
313
323
}
314
324
}
315
325
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
+
316
353
void CalendarProviderInstance::JsonToDayStruct (Json::Value & root, Structs::DayStruct::Type & value)
317
354
{
318
355
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 ())
320
357
{
321
- value.date .SetValue (t.asUInt ());
358
+ Structs::DateStruct::Type date;
359
+ JsonToDateStruct (t, date);
360
+ value.date .SetValue (date);
322
361
}
323
362
324
363
t = root.get (" DaysOfWeek" , Json::Value ());
@@ -478,6 +517,8 @@ void CalendarProviderInstance::FreeMemoryCalendarPeriodStructList(DataModel::Lis
478
517
479
518
static std::unique_ptr<CalendarProviderInstance> gMIDelegate ;
480
519
static std::unique_ptr<EnergyCalendarServer> gMIInstance ;
520
+ static BitMask<Feature> gMIFeature = BitMask<Feature, uint32_t >(
521
+ Feature::kPricingTier , Feature::kFriendlyCredit , Feature::kAuxiliaryLoad , Feature::kPeakPeriod );
481
522
482
523
void emberAfEnergyCalendarClusterInitCallback (chip::EndpointId endpointId)
483
524
{
@@ -489,8 +530,7 @@ void emberAfEnergyCalendarClusterInitCallback(chip::EndpointId endpointId)
489
530
{
490
531
gMIDelegate ->Init ();
491
532
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 );
494
534
495
535
gMIInstance ->Init ();
496
536
@@ -506,3 +546,20 @@ CalendarProviderInstance * chip::app::Clusters::EnergyCalendar::GetProvider()
506
546
{
507
547
return &(*gMIDelegate );
508
548
}
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
+ }
0 commit comments