21
21
#include < app/util/attribute-storage.h>
22
22
23
23
#include " energy-calendar-instance.h"
24
+ #include < lib/support/logging/TextOnlyLogging.h>
24
25
25
26
using namespace chip ;
26
27
using namespace chip ::app;
@@ -31,12 +32,22 @@ using namespace chip::app::Clusters::EnergyCalendar::Attributes;
31
32
32
33
constexpr uint32_t kOneDay = 24 * 60 * 60 ;
33
34
34
- static TransitionDayOfWeekBitmap GetWeekDate ( uint64_t date)
35
+ static TransitionDayOfWeekBitmap GetDayOfWeek ( uint32_t date)
35
36
{
36
- tm calendarTime{};
37
- time_t dt = date;
38
- localtime_r (&dt, &calendarTime);
39
- return (TransitionDayOfWeekBitmap) (calendarTime.tm_wday );
37
+ uint16_t year;
38
+ uint8_t month;
39
+ uint8_t dayOfMonth;
40
+ uint8_t hour;
41
+ uint8_t minute;
42
+ uint8_t second;
43
+ uint16_t dayOfYear;
44
+
45
+ ChipEpochToCalendarTime (date, year, month, dayOfMonth, hour, minute, second);
46
+ uint8_t weekDayOffeset = FirstWeekdayOfYear (year);
47
+ CalendarDateToOrdinalDate (year, month, dayOfMonth, dayOfYear);
48
+ uint8_t weekDay = static_cast <uint8_t >((dayOfYear -1 + weekDayOffeset) % kDaysPerWeek );
49
+
50
+ return static_cast <TransitionDayOfWeekBitmap>(1 << weekDay);
40
51
}
41
52
42
53
#if 0
@@ -63,6 +74,7 @@ chip::app::Clusters::EnergyCalendar::CalendarProviderInstance::~CalendarProvider
63
74
64
75
void CalendarProviderInstance::Init (void )
65
76
{
77
+ ChipLogProgress (Zcl, " CalendarProviderInstance::Init" );
66
78
SetDefault ();
67
79
}
68
80
@@ -91,12 +103,14 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
91
103
DataModel::Nullable<uint32_t > eventID;
92
104
Structs::PeakPeriodStruct::Type peak;
93
105
106
+ ChipLogProgress (Zcl, " CalendarProviderInstance::LoadJson" );
94
107
if (root.isMember (" CalendarID" ))
95
108
{
109
+ ChipLogProgress (Zcl, " CalendarProviderInstance::LoadJson have CalendaID" );
96
110
value = root.get (" CalendarID" , Json::Value ());
97
- if (value.isInt ())
111
+ if (value.isUInt ())
98
112
{
99
- calendarID.SetNonNull (value.asInt ());
113
+ calendarID.SetNonNull (value.asUInt ());
100
114
}
101
115
else
102
116
{
@@ -115,7 +129,7 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
115
129
}
116
130
if (value.isString ())
117
131
{
118
- size_t len = value.asString ().size () + 1 ;
132
+ size_t len = value.asString ().size ();
119
133
char * str = (char *) chip::Platform::MemoryAlloc (len);
120
134
memcpy (str, value.asCString (), len);
121
135
CharSpan nameString (str, len);
@@ -127,9 +141,9 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
127
141
if (root.isMember (" ProviderID" ))
128
142
{
129
143
value = root.get (" ProviderID" , Json::Value ());
130
- if (value.isInt ())
144
+ if (value.isUInt ())
131
145
{
132
- providerID.SetNonNull (value.asInt ());
146
+ providerID.SetNonNull (value.asUInt ());
133
147
}
134
148
else
135
149
{
@@ -143,9 +157,9 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
143
157
if (root.isMember (" StartDate" ))
144
158
{
145
159
value = root.get (" StartDate" , Json::Value ());
146
- if (value.isInt ())
160
+ if (value.isUInt ())
147
161
{
148
- mStartDate .SetNonNull (value.asInt ());
162
+ mStartDate .SetNonNull (value.asUInt ());
149
163
}
150
164
else
151
165
{
@@ -212,9 +226,9 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
212
226
if (root.isMember (" EventID" ))
213
227
{
214
228
value = root.get (" EventID" , Json::Value ());
215
- if (value.isInt ())
229
+ if (value.isUInt ())
216
230
{
217
- eventID.SetNonNull (value.asInt ());
231
+ eventID.SetNonNull (value.asUInt ());
218
232
}
219
233
else
220
234
{
@@ -234,8 +248,10 @@ void chip::app::Clusters::EnergyCalendar::CalendarProviderInstance::ErrorMessage
234
248
va_end (v);
235
249
}
236
250
237
- DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay (uint64_t date)
251
+ DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay (uint32_t date)
238
252
{
253
+ DataModel::Nullable<Structs::DayStruct::Type> result = std::nullopt;
254
+
239
255
for (auto & day : mSpecialDays )
240
256
{
241
257
if (day.date .HasValue () && day.date .Value () == date)
@@ -244,13 +260,18 @@ DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay(u
244
260
}
245
261
}
246
262
247
- TransitionDayOfWeekBitmap week_day = GetWeekDate (date);
263
+ if (mStartDate .ValueOr (0 ) > date)
264
+ {
265
+ return result;
266
+ }
267
+
268
+ TransitionDayOfWeekBitmap week_day = GetDayOfWeek (date);
248
269
249
270
for (auto & period : mCalendarPeriods )
250
271
{
251
- if (! period.startDate .IsNull () && period. startDate . Value () < date)
272
+ if (period.startDate .ValueOr ( 0 ) > date)
252
273
{
253
- continue ;
274
+ break ;
254
275
}
255
276
256
277
bool calendarByWeek = period.days .size () > 0 && period.days [0 ].daysOfWeek .HasValue ();
@@ -260,29 +281,30 @@ DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay(u
260
281
{
261
282
if (day.daysOfWeek .ValueOr (0 ).Has (week_day))
262
283
{
263
- return day;
284
+ result = day;
285
+ break ;
264
286
}
265
287
}
266
288
}
267
289
else // loop calendar
268
290
{
269
291
uint32_t index = static_cast <uint32_t >((date - period.startDate .ValueOr (0 )) / kSecondsPerDay % period.days .size ());
270
- return period.days [index ];
292
+ result = period.days [index ];
271
293
}
272
294
}
273
295
274
- return DataModel::Nullable<Structs::DayStruct::Type>() ;
296
+ return result ;
275
297
}
276
298
277
299
void CalendarProviderInstance::JsonToCalendarPeriodStruct (Json::Value & root, Structs::CalendarPeriodStruct::Type & value)
278
300
{
279
- Json::Value t = root.get (" startDate " , Json::Value ());
280
- if (!t.empty () && t.isInt ())
301
+ Json::Value t = root.get (" StartDate " , Json::Value ());
302
+ if (!t.empty () && t.isUInt ())
281
303
{
282
- value.startDate .SetNonNull (t.asInt ());
304
+ value.startDate .SetNonNull (t.asUInt ());
283
305
}
284
306
285
- t = root.get (" days " , Json::Value ());
307
+ t = root.get (" Days " , Json::Value ());
286
308
if (!t.empty () && t.isArray ())
287
309
{
288
310
DataModel::List<Structs::DayStruct::Type> * days = (DataModel::List<Structs::DayStruct::Type> *) &value.days ;
@@ -292,57 +314,57 @@ void CalendarProviderInstance::JsonToCalendarPeriodStruct(Json::Value & root, St
292
314
293
315
void CalendarProviderInstance::JsonToDayStruct (Json::Value & root, Structs::DayStruct::Type & value)
294
316
{
295
- Json::Value t = root.get (" date " , Json::Value ());
296
- if (!t.empty () && t.isInt ())
317
+ Json::Value t = root.get (" Date " , Json::Value ());
318
+ if (!t.empty () && t.isUInt ())
297
319
{
298
- value.date .SetValue (t.asInt ());
320
+ value.date .SetValue (t.asUInt ());
299
321
}
300
322
301
- t = root.get (" daysOfWeek " , Json::Value ());
302
- if (!t.empty () && t.isInt ())
323
+ t = root.get (" DaysOfWeek " , Json::Value ());
324
+ if (!t.empty () && t.isUInt ())
303
325
{
304
- value.daysOfWeek .SetValue (chip::BitMask<TransitionDayOfWeekBitmap>((uint8_t ) t.asInt ()));
326
+ value.daysOfWeek .SetValue (chip::BitMask<TransitionDayOfWeekBitmap>((uint8_t ) t.asUInt ()));
305
327
}
306
328
307
- t = root.get (" transitions " , Json::Value ());
329
+ t = root.get (" Transitions " , Json::Value ());
308
330
if (!t.empty () && t.isArray ())
309
331
{
310
332
DataModel::List<Structs::TransitionStruct::Type> * transitions =
311
333
(DataModel::List<Structs::TransitionStruct::Type> *) &value.transitions ;
312
334
JsonToTransitionStructList (t, *transitions);
313
335
}
314
336
315
- t = root.get (" calendarID " , Json::Value ());
316
- if (!t.empty () && t.isInt ())
337
+ t = root.get (" CalendarID " , Json::Value ());
338
+ if (!t.empty () && t.isUInt ())
317
339
{
318
- value.calendarID .SetValue (t.asInt ());
340
+ value.calendarID .SetValue (t.asUInt ());
319
341
}
320
342
}
321
343
322
344
void CalendarProviderInstance::JsonToPeakPeriodStruct (Json::Value & root, Structs::PeakPeriodStruct::Type & value)
323
345
{
324
- Json::Value t = root.get (" severity " , Json::Value ());
325
- if (!t.empty () && t.isInt ())
346
+ Json::Value t = root.get (" Severity " , Json::Value ());
347
+ if (!t.empty () && t.isUInt ())
326
348
{
327
- value.severity = static_cast <PeakPeriodSeverityEnum>(t.asInt ());
349
+ value.severity = static_cast <PeakPeriodSeverityEnum>(t.asUInt ());
328
350
}
329
351
330
- t = root.get (" peakPeriod " , Json::Value ());
331
- if (!t.empty () && t.isInt ())
352
+ t = root.get (" PeakPeriod " , Json::Value ());
353
+ if (!t.empty () && t.isUInt ())
332
354
{
333
- value.peakPeriod = static_cast <uint16_t >(t.asInt ());
355
+ value.peakPeriod = static_cast <uint16_t >(t.asUInt ());
334
356
}
335
357
336
- t = root.get (" startTime " , Json::Value ());
337
- if (!t.empty () && t.isInt ())
358
+ t = root.get (" StartTime " , Json::Value ());
359
+ if (!t.empty () && t.isUInt ())
338
360
{
339
- value.startTime .SetNonNull (t.asInt ());
361
+ value.startTime .SetNonNull (t.asUInt ());
340
362
}
341
363
342
- t = root.get (" endTime " , Json::Value ());
343
- if (!t.empty () && t.isInt ())
364
+ t = root.get (" EndTime " , Json::Value ());
365
+ if (!t.empty () && t.isUInt ())
344
366
{
345
- value.endTime .SetNonNull (t.asInt ());
367
+ value.endTime .SetNonNull (t.asUInt ());
346
368
}
347
369
}
348
370
@@ -387,28 +409,28 @@ void CalendarProviderInstance::JsonToTransitionStructList(Json::Value & root,
387
409
{
388
410
Json::Value v = root[i];
389
411
390
- Json::Value t = v.get (" transitionTime " , Json::Value ());
391
- if (!t.empty () && t.isInt ())
412
+ Json::Value t = v.get (" TransitionTime " , Json::Value ());
413
+ if (!t.empty () && t.isUInt ())
392
414
{
393
- value[i].transitionTime = static_cast <uint16_t >(t.asInt ());
415
+ value[i].transitionTime = static_cast <uint16_t >(t.asUInt ());
394
416
}
395
417
396
- t = v.get (" priceTier " , Json::Value ());
397
- if (!t.empty () && t.isInt ())
418
+ t = v.get (" PriceTier " , Json::Value ());
419
+ if (!t.empty () && t.isUInt ())
398
420
{
399
- value[i].priceTier .SetValue (t.asInt ());
421
+ value[i].priceTier .SetValue (t.asUInt ());
400
422
}
401
423
402
- t = v.get (" friendlyCredit " , Json::Value ());
424
+ t = v.get (" FriendlyCredit " , Json::Value ());
403
425
if (!t.empty () && t.isBool ())
404
426
{
405
427
value[i].friendlyCredit .SetValue (t.asBool ());
406
428
}
407
429
408
- t = v.get (" auxiliaryLoad " , Json::Value ());
409
- if (!t.empty () && t.isInt ())
430
+ t = v.get (" AuxiliaryLoad " , Json::Value ());
431
+ if (!t.empty () && t.isUInt ())
410
432
{
411
- value[i].auxiliaryLoad .SetValue (chip::BitMask<AuxiliaryLoadBitmap>((uint8_t ) t.asInt ()));
433
+ value[i].auxiliaryLoad .SetValue (chip::BitMask<AuxiliaryLoadBitmap>((uint8_t ) t.asUInt ()));
412
434
}
413
435
}
414
436
}
@@ -469,7 +491,13 @@ void emberAfEnergyCalendarClusterInitCallback(chip::EndpointId endpointId)
469
491
gMIInstance = std::make_unique<EnergyCalendarServer>(BitMask<Feature, uint32_t >(
470
492
Feature::kPricingTier , Feature::kFriendlyCredit , Feature::kAuxiliaryLoad , Feature::kPeakPeriod ));
471
493
472
- gMIInstance ->AddCalendarProvider (&(*gMIDelegate ));
494
+ gMIInstance ->Init ();
495
+
496
+ CHIP_ERROR err = gMIInstance ->AddCalendarProvider (&(*gMIDelegate ));
497
+ if (err != CHIP_NO_ERROR)
498
+ {
499
+ ChipLogError (NotSpecified, " Failed to add Calendar provider: %s" , err.AsString ());
500
+ }
473
501
}
474
502
}
475
503
0 commit comments