Merge pull request #1 from mmone/autostatus

Merge "Autostatus" Branch
This commit is contained in:
mmone 2018-03-02 12:30:37 +01:00 committed by GitHub
commit 823d12e6ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 25 deletions

View File

@ -2,18 +2,21 @@
from __future__ import absolute_import from __future__ import absolute_import
import datetime import datetime
import logging import logging
from enum import Enum
import octoprint.plugin import octoprint.plugin
import octoprint.plugin.core import octoprint.plugin.core
from octoprint.util.comm import parse_firmware_line
class KlipperPlugin( class KlipperPlugin(
octoprint.plugin.StartupPlugin, octoprint.plugin.StartupPlugin,
octoprint.plugin.TemplatePlugin, octoprint.plugin.TemplatePlugin,
octoprint.plugin.SettingsPlugin, octoprint.plugin.SettingsPlugin,
octoprint.plugin.AssetPlugin): octoprint.plugin.AssetPlugin,
octoprint.plugin.EventHandlerPlugin):
_parsingResponse = False _parsingResponse = False
_message = "" _message = ""
#-- Startupt Plugin #-- Startupt Plugin
def on_after_startup(self): def on_after_startup(self):
@ -43,7 +46,7 @@ class KlipperPlugin(
def get_template_configs(self): def get_template_configs(self):
return [ return [
# dict(type="navbar", custom_bindings=True), dict(type="navbar", custom_bindings=True),
dict(type="settings", 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="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), dict(type="generic", name="PID Tuning", template="klipper_pid_tuning_dialog.jinja2", custom_bindings=True),
@ -65,42 +68,68 @@ class KlipperPlugin(
less=["css/klipper.less"] less=["css/klipper.less"]
) )
#-- 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.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 #-- GCODE Hook
def on_parse_gcode(self, comm, line, *args, **kwargs): 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._parsingResponse = True
self._message = self._message + line.strip('/') self._message = self._message + line.strip('/')
else: else:
if self._parsingResponse: if self._parsingResponse:
self._parsingResponse = False self._parsingResponse = False
self.logInfo(self._message) self.logInfo(self._message)
self._message = "" self._message = ""
if "!!" in line: if "!!" in line:
self.logError(line.strip('!')) msg = line.strip('!')
self.updateStatus("error", msg)
self.logError(msg)
return line return line
#-- Helpers #-- Helpers
def sendMessage(self, type, subtype, payload):
def logInfo(self, message):
self._plugin_manager.send_plugin_message( self._plugin_manager.send_plugin_message(
self._identifier, self._identifier,
dict( dict(
time=datetime.datetime.now().strftime("%H:%M:%S"), time=datetime.datetime.now().strftime("%H:%M:%S"),
type="info", message=message) type=type, payload=payload)
) )
def logError(self, error): def pollStatus(self):
self._plugin_manager.send_plugin_message( self._printer.commands("STATUS")
self._identifier,
dict( def updateStatus(self, type, status):
time=datetime.datetime.now().strftime("%H:%M:%S"), self.sendMessage("status", type, status)
type="error",
message=error) def logInfo(self, message):
) self.sendMessage("log", "info", message)
def logError(self, error):
self.sendMessage("log", "error", message)
@ -113,6 +142,6 @@ def __plugin_load__():
__plugin_implementation__ = KlipperPlugin() __plugin_implementation__ = KlipperPlugin()
__plugin_hooks__ = { __plugin_hooks__ = {
"octoprint.comm.protocol.gcode.received": __plugin_implementation__.on_parse_gcode, "octoprint.comm.protocol.gcode.received": __plugin_implementation__.on_parse_gcode
} }

View File

@ -32,17 +32,14 @@ $(function() {
} }
self.onGetStatus = function() { self.onGetStatus = function() {
self.shortStatus("Updating Status ...")
OctoPrint.control.sendGcode("Status") OctoPrint.control.sendGcode("Status")
} }
self.onRestartFirmware = function() { self.onRestartFirmware = function() {
self.shortStatus("Restarting Firmware ...");
OctoPrint.control.sendGcode("FIRMWARE_RESTART") OctoPrint.control.sendGcode("FIRMWARE_RESTART")
}; };
self.onRestartHost = function() { self.onRestartHost = function() {
self.shortStatus("Restarting Host ...");
OctoPrint.control.sendGcode("RESTART") OctoPrint.control.sendGcode("RESTART")
}; };
@ -53,7 +50,11 @@ $(function() {
self.onDataUpdaterPluginMessage = function(plugin, message) { self.onDataUpdaterPluginMessage = function(plugin, message) {
if(plugin == "klipper") { 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"]);
}
} }
} }

View File

@ -1 +1 @@
<!--<a href="#tab_plugin_klipper" data-bind="text: shortStatus">Idle</a>--> <a href="#tab_plugin_klipper" data-bind="text: shortStatus">Idle</a>