# - 1 byte payload length (without AIM, so 13 for EAN-13)
# - 3 bytes AIM, e.g. "]E0" for EAN-13
# - payload bytes
# - Rest is filled with zeros
hidmsg = h.read(255, 1000)
try:
flag = hidmsg.index(ord(']'))
except ValueError:
continue
if len(hidmsg) < flag+3:
continue
try:
end = hidmsg[flag:].index(0)
end += flag
except ValueError:
continue
aim = bytes(hidmsg[flag:flag+3]).decode()
data = bytes(hidmsg[flag+3:end]).decode().strip()
print(aim, hidmsg[flag+3:end], data)
if not aim.startswith(self.aim):
print('aim mismatch')
continue
if any(filter(lambda c: c not in self.chars, data)):
print('chars mismatch')
continue
url = self.url.format(data)
if self.sock:
try:
print(f'Moving mouse via ydotoold')
self.sock.sendto(moveleft, self.ydotool_socket)
self.sock.sendto(evsyn, self.ydotool_socket)
self.sock.sendto(moveright, self.ydotool_socket)
self.sock.sendto(evsyn, self.ydotool_socket)
except Exception as e:
print(e)
if not self.e.is_set():
with self.q.mutex:
print('clearing queue')
self.q.queue.clear()
print(f'Opening URL {url}')
webbrowser.open(url, new=2)
self.q.put(data)
print(f'enqued {data}')
except Exception as e:
print(e)
time.sleep(5)
def main():
ap = argparse.ArgumentParser(sys.argv[0], description='Opens a website when a barcode is scanned with an USB-HID barcode scanner.')
ap.add_argument('--url', '-u', type=str, help='The URL to open. "{}" is replaced by the barcode data. Default: http://localhost/?ean={}', default='http://localhost/?ean={}')
ap.add_argument('--vid', '-v', type=str, help='Connect to device using this USB vendor ID.')
ap.add_argument('--pid', '-p', type=str, help='Connect to device using this USB product ID.')
ap.add_argument('--aim', '-A', type=str, help='AIM filter prefix, e.g. "]E0" for EAN13, "]E" for all EAN variants, or "]" for all barcodes. Default: "]"', default=']')