Skip to content

Commit e00c17e

Browse files
Debug GetDayOfWeek(), problem with Json Uint types, selecting days
1 parent 1d8895a commit e00c17e

File tree

8 files changed

+286
-170
lines changed

8 files changed

+286
-170
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class CalendarProviderInstance : public CalendarProvider
5353
CHIP_ERROR LoadJson(Json::Value & root);
5454

5555
/* owerride */
56-
DataModel::Nullable<Structs::DayStruct::Type> GetDay(uint64_t day) override;
56+
DataModel::Nullable<Structs::DayStruct::Type> GetDay(uint32_t day) override;
5757

5858
void ErrorMessage(EndpointId ep, const char * msg, ...) override;
5959

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

+86-58
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <app/util/attribute-storage.h>
2222

2323
#include "energy-calendar-instance.h"
24+
#include <lib/support/logging/TextOnlyLogging.h>
2425

2526
using namespace chip;
2627
using namespace chip::app;
@@ -31,12 +32,22 @@ using namespace chip::app::Clusters::EnergyCalendar::Attributes;
3132

3233
constexpr uint32_t kOneDay = 24 * 60 * 60;
3334

34-
static TransitionDayOfWeekBitmap GetWeekDate(uint64_t date)
35+
static TransitionDayOfWeekBitmap GetDayOfWeek(uint32_t date)
3536
{
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);
4051
}
4152

4253
#if 0
@@ -63,6 +74,7 @@ chip::app::Clusters::EnergyCalendar::CalendarProviderInstance::~CalendarProvider
6374

6475
void CalendarProviderInstance::Init(void)
6576
{
77+
ChipLogProgress(Zcl, "CalendarProviderInstance::Init");
6678
SetDefault();
6779
}
6880

@@ -91,12 +103,14 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
91103
DataModel::Nullable<uint32_t> eventID;
92104
Structs::PeakPeriodStruct::Type peak;
93105

106+
ChipLogProgress(Zcl, "CalendarProviderInstance::LoadJson");
94107
if (root.isMember("CalendarID"))
95108
{
109+
ChipLogProgress(Zcl, "CalendarProviderInstance::LoadJson have CalendaID");
96110
value = root.get("CalendarID", Json::Value());
97-
if (value.isInt())
111+
if (value.isUInt())
98112
{
99-
calendarID.SetNonNull(value.asInt());
113+
calendarID.SetNonNull(value.asUInt());
100114
}
101115
else
102116
{
@@ -115,7 +129,7 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
115129
}
116130
if (value.isString())
117131
{
118-
size_t len = value.asString().size() + 1;
132+
size_t len = value.asString().size();
119133
char * str = (char *) chip::Platform::MemoryAlloc(len);
120134
memcpy(str, value.asCString(), len);
121135
CharSpan nameString(str, len);
@@ -127,9 +141,9 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
127141
if (root.isMember("ProviderID"))
128142
{
129143
value = root.get("ProviderID", Json::Value());
130-
if (value.isInt())
144+
if (value.isUInt())
131145
{
132-
providerID.SetNonNull(value.asInt());
146+
providerID.SetNonNull(value.asUInt());
133147
}
134148
else
135149
{
@@ -143,9 +157,9 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
143157
if (root.isMember("StartDate"))
144158
{
145159
value = root.get("StartDate", Json::Value());
146-
if (value.isInt())
160+
if (value.isUInt())
147161
{
148-
mStartDate.SetNonNull(value.asInt());
162+
mStartDate.SetNonNull(value.asUInt());
149163
}
150164
else
151165
{
@@ -212,9 +226,9 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
212226
if (root.isMember("EventID"))
213227
{
214228
value = root.get("EventID", Json::Value());
215-
if (value.isInt())
229+
if (value.isUInt())
216230
{
217-
eventID.SetNonNull(value.asInt());
231+
eventID.SetNonNull(value.asUInt());
218232
}
219233
else
220234
{
@@ -234,8 +248,10 @@ void chip::app::Clusters::EnergyCalendar::CalendarProviderInstance::ErrorMessage
234248
va_end(v);
235249
}
236250

237-
DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay(uint64_t date)
251+
DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay(uint32_t date)
238252
{
253+
DataModel::Nullable<Structs::DayStruct::Type> result = std::nullopt;
254+
239255
for (auto & day : mSpecialDays)
240256
{
241257
if (day.date.HasValue() && day.date.Value() == date)
@@ -244,13 +260,18 @@ DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay(u
244260
}
245261
}
246262

247-
TransitionDayOfWeekBitmap week_day = GetWeekDate(date);
263+
if (mStartDate.ValueOr(0) > date)
264+
{
265+
return result;
266+
}
267+
268+
TransitionDayOfWeekBitmap week_day = GetDayOfWeek(date);
248269

249270
for (auto & period : mCalendarPeriods)
250271
{
251-
if (!period.startDate.IsNull() && period.startDate.Value() < date)
272+
if (period.startDate.ValueOr(0) > date)
252273
{
253-
continue;
274+
break;
254275
}
255276

256277
bool calendarByWeek = period.days.size() > 0 && period.days[0].daysOfWeek.HasValue();
@@ -260,29 +281,30 @@ DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay(u
260281
{
261282
if (day.daysOfWeek.ValueOr(0).Has(week_day))
262283
{
263-
return day;
284+
result = day;
285+
break;
264286
}
265287
}
266288
}
267289
else // loop calendar
268290
{
269291
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];
271293
}
272294
}
273295

274-
return DataModel::Nullable<Structs::DayStruct::Type>();
296+
return result;
275297
}
276298

277299
void CalendarProviderInstance::JsonToCalendarPeriodStruct(Json::Value & root, Structs::CalendarPeriodStruct::Type & value)
278300
{
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())
281303
{
282-
value.startDate.SetNonNull(t.asInt());
304+
value.startDate.SetNonNull(t.asUInt());
283305
}
284306

285-
t = root.get("days", Json::Value());
307+
t = root.get("Days", Json::Value());
286308
if (!t.empty() && t.isArray())
287309
{
288310
DataModel::List<Structs::DayStruct::Type> * days = (DataModel::List<Structs::DayStruct::Type> *) &value.days;
@@ -292,57 +314,57 @@ void CalendarProviderInstance::JsonToCalendarPeriodStruct(Json::Value & root, St
292314

293315
void CalendarProviderInstance::JsonToDayStruct(Json::Value & root, Structs::DayStruct::Type & value)
294316
{
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())
297319
{
298-
value.date.SetValue(t.asInt());
320+
value.date.SetValue(t.asUInt());
299321
}
300322

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())
303325
{
304-
value.daysOfWeek.SetValue(chip::BitMask<TransitionDayOfWeekBitmap>((uint8_t) t.asInt()));
326+
value.daysOfWeek.SetValue(chip::BitMask<TransitionDayOfWeekBitmap>((uint8_t) t.asUInt()));
305327
}
306328

307-
t = root.get("transitions", Json::Value());
329+
t = root.get("Transitions", Json::Value());
308330
if (!t.empty() && t.isArray())
309331
{
310332
DataModel::List<Structs::TransitionStruct::Type> * transitions =
311333
(DataModel::List<Structs::TransitionStruct::Type> *) &value.transitions;
312334
JsonToTransitionStructList(t, *transitions);
313335
}
314336

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())
317339
{
318-
value.calendarID.SetValue(t.asInt());
340+
value.calendarID.SetValue(t.asUInt());
319341
}
320342
}
321343

322344
void CalendarProviderInstance::JsonToPeakPeriodStruct(Json::Value & root, Structs::PeakPeriodStruct::Type & value)
323345
{
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())
326348
{
327-
value.severity = static_cast<PeakPeriodSeverityEnum>(t.asInt());
349+
value.severity = static_cast<PeakPeriodSeverityEnum>(t.asUInt());
328350
}
329351

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())
332354
{
333-
value.peakPeriod = static_cast<uint16_t>(t.asInt());
355+
value.peakPeriod = static_cast<uint16_t>(t.asUInt());
334356
}
335357

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())
338360
{
339-
value.startTime.SetNonNull(t.asInt());
361+
value.startTime.SetNonNull(t.asUInt());
340362
}
341363

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())
344366
{
345-
value.endTime.SetNonNull(t.asInt());
367+
value.endTime.SetNonNull(t.asUInt());
346368
}
347369
}
348370

@@ -387,28 +409,28 @@ void CalendarProviderInstance::JsonToTransitionStructList(Json::Value & root,
387409
{
388410
Json::Value v = root[i];
389411

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())
392414
{
393-
value[i].transitionTime = static_cast<uint16_t>(t.asInt());
415+
value[i].transitionTime = static_cast<uint16_t>(t.asUInt());
394416
}
395417

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())
398420
{
399-
value[i].priceTier.SetValue(t.asInt());
421+
value[i].priceTier.SetValue(t.asUInt());
400422
}
401423

402-
t = v.get("friendlyCredit", Json::Value());
424+
t = v.get("FriendlyCredit", Json::Value());
403425
if (!t.empty() && t.isBool())
404426
{
405427
value[i].friendlyCredit.SetValue(t.asBool());
406428
}
407429

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())
410432
{
411-
value[i].auxiliaryLoad.SetValue(chip::BitMask<AuxiliaryLoadBitmap>((uint8_t) t.asInt()));
433+
value[i].auxiliaryLoad.SetValue(chip::BitMask<AuxiliaryLoadBitmap>((uint8_t) t.asUInt()));
412434
}
413435
}
414436
}
@@ -469,7 +491,13 @@ void emberAfEnergyCalendarClusterInitCallback(chip::EndpointId endpointId)
469491
gMIInstance = std::make_unique<EnergyCalendarServer>(BitMask<Feature, uint32_t>(
470492
Feature::kPricingTier, Feature::kFriendlyCredit, Feature::kAuxiliaryLoad, Feature::kPeakPeriod));
471493

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+
}
473501
}
474502
}
475503

examples/all-clusters-app/all-clusters-common/src/meter-identification-stub.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ static std::unique_ptr<MeterIdentificationInstance> gMIInstance;
2828

2929
void emberAfMeterIdentificationClusterInitCallback(chip::EndpointId endpointId)
3030
{
31+
ChipLogProgress(Zcl, "emberAfMeterIdentificationClusterInitCallback %d", (int)endpointId);
32+
3133
VerifyOrDie(endpointId == 1); // this cluster is only enabled for endpoint 1.
3234
VerifyOrDie(!gMIInstance);
3335

examples/all-clusters-app/linux/AllClustersCommandDelegate.cpp

+36-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include <rvc-modes.h>
4040

4141
#include <string>
42+
#include <fstream>
43+
#include <iostream>
4244

4345
using namespace chip;
4446
using namespace chip::app;
@@ -508,7 +510,40 @@ void AllClustersAppCommandHandler::OnOperationalStateChange(std::string device,
508510
void AllClustersAppCommandHandler::OnEnergyCalendarHandler(Json::Value param)
509511
{
510512
EnergyCalendar::CalendarProviderInstance * provider = EnergyCalendar::GetProvider();
511-
provider->LoadJson(param);
513+
if (provider == nullptr)
514+
{
515+
ChipLogError(NotSpecified, "AllClusters App: Has not Calendar provider");
516+
return;
517+
}
518+
519+
if (param.isMember("Configure"))
520+
{
521+
Json::Value config = param.get("CalendarName", Json::Value());
522+
if (config.isString())
523+
{
524+
Json::CharReaderBuilder builder;
525+
Json::Value value;
526+
std::ifstream ifs;
527+
ifs.open(config.asCString());
528+
Json::String errs;
529+
if (!parseFromStream(builder, ifs, &value, &errs)) {
530+
ChipLogError(NotSpecified,
531+
"AllClusters App: Error parsing JSON file %s with error %s:", config.asCString(), errs.c_str());
532+
}
533+
534+
if (value.empty() || !value.isObject())
535+
{
536+
ChipLogError(NotSpecified, "AllClusters App: Invalid JSON file %s", config.asCString());
537+
return;
538+
}
539+
540+
provider->LoadJson(value);
541+
}
542+
}
543+
else
544+
{
545+
provider->LoadJson(param);
546+
}
512547
}
513548

514549
void AllClustersAppCommandHandler::OnMeterIdentificationHandler(Json::Value param)

0 commit comments

Comments
 (0)