Skip to content

Commit

Permalink
EVNotify: expand exception handler to skip the whole loop in case of …
Browse files Browse the repository at this point in the history
…communication problem (#82)

* EVNotify: expand exception handler to skip the whole loop in case of
communication problem; before it would not set some variables causing
unhandled exceptions because of undefined variables

* SocketCAN: fix the iso-tp implementation, it was broken. Luckily PnP
doesn't use that code path ...
Also remove unneeded break.

Co-authored-by: Malte Schröder <[email protected]>
  • Loading branch information
noradtux and Malte Schröder authored Nov 11, 2020
1 parent 8374591 commit ee3e5cc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 42 deletions.
5 changes: 2 additions & 3 deletions dongle/socket_can.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def send_command_ex_canraw(self, cmd, cantx, canrx):
sock.bind((self._config['port'],))
sock.settimeout(0.2)

sock.setFiltersEx([{
sock.set_filters_ex([{
'id': canrx,
'mask': 0x1fffffff if self._is_extended else 0x7ff
}])
Expand Down Expand Up @@ -233,7 +233,6 @@ def send_command_ex_canraw(self, cmd, cantx, canrx):

data_len = msg_data[0] & 0x0f
data = bytes(msg_data[1:data_len+1])
break

elif frame_type == 0x10:
if self._log.isEnabledFor(logging.DEBUG):
Expand All @@ -245,7 +244,7 @@ def send_command_ex_canraw(self, cmd, cantx, canrx):
if self._log.isEnabledFor(logging.DEBUG):
self._log.debug("Send flow control message")

flow_msg = CANFMT.pack(cantx, 8, b'\x00\x00\x00\x00\x00\x00\x00')
flow_msg = CANFMT.pack(cantx, 8, b'\x30\x00\x00\x00\x00\x00\x00\x00')

sock.send(flow_msg)

Expand Down
78 changes: 39 additions & 39 deletions evnotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,48 +158,48 @@ def submit_data(self):
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)

# 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

# Prime next loop iteration
if self._running:
interval = self._poll_interval - (time() - now)
Expand Down

0 comments on commit ee3e5cc

Please sign in to comment.