forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviceEnergyManagementDelegateImpl.cpp
371 lines (315 loc) · 12.3 KB
/
DeviceEnergyManagementDelegateImpl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "DeviceEnergyManagementDelegateImpl.h"
#include <app/EventLogging.h>
using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::DeviceEnergyManagement;
using namespace chip::app::Clusters::DeviceEnergyManagement::Attributes;
using chip::Optional;
using namespace chip::app;
using CostsList = DataModel::List<const Structs::CostStruct::Type>;
/**
* @brief Delegate handler for PowerAdjustRequest
*
* Note: checking of the validity of the PowerAdjustRequest has been done by the lower layer
*
* This function needs to notify the appliance that it should apply a new power setting.
* It should:
* 1) notify the appliance - if the appliance hardware cannot be adjusted, then return Failure
* 2) start a timer (or restart the existing PowerAdjust timer) for duration seconds
* 3) generate a PowerAdjustStart event (if there is not an existing PowerAdjustRequest running)
* 4) if appropriate, update the forecast with the new expected end time
*
* and when the timer expires:
* 5) notify the appliance's that it can resume its intended power setting (or go idle)
* 6) generate a PowerAdjustEnd event with cause NormalCompletion
* 7) if necessary, update the forecast with new expected end time
*/
Status DeviceEnergyManagementDelegate::PowerAdjustRequest(const int64_t power, const uint32_t duration, AdjustmentCauseEnum cause)
{
Status status = Status::UnsupportedCommand; // Status::Success;
// TODO: implement
mEsaState = ESAStateEnum::kPowerAdjustActive;
// TODO: Generate a PowerAdjustStart Event, then begins to adjust its power
// When done, raise PowerAdjustEnd & ESAState set to kOnline.
MatterReportingAttributeChangeCallback(mEndpointId, DeviceEnergyManagement::Id, ESAState::Id);
return status;
}
/**
* @brief Delegate handler for CancelPowerAdjustRequest
*
* Note: checking of the validity of the CancelPowerAdjustRequest has been done by the lower layer
*
* This function needs to notify the appliance that it should resume its intended power setting (or go idle).
* It should:
* 1) notify the appliance's that it can resume its intended power setting (or go idle)
* 2) generate a PowerAdjustEnd event with cause code Cancelled
* 3) if necessary, update the forecast with new expected end time
*/
Status DeviceEnergyManagementDelegate::CancelPowerAdjustRequest()
{
Status status = Status::UnsupportedCommand; // Status::Success;
// TODO: implement
/* TODO: If the command is accepted, the ESA SHALL generate an PowerAdjustEnd Event. */
mEsaState = ESAStateEnum::kOnline;
MatterReportingAttributeChangeCallback(mEndpointId, DeviceEnergyManagement::Id, ESAState::Id);
return status;
}
/**
* @brief Delegate handler for StartTimeAdjustRequest
*
* Note: checking of the validity of the StartTimeAdjustRequest has been done by the lower layer
*
* This function needs to notify the appliance that the forecast has been updated by a client.
*
* It should:
* 1) update the forecast attribute with the revised start time
* 2) send a callback notification to the appliance so it can refresh its internal schedule
*/
Status DeviceEnergyManagementDelegate::StartTimeAdjustRequest(const uint32_t requestedStartTime, AdjustmentCauseEnum cause)
{
DataModel::Nullable<Structs::ForecastStruct::Type> forecast = GetForecast();
if (forecast.IsNull())
{
return Status::Failure;
}
uint32_t duration = forecast.Value().endTime - forecast.Value().startTime; // the current entire forecast duration
/* Modify start time and end time */
forecast.Value().startTime = requestedStartTime;
forecast.Value().endTime = requestedStartTime + duration;
SetForecast(forecast); // This will increment forecast ID
// TODO: callback to the appliance to notify it of a new start time
return Status::Success;
}
/**
* @brief Delegate handler for Pause Request
*
* Note: checking of the validity of the Pause Request has been done by the lower layer
*
* This function needs to notify the appliance that it should now pause.
* It should:
* 1) pause the appliance - if the appliance hardware cannot be paused, then return Failure
* 2) start a timer for duration seconds
* 3) generate a Paused event
* 4) update the forecast with the new expected end time
*
* and when the timer expires:
* 5) restore the appliance's operational state
* 6) generate a Resumed event
* 7) if necessary, update the forecast with new expected end time
*/
Status DeviceEnergyManagementDelegate::PauseRequest(const uint32_t duration, AdjustmentCauseEnum cause)
{
Status status = Status::UnsupportedCommand; // Status::Success;
// TODO: implement the behaviour above
return status;
}
/**
* @brief Delegate handler for ResumeRequest
*
* Note: checking of the validity of the ResumeRequest has been done by the lower layer
*
* This function needs to notify the appliance that it should now resume operation
*
* It should:
* 1) restore the appliance's operational state
* 2) generate a Resumed event
* 3) update the forecast with new expected end time (given that the pause duration was shorter than originally requested)
*
*/
Status DeviceEnergyManagementDelegate::ResumeRequest()
{
Status status = Status::UnsupportedCommand; // Status::Success;
// TODO: implement the behaviour above
SetESAState(ESAStateEnum::kOnline);
return status;
}
/**
* @brief Delegate handler for ModifyForecastRequest
*
* Note: Only basic checking of the validity of the ModifyForecastRequest has been
* done by the lower layer. This is a more complex use-case and requires higher-level
* work by the delegate.
*
* It should:
* 1) determine if the new forecast adjustments are acceptable to the appliance
* - if not return Failure. For example, if it may cause the home to be too hot
* or too cold, or a battery to be insufficiently charged
* 2) if the slot adjustments are acceptable, then update the forecast
* 3) notify the appliance to follow the revised schedule
*/
Status DeviceEnergyManagementDelegate::ModifyForecastRequest(
const uint32_t forecastId, const DataModel::DecodableList<Structs::SlotAdjustmentStruct::DecodableType> & slotAdjustments,
AdjustmentCauseEnum cause)
{
Status status = Status::UnsupportedCommand; // Status::Success;
// TODO: implement the behaviour above
return status;
}
/**
* @brief Delegate handler for RequestConstraintBasedForecast
*
* Note: Only basic checking of the validity of the RequestConstraintBasedForecast has been
* done by the lower layer. This is a more complex use-case and requires higher-level
* work by the delegate.
*
* It should:
* 1) perform a higher level optimization (e.g. using tariff information, and user preferences)
* 2) if a solution can be found, then update the forecast, else return Failure
* 3) notify the appliance to follow the revised schedule
*/
Status DeviceEnergyManagementDelegate::RequestConstraintBasedForecast(
const DataModel::DecodableList<Structs::ConstraintsStruct::DecodableType> & constraints, AdjustmentCauseEnum cause)
{
Status status = Status::UnsupportedCommand; // Status::Success;
// TODO: implement the behaviour above
return status;
}
/**
* @brief Delegate handler for CancelRequest
*
* Note: This is a more complex use-case and requires higher-level work by the delegate.
*
* It SHALL:
* 1) Check if the forecastUpdateReason was already InternalOptimization (and reject the command)
* 2) Update its forecast (based on its optimization strategy) ignoring previous requests
* 3) Update its Forecast attribute to match its new intended operation, and update the
* ForecastStruct.ForecastUpdateReason to `Internal Optimization`.
*/
Status DeviceEnergyManagementDelegate::CancelRequest()
{
Status status = Status::UnsupportedCommand; // Status::Success;
// TODO: implement the behaviour above
return status;
}
// ------------------------------------------------------------------
// Get attribute methods
ESATypeEnum DeviceEnergyManagementDelegate::GetESAType()
{
return mEsaType;
}
bool DeviceEnergyManagementDelegate::GetESACanGenerate()
{
return mEsaCanGenerate;
}
ESAStateEnum DeviceEnergyManagementDelegate::GetESAState()
{
return mEsaState;
}
int64_t DeviceEnergyManagementDelegate::GetAbsMinPower()
{
return mAbsMinPower;
}
int64_t DeviceEnergyManagementDelegate::GetAbsMaxPower()
{
return mAbsMaxPower;
}
PowerAdjustmentCapability::TypeInfo::Type DeviceEnergyManagementDelegate::GetPowerAdjustmentCapability()
{
return mPowerAdjustmentCapability;
}
DataModel::Nullable<Structs::ForecastStruct::Type> DeviceEnergyManagementDelegate::GetForecast()
{
return mForecast;
}
OptOutStateEnum DeviceEnergyManagementDelegate::GetOptOutState()
{
return mOptOutState;
}
// ------------------------------------------------------------------
// Set attribute methods
CHIP_ERROR DeviceEnergyManagementDelegate::SetESAType(ESATypeEnum newValue)
{
ESATypeEnum oldValue = mEsaType;
if (newValue >= ESATypeEnum::kUnknownEnumValue)
{
return CHIP_IM_GLOBAL_STATUS(ConstraintError);
}
mEsaType = newValue;
if (oldValue != newValue)
{
ChipLogDetail(AppServer, "mEsaType updated to %d", static_cast<int>(mEsaType));
MatterReportingAttributeChangeCallback(mEndpointId, DeviceEnergyManagement::Id, ESAType::Id);
}
return CHIP_NO_ERROR;
}
CHIP_ERROR DeviceEnergyManagementDelegate::SetESACanGenerate(bool newValue)
{
bool oldValue = mEsaCanGenerate;
mEsaCanGenerate = newValue;
if (oldValue != newValue)
{
ChipLogDetail(AppServer, "mEsaCanGenerate updated to %d", static_cast<int>(mEsaCanGenerate));
MatterReportingAttributeChangeCallback(mEndpointId, DeviceEnergyManagement::Id, ESACanGenerate::Id);
}
return CHIP_NO_ERROR;
}
CHIP_ERROR DeviceEnergyManagementDelegate::SetESAState(ESAStateEnum newValue)
{
ESAStateEnum oldValue = mEsaState;
if (newValue >= ESAStateEnum::kUnknownEnumValue)
{
return CHIP_IM_GLOBAL_STATUS(ConstraintError);
}
mEsaState = newValue;
if (oldValue != newValue)
{
ChipLogDetail(AppServer, "mEsaState updated to %d", static_cast<int>(mEsaState));
MatterReportingAttributeChangeCallback(mEndpointId, DeviceEnergyManagement::Id, ESAState::Id);
}
return CHIP_NO_ERROR;
}
CHIP_ERROR DeviceEnergyManagementDelegate::SetAbsMinPower(int64_t newValue)
{
int64_t oldValue = mAbsMinPower;
mAbsMinPower = newValue;
if (oldValue != newValue)
{
ChipLogDetail(AppServer, "mAbsMinPower updated to %d", static_cast<int>(mAbsMinPower));
MatterReportingAttributeChangeCallback(mEndpointId, DeviceEnergyManagement::Id, AbsMinPower::Id);
}
return CHIP_NO_ERROR;
}
CHIP_ERROR DeviceEnergyManagementDelegate::SetAbsMaxPower(int64_t newValue)
{
int64_t oldValue = mAbsMaxPower;
mAbsMaxPower = newValue;
if (oldValue != newValue)
{
ChipLogDetail(AppServer, "mAbsMaxPower updated to %d", static_cast<int>(mAbsMaxPower));
MatterReportingAttributeChangeCallback(mEndpointId, DeviceEnergyManagement::Id, AbsMaxPower::Id);
}
return CHIP_NO_ERROR;
}
CHIP_ERROR
DeviceEnergyManagementDelegate::SetPowerAdjustmentCapability(PowerAdjustmentCapability::TypeInfo::Type powerAdjustmentCapability)
{
// TODO see Issue #31147
return CHIP_NO_ERROR;
}
CHIP_ERROR DeviceEnergyManagementDelegate::SetForecast(DataModel::Nullable<Structs::ForecastStruct::Type> forecast)
{
// TODO see Issue #31147
return CHIP_NO_ERROR;
}
CHIP_ERROR DeviceEnergyManagementDelegate::SetOptOutState(OptOutStateEnum newValue)
{
return CHIP_NO_ERROR;
}