From 2e4a858661b9b8070521021896f86a1cc71c0638 Mon Sep 17 00:00:00 2001 From: Martin Muehlhaeuser Date: Thu, 8 Feb 2018 15:38:48 +0000 Subject: [PATCH 1/2] status: Basic status display working --- octoprint_klipper/__init__.py | 65 +++++++++++++------ octoprint_klipper/static/js/klipper.js | 9 +-- .../templates/klipper_navbar.jinja2 | 2 +- 3 files changed, 51 insertions(+), 25 deletions(-) diff --git a/octoprint_klipper/__init__.py b/octoprint_klipper/__init__.py index 183ce0e..289e969 100644 --- a/octoprint_klipper/__init__.py +++ b/octoprint_klipper/__init__.py @@ -2,18 +2,21 @@ from __future__ import absolute_import import datetime import logging +from enum import Enum import octoprint.plugin import octoprint.plugin.core +from octoprint.util.comm import parse_firmware_line class KlipperPlugin( octoprint.plugin.StartupPlugin, octoprint.plugin.TemplatePlugin, octoprint.plugin.SettingsPlugin, - octoprint.plugin.AssetPlugin): + octoprint.plugin.AssetPlugin, + octoprint.plugin.EventHandlerPlugin): _parsingResponse = False _message = "" - + #-- Startupt Plugin def on_after_startup(self): @@ -43,7 +46,7 @@ class KlipperPlugin( def get_template_configs(self): return [ -# dict(type="navbar", custom_bindings=True), + dict(type="navbar", custom_bindings=True), dict(type="settings", custom_bindings=True), dict(type="generic", name="Assisted Bed Leveling", template="klipper_leveling_dialog.jinja2", custom_bindings=True), dict(type="generic", name="PID Tuning", template="klipper_pid_tuning_dialog.jinja2", custom_bindings=True), @@ -65,42 +68,64 @@ class KlipperPlugin( less=["css/klipper.less"] ) + #-- Event Handler Plugin + + def on_event(self, event, payload): + if "Connecting" == event: + self.updateStatus("info", "Connecting ...") + elif "Connected" == event: + self.updateStatus("info", "Connected to Host") + self.logInfo("Connected to host via %s @%sbps" % (payload["port"], payload["baudrate"])) + elif "Disconnected" == event: + self.updateStatus("info", "Disconnected from Host") + elif "Error" == event: + self.updateStatus("error", "Error") + self.logError(payload["error"]) + #-- GCODE Hook def on_parse_gcode(self, comm, line, *args, **kwargs): - if "//" in line: + if "FIRMWARE_VERSION" in line: + printerInfo = parse_firmware_line(line) + + if "FIRMWARE_VERSION" in printerInfo: + self.logInfo("Firmware version: %s" % printerInfo["FIRMWARE_VERSION"]) + + elif "//" in line: self._parsingResponse = True self._message = self._message + line.strip('/') + else: if self._parsingResponse: self._parsingResponse = False self.logInfo(self._message) self._message = "" + if "!!" in line: - self.logError(line.strip('!')) + msg = line.strip('!') + self.updateStatus("error", msg) + self.logError(msg) + return line - - + #-- Helpers - - def logInfo(self, message): + def sendMessage(self, type, subtype, payload): self._plugin_manager.send_plugin_message( self._identifier, dict( time=datetime.datetime.now().strftime("%H:%M:%S"), - type="info", message=message) - ) + type=type, payload=payload) + ) - def logError(self, error): - self._plugin_manager.send_plugin_message( - self._identifier, - dict( - time=datetime.datetime.now().strftime("%H:%M:%S"), - type="error", - message=error) - ) + def updateStatus(self, type, status): + self.sendMessage("status", type, status) + + def logInfo(self, message): + self.sendMessage("log", "info", message) + def logError(self, error): + self.sendMessage("log", "error", message) @@ -113,6 +138,6 @@ def __plugin_load__(): __plugin_implementation__ = KlipperPlugin() __plugin_hooks__ = { - "octoprint.comm.protocol.gcode.received": __plugin_implementation__.on_parse_gcode, + "octoprint.comm.protocol.gcode.received": __plugin_implementation__.on_parse_gcode } diff --git a/octoprint_klipper/static/js/klipper.js b/octoprint_klipper/static/js/klipper.js index a7524c1..c3eecba 100644 --- a/octoprint_klipper/static/js/klipper.js +++ b/octoprint_klipper/static/js/klipper.js @@ -32,17 +32,14 @@ $(function() { } self.onGetStatus = function() { - self.shortStatus("Updating Status ...") OctoPrint.control.sendGcode("Status") } self.onRestartFirmware = function() { - self.shortStatus("Restarting Firmware ..."); OctoPrint.control.sendGcode("FIRMWARE_RESTART") }; self.onRestartHost = function() { - self.shortStatus("Restarting Host ..."); OctoPrint.control.sendGcode("RESTART") }; @@ -53,7 +50,11 @@ $(function() { self.onDataUpdaterPluginMessage = function(plugin, message) { if(plugin == "klipper") { - self.logMessage(message["time"], message["type"], message["message"]); + if(message["type"] == "status") { + self.shortStatus(message["payload"]); + } else { + self.logMessage(message["time"], message["subtype"], message["payload"]); + } } } diff --git a/octoprint_klipper/templates/klipper_navbar.jinja2 b/octoprint_klipper/templates/klipper_navbar.jinja2 index c7c2fdc..881f9a9 100644 --- a/octoprint_klipper/templates/klipper_navbar.jinja2 +++ b/octoprint_klipper/templates/klipper_navbar.jinja2 @@ -1 +1 @@ - +Idle From efabe991dfdbb94afe4d0bd962a46cefca0f3eaa Mon Sep 17 00:00:00 2001 From: Martin Muehlhaeuser Date: Mon, 26 Feb 2018 10:46:33 +0000 Subject: [PATCH 2/2] Fix: Indentation error --- octoprint_klipper/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/octoprint_klipper/__init__.py b/octoprint_klipper/__init__.py index 289e969..339ea80 100644 --- a/octoprint_klipper/__init__.py +++ b/octoprint_klipper/__init__.py @@ -71,13 +71,14 @@ class KlipperPlugin( #-- Event Handler Plugin def on_event(self, event, payload): + self.pollStatus() if "Connecting" == event: self.updateStatus("info", "Connecting ...") elif "Connected" == event: - self.updateStatus("info", "Connected to Host") + self.updateStatus("info", "Connected to host") self.logInfo("Connected to host via %s @%sbps" % (payload["port"], payload["baudrate"])) elif "Disconnected" == event: - self.updateStatus("info", "Disconnected from Host") + self.updateStatus("info", "Disconnected from host") elif "Error" == event: self.updateStatus("error", "Error") self.logError(payload["error"]) @@ -118,6 +119,9 @@ class KlipperPlugin( type=type, payload=payload) ) + def pollStatus(self): + self._printer.commands("STATUS") + def updateStatus(self, type, status): self.sendMessage("status", type, status)