def _get_printer_info(self, printer: Printer) -> Dict: printer_info = printer.__dict__.copy() octorest_client = None try: session = requests.Session() # overriding the default session OctoRest uses to set my desired timeout session.get = partial(session.get, timeout=3) octorest_client = OctoRest(url=printer.url, apikey=printer.apiKey, session=session) except (RuntimeError, requests.exceptions.ConnectionError): printer_info['state'] = "OctoPrint unavailable" return printer_info printer_info['state'] = octorest_client.state() # Get the printer model printer_profiles = octorest_client.printer_profiles()['profiles'] # Assuming printer_profiles is not empty, and there's one profile marked as default default_profile = next(profile for profile in printer_profiles.values() if profile['default'] is True) printer_info['model'] = default_profile['model'] printer_info['jobInfo'] = octorest_client.job_info() return printer_info
def init_client(self): print("Initializing OctoREST client...") while True: try: client = OctoRest(url="http://{}".format(self.octo_url), apikey=self.octo_key) print("Initialized OctoREST client") state = client.state() print(str(state)) print(str(client.connection_info())) # if "Error" in state or "Closed" in state: print("Printer is not connected. Trying to connect...") print(client.connect()) return client except Exception as ex: print( "Initialized of OctoREST client failed. Retrying in 10secs..." ) print(ex) time.sleep(10)