-
Notifications
You must be signed in to change notification settings - Fork 117
/
evse_man.h
523 lines (452 loc) · 15.3 KB
/
evse_man.h
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#ifndef _OPENEVSE_EVSE_MAN_H
#define _OPENEVSE_EVSE_MAN_H
#include <Arduino.h>
#include <RapiSender.h>
#include <openevse.h>
#include <MicroTasks.h>
#include "evse_state.h"
#include "evse_monitor.h"
#include "event_log.h"
#include "json_serialize.h"
#include "app_config.h"
typedef uint32_t EvseClient;
#define EvseClient_Vendor_SHIFT 16
#define EvseClient_Vendor_OpenEVSE 0x0001
#define EvseClient_Vendor_OpenEnergyMonitor 0x0002
#define EvseClient_Vendor_BigJungle 0x0003
#define EvseClient_Vendor_Unregistered 0xFFFE
#define EvseClient_Vendor_Reserved 0xFFFF
#define EVC(Vendor, Client) ((EvseClient)((((Vendor) << EvseClient_Vendor_SHIFT) | (Client))))
#define EvseClient_OpenEVSE_Manual EVC(EvseClient_Vendor_OpenEVSE, 0x0001)
#define EvseClient_OpenEVSE_Divert EVC(EvseClient_Vendor_OpenEVSE, 0x0002)
#define EvseClient_OpenEVSE_Boost EVC(EvseClient_Vendor_OpenEVSE, 0x0003)
#define EvseClient_OpenEVSE_Schedule EVC(EvseClient_Vendor_OpenEVSE, 0x0004)
#define EvseClient_OpenEVSE_Limit EVC(EvseClient_Vendor_OpenEVSE, 0x0006)
#define EvseClient_OpenEVSE_Error EVC(EvseClient_Vendor_OpenEVSE, 0x0007)
#define EvseClient_OpenEVSE_Ohm EVC(EvseClient_Vendor_OpenEVSE, 0x0008)
#define EvseClient_OpenEVSE_OCPP EVC(EvseClient_Vendor_OpenEVSE, 0x0009)
#define EvseClient_OpenEVSE_RFID EVC(EvseClient_Vendor_OpenEVSE, 0x000A)
#define EvseClient_OpenEVSE_MQTT EVC(EvseClient_Vendor_OpenEVSE, 0x000B)
#define EvseClient_OpenEVSE_Shaper EVC(EvseClient_Vendor_OpenEVSE, 0x000C)
#define EvseClient_OpenEnergyMonitor_DemandShaper EVC(EvseClient_Vendor_OpenEnergyMonitor, 0x0001)
#define EvseClient_NULL ((EvseClient)UINT32_MAX)
#define EvseManager_Priority_Default 10
#define EvseManager_Priority_Divert 50
#define EvseManager_Priority_Timer 100
#define EvseManager_Priority_Boost 200
#define EvseManager_Priority_API 500
#define EvseManager_Priority_MQTT 500
#define EvseManager_Priority_Ohm 500
#define EvseManager_Priority_Manual 1000
#define EvseManager_Priority_RFID 1030
#define EvseManager_Priority_OCPP 1050
#define EvseManager_Priority_Limit 1100
#define EvseManager_Priority_Safety 5000
#define EvseManager_Priority_Error 10000
#define EVSE_VEHICLE_SOC (1 << 0)
#define EVSE_VEHICLE_RANGE (1 << 1)
#define EVSE_VEHICLE_ETA (1 << 2)
#ifndef EVSE_MANAGER_MAX_CLIENT_CLAIMS
#define EVSE_MANAGER_MAX_CLIENT_CLAIMS 10
#endif // !EVSE_MANAGER_MAX_CLIENT_CLAIMS
class EvseProperties : virtual public JsonSerialize<512>
{
private:
EvseState _state;
uint32_t _charge_current;
uint32_t _max_current;
uint32_t _energy_limit;
uint32_t _time_limit;
bool _auto_release;
bool _has_auto_release = false;
public:
EvseProperties();
EvseProperties(EvseState state);
void clear();
// Get/set the EVSE state, either active or disabled
EvseState getState() {
return _state;
}
void setState(EvseState state) {
_state = state;
}
// Get/set charge current,
uint32_t getChargeCurrent() {
return _charge_current;
}
void setChargeCurrent(uint32_t charge_current) {
_charge_current = charge_current;
}
// Get/set the max current, overides limits the charge current (irrespective of priority) but
// does not override the configured max charge current of the hardware.
uint32_t getMaxCurrent() {
return _max_current;
}
void setMaxCurrent(uint32_t max_current) {
_max_current = max_current;
}
// Get/set the energy max to transfer for this charge session/client, after which the default
// session state will be set to EvseState::Disabled and the client automatically released.
uint32_t getEnergyLimit() {
return _energy_limit;
}
void setEnergyLimit(uint32_t energy_limit) {
_energy_limit = energy_limit;
}
// Get/set the time to stop the charge session/client, after which the default session state
// will be set to EvseState::Disabled and the client automatically released.
uint32_t getTimeLimit() {
return _time_limit;
}
void setTimeLimit(uint32_t time_limit) {
_time_limit = time_limit;
}
// Get/set the client auto release state. With the client auto release enabled the client claim
// will automatically be released at the end of the charging session.
bool isAutoRelease() {
return _auto_release;
}
bool hasAutoRelease() {
return _has_auto_release;
}
void setAutoRelease(bool auto_release) {
_auto_release = auto_release;
_has_auto_release = true;
}
EvseProperties & operator = (EvseProperties &rhs);
EvseProperties & operator = (EvseState &rhs) {
_state = rhs;
return *this;
}
bool equals(EvseProperties &rhs) {
return this->_state == rhs._state &&
this->_charge_current == rhs._charge_current &&
this->_max_current == rhs._max_current &&
this->_energy_limit == rhs._energy_limit &&
this->_time_limit == rhs._time_limit &&
this->_auto_release == rhs._auto_release;
}
bool equals(EvseState &rhs) {
return this->_state == rhs;
}
bool operator == (EvseProperties &rhs) {
return this->equals(rhs);
}
bool operator == (EvseState &rhs) {
return this->equals(rhs);
}
bool operator != (EvseProperties &rhs) {
return !equals(rhs);
}
bool operator != (EvseState &rhs) {
return !equals(rhs);
}
using JsonSerialize::deserialize;
virtual bool deserialize(JsonObject &obj);
using JsonSerialize::serialize;
virtual bool serialize(JsonObject &obj);
};
class EvseManager : public MicroTasks::Task
{
private:
class Claim
{
private:
EvseClient _client;
int _priority;
EvseProperties _properties;
public:
Claim();
bool claim(EvseClient client, int priority, EvseProperties &target);
void release();
bool isValid() {
return _client != EvseClient_NULL;
}
bool operator==(EvseClient rhs) const {
return _client == rhs;
}
EvseClient getClient() {
return _client;
}
int getPriority() {
return _priority;
}
EvseState getState() {
return _properties.getState();
}
uint32_t getChargeCurrent() {
return _properties.getChargeCurrent();
}
uint32_t getMaxCurrent() {
return _properties.getMaxCurrent();
}
uint32_t getEnergyLimit() {
return _properties.getEnergyLimit();
}
uint32_t getTimeLimit() {
return _properties.getTimeLimit();
}
bool isAutoRelease() {
return _properties.isAutoRelease();
}
bool hasAutoRelease() {
return _properties.hasAutoRelease();
}
EvseProperties &getProperties() {
return _properties;
}
};
RapiSender _sender;
EvseMonitor _monitor;
EventLog &_eventLog;
Claim _clients[EVSE_MANAGER_MAX_CLIENT_CLAIMS];
MicroTasks::EventListener _evseStateListener;
MicroTasks::EventListener _evseBootListener;
MicroTasks::EventListener _sessionCompleteListener;
EvseProperties _targetProperties;
bool _hasClaims;
uint8_t _version;
EvseClient _state_client;
EvseClient _charge_current_client;
EvseClient _max_current_client;
EvseClient _energy_limit_client;
EvseClient _time_limit_client;
bool _sleepForDisable;
bool _evaluateClaims;
bool _evaluateTargetState;
int _waitingForEvent;
uint32_t _vehicleValid;
uint32_t _vehicleUpdated;
uint32_t _vehicleLastUpdated;
int _vehicleStateOfCharge;
int _vehicleRange;
int _vehicleEta;
void initialiseEvse();
bool findClaim(EvseClient client, Claim **claim = NULL);
bool evaluateClaims(EvseProperties &properties);
void releaseAutoReleaseClaims();
bool setTargetState(EvseProperties &properties);
EvseState getActiveState() {
return _monitor.isDisabled() ? EvseState::Disabled : EvseState::Active;
}
protected:
void setup();
unsigned long loop(MicroTasks::WakeReason reason);
public:
EvseManager(Stream &port, EventLog &eventLog);
~EvseManager();
bool begin();
bool claim(EvseClient client, int priority, EvseProperties &target);
bool release(EvseClient client);
bool clientHasClaim(EvseClient client);
uint8_t getClaimsVersion();
EvseProperties &getClaimProperties(EvseClient client);
EvseState getState(EvseClient client = EvseClient_NULL);
uint32_t getChargeCurrent(EvseClient client = EvseClient_NULL);
uint32_t getMaxCurrent(EvseClient client = EvseClient_NULL);
uint32_t getEnergyLimit(EvseClient client = EvseClient_NULL);
uint32_t getTimeLimit(EvseClient client = EvseClient_NULL);
bool serializeClaims(DynamicJsonDocument &doc);
bool serializeClaim(DynamicJsonDocument &doc, EvseClient client);
bool serializeTarget(DynamicJsonDocument &doc);
// Evse Status
bool isConnected() {
return OpenEVSE.isConnected();
}
bool isActive() {
return getActiveState() == EvseState::Active;
}
uint8_t getEvseState() {
return _monitor.getEvseState();
}
uint8_t getPilotState() {
return _monitor.getPilotState();
}
uint32_t getFlags() {
return _monitor.getFlags();
}
uint8_t getStateColour();
bool isVehicleConnected() {
return _monitor.isVehicleConnected();
}
bool isError() {
return _monitor.isError();
}
bool isCharging() {
return _monitor.isCharging();
}
double getAmps() {
return _monitor.getAmps();
}
double getVoltage() {
return _monitor.getVoltage();
}
void setVoltage(double volts) {
_monitor.setVoltage(volts);
}
uint32_t getSessionElapsed() {
return _monitor.getSessionElapsed();
}
double getSessionEnergy() {
return _monitor.getSessionEnergy();
}
double getTotalEnergy() {
return _monitor.getTotalEnergy();
}
long getFaultCountGFCI() {
return _monitor.getFaultCountGFCI();
}
long getFaultCountNoGround() {
return _monitor.getFaultCountNoGround();
}
long getFaultCountStuckRelay() {
return _monitor.getFaultCountStuckRelay();
}
double getTemperature(uint8_t sensor) {
return _monitor.getTemperature(sensor);
}
double isTemperatureValid(uint8_t sensor) {
return _monitor.isTemperatureValid(sensor);
}
bool isDiodeCheckEnabled() {
return _monitor.isDiodeCheckEnabled();
}
bool isVentRequiredEnabled() {
return _monitor.isVentRequiredEnabled();
}
bool isGroundCheckEnabled() {
return _monitor.isGroundCheckEnabled();
}
bool isStuckRelayCheckEnabled() {
return _monitor.isStuckRelayCheckEnabled();
}
bool isGfiTestEnabled() {
return _monitor.isGfiTestEnabled();
}
bool isTemperatureCheckEnabled() {
return _monitor.isTemperatureCheckEnabled();
}
bool isButtonDisabled() {
return _monitor.isButtonDisabled();
}
bool isAutoStartDisabled() {
return _monitor.isAutoStartDisabled();
}
bool isSerialDebugEnabled() {
return _monitor.isSerialDebugEnabled();
}
EvseMonitor::ServiceLevel getActualServiceLevel() {
return _monitor.getActualServiceLevel();
}
EvseMonitor::ServiceLevel getServiceLevel() {
return _monitor.getServiceLevel();
}
void setServiceLevel(EvseMonitor::ServiceLevel level) {
_monitor.setServiceLevel(level);
}
EvseMonitor::LcdType getLcdType() {
return _monitor.getLcdType();
}
const char *getFirmwareVersion() {
return _monitor.getFirmwareVersion();
}
const char *getSerial() {
return _monitor.getSerial();
}
long getMinCurrent() {
return _monitor.getMinCurrent();
}
void setMaxConfiguredCurrent(long amps);
long getMaxConfiguredCurrent() {
return _monitor.getMaxConfiguredCurrent();
}
long getMaxHardwareCurrent() {
return _monitor.getMaxHardwareCurrent();
}
void configureCurrentSensorScale(long scale, long offset) {
_monitor.configureCurrentSensorScale(scale, offset);
}
long getCurrentSensorScale() {
return _monitor.getCurrentSensorScale();
}
long getCurrentSensorOffset() {
return _monitor.getCurrentSensorOffset();
}
void enableFeature(uint8_t feature, bool enabled, std::function<void(int ret)> callback = NULL) {
_monitor.enableFeature(feature, enabled, callback);
}
void enableDiodeCheck(bool enabled, std::function<void(int ret)> callback = NULL) {
_monitor.enableDiodeCheck(enabled, callback);
}
void enableGfiTestCheck(bool enabled, std::function<void(int ret)> callback = NULL) {
_monitor.enableGfiTestCheck(enabled, callback);
}
void enableGroundCheck(bool enabled, std::function<void(int ret)> callback = NULL) {
_monitor.enableGroundCheck(enabled, callback);
}
void enableStuckRelayCheck(bool enabled, std::function<void(int ret)> callback = NULL) {
_monitor.enableStuckRelayCheck(enabled, callback);
}
void enableVentRequired(bool enabled, std::function<void(int ret)> callback = NULL) {
_monitor.enableVentRequired(enabled, callback);
}
void enableTemperatureCheck(bool enabled, std::function<void(int ret)> callback = NULL) {
_monitor.enableTemperatureCheck(enabled, callback);
}
// Get/set the vehicle state
int getVehicleStateOfCharge() {
return _vehicleStateOfCharge;
}
int getVehicleRange() {
return _vehicleRange;
}
int getVehicleEta() {
return _vehicleEta;
}
uint32_t getVehicleLastUpdated() {
return _vehicleLastUpdated;
}
int isVehicleStateOfChargeValid() {
return 0 != (_vehicleValid & EVSE_VEHICLE_SOC);
}
int isVehicleRangeValid() {
return 0 != (_vehicleValid & EVSE_VEHICLE_RANGE);
}
int isVehicleEtaValid() {
return 0 != (_vehicleValid & EVSE_VEHICLE_ETA);
}
void setVehicleStateOfCharge(int vehicleStateOfCharge);
void setVehicleRange(int vehicleRange);
void setVehicleEta(int vehicleEta);
// Get/set the 'disabled' mode
bool isSleepForDisable() {
return _sleepForDisable;
}
void setSleepForDisable(bool sleepForDisable);
// Temp until everything uses EvseManager
RapiSender &getSender() {
return _sender;
}
// Get the OpenEVSE API
OpenEVSEClass &getOpenEVSE() {
return OpenEVSE;
}
// Register for events
void onStateChange(MicroTasks::EventListener *listner) {
_monitor.onStateChange(listner);
}
void onSettingsChanged(MicroTasks::EventListener *listner) {
_monitor.onSettingsChanged(listner);
}
void onDataReady(MicroTasks::EventListener *listner) {
_monitor.onDataReady(listner);
}
void onBootReady(MicroTasks::EventListener *listner) {
_monitor.onBootReady(listner);
}
void onSessionComplete(MicroTasks::EventListener *listner) {
_monitor.onSessionComplete(listner);
}
bool isRapiCommandBlocked(String rapi);
};
#endif // !_OPENEVSE_EVSE_MAN_H