Skip to content

Instantly share code, notes, and snippets.

@kjagiello
Last active October 29, 2015 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjagiello/86eb0b40f3f24f2b90d0 to your computer and use it in GitHub Desktop.
Save kjagiello/86eb0b40f3f24f2b90d0 to your computer and use it in GitHub Desktop.
import argparse
import logging
from thunderclient import Thunder
from evdev import InputDevice, list_devices, ecodes
from evdev import categorize
logger = logging.getLogger(__name__)
def read_codes(device):
events = (e for e in device.read_loop() if e.type == ecodes.EV_KEY)
events = map(categorize, events)
events = (e for e in events if e.keystate == e.key_up)
buff = []
for e in events:
if e.scancode == ecodes.KEY_ENTER:
ascii = [e.keycode.split('_')[1] for e in buff]
buff = []
yield ''.join(ascii)
else:
buff.append(e)
def main(device_name, host, port, apikey, apisecret, verbose=False):
# Configure logging to the console if requested.
if verbose:
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
devices = map(InputDevice, list_devices())
devices = [d for d in devices if d.name.strip() == device_name]
assert devices, ('No Barcode Reader device found (did you forget to run '
'the script as root?).')
device = devices[0]
device.grab()
tc = Thunder(apikey, apisecret, host, port)
for code in read_codes(device):
tc.send_message_to_channel('product', code)
logger.debug('Code %d has been sent to the \'cards\' channel.', code)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('device', type=str,
help='The name of the device to read from.')
parser.add_argument('host', type=str,
help='Thunderpush host to communicate with.')
parser.add_argument('apikey', type=str)
parser.add_argument('apisecret', type=str)
parser.add_argument('--port', type=int, default=80,
help='Thunderpush port (default 80).')
parser.add_argument('-v', '--verbose', help='Increase output verbosity.',
action='store_true')
args = parser.parse_args()
main(args.device, args.host, args.port, args.apikey, args.apisecret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment