Skip to content

Commit 7298a3b

Browse files
committed
Trigger an event when the charge session is complete
1 parent bc3f804 commit 7298a3b

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/evse_monitor.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
EVSE_MONITOR_ENERGY_BOOT_READY \
7171
)
7272

73+
#define EVSE_MONITOR_SESSION_COMPLETE_MASK OPENEVSE_VFLAG_EV_CONNECTED
74+
#define EVSE_MONITOR_SESSION_COMPLETE_TRIGGER 0
75+
7376
EvseMonitor::EvseStateEvent::EvseStateEvent() :
7477
MicroTasks::Event(),
7578
_evse_state(OPENEVSE_STATE_STARTING)
@@ -101,14 +104,42 @@ EvseMonitor::DataReady::DataReady(uint32_t ready) :
101104
{
102105
}
103106

104-
void EvseMonitor::DataReady::ready(uint32_t data)
107+
bool EvseMonitor::DataReady::ready(uint32_t data)
105108
{
106109
_state |= data;
107110
if(_ready == (_state & _ready))
108111
{
109112
Trigger();
110113
_state = 0;
114+
return true;
115+
}
116+
117+
return false;
118+
}
119+
120+
EvseMonitor::StateChangeEvent::StateChangeEvent(uint32_t mask, uint32_t trigger) :
121+
MicroTasks::Event(),
122+
_state(0),
123+
_mask(mask),
124+
_trigger(trigger)
125+
{
126+
}
127+
128+
bool EvseMonitor::StateChangeEvent::update(uint32_t data)
129+
{
130+
// Only interested in the mask bits changing
131+
data &= _mask;
132+
if(_state != data)
133+
{
134+
_state = data;
135+
if(_trigger == _state)
136+
{
137+
Trigger();
138+
return true;
139+
}
111140
}
141+
142+
return false;
112143
}
113144

114145
EvseMonitor::EvseMonitor(OpenEVSEClass &openevse) :
@@ -131,6 +162,7 @@ EvseMonitor::EvseMonitor(OpenEVSEClass &openevse) :
131162
_max_hardware_current(0),
132163
_data_ready(EVSE_MONITOR_DATA_READY),
133164
_boot_ready(EVSE_MONITOR_BOOT_READY),
165+
_session_complete(EVSE_MONITOR_SESSION_COMPLETE_MASK, EVSE_MONITOR_SESSION_COMPLETE_TRIGGER),
134166
_count(0),
135167
_heartbeat(false)
136168
#ifdef ENABLE_MCP9808
@@ -226,6 +258,7 @@ void EvseMonitor::evseStateChanged()
226258
if(!isCharging()) {
227259
_amp = 0;
228260
}
261+
_session_complete.update(getFlags());
229262
}
230263

231264
unsigned long EvseMonitor::loop(MicroTasks::WakeReason reason)

src/evse_monitor.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,19 @@ class EvseMonitor : public MicroTasks::Task
6868
public:
6969
DataReady(uint32_t ready);
7070

71-
void ready(uint32_t data);
71+
bool ready(uint32_t data);
72+
};
73+
74+
class StateChangeEvent : public MicroTasks::Event
75+
{
76+
private:
77+
uint32_t _state;
78+
uint32_t _mask;
79+
uint32_t _trigger;
80+
public:
81+
StateChangeEvent(uint32_t mask, uint32_t trigger);
82+
83+
bool update(uint32_t state);
7284
};
7385

7486
class Tempurature
@@ -127,6 +139,7 @@ class EvseMonitor : public MicroTasks::Task
127139

128140
DataReady _data_ready;
129141
DataReady _boot_ready;
142+
StateChangeEvent _session_complete;
130143

131144
uint32_t _count;
132145
bool _heartbeat;
@@ -280,6 +293,9 @@ class EvseMonitor : public MicroTasks::Task
280293
void onBootReady(MicroTasks::EventListener *listner) {
281294
_boot_ready.Register(listner);
282295
}
296+
void onSessionComplete(MicroTasks::EventListener *listner) {
297+
_session_complete.Register(listner);
298+
}
283299
};
284300

285301
#endif // _OPENEVSE_EVSE_MONITOR_H

0 commit comments

Comments
 (0)