#!/usr/bin/python3 import serial import time import codecs import re import pika import json import os import hashlib # docker run -v "`pwd`:/src:ro" --device "/dev/ttyACM0:/dev/ttyACM0" -e "RABBIT_HOST=100.64.240.14" -e "RABBIT_PORT=5672" -e "RABBIT_USER=guest" -e "RABBIT_PASSWORD=itrw8pyNK8a7hipM" -e "RABBIT_USESSL=false" -e "RABBIT_EXCHANGE=fox.barcode" -e "ALLOW_SHORT_PAIRING_TAG=true" -e "ALLOW_LONG_PAIRING_TAG=true" -ti python:3 bash # pip install --no-cache-dir -r list.txt # pip3 install serial # pip3 install pika scannerPort=os.environ.get('SCANNER_PORT', "/dev/ttyACM0") pingTimeout=os.environ.get('PING_TIMEOUT', 5) rabbitHost=os.environ.get('RABBIT_HOST', None) rabbitPort=os.environ.get('RABBIT_PORT', 5672) #rabbitUser="guest" rabbitUser=os.environ.get('RABBIT_USER', None) #rabbitPass="itrw8pyNK8a7hipM" rabbitPass=os.environ.get('RABBIT_PASSWORD', None) rabbitSSL=os.environ.get('RABBIT_USE_SSL', "false").lower()=="true" rabbitExchange=os.environ.get('RABBIT_EXCHANGE', "fox.barcode") allowShortPairingTag=os.environ.get('ALLOW_SHORT_PAIRING', "true").lower()=="true" allowLongPairingTag=os.environ.get('ALLOW_LONG_PAIRING', "true").lower()=="true" routingTag=os.environ.get('DEFAULT_TAG', None) debug_enabled=os.environ.get('DEBUG_ENABLED', "false").lower()=="true" codelist = [] ser = serial.Serial( port = scannerPort,\ timeout=0) print("Scanner connected to: " + ser.portstr) count=1 parameters = pika.URLParameters('amqp://'+rabbitUser+':'+rabbitPass+'@'+rabbitHost+':'+str(rabbitPort)+'/%2F') connection = pika.BlockingConnection(parameters) channel = connection.channel() if (routingTag): channel.basic_publish(exchange=rabbitExchange, routing_key=routingTag, body=json.dumps({"type": "service", "data": "scannerReady","timeout":pingTimeout})) tmx=0; while True: tmx+=1 if (tmx > pingTimeout*10): tmx=0 if (debug_enabled): print("Ping sent "+str(routingTag)) if (routingTag): channel.basic_publish(exchange=rabbitExchange, routing_key=routingTag, body=json.dumps({"type": "service", "data": "scannerReady","timeout":pingTimeout})) time.sleep(0.1) for byte in ser.readline(): if (byte==128): continue if (byte=="\r"): continue if byte==13: try: codestring=codecs.decode(bytes(codelist),"utf-8") if (len(codestring)>1): m = re.match('^fxc([0-9a-f]{20})xf$', codestring) m2=re.match('^70918([0-9]{9})819070$', codestring) if (m and allowLongPairingTag): print("Pairing request. RoutingTag: "+m.group(1)) routingTag=m.group(1) channel.basic_publish(exchange=rabbitExchange, routing_key=routingTag, body=json.dumps({"type": "service", "data": "scannerReady","timeout":pingTimeout})) elif(m2 and allowShortPairingTag): rt2=hashlib.sha256(m2.group(1).encode()); routingTag=rt2.hexdigest()[5:25]; print("Pairing request. RoutingTag: "+routingTag) channel.basic_publish(exchange=rabbitExchange, routing_key=routingTag, body=json.dumps({"type": "service", "data": "scannerReady","timeout":pingTimeout})) else: if (debug_enabled): print("Read: "+codestring) tmx=0 if (routingTag): channel.basic_publish(exchange=rabbitExchange, routing_key=routingTag, body=json.dumps({"type": "code", "data": codestring,"timeout":pingTimeout})) if (debug_enabled): print("Sent") except: print("read failed") codelist=[] else: codelist.append(byte)