paneldue: Add configparser support

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-08-05 20:59:22 -04:00
parent 52907155d2
commit f114985dc6
1 changed files with 26 additions and 44 deletions

View File

@ -9,9 +9,8 @@ import time
import json import json
import errno import errno
import logging import logging
import tempfile
from utils import ServerError from utils import ServerError
from tornado import gen, netutil from tornado import gen
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from tornado.locks import Lock from tornado.locks import Lock
@ -21,28 +20,16 @@ class PanelDueError(ServerError):
pass pass
class SerialConnection: class SerialConnection:
def __init__(self, paneldue): def __init__(self, config, paneldue):
self.ioloop = IOLoop.current() self.ioloop = IOLoop.current()
self.paneldue = paneldue self.paneldue = paneldue
self.port = "" self.port = config.get('serial')
self.baud = 57600 self.baud = config.getint('baud', 57600)
self.sendlock = Lock() self.sendlock = Lock()
self.partial_input = b"" self.partial_input = b""
self.ser = self.fd = None self.ser = self.fd = None
self.connected = False self.connected = False
self.ioloop.spawn_callback(self._connect)
def load_config(self, config):
port = config.get('serial', None)
baud = int(config.get('baud', 57600))
if port is None:
logging.info("No serial port specified, cannot connect")
return
if port != self.port or baud != self.baud or \
not self.connected:
self.disconnect()
self.port = port
self.baud = baud
self.ioloop.spawn_callback(self._connect)
def disconnect(self): def disconnect(self):
if self.connected: if self.connected:
@ -137,13 +124,13 @@ class SerialConnection:
class PanelDue: class PanelDue:
def __init__(self, server): def __init__(self, config):
self.server = server self.server = config.get_server()
self.ioloop = IOLoop.current() self.ioloop = IOLoop.current()
self.ser_conn = SerialConnection(self) self.ser_conn = SerialConnection(config, self)
self.file_manager = self.server.load_plugin('file_manager') self.file_manager = self.server.lookup_plugin('file_manager')
self.kinematics = "none" self.kinematics = "none"
self.machine_name = "Klipper" self.machine_name = config.get('machine_name', "Klipper")
self.firmware_name = "Repetier | Klipper" self.firmware_name = "Repetier | Klipper"
self.last_message = None self.last_message = None
self.last_gcode_response = None self.last_gcode_response = None
@ -154,14 +141,25 @@ class PanelDue:
self.printer_state = { self.printer_state = {
'gcode': {}, 'toolhead': {}, 'virtual_sdcard': {}, 'gcode': {}, 'toolhead': {}, 'virtual_sdcard': {},
'fan': {}, 'display_status': {}, 'print_stats': {}} 'fan': {}, 'display_status': {}, 'print_stats': {}}
self.available_macros = {}
self.non_trivial_keys = []
self.extruder_count = 0 self.extruder_count = 0
self.heaters = [] self.heaters = []
self.is_ready = False self.is_ready = False
self.is_shutdown = False self.is_shutdown = False
self.last_printer_state = 'C' self.last_printer_state = 'C'
# Set up macros
self.available_macros = {}
macros = config.get('macros', None)
if macros is not None:
# The macro's configuration name is the key, whereas the full
# command is the value
macros = [m for m in macros.split('\n') if m.strip()]
self.available_macros = {m.split()[0]: m for m in macros}
ntkeys = config.get('non_trivial_keys', "Klipper state")
self.non_trivial_keys = [k for k in ntkeys.split('\n') if k.strip()]
logging.info("PanelDue Configured")
# Register server events # Register server events
self.server.register_event_handler( self.server.register_event_handler(
"server:klippy_state_changed", self.handle_klippy_state) "server:klippy_state_changed", self.handle_klippy_state)
@ -197,23 +195,6 @@ class PanelDue:
'M999': lambda args: "FIRMWARE_RESTART" 'M999': lambda args: "FIRMWARE_RESTART"
} }
def load_config(self, config):
self.ser_conn.load_config(config)
self.machine_name = config.get('machine_name', self.machine_name)
macros = config.get('macros', None)
if macros is not None:
# The macro's configuration name is the key, whereas the full
# command is the value
macros = [m for m in macros.split('\n') if m.strip()]
self.available_macros = {m.split()[0]: m for m in macros}
else:
self.available_macros = {}
ntkeys = config.get('non_trivial_keys', "Klipper state")
self.non_trivial_keys = [k for k in ntkeys.split('\n') if k.strip()]
self.ioloop.spawn_callback(self.write_response, {'status': 'C'})
logging.info("PanelDue Configured")
async def _klippy_request(self, command, method='GET', args={}): async def _klippy_request(self, command, method='GET', args={}):
request = self.server.make_request(command, method, args) request = self.server.make_request(command, method, args)
result = await request.wait() result = await request.wait()
@ -222,6 +203,7 @@ class PanelDue:
return result return result
async def handle_klippy_state(self, state): async def handle_klippy_state(self, state):
# XXX - Add a "connected" state and send a "C" to paneldue?
if state == "ready": if state == "ready":
await self._process_klippy_ready() await self._process_klippy_ready()
elif state == "shutdown": elif state == "shutdown":
@ -692,5 +674,5 @@ class PanelDue:
async def close(self): async def close(self):
self.ser_conn.disconnect() self.ser_conn.disconnect()
def load_plugin(server): def load_plugin(config):
return PanelDue(server) return PanelDue(config)