forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEVSEManufacturerImpl.cpp
435 lines (368 loc) · 16.6 KB
/
EVSEManufacturerImpl.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
*
* Copyright (c) 2023-2024 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 <DEMManufacturerDelegate.h>
#include <DeviceEnergyManagementDelegateImpl.h>
#include <EVSEManufacturerImpl.h>
#include <EnergyEvseManager.h>
#include <EnergyTimeUtils.h>
#include <app/clusters/device-energy-management-server/DeviceEnergyManagementTestEventTriggerHandler.h>
#include <app/clusters/electrical-energy-measurement-server/EnergyReportingTestEventTriggerHandler.h>
#include <app/clusters/electrical-energy-measurement-server/electrical-energy-measurement-server.h>
#include <app/clusters/energy-evse-server/EnergyEvseTestEventTriggerHandler.h>
#include <app/clusters/power-source-server/power-source-server.h>
#include <app/server/Server.h>
#include <app-common/zap-generated/attributes/Accessors.h>
#include <protocols/interaction_model/StatusCode.h>
using namespace chip;
using namespace chip::app;
using namespace chip::app::DataModel;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::EnergyEvse;
using namespace chip::app::Clusters::ElectricalPowerMeasurement;
using namespace chip::app::Clusters::ElectricalEnergyMeasurement;
using namespace chip::app::Clusters::ElectricalEnergyMeasurement::Structs;
using namespace chip::app::Clusters::PowerSource;
using namespace chip::app::Clusters::PowerSource::Attributes;
using Protocols::InteractionModel::Status;
CHIP_ERROR EVSEManufacturer::Init()
{
/* Manufacturers should modify this to do any custom initialisation */
/* Register callbacks */
EnergyEvseDelegate * dg = GetEvseManufacturer()->GetEvseDelegate();
if (dg == nullptr)
{
ChipLogError(AppServer, "EVSE Delegate is not initialized");
return CHIP_ERROR_UNINITIALIZED;
}
dg->HwRegisterEvseCallbackHandler(ApplicationCallbackHandler, reinterpret_cast<intptr_t>(this));
ReturnErrorOnFailure(InitializePowerMeasurementCluster());
ReturnErrorOnFailure(InitializePowerSourceCluster());
DeviceEnergyManagementDelegate * dem = GetEvseManufacturer()->GetDEMDelegate();
VerifyOrReturnLogError(dem != nullptr, CHIP_ERROR_UNINITIALIZED);
/* For Device Energy Management we need the ESA to be Online and ready to accept commands */
dem->SetESAState(ESAStateEnum::kOnline);
/*
* This is an example implementation for manufacturers to consider
*
* For Manufacturer to specify the hardware capability in mA:
* dg->HwSetMaxHardwareCurrentLimit(32000); // 32A
*
* For Manufacturer to specify the CircuitCapacity in mA (e.g. from DIP switches)
* dg->HwSetCircuitCapacity(20000); // 20A
*
*/
/* Once the system is initialised then check to see if the state was restored
* (e.g. after a power outage), and if the Enable timer check needs to be started
*/
dg->ScheduleCheckOnEnabledTimeout();
return CHIP_NO_ERROR;
}
/*
* When the EV is plugged in, and asking for demand change the state
* and set the CableAssembly current limit
*
* EnergyEvseDelegate * dg = GetEvseManufacturer()->GetEvseDelegate();
* VerifyOrReturnError(dg != nullptr, CHIP_ERROR_UNINITIALIZED);
*
* dg->HwSetState(StateEnum::kPluggedInDemand);
* dg->HwSetCableAssemblyLimit(63000); // 63A = 63000mA
*
*
* If the vehicle ID can be retrieved (e.g. over Powerline)
* dg->HwSetVehicleID(CharSpan::fromCharString("TEST_VEHICLE_123456789"));
*
*
* If the EVSE has an RFID sensor, the RFID value read can cause an event to be sent
* (e.g. can be used to indicate if a user as tried to activate the charging)
*
* uint8_t uid[10] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE };
* dg->HwSetRFID(ByteSpan(uid));
*/
CHIP_ERROR EVSEManufacturer::Shutdown()
{
return CHIP_NO_ERROR;
}
/**
* @brief Allows a client application to initialise the Accuracy, Measurement types etc
*/
CHIP_ERROR EVSEManufacturer::InitializePowerMeasurementCluster()
{
EVSEManufacturer * mn = GetEvseManufacturer();
VerifyOrReturnError(mn != nullptr, CHIP_ERROR_UNINITIALIZED);
ElectricalPowerMeasurementDelegate * dg = mn->GetEPMDelegate();
VerifyOrReturnError(dg != nullptr, CHIP_ERROR_UNINITIALIZED);
ReturnErrorOnFailure(dg->SetPowerMode(PowerModeEnum::kAc));
return CHIP_NO_ERROR;
}
/**
* @brief Allows a client application to initialise the PowerSource cluster
*/
CHIP_ERROR EVSEManufacturer::InitializePowerSourceCluster()
{
Protocols::InteractionModel::Status status;
status = PowerSource::Attributes::Status::Set(EndpointId(0) /*RootNode*/, PowerSourceStatusEnum::kActive);
VerifyOrReturnError(status == Protocols::InteractionModel::Status::Success, CHIP_ERROR_INTERNAL);
status =
PowerSource::Attributes::FeatureMap::Set(EndpointId(0 /*RootNode*/), static_cast<uint32_t>(PowerSource::Feature::kWired));
VerifyOrReturnError(status == Protocols::InteractionModel::Status::Success, CHIP_ERROR_INTERNAL);
status = PowerSource::Attributes::WiredNominalVoltage::Set(EndpointId(0 /*RootNode*/), 230'000); // 230V in mv
VerifyOrReturnError(status == Protocols::InteractionModel::Status::Success, CHIP_ERROR_INTERNAL);
status = PowerSource::Attributes::WiredMaximumCurrent::Set(EndpointId(0 /*RootNode*/), 32'000); // 32A in mA
VerifyOrReturnError(status == Protocols::InteractionModel::Status::Success, CHIP_ERROR_INTERNAL);
status = PowerSource::Attributes::WiredCurrentType::Set(EndpointId(0 /*RootNode*/), PowerSource::WiredCurrentTypeEnum::kAc);
VerifyOrReturnError(status == Protocols::InteractionModel::Status::Success, CHIP_ERROR_INTERNAL);
status = PowerSource::Attributes::Description::Set(EndpointId(0 /*RootNode*/), CharSpan::fromCharString("Primary Mains Power"));
VerifyOrReturnError(status == Protocols::InteractionModel::Status::Success, CHIP_ERROR_INTERNAL);
chip::EndpointId endpointArray[] = { 1 /* EVSE Endpoint */ };
Span<EndpointId> endpointList = Span<EndpointId>(endpointArray);
// Note per API - we do not need to maintain the span after the SetEndpointList has been called
// since it takes a copy (see power-source-server.cpp)
PowerSourceServer::Instance().SetEndpointList(0 /* Root Node */, endpointList);
return CHIP_NO_ERROR;
}
/**
* @brief Allows a client application to send in power readings into the system
*
* @param[in] aEndpointId - Endpoint to send to EPM Cluster
* @param[in] aActivePower_mW - ActivePower measured in milli-watts
* @param[in] aVoltage_mV - Voltage measured in milli-volts
* @param[in] aActiveCurrent_mA - ActiveCurrent measured in milli-amps
*/
CHIP_ERROR EVSEManufacturer::SendPowerReading(EndpointId aEndpointId, int64_t aActivePower_mW, int64_t aVoltage_mV,
int64_t aActiveCurrent_mA)
{
EVSEManufacturer * mn = GetEvseManufacturer();
VerifyOrReturnError(mn != nullptr, CHIP_ERROR_UNINITIALIZED);
ElectricalPowerMeasurementDelegate * dg = mn->GetEPMDelegate();
VerifyOrReturnError(dg != nullptr, CHIP_ERROR_UNINITIALIZED);
dg->SetActivePower(MakeNullable(aActivePower_mW));
dg->SetVoltage(MakeNullable(aVoltage_mV));
dg->SetActiveCurrent(MakeNullable(aActiveCurrent_mA));
return CHIP_NO_ERROR;
}
/**
* @brief Allows a client application to send cumulative energy readings into the system
*
* This is a helper function to add timestamps to the readings
*
* @param[in] aCumulativeEnergyImported -total energy imported in milli-watthours
* @param[in] aCumulativeEnergyExported -total energy exported in milli-watthours
*/
CHIP_ERROR EVSEManufacturer::SendCumulativeEnergyReading(EndpointId aEndpointId, int64_t aCumulativeEnergyImported,
int64_t aCumulativeEnergyExported)
{
MeasurementData * data = MeasurementDataForEndpoint(aEndpointId);
VerifyOrReturnError(data != nullptr, CHIP_ERROR_UNINITIALIZED);
EnergyMeasurementStruct::Type energyImported;
EnergyMeasurementStruct::Type energyExported;
/** IMPORT */
// Copy last endTimestamp into new startTimestamp if it exists
energyImported.startTimestamp.ClearValue();
energyImported.startSystime.ClearValue();
if (data->cumulativeImported.HasValue())
{
energyImported.startTimestamp = data->cumulativeImported.Value().endTimestamp;
energyImported.startSystime = data->cumulativeImported.Value().endSystime;
}
energyImported.energy = aCumulativeEnergyImported;
/** EXPORT */
// Copy last endTimestamp into new startTimestamp if it exists
energyExported.startTimestamp.ClearValue();
energyExported.startSystime.ClearValue();
if (data->cumulativeExported.HasValue())
{
energyExported.startTimestamp = data->cumulativeExported.Value().endTimestamp;
energyExported.startSystime = data->cumulativeExported.Value().endSystime;
}
energyExported.energy = aCumulativeEnergyExported;
// Get current timestamp
uint32_t currentTimestamp;
CHIP_ERROR err = GetEpochTS(currentTimestamp);
if (err == CHIP_NO_ERROR)
{
// use EpochTS
energyImported.endTimestamp.SetValue(currentTimestamp);
energyExported.endTimestamp.SetValue(currentTimestamp);
}
else
{
ChipLogError(AppServer, "GetEpochTS returned error getting timestamp %" CHIP_ERROR_FORMAT, err.Format());
// use systemTime as a fallback
System::Clock::Milliseconds64 system_time_ms =
std::chrono::duration_cast<System::Clock::Milliseconds64>(chip::Server::GetInstance().TimeSinceInit());
uint64_t nowMS = static_cast<uint64_t>(system_time_ms.count());
energyImported.endSystime.SetValue(nowMS);
energyExported.endSystime.SetValue(nowMS);
}
// call the SDK to update attributes and generate an event
if (!NotifyCumulativeEnergyMeasured(aEndpointId, MakeOptional(energyImported), MakeOptional(energyExported)))
{
ChipLogError(AppServer, "Failed to notify Cumulative Energy reading.");
return CHIP_ERROR_INTERNAL;
}
return CHIP_NO_ERROR;
}
/**
* @brief Allows a client application to send periodic energy readings into the system
*
* This is a helper function to add timestamps to the readings
*
* @param[in] aPeriodicEnergyImported - energy imported in milli-watthours in last period
* @param[in] aPeriodicEnergyExported - energy exported in milli-watthours in last period
*/
CHIP_ERROR EVSEManufacturer::SendPeriodicEnergyReading(EndpointId aEndpointId, int64_t aPeriodicEnergyImported,
int64_t aPeriodicEnergyExported)
{
MeasurementData * data = MeasurementDataForEndpoint(aEndpointId);
VerifyOrReturnError(data != nullptr, CHIP_ERROR_UNINITIALIZED);
EnergyMeasurementStruct::Type energyImported;
EnergyMeasurementStruct::Type energyExported;
/** IMPORT */
// Copy last endTimestamp into new startTimestamp if it exists
energyImported.startTimestamp.ClearValue();
energyImported.startSystime.ClearValue();
if (data->periodicImported.HasValue())
{
energyImported.startTimestamp = data->periodicImported.Value().endTimestamp;
energyImported.startSystime = data->periodicImported.Value().endSystime;
}
energyImported.energy = aPeriodicEnergyImported;
/** EXPORT */
// Copy last endTimestamp into new startTimestamp if it exists
energyExported.startTimestamp.ClearValue();
energyExported.startSystime.ClearValue();
if (data->periodicExported.HasValue())
{
energyExported.startTimestamp = data->periodicExported.Value().endTimestamp;
energyExported.startSystime = data->periodicExported.Value().endSystime;
}
energyExported.energy = aPeriodicEnergyExported;
// Get current timestamp
uint32_t currentTimestamp;
CHIP_ERROR err = GetEpochTS(currentTimestamp);
if (err == CHIP_NO_ERROR)
{
// use EpochTS
energyImported.endTimestamp.SetValue(currentTimestamp);
energyExported.endTimestamp.SetValue(currentTimestamp);
}
else
{
ChipLogError(AppServer, "GetEpochTS returned error getting timestamp");
// use systemTime as a fallback
System::Clock::Milliseconds64 system_time_ms =
std::chrono::duration_cast<System::Clock::Milliseconds64>(chip::Server::GetInstance().TimeSinceInit());
uint64_t nowMS = static_cast<uint64_t>(system_time_ms.count());
energyImported.endSystime.SetValue(nowMS);
energyExported.endSystime.SetValue(nowMS);
}
// call the SDK to update attributes and generate an event
if (!NotifyPeriodicEnergyMeasured(aEndpointId, MakeOptional(energyImported), MakeOptional(energyExported)))
{
ChipLogError(AppServer, "Failed to notify Cumulative Energy reading.");
return CHIP_ERROR_INTERNAL;
}
return CHIP_NO_ERROR;
}
/**
* @brief Main Callback handler - to be implemented by Manufacturer
*
* @param EVSECbInfo describes the type of call back, and a union of structs
* which contain relevant info for the specific callback type
*
* @param arg - optional pointer to some context information (see register function)
*/
void EVSEManufacturer::ApplicationCallbackHandler(const EVSECbInfo * cb, intptr_t arg)
{
EVSEManufacturer * pClass = reinterpret_cast<EVSEManufacturer *>(arg);
switch (cb->type)
{
case EVSECallbackType::StateChanged:
ChipLogProgress(AppServer, "EVSE callback - state changed");
break;
case EVSECallbackType::ChargeCurrentChanged:
ChipLogProgress(AppServer, "EVSE callback - maxChargeCurrent changed to %ld",
static_cast<long>(cb->ChargingCurrent.maximumChargeCurrent));
break;
case EVSECallbackType::EnergyMeterReadingRequested:
ChipLogProgress(AppServer, "EVSE callback - EnergyMeterReadingRequested");
if (cb->EnergyMeterReadingRequest.meterType == ChargingDischargingType::kCharging)
{
*(cb->EnergyMeterReadingRequest.energyMeterValuePtr) = pClass->mLastChargingEnergyMeter;
}
else
{
*(cb->EnergyMeterReadingRequest.energyMeterValuePtr) = pClass->mLastDischargingEnergyMeter;
}
break;
default:
ChipLogError(AppServer, "Unhandled EVSE Callback type %d", static_cast<int>(cb->type));
}
}
// The PowerAdjustEnd event needs to report the approximate energy used by the ESA during the session.
int64_t EVSEManufacturer::GetApproxEnergyDuringSession()
{
return 300;
}
CHIP_ERROR EVSEManufacturer::HandleDeviceEnergyManagementPowerAdjustRequest(const int64_t powerMw, const uint32_t durationS,
AdjustmentCauseEnum cause)
{
return CHIP_NO_ERROR;
}
CHIP_ERROR EVSEManufacturer::HandleDeviceEnergyManagementPowerAdjustCompletion()
{
return CHIP_NO_ERROR;
}
CHIP_ERROR EVSEManufacturer::HandleDeviceEnergyManagementCancelPowerAdjustRequest(CauseEnum cause)
{
return CHIP_NO_ERROR;
}
CHIP_ERROR EVSEManufacturer::HandleDeviceEnergyManagementStartTimeAdjustRequest(const uint32_t requestedStartTimeUtc,
AdjustmentCauseEnum cause)
{
return CHIP_NO_ERROR;
}
CHIP_ERROR EVSEManufacturer::HandleDeviceEnergyManagementPauseRequest(const uint32_t durationS, AdjustmentCauseEnum cause)
{
return CHIP_NO_ERROR;
}
CHIP_ERROR EVSEManufacturer::HandleDeviceEnergyManagementCancelPauseRequest(CauseEnum cause)
{
return CHIP_NO_ERROR;
}
CHIP_ERROR EVSEManufacturer::HandleDeviceEnergyManagementPauseCompletion()
{
return CHIP_NO_ERROR;
}
CHIP_ERROR EVSEManufacturer::HandleDeviceEnergyManagementCancelRequest()
{
return CHIP_NO_ERROR;
}
CHIP_ERROR EVSEManufacturer::HandleModifyForecastRequest(
const uint32_t forecastID,
const DataModel::DecodableList<DeviceEnergyManagement::Structs::SlotAdjustmentStruct::DecodableType> & slotAdjustments,
AdjustmentCauseEnum cause)
{
return CHIP_NO_ERROR;
}
CHIP_ERROR EVSEManufacturer::RequestConstraintBasedForecast(
const DataModel::DecodableList<DeviceEnergyManagement::Structs::ConstraintsStruct::DecodableType> & constraints,
AdjustmentCauseEnum cause)
{
return CHIP_NO_ERROR;
}