Skip to content

Commit cd4e4be

Browse files
Fix choosing transinion bug
Fix epoch value is not round on midnight bug
1 parent 5ddd570 commit cd4e4be

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

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

-4
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,8 @@ class CalendarProviderInstance : public CalendarProvider
5959

6060
private:
6161
// Attributes contaners allocated memory
62-
DataModel::Nullable<uint32_t> mStartDate;
63-
6462
DataModel::List<Structs::CalendarPeriodStruct::Type> mCalendarPeriods;
6563
DataModel::List<Structs::DayStruct::Type> mSpecialDays;
66-
DataModel::Nullable<Structs::PeakPeriodStruct::Type> mCurrentPeak;
67-
DataModel::Nullable<Structs::PeakPeriodStruct::Type> mNextPeak;
6864

6965
void JsonToCalendarPeriodStruct(Json::Value & root, Structs::CalendarPeriodStruct::Type & value);
7066
void JsonToDayStruct(Json::Value & root, Structs::DayStruct::Type & value);

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

+21-16
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
9595
DataModel::Nullable<uint32_t> calendarID;
9696
DataModel::Nullable<uint32_t> providerID;
9797
DataModel::Nullable<uint32_t> eventID;
98+
DataModel::Nullable<uint32_t> startDate;
9899
Structs::PeakPeriodStruct::Type peak;
100+
DataModel::Nullable<Structs::PeakPeriodStruct::Type> currentPeak;
101+
DataModel::Nullable<Structs::PeakPeriodStruct::Type> nextPeak;
102+
99103

100104
if (root.isMember("Features"))
101105
{
@@ -155,11 +159,11 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
155159
value = root.get("StartDate", Json::Value());
156160
if (value.isUInt() && value.asUInt() < static_cast<uint32_t>(-1))
157161
{
158-
mStartDate.SetNonNull(value.asUInt());
162+
startDate.SetNonNull(value.asUInt());
159163
}
160164
else
161165
{
162-
mStartDate.SetNull();
166+
startDate.SetNull();
163167
}
164168
}
165169

@@ -172,7 +176,7 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
172176
JsonToCalendarPeriodStructList(value, mCalendarPeriods);
173177
}
174178
}
175-
SetCalendarPeriod(mStartDate, mCalendarPeriods);
179+
SetCalendarPeriod(startDate, mCalendarPeriods);
176180
}
177181

178182
if (root.isMember("SpecialDays"))
@@ -194,11 +198,11 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
194198
if (!value.empty())
195199
{
196200
JsonToPeakPeriodStruct(value, peak);
197-
mCurrentPeak.SetNonNull(peak);
201+
currentPeak.SetNonNull(peak);
198202
}
199203
else
200204
{
201-
mCurrentPeak.SetNull();
205+
currentPeak.SetNull();
202206
}
203207
}
204208

@@ -208,15 +212,15 @@ CHIP_ERROR CalendarProviderInstance::LoadJson(Json::Value & root)
208212
if (!value.empty())
209213
{
210214
JsonToPeakPeriodStruct(value, peak);
211-
mNextPeak.SetNonNull(peak);
215+
nextPeak.SetNonNull(peak);
212216
}
213217
else
214218
{
215-
mNextPeak.SetNull();
219+
nextPeak.SetNull();
216220
}
217221
}
218222

219-
SetPeakPeriods(mCurrentPeak, mNextPeak);
223+
SetPeakPeriods(currentPeak, nextPeak);
220224
}
221225

222226
if (root.isMember("EventID"))
@@ -256,26 +260,27 @@ DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay(u
256260

257261
ParceDay(date, year, month, dayOfMonth, dayOfWeek);
258262

259-
for (auto & day : mSpecialDays)
263+
for (auto & day : GetSpecialDays())
260264
{
261265
if (day.date.HasValue())
262266
{
263-
dt = static_cast<uint32_t>(day.date.Value() - (day.date.Value() % kSecInOneDay));
267+
dt = static_cast<uint32_t>(day.date.ValueOr(0) - (day.date.ValueOr(0) % kSecInOneDay));
264268
if (dt == date)
265269
{
266270
return day;
267271
}
268272
}
269273
}
270274

271-
if (mStartDate.ValueOr(0) > date)
275+
if (GetStartDate().ValueOr(0) > date)
272276
{
273277
return result;
274278
}
275279

276-
for (auto & period : mCalendarPeriods)
280+
for (auto & period : GetCalendarPeriods())
277281
{
278-
if (period.startDate.ValueOr(0) > date)
282+
dt = static_cast<uint32_t>(period.startDate.ValueOr(0) - (period.startDate.ValueOr(0) % kSecInOneDay));
283+
if (dt > date)
279284
{
280285
break;
281286
}
@@ -294,7 +299,7 @@ DataModel::Nullable<Structs::DayStruct::Type> CalendarProviderInstance::GetDay(u
294299
}
295300
else // loop calendar
296301
{
297-
uint32_t index = static_cast<uint32_t>((date - period.startDate.ValueOr(0)) / kSecondsPerDay % period.days.size());
302+
uint32_t index = static_cast<uint32_t>((date - dt) / kSecondsPerDay % period.days.size());
298303
result = period.days[index];
299304
}
300305
}
@@ -356,7 +361,7 @@ void CalendarProviderInstance::JsonToPeakPeriodStruct(Json::Value & root, Struct
356361
}
357362

358363
t = root.get("PeakPeriod", Json::Value());
359-
if (!t.empty() && t.isUInt() && t.asUInt() < static_cast<uint16_t>(-1))
364+
if (!t.empty() && t.isUInt() && t.asUInt() <= static_cast<uint16_t>(-1))
360365
{
361366
value.peakPeriod = static_cast<uint16_t>(t.asUInt());
362367
}
@@ -416,7 +421,7 @@ void CalendarProviderInstance::JsonToTransitionStructList(Json::Value & root,
416421
Json::Value v = root[i];
417422

418423
Json::Value t = v.get("TransitionTime", Json::Value());
419-
if (!t.empty() && t.isUInt() && t.asUInt() < static_cast<uint16_t>(-1))
424+
if (!t.empty() && t.isUInt() && t.asUInt() <= static_cast<uint16_t>(-1))
420425
{
421426
value[i].transitionTime = static_cast<uint16_t>(t.asUInt());
422427
}

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

+15-7
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ bool CalendarProvider::CheckDay(const Structs::DayStruct::Type & day)
330330

331331
VerifyOrReturnLogSend((day.transitions.size() > 0 && day.transitions.size() <= 48), mEndpoint,
332332
"Day transinions list must have from 1 to 48 records");
333+
334+
VerifyOrReturnLogSend((day.transitions[0].transitionTime == 0), mEndpoint,
335+
"Fisrt Day transinion must have transitionTime = 0");
333336

334337
uint32_t time = 0;
335338
for (auto & transition : day.transitions)
@@ -436,33 +439,38 @@ CalendarProvider * EnergyCalendarServer::GetProvider(EndpointId ep)
436439
DataModel::Nullable<Structs::TransitionStruct::Type> EnergyCalendarServer::GetTransition(EndpointId ep)
437440
{
438441
CalendarProvider * provider = GetProvider(ep);
439-
if (provider == nullptr || provider->GetCurrentDay().IsNull())
442+
if (provider == nullptr)
440443
{
441444
return DataModel::Nullable<Structs::TransitionStruct::Type>();
442445
}
443446

444-
Structs::DayStruct::Type & currentDay = provider->GetCurrentDay().Value();
447+
DataModel::Nullable<Structs::DayStruct::Type> currentDay = provider->GetCurrentDay();
448+
if (currentDay.IsNull())
449+
{
450+
return DataModel::Nullable<Structs::TransitionStruct::Type>();
451+
}
445452

446453
uint32_t time = GetCurrentTime();
447454

448-
auto transition = currentDay.transitions.begin();
449455
uint32_t next_tr_time = kSecInOneDay;
450456

451457
const Structs::TransitionStruct::Type * current = nullptr;
452458

453-
while (transition != currentDay.transitions.end())
459+
for (auto & transition : currentDay.Value().transitions)
454460
{
455-
auto tr_time = transition->transitionTime;
461+
auto tr_time = transition.transitionTime;
456462
if (tr_time <= time && (current == nullptr || current->transitionTime < tr_time))
457463
{
458-
current = transition;
464+
current = &transition;
459465
}
460466
if ((time > tr_time) && (time < next_tr_time))
461467
{
462468
next_tr_time = time;
463469
}
464470
}
465-
return DataModel::Nullable<Structs::TransitionStruct::Type>(*current);
471+
472+
return current ? DataModel::Nullable<Structs::TransitionStruct::Type>(*current)
473+
: DataModel::Nullable<Structs::TransitionStruct::Type>();
466474
}
467475

468476
// AttributeAccessInterface

0 commit comments

Comments
 (0)