def get_qr_queue(host=celeryconfig.SERVER_HOST): if not is_bootstrapped: bootstrap() res = Resource(host, manager=manager) auth_params = {'username':celeryconfig.ZPRINTER_USERNAME, 'api_key': celeryconfig.ZPRINTER_API_KEY} r = res.get('/api/zebra_queue/', params_dict=auth_params) json = simplejson.loads(r.body_string()) if len(printer_dict.keys()) == 0: get_printers() if len(json['objects']) > 0: for instance in json['objects']: uri = instance['resource_uri'] zpl_code= instance['zpl_code'] printer_uri = instance['destination_printer'] printer_ip = printer_dict[printer_uri]['ip_address'] printer_port = printer_dict[printer_uri]['port'] instance['fulfilled_date'] = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.000") res.put(uri, simplejson.dumps(instance), headers={'Content-Type': 'application/json'}, params_dict=auth_params) do_send(printer_ip, printer_port, zpl_code, recv=False) else: logging.debug("no jobs")
def set_alert_destination(): """Set the printer alert endpoing to this host's listening port """ listener_ip_address = celeryconfig.ZPRINTER_PROXY #listener_ip_address = '192.168.10.20' # actual setup listener_port = 9111 if len(printer_dict.keys()) == 0: get_printers() alert_text=""" ^XA ^SX*,D,Y,Y,%s,9111 ^XZ """ % (listener_ip_address) for k,v in printer_dict.items(): host = v['ip_address'] port = v['port'] do_send(host, port, alert_text)
def get_printer_heartbeat(): """ Task to actively monitor the printer's status (vs. waiting to get alerts) """ if len(printer_dict.keys()) == 0: get_printers() msg_text = """^XA^HH^XZ""" for k,v in printer_dict.items(): print_host = v['ip_address'] print_port = v['port'] printer_uri = v['resource_uri'] info = do_send(print_host, print_port, msg_text, recv=True) #prepare the rest resource for sending info to server res = Resource(celeryconfig.SERVER_HOST, manager=manager) auth_params = {'username':celeryconfig.ZPRINTER_USERNAME, 'api_key': celeryconfig.ZPRINTER_API_KEY} new_instance = dict() new_instance['printer'] = printer_uri new_instance['event_date'] = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.000") new_instance['status'] = 'printer uptime heartbeat' if info is None or info == False: #failed to reach printer or receive data new_instance['is_cleared'] = False elif isinstance(info, str): #it's a string with info if info.count('ZPL MODE') > 0: #we got diagnostic info from printer new_instance['is_cleared'] = True else: #something else, error new_instance['is_cleared'] = False try: res.post('api/zebra_status/', simplejson.dumps(new_instance), headers={'Content-Type': 'application/json'}, params_dict=auth_params) except Exception, ex: logging.error("Error trying to post heartbeat to host %s" % ex)