Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notified when device is disconnected? #48

Open
pmoosman opened this issue Jun 10, 2023 · 2 comments
Open

Notified when device is disconnected? #48

pmoosman opened this issue Jun 10, 2023 · 2 comments

Comments

@pmoosman
Copy link

In my application I would like to get notified if the device gets disconnected.

The underlying python-can package calls a listener's on_error() method when a error occurs on the receive thread. I wonder about adding an on_error() method override to the MessageListener class and allow users of python-can-j1939 to provide an on_error() override.

Thoughts?

@grant-allan-ctct
Copy link
Collaborator

When I looked at this class ElectronicControlUnit code from here

        #: Includes at least MessageListener.
        self._listeners = [MessageListener(self)]

I wondered whether someone had intended to someday support add_listener and remove_listener functions for modifying the _listeners collection (similar to how we already have add_timer and remove_timer provided already by this class for modifying the _timer_events collection). If we did so, it might meet your need too @pmoosman ?

An advantage of allowing new, different listeners to be added is that the MessageListener class itself does certain things that might not in-general be wanted, e.g. it drops any error frames on the floor, in this line.

The python-can package has some nice-looking listeners - it would be great to be able to add one of those stock listeners, or for someone to add their own custom listener, into the _listeners collection under some circumstances.

@grant-allan-ctct
Copy link
Collaborator

I have been experimenting some more with adding/removing arbitrary python-can listeners. Below is an approach that will allow us to very easily add a stock listener - such as a listener that will save a transcription of all of the CAN traffic to a file for later analysis, which would be as easy as add_listener(can.Logger("EverythingThatHappened.asc")) - or a custom listener we might have written. Using similar technique and a custom listener, it should be possible to easily add a listener that responds in some desired way to any CAN errors that occur.

class MultiListenerECU(j1939.ElectronicControlUnit):

  def add_listener(self, listener):
    self._listeners.append(listener)
    if self._notifier is not None:
      self._notifier.add_listener(listener)

  def remove_listener(self, listener):
    self._listeners.remove(listener)
    if self._notifier is not None:
      self._notifier.remove_listener(listener)

But I also think that, better than the above sub-classing workaround, it would be convenient to have these add_listener and remove_listener functions added to the ElectronicControlUnit class itself. But also unit tests to prove it out, and to demonstrate some typical usage scenarios. Maybe the subclassing idea might be of some help in the meantime @pmoosman ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants