kantlivelong / OctoPrint-PSUControl

Smart control of your power supply via GPIO, GCODE Command, System Command, or variety of sub-plugins.
GNU Affero General Public License v3.0
203 stars 113 forks source link

`Turn off when an unrecoverable firmware or communication error occurs` does not work on serial exception #260

Open shadow578 opened 1 year ago

shadow578 commented 1 year ago

What were you doing?

  1. configure PSU Control normally (using system command switching, but shouldn't matter for this issue)
  2. enable Turn off when an unrecoverable firmware or communication error occurs option
  3. turn on the psu and connect the printer
  4. cause a serial communication error (e.g. unplug the printer usb cable)

What did you expect to happen?

I'd expect the psu to shut down when a communication error occurs.

What happened instead?

Printer is disconnected (with error), but psu stays on.

Version of OctoPrint-PSUControl

1.0.6

Operating System running OctoPrint

OctoPrint 1.8.6, Python 3.9.2, Linux

Printer model & used firmware incl. version

Link to octoprint.log with octoprnt.plugins.psucontrol set to DEBUG

https://gist.github.com/shadow578/b7cb8de193a29c4435b81cf0c9be9529

shadow578 commented 1 year ago

Ok, so i did some digging and it seems like OctoPrint does not raise a ERROR event when the serial connection fails (at all). Since the plugin only checks for the ERROR event, it cannot do anything when the serial connection does fail.

However, OctoPrint does raise a DISCONNECTED event. when handling this event*, the printer does turn of as expected:

def on_event(self, event, payload):
    if event == Events.CLIENT_OPENED:
        self._plugin_manager.send_plugin_message(self._identifier, dict(isPSUOn=self.isPSUOn))
        return
    elif event == Events.ERROR and self.config['turnOffWhenError']:
        self._logger.info("Firmware or communication error detected. Turning PSU Off")
        self.turn_psu_off()
        return
    elif event == Events.DISCONNECTED and self.isPSUOn: # <- added branch
        self._logger.info("Printer disconnect detected. Turning PSU Off")
        self.turn_psu_off()
        return

* event handling was botched directly into the installed plugin, in init.py. Will break on next update, but good enough for testing i guess.


is this expected behaviour (by the plugin and OctoPrint)? if yes, this should be clearly mentioned in the docs.

additionally, would it make sense to add an config option like 'Turn off on printer disconnect' that turns off the psu on DISCONNECTED event?

additionally, i saw that OctoPrint has the following events too (see https://docs.octoprint.org/en/master/events/index.html#printer-communication):

Maybe adding (configurable) options to turn off the psu on those events could be helpful too?