-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
evnotify.py
212 lines (175 loc) · 7.44 KB
/
evnotify.py
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
""" Transmit data to EVNotify and handle notifications """
from time import time, sleep
from threading import Thread, Condition
import logging
import EVNotifyAPI
EVN_SETTINGS_INTERVAL = 300
ABORT_NOTIFICATION_INTERVAL = 60
EXTENDED_FIELDS = { # value is decimal places
'auxBatteryVoltage': 1,
'batteryInletTemperature': 1,
'batteryMaxTemperature': 1,
'batteryMinTemperature': 1,
'cumulativeEnergyCharged': 1,
'cumulativeEnergyDischarged': 1,
'charging': 0,
'normalChargePort': 0,
'rapidChargePort': 0,
'dcBatteryCurrent': 2,
'dcBatteryPower': 2,
'dcBatteryVoltage': 2,
'soh': 0,
'externalTemperature': 1,
'odo': 0
}
ARMED = 0
SENT = 1
FAILED = -1
class EVNotify:
""" Interface to EVNotify. """
def __init__(self, config, car):
self._log = logging.getLogger("EVNotiPi/EVNotify")
self._log.info("Initializing EVNotify")
self._car = car
self._config = config
self._poll_interval = config['interval']
self._running = False
self._thread = None
self._data = []
self._gps_data = []
self._data_lock = Condition()
def start(self):
""" Start submit thread. """
self._running = True
self._thread = Thread(target=self.submit_data, name="EVNotiPi/EVNotify")
self._thread.start()
self._car.register_data(self.data_callback)
def stop(self):
""" Stop submit thread. """
self._car.unregister_data(self.data_callback)
self._running = False
with self._data_lock:
self._data_lock.notify()
self._thread.join()
def data_callback(self, data):
""" Callback to be called from 'car'. """
self._log.debug("Enqeue...")
with self._data_lock:
self._data.append(data)
self._data_lock.notify()
def submit_data(self):
""" Thread that submits data to EVNotify in regular intervals. """
log = self._log
evn = EVNotifyAPI.EVNotify(self._config['akey'], self._config['token'])
abort_notification = ARMED
charging_start_soc = 0
last_charging = time()
last_charging_soc = 0
last_evn_settings_poll = 0
is_charging = 0
is_connected = 0
settings = None
soc_notification = ARMED
soc_threshold = self._config.get('soc_threshold', 100)
log.info("Get settings from backend")
while self._running and settings is None:
try:
settings = evn.getSettings()
except EVNotifyAPI.CommunicationError as err:
log.info("Waiting for network connectivity (%s)", err)
sleep(3)
while self._running:
with self._data_lock:
log.debug('Waiting...')
self._data_lock.wait(max(10, self._poll_interval))
now = time()
# Detect aborted charge
if ((now - last_charging > ABORT_NOTIFICATION_INTERVAL and
charging_start_soc > 0 and 0 < last_charging_soc < soc_threshold and
abort_notification is ARMED) or abort_notification is FAILED):
log.info("Aborted charge detected, send abort notification")
try:
evn.sendNotification(True)
abort_notification = SENT
except EVNotifyAPI.CommunicationError as err:
log.error("Communication Error: %s", err)
abort_notification = FAILED
if len(self._data) == 0:
continue
new_data = self._data.copy()
self._data.clear()
log.debug("Transmit...")
avgs = {
'dcBatteryCurrent': [],
'dcBatteryPower': [],
'dcBatteryVoltage': [],
'speed': [],
'latitude': [],
'longitude': [],
'altitude': [],
}
for data in new_data:
for key, values in avgs.items():
if data.get(key, None) is not None:
values.append(data[key])
# Need to copy data here because we update it later
data = new_data[-1]
data.update({k: sum(v)/len(v)
for k, v in avgs.items() if len(v) > 0})
try:
if (data['SOC_DISPLAY'] is not None or
data['SOC_BMS'] is not None):
evn.setSOC(data['SOC_DISPLAY'], data['SOC_BMS'])
current_soc = data['SOC_DISPLAY'] or data['SOC_BMS']
is_charging = bool(data['charging'])
is_connected = bool(data['normalChargePort'] or data['rapidChargePort'])
extended_data = {a: round(data[a], EXTENDED_FIELDS[a])
for a in EXTENDED_FIELDS if data[a] is not None}
log.debug(extended_data)
evn.setExtended(extended_data)
if data['fix_mode'] > 1:
location = {a: data[a]
for a in ('latitude', 'longitude', 'speed')}
evn.setLocation({'location': location})
# Notification handling from here on
if is_charging and now - last_evn_settings_poll > EVN_SETTINGS_INTERVAL:
try:
settings = evn.getSettings()
last_evn_settings_poll = now
if 'soc' in settings:
new_soc = int(settings['soc'])
if new_soc != soc_threshold:
soc_threshold = new_soc
log.info("New notification threshold: %i",
soc_threshold)
except EVNotifyAPI.CommunicationError as err:
log.error("Communication error occured %s", err)
# track charging started
if is_charging and charging_start_soc == 0:
charging_start_soc = current_soc or 0
elif not is_connected: # Rearm abort notification
charging_start_soc = 0
abort_notification = ARMED
# SoC threshold notification
if ((is_charging and 0 < last_charging_soc < soc_threshold <= current_soc)
or soc_notification is FAILED):
log.info("Notification threshold(%i) reached: %i",
soc_threshold, current_soc)
try:
evn.sendNotification()
soc_notification = ARMED
except EVNotifyAPI.CommunicationError as err:
log.info("Communication Error: %s", err)
soc_notification = FAILED
if is_charging:
last_charging = now
last_charging_soc = current_soc
except EVNotifyAPI.CommunicationError as err:
log.info("Communication Error: %s", err)
# Prime next loop iteration
if self._running:
interval = self._poll_interval - (time() - now)
sleep(max(0, interval))
def check_thread(self):
""" Return running state of thread. """
return self._thread.is_alive()