From 3bfcbcfe6c6039fc094ff101e8fd41d1976a994e Mon Sep 17 00:00:00 2001 From: jimmyjon711 Date: Sat, 7 Dec 2024 17:55:42 -0700 Subject: [PATCH] Updates to spoolman - When lane is unloaded with BT_TOOL_UNLOAD active spool is set to none - When lane is ejected spool is removed from variables and saved --- extras/AFC.py | 7 +++++++ extras/AFC_NightOwl.py | 2 +- extras/AFC_prep.py | 4 ++++ extras/AFC_spool.py | 26 +++++++++++++++++--------- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/extras/AFC.py b/extras/AFC.py index bb1ad4d..035d0e5 100644 --- a/extras/AFC.py +++ b/extras/AFC.py @@ -487,6 +487,10 @@ def cmd_LANE_UNLOAD(self, gcmd): self.lanes[CUR_LANE.unit][CUR_LANE.name]['hub_loaded'] = CUR_LANE.hub_load self.save_vars() CUR_LANE.status = None + + # Removing spool from vars since it was ejected + self.SPOOL.set_spoolID( CUR_LANE, "") + else: self.gcode.respond_info('LANE ' + CUR_LANE.name + ' IS TOOL LOADED') @@ -684,6 +688,9 @@ def cmd_TOOL_UNLOAD(self, gcmd): CUR_LANE = self.printer.lookup_object('AFC_stepper '+ lane) self.TOOL_UNLOAD(CUR_LANE) + # User manually unloaded spool from toolhead, remove spool from active status + self.SPOOL.set_active_spool( None ) + def TOOL_UNLOAD(self, CUR_LANE): """ This function handles the unloading of a specified lane from the tool. It performs diff --git a/extras/AFC_NightOwl.py b/extras/AFC_NightOwl.py index 473f293..461d30a 100644 --- a/extras/AFC_NightOwl.py +++ b/extras/AFC_NightOwl.py @@ -12,7 +12,7 @@ def handle_connect(self): self.AFC = self.printer.lookup_object('AFC') self.logo = 'Night Owl Ready' - self.logo ='R , ,\n' + self.logo ='R , ,\n' self.logo+='E )\___/(\n' self.logo+='A {(@)v(@)}\n' self.logo+='D {|~~~|}\n' diff --git a/extras/AFC_prep.py b/extras/AFC_prep.py index 63a4433..64dc5e9 100644 --- a/extras/AFC_prep.py +++ b/extras/AFC_prep.py @@ -162,6 +162,10 @@ def PREP(self, gcmd): if not self.AFC.extruders[EXTRUDE]['lane_loaded']: self.AFC.gcode.respond_info("{} loaded with out identifying lane in AFC.vars.tool file".format(EXTRUDE)) + # Defaulting to no active spool, putting at end so endpoint has time to register + if self.AFC.current is None: + self.AFC.SPOOL.set_active_spool( None ) + def load_config(config): return afcPrep(config) diff --git a/extras/AFC_spool.py b/extras/AFC_spool.py index aad7ea2..97e1fe0 100644 --- a/extras/AFC_spool.py +++ b/extras/AFC_spool.py @@ -26,6 +26,8 @@ def handle_connect(self): self.gcode.register_mux_command('SET_RUNOUT',None,None, self.cmd_SET_RUNOUT, desc=self.cmd_SET_RUNOUT_help) self.gcode.register_mux_command('SET_MAP',None,None, self.cmd_SET_MAP, desc=self.cmd_SET_MAP_help) + self.URL = 'http://{}:{}/api/v1/spool/'.format(self.AFC.spoolman_ip, self.AFC.spoolman_port) + cmd_SET_MAP_help = "change filaments color" def cmd_SET_MAP(self, gcmd): @@ -99,14 +101,16 @@ def cmd_SET_COLOR(self, gcmd): def set_active_spool(self, ID): webhooks = self.printer.lookup_object('webhooks') if self.AFC.spoolman_ip != None: - if ID: - args = {'spool_id' : int(ID)} - try: - webhooks.call_remote_method("spoolman_set_active_spool", **args) - except self.printer.command_error: - self.gcode._respond_error("Error trying to set active spool") + if ID and ID is not None: + id = int(ID) else: - self.gcode.respond_info("Spool ID not set, cannot update spoolman with active spool") + id = None + + args = {'spool_id' : id } + try: + webhooks.call_remote_method("spoolman_set_active_spool", **args) + except self.printer.command_error as e: + self.gcode._respond_error("Error trying to set active spool \n{}".format(e)) cmd_SET_SPOOLID_help = "change filaments ID" def cmd_SET_SPOOLID(self, gcmd): @@ -134,14 +138,18 @@ def cmd_SET_SPOOLID(self, gcmd): return SpoolID = gcmd.get('SPOOL_ID', '') CUR_LANE = self.printer.lookup_object('AFC_stepper ' + lane) + self.set_spoolID(CURLANE, SpoolID) + + def set_spoolID(self, CUR_LANE, SpoolID): + if self.AFC.spoolman_ip !=None: if SpoolID !='': try: - url = 'http://' + self.AFC.spoolman_ip + ':'+ self.AFC.spoolman_port +"/api/v1/spool/" + SpoolID + url = "{}{}".format(self.URL, SpoolID) result = json.load(urlopen(url)) self.AFC.lanes[CUR_LANE.unit][CUR_LANE.name]['spool_id'] = SpoolID self.AFC.lanes[CUR_LANE.unit][CUR_LANE.name]['material'] = result['filament']['material'] self.AFC.lanes[CUR_LANE.unit][CUR_LANE.name]['color'] = '#' + result['filament']['color_hex'] - self.AFC.lanes[CUR_LANE.unit][CUR_LANE.name]['weight'] = result['remaining_weight'] + if 'remaining_weight' in result: self.AFC.lanes[CUR_LANE.unit][CUR_LANE.name]['weight'] = result['remaining_weight'] except: self.AFC.ERROR.AFC_error("Error when trying to get Spoolman data for ID:{}".format(SpoolID)) else: