99"""
1010
1111import logging
12-
13- try :
14- import queue
15- except ImportError :
16- import Queue as queue
12+ from collections import deque
1713
1814from can import Message
1915from can .bus import BusABC
2218
2319try :
2420 import ics
25- except ImportError :
26- logger .error (
21+ except ImportError as ie :
22+ logger .warning (
2723 "You won't be able to use the ICS NeoVi can backend without the "
28- "python-ics module installed!"
24+ "python-ics module installed!: %s" , ie
2925 )
3026 ics = None
3127
@@ -52,7 +48,7 @@ def __init__(self, channel=None, can_filters=None, **config):
5248 """
5349 super (NeoViBus , self ).__init__ (channel , can_filters , ** config )
5450 if ics is None :
55- raise Exception ('Please install python-ics' )
51+ raise ImportError ('Please install python-ics' )
5652
5753 logger .info ("CAN Filters: {}" .format (can_filters ))
5854 logger .info ("Got configuration of: {}" .format (config ))
@@ -61,6 +57,7 @@ def __init__(self, channel=None, can_filters=None, **config):
6157 config .get ('use_system_timestamp' , False )
6258 )
6359
60+ # TODO: Add support for multiples channels
6461 try :
6562 channel = int (channel )
6663 except ValueError :
@@ -81,7 +78,7 @@ def __init__(self, channel=None, can_filters=None, **config):
8178
8279 self .sw_filters = None
8380 self .set_filters (can_filters )
84- self .rx_buffer = queue . Queue ()
81+ self .rx_buffer = deque ()
8582 self .opened = True
8683
8784 self .network = int (channel ) if channel is not None else None
@@ -129,14 +126,9 @@ def _process_msg_queue(self, timeout=None):
129126 for ics_msg in messages :
130127 if ics_msg .NetworkID != self .network :
131128 continue
132- if ics_msg .ArbIDOrHeader == 0 :
133- # Looks like ICS device sends frames with ArbIDOrHeader = 0
134- # Need to find out exactly what they are for
135- # Filtering them for now
136- continue
137129 if not self ._is_filter_match (ics_msg .ArbIDOrHeader ):
138130 continue
139- self .rx_buffer .put (ics_msg )
131+ self .rx_buffer .append (ics_msg )
140132 if errors :
141133 logger .warning ("%d errors found" % errors )
142134
@@ -196,18 +188,21 @@ def _ics_msg_to_message(self, ics_msg):
196188 ),
197189 is_remote_frame = bool (
198190 ics_msg .StatusBitField & ics .SPY_STATUS_REMOTE_FRAME
199- )
191+ ),
192+ channel = ics_msg .NetworkID
200193 )
201194
202195 def recv (self , timeout = None ):
203- try :
196+ msg = None
197+ if not self .rx_buffer :
204198 self ._process_msg_queue (timeout = timeout )
205- ics_msg = self .rx_buffer .get_nowait ()
206- self .rx_buffer .task_done ()
207- return self ._ics_msg_to_message (ics_msg )
208- except queue .Empty :
199+
200+ try :
201+ ics_msg = self .rx_buffer .popleft ()
202+ msg = self ._ics_msg_to_message (ics_msg )
203+ except IndexError :
209204 pass
210- return None
205+ return msg
211206
212207 def send (self , msg , timeout = None ):
213208 if not self .opened :
0 commit comments