Skip to content

Commit 486f776

Browse files
pierreluctghardbyte
authored andcommitted
Adding ICSApiError class and error handling
1 parent 1d708a0 commit 486f776

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

can/interfaces/ics_neovi/neovi_bus.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import logging
1212
from collections import deque
1313

14-
from can import Message
14+
from can import Message, CanError
1515
from can.bus import BusABC
1616

1717
logger = logging.getLogger(__name__)
@@ -26,6 +26,35 @@
2626
ics = None
2727

2828

29+
class ICSApiError(CanError):
30+
# A critical error which affects operation or accuracy.
31+
ICS_SPY_ERR_CRITICAL = 0x10
32+
# An error which is not understood.
33+
ICS_SPY_ERR_QUESTION = 0x20
34+
# An important error which may be critical depending on the application
35+
ICS_SPY_ERR_EXCLAMATION = 0x30
36+
# An error which probably does not need attention.
37+
ICS_SPY_ERR_INFORMATION = 0x40
38+
39+
def __init__(
40+
self, error_number, description_short, description_long,
41+
severity, restart_needed
42+
):
43+
super(ICSApiError, self).__init__(description_short)
44+
self.error_number = error_number
45+
self.description_short = description_short
46+
self.description_long = description_long
47+
self.severity = severity
48+
self.restart_needed = restart_needed == 1
49+
50+
def __str__(self):
51+
return "{} {}".format(self.description_short, self.description_long)
52+
53+
@property
54+
def is_critical(self):
55+
return self.severity == self.ICS_SPY_ERR_CRITICAL
56+
57+
2958
class NeoViBus(BusABC):
3059
"""
3160
The CAN Bus implemented for the python_ics interface
@@ -127,10 +156,13 @@ def _process_msg_queue(self, timeout=None):
127156
continue
128157
self.rx_buffer.append(ics_msg)
129158
if errors:
130-
logger.warning("%d errors found" % errors)
159+
logger.warning("%d error(s) found" % errors)
131160

132161
for msg in ics.get_error_messages(self.dev):
133-
logger.warning(msg)
162+
error = ICSApiError(*msg)
163+
if error.is_critical:
164+
raise error
165+
logger.warning(error)
134166

135167
def _is_filter_match(self, arb_id):
136168
"""
@@ -219,7 +251,11 @@ def send(self, msg, timeout=None):
219251
message.StatusBitField = flags
220252
message.StatusBitField2 = 0
221253
message.NetworkID = self.network
222-
ics.transmit_messages(self.dev, message)
254+
255+
try:
256+
ics.transmit_messages(self.dev, message)
257+
except ics.RuntimeError:
258+
raise ICSApiError(*ics.get_last_api_error(self.dev))
223259

224260
def set_filters(self, can_filters=None):
225261
"""Apply filtering to all messages received by this Bus.

0 commit comments

Comments
 (0)