printer: cleanup
This commit is contained in:
parent
09f992d3b5
commit
2c30bdeddf
@ -6,68 +6,54 @@ from gi.repository import Gdk, GLib
|
|||||||
|
|
||||||
|
|
||||||
class Printer:
|
class Printer:
|
||||||
def __init__(self, printer_info, data, state_execute_cb):
|
def __init__(self, state_cb):
|
||||||
self.klipper = {"version": printer_info['software_version']}
|
|
||||||
self.tempstore = None
|
|
||||||
self.config = None
|
self.config = None
|
||||||
|
self.data = None
|
||||||
self.state = "disconnected"
|
self.state = "disconnected"
|
||||||
self.state_cb = state_execute_cb
|
self.state_cb = state_cb
|
||||||
self.power_devices = {}
|
self.state_callbacks = None
|
||||||
self.store_timeout = False
|
|
||||||
self.data = data
|
|
||||||
self.devices = {}
|
self.devices = {}
|
||||||
self.power_devices = {}
|
self.power_devices = {}
|
||||||
self.state_callbacks = {
|
|
||||||
"disconnected": None,
|
|
||||||
"error": None,
|
|
||||||
"paused": None,
|
|
||||||
"printing": None,
|
|
||||||
"ready": None,
|
|
||||||
"startup": None,
|
|
||||||
"shutdown": None
|
|
||||||
}
|
|
||||||
self.tools = []
|
self.tools = []
|
||||||
self.extrudercount = 0
|
self.extrudercount = 0
|
||||||
self.tempdevcount = 0
|
self.tempdevcount = 0
|
||||||
self.fancount = 0
|
self.fancount = 0
|
||||||
self.output_pin_count = 0
|
self.output_pin_count = 0
|
||||||
|
self.store_timeout = None
|
||||||
|
self.tempstore = {}
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
GLib.source_remove(self.store_timeout)
|
self.config = None
|
||||||
self.store_timeout = None
|
self.data = None
|
||||||
self.state = None
|
self.state = None
|
||||||
self.state_cb = None
|
self.state_cb = None
|
||||||
self.data = None
|
self.state_callbacks = None
|
||||||
self.devices = None
|
self.devices = None
|
||||||
self.power_devices = None
|
self.power_devices = None
|
||||||
self.state_callbacks = None
|
|
||||||
self.tools = None
|
self.tools = None
|
||||||
self.extrudercount = None
|
self.extrudercount = None
|
||||||
self.tempdevcount = None
|
self.tempdevcount = None
|
||||||
self.fancount = None
|
self.fancount = None
|
||||||
self.config = None
|
|
||||||
self.klipper = None
|
|
||||||
self.tempstore = None
|
|
||||||
self.output_pin_count = None
|
self.output_pin_count = None
|
||||||
|
self.store_timeout = None
|
||||||
|
self.tempstore = None
|
||||||
|
if self.store_timeout is not None:
|
||||||
|
GLib.source_remove(self.store_timeout)
|
||||||
|
self.store_timeout = None
|
||||||
|
|
||||||
def reinit(self, printer_info, data):
|
def reinit(self, printer_info, data):
|
||||||
self.config = data['configfile']['config']
|
self.config = data['configfile']['config']
|
||||||
|
self.data = data
|
||||||
|
self.devices = {}
|
||||||
|
self.tools = []
|
||||||
self.extrudercount = 0
|
self.extrudercount = 0
|
||||||
self.tempdevcount = 0
|
self.tempdevcount = 0
|
||||||
self.fancount = 0
|
self.fancount = 0
|
||||||
self.output_pin_count = 0
|
self.output_pin_count = 0
|
||||||
self.tools = []
|
|
||||||
self.devices = {}
|
|
||||||
self.data = data
|
|
||||||
self.klipper = {}
|
|
||||||
self.tempstore = {}
|
self.tempstore = {}
|
||||||
if not self.store_timeout:
|
if not self.store_timeout:
|
||||||
self.store_timeout = GLib.timeout_add_seconds(1, self._update_temp_store)
|
self.store_timeout = GLib.timeout_add_seconds(1, self._update_temp_store)
|
||||||
|
|
||||||
self.klipper = {
|
|
||||||
"version": printer_info['software_version']
|
|
||||||
}
|
|
||||||
|
|
||||||
for x in self.config.keys():
|
for x in self.config.keys():
|
||||||
if x[:8] == "extruder":
|
if x[:8] == "extruder":
|
||||||
self.tools.append(x)
|
self.tools.append(x)
|
||||||
@ -99,10 +85,8 @@ class Printer:
|
|||||||
name = x.split()[1] if len(x.split()) > 1 else x
|
name = x.split()[1] if len(x.split()) > 1 else x
|
||||||
if not name.startswith("_"):
|
if not name.startswith("_"):
|
||||||
self.fancount += 1
|
self.fancount += 1
|
||||||
if x.startswith('output_pin '):
|
if x.startswith('output_pin ') and not x.split()[1].startswith("_"):
|
||||||
# Support for hiding devices by name
|
self.output_pin_count += 1
|
||||||
if not x.split()[1].startswith("_"):
|
|
||||||
self.output_pin_count += 1
|
|
||||||
if x.startswith('bed_mesh '):
|
if x.startswith('bed_mesh '):
|
||||||
r = self.config[x]
|
r = self.config[x]
|
||||||
r['x_count'] = int(r['x_count'])
|
r['x_count'] = int(r['x_count'])
|
||||||
@ -114,7 +98,7 @@ class Printer:
|
|||||||
r['points'] = [[float(j.strip()) for j in i.split(",")] for i in r['points'].strip().split("\n")]
|
r['points'] = [[float(j.strip()) for j in i.split(",")] for i in r['points'].strip().split("\n")]
|
||||||
self.process_update(data)
|
self.process_update(data)
|
||||||
|
|
||||||
logging.info(f"Klipper version: {self.klipper['version']}")
|
logging.info(f"Klipper version: {printer_info['software_version']}")
|
||||||
logging.info(f"# Extruders: {self.extrudercount}")
|
logging.info(f"# Extruders: {self.extrudercount}")
|
||||||
logging.info(f"# Temperature devices: {self.tempdevcount}")
|
logging.info(f"# Temperature devices: {self.tempdevcount}")
|
||||||
logging.info(f"# Fans: {self.fancount}")
|
logging.info(f"# Fans: {self.fancount}")
|
||||||
@ -138,12 +122,12 @@ class Printer:
|
|||||||
|
|
||||||
def evaluate_state(self):
|
def evaluate_state(self):
|
||||||
# webhooks states: startup, ready, shutdown, error
|
# webhooks states: startup, ready, shutdown, error
|
||||||
if self.data['webhooks']['state'] == "ready":
|
# print_stats: standby, printing, paused, error, complete
|
||||||
if self.data['print_stats']: # standby, printing, paused, error, complete
|
if self.data['webhooks']['state'] == "ready" and self.data['print_stats']:
|
||||||
if self.data['print_stats']['state'] == 'paused' or self.data.get('pause_resume').get('is_paused'):
|
if self.data['print_stats']['state'] == 'paused' or self.data.get('pause_resume').get('is_paused'):
|
||||||
return "paused"
|
return "paused"
|
||||||
if self.data['print_stats']['state'] == 'printing':
|
if self.data['print_stats']['state'] == 'printing':
|
||||||
return "printing"
|
return "printing"
|
||||||
return self.data['webhooks']['state']
|
return self.data['webhooks']['state']
|
||||||
|
|
||||||
def process_status_update(self):
|
def process_status_update(self):
|
||||||
@ -246,9 +230,6 @@ class Printer:
|
|||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def get_klipper_version(self):
|
|
||||||
return self.klipper['version']
|
|
||||||
|
|
||||||
def get_power_devices(self):
|
def get_power_devices(self):
|
||||||
return list(self.power_devices)
|
return list(self.power_devices)
|
||||||
|
|
||||||
@ -264,9 +245,6 @@ class Printer:
|
|||||||
return self.data[stat][substat] if substat in self.data[stat] else {}
|
return self.data[stat][substat] if substat in self.data[stat] else {}
|
||||||
return self.data[stat]
|
return self.data[stat]
|
||||||
|
|
||||||
def get_state(self):
|
|
||||||
return self.state
|
|
||||||
|
|
||||||
def set_dev_temps(self, dev, temp, target=None, power=None):
|
def set_dev_temps(self, dev, temp, target=None, power=None):
|
||||||
if dev in self.devices:
|
if dev in self.devices:
|
||||||
self.devices[dev]['temperature'] = temp
|
self.devices[dev]['temperature'] = temp
|
||||||
@ -306,9 +284,6 @@ class Printer:
|
|||||||
return self.config[pin]["value"]
|
return self.config[pin]["value"]
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def get_extruder_count(self):
|
|
||||||
return self.extrudercount
|
|
||||||
|
|
||||||
def get_temp_store_devices(self):
|
def get_temp_store_devices(self):
|
||||||
if self.tempstore is not None:
|
if self.tempstore is not None:
|
||||||
return list(self.tempstore)
|
return list(self.tempstore)
|
||||||
@ -356,11 +331,6 @@ class Printer:
|
|||||||
def config_section_exists(self, section):
|
def config_section_exists(self, section):
|
||||||
return section in self.get_config_section_list()
|
return section in self.get_config_section_list()
|
||||||
|
|
||||||
def set_callbacks(self, callbacks):
|
|
||||||
for name, cb in callbacks.items():
|
|
||||||
if name in list(self.state_callbacks):
|
|
||||||
self.state_callbacks[name] = cb
|
|
||||||
|
|
||||||
def set_dev_stat(self, dev, stat, value):
|
def set_dev_stat(self, dev, stat, value):
|
||||||
if dev not in self.devices:
|
if dev not in self.devices:
|
||||||
return
|
return
|
||||||
|
@ -34,7 +34,7 @@ class TemperaturePanel(ScreenPanel):
|
|||||||
self.grid.attach(self.create_left_panel(), 0, 0, 1, 1)
|
self.grid.attach(self.create_left_panel(), 0, 0, 1, 1)
|
||||||
|
|
||||||
# When printing start in temp_delta mode and only select tools
|
# When printing start in temp_delta mode and only select tools
|
||||||
state = self._printer.get_state()
|
state = self._printer.state
|
||||||
logging.info(state)
|
logging.info(state)
|
||||||
selection = []
|
selection = []
|
||||||
if state not in ["printing", "paused"]:
|
if state not in ["printing", "paused"]:
|
||||||
|
57
screen.py
57
screen.py
@ -160,18 +160,8 @@ class KlipperScreen(Gtk.Window):
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.connecting_to_printer = name
|
self.connecting_to_printer = name
|
||||||
|
|
||||||
# Cleanup
|
|
||||||
if self.files is not None:
|
|
||||||
self.files.reset()
|
|
||||||
self.files = None
|
|
||||||
if self.printer is not None:
|
|
||||||
self.printer.reset()
|
|
||||||
self.printer = None
|
|
||||||
|
|
||||||
for printer in self._config.get_printers():
|
for printer in self._config.get_printers():
|
||||||
pname = list(printer)[0]
|
pname = list(printer)[0]
|
||||||
|
|
||||||
if pname != name:
|
if pname != name:
|
||||||
continue
|
continue
|
||||||
data = printer[pname]
|
data = printer[pname]
|
||||||
@ -183,26 +173,15 @@ class KlipperScreen(Gtk.Window):
|
|||||||
|
|
||||||
logging.info(f"Connecting to printer: {name}")
|
logging.info(f"Connecting to printer: {name}")
|
||||||
self.apiclient = KlippyRest(data["moonraker_host"], data["moonraker_port"], data["moonraker_api_key"])
|
self.apiclient = KlippyRest(data["moonraker_host"], data["moonraker_port"], data["moonraker_api_key"])
|
||||||
|
# Cleanup
|
||||||
self.printer = Printer({
|
if self.files is not None:
|
||||||
"software_version": "Unknown"
|
self.files.reset()
|
||||||
}, {
|
self.files = None
|
||||||
'configfile': {
|
if self.printer is not None:
|
||||||
'config': {}
|
self.printer.reset()
|
||||||
},
|
self.printer = None
|
||||||
'print_stats': {
|
self.printer = Printer(self.state_execute)
|
||||||
'state': 'disconnected'
|
self.printer.state_callbacks = {
|
||||||
},
|
|
||||||
'virtual_sdcard': {
|
|
||||||
'is_active': False
|
|
||||||
}
|
|
||||||
}, self.state_execute)
|
|
||||||
|
|
||||||
self._remove_all_panels()
|
|
||||||
self.base_panel.show_printer_select(True)
|
|
||||||
self.printer_initializing(_("Connecting to %s") % name)
|
|
||||||
|
|
||||||
self.printer.set_callbacks({
|
|
||||||
"disconnected": self.state_disconnected,
|
"disconnected": self.state_disconnected,
|
||||||
"error": self.state_error,
|
"error": self.state_error,
|
||||||
"paused": self.state_paused,
|
"paused": self.state_paused,
|
||||||
@ -210,7 +189,10 @@ class KlipperScreen(Gtk.Window):
|
|||||||
"ready": self.state_ready,
|
"ready": self.state_ready,
|
||||||
"startup": self.state_startup,
|
"startup": self.state_startup,
|
||||||
"shutdown": self.state_shutdown
|
"shutdown": self.state_shutdown
|
||||||
})
|
}
|
||||||
|
self._remove_all_panels()
|
||||||
|
self.base_panel.show_printer_select(True)
|
||||||
|
self.printer_initializing(_("Connecting to %s") % name)
|
||||||
|
|
||||||
self._ws = KlippyWebsocket(self,
|
self._ws = KlippyWebsocket(self,
|
||||||
{
|
{
|
||||||
@ -225,7 +207,6 @@ class KlipperScreen(Gtk.Window):
|
|||||||
self.files = KlippyFiles(self)
|
self.files = KlippyFiles(self)
|
||||||
self._ws.initial_connect()
|
self._ws.initial_connect()
|
||||||
self.connecting = False
|
self.connecting = False
|
||||||
|
|
||||||
self.connected_printer = name
|
self.connected_printer = name
|
||||||
self.base_panel.set_ks_printer_cfg(name)
|
self.base_panel.set_ks_printer_cfg(name)
|
||||||
logging.debug(f"Connected to printer: {name}")
|
logging.debug(f"Connected to printer: {name}")
|
||||||
@ -487,9 +468,6 @@ class KlipperScreen(Gtk.Window):
|
|||||||
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
|
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
|
||||||
)
|
)
|
||||||
|
|
||||||
def is_printing(self):
|
|
||||||
return self.printer.get_state() == "printing"
|
|
||||||
|
|
||||||
def is_updating(self):
|
def is_updating(self):
|
||||||
return self.updating
|
return self.updating
|
||||||
|
|
||||||
@ -748,9 +726,8 @@ class KlipperScreen(Gtk.Window):
|
|||||||
self.printer.change_state(self.printer.state)
|
self.printer.change_state(self.printer.state)
|
||||||
|
|
||||||
def _websocket_callback(self, action, data):
|
def _websocket_callback(self, action, data):
|
||||||
if self.connecting is True:
|
if self.connecting:
|
||||||
return
|
return
|
||||||
|
|
||||||
if action == "notify_klippy_disconnected":
|
if action == "notify_klippy_disconnected":
|
||||||
self.printer.change_state("disconnected")
|
self.printer.change_state("disconnected")
|
||||||
return
|
return
|
||||||
@ -758,7 +735,7 @@ class KlipperScreen(Gtk.Window):
|
|||||||
self.printer.change_state("shutdown")
|
self.printer.change_state("shutdown")
|
||||||
elif action == "notify_klippy_ready":
|
elif action == "notify_klippy_ready":
|
||||||
self.printer.change_state("ready")
|
self.printer.change_state("ready")
|
||||||
elif action == "notify_status_update" and self.printer.get_state() != "shutdown":
|
elif action == "notify_status_update" and self.printer.state != "shutdown":
|
||||||
self.printer.process_update(data)
|
self.printer.process_update(data)
|
||||||
elif action == "notify_filelist_changed":
|
elif action == "notify_filelist_changed":
|
||||||
if self.files is not None:
|
if self.files is not None:
|
||||||
@ -771,13 +748,13 @@ class KlipperScreen(Gtk.Window):
|
|||||||
logging.debug("Power status changed: %s", data)
|
logging.debug("Power status changed: %s", data)
|
||||||
self.printer.process_power_update(data)
|
self.printer.process_power_update(data)
|
||||||
self.panels['splash_screen'].check_power_status()
|
self.panels['splash_screen'].check_power_status()
|
||||||
elif self.printer.get_state() not in ["error", "shutdown"] and action == "notify_gcode_response":
|
elif action == "notify_gcode_response" and self.printer.state not in ["error", "shutdown"]:
|
||||||
if not (data.startswith("B:") or data.startswith("T:")):
|
if not (data.startswith("B:") or data.startswith("T:")):
|
||||||
if data.startswith("echo: "):
|
if data.startswith("echo: "):
|
||||||
self.show_popup_message(data[6:], 1)
|
self.show_popup_message(data[6:], 1)
|
||||||
elif data.startswith("!! "):
|
elif data.startswith("!! "):
|
||||||
self.show_popup_message(data[3:], 3)
|
self.show_popup_message(data[3:], 3)
|
||||||
if "SAVE_CONFIG" in data and self.printer.get_state() == "ready":
|
if "SAVE_CONFIG" in data and self.printer.state == "ready":
|
||||||
script = {"script": "SAVE_CONFIG"}
|
script = {"script": "SAVE_CONFIG"}
|
||||||
self._confirm_send_action(
|
self._confirm_send_action(
|
||||||
None,
|
None,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user