<commit_before>"""
Read and write RFID tags for the Relay app
"""

from functools import partial
import logging
from signal import signal, SIGTERM, SIGINT
import sys
import time

import mercury
from relay_config import Config
from relay_websocket import RelayWebsocket

logging.basicConfig(
    name=__name__,
    filename='rfid.log',
    level=logging.INFO,
    format='%(asctime)s %(levelname)s %(message)s'
)

cfg = Config()
ws = None
dedup_cache = {}
DEDUP_THRESHOLD = 5.0


def post(epc_obj):
    epc = repr(epc_obj).strip('\'')  # ugh
    hex_numbers = [epc[i:i+2] for i in range(0, len(epc), 2)]
    chars = [chr(int(ch, 16)) for ch in hex_numbers]
    tag = ''.join(chars)
    now = time.time()
    if now - dedup_cache.get(tag, 0.0) > DEDUP_THRESHOLD:
        dedup_cache[tag] = now
        if ws:
            ws.send(tag)
    else:
        logging.debug('duplicate read %s' % tag)


def sig_handler(sig, frame):
    logging.info('caught signal %d' % sig)
    sys.exit(0)



if __name__ == '__main__':
    logging.info('starting')
    reader = None

    signal(SIGTERM, partial(sig_handler))
    signal(SIGINT, partial(sig_handler))

    try:
        reader = mercury.Reader('tmr:///dev/ttyUSB0')
        reader.set_region('NA2')
        reader.set_read_plan([1], 'GEN2', read_power=2600)
        reader.start_reading(post, on_time=250, off_time=250)
        ws = RelayWebsocket()
    except Exception as e:<commit_msg>add batch writing for bib provisioning<commit_after>"""
Read and write RFID tags for the Relay app
"""

import argparse
from functools import partial
import logging
from signal import signal, SIGTERM, SIGINT
import sys
import time

import mercury
from relay_config import Config
from relay_websocket import RelayWebsocket

logging.basicConfig(
    name=__name__,
    filename='rfid.log',
    level=logging.INFO,
    format='%(asctime)s %(levelname)s %(message)s'
)

cfg = Config()
ws = None
dedup_cache = {}
DEDUP_THRESHOLD = 5.0


def post(epc_obj):
    epc = repr(epc_obj).strip('\'')  # ugh
    hex_numbers = [epc[i:i+2] for i in range(0, len(epc), 2)]
    chars = [chr(int(ch, 16)) for ch in hex_numbers]
    tag = ''.join(chars)
    now = time.time()
    if now - dedup_cache.get(tag, 0.0) > DEDUP_THRESHOLD:
        dedup_cache[tag] = now
        if ws:
            ws.send(tag)
    else:
        logging.debug('duplicate read %s' % tag)


def sig_handler(sig, frame):
    logging.info('caught signal %d' % sig)
    sys.exit(0)


if __name__ == '__main__':
    logging.info('starting')
    reader = None

    signal(SIGTERM, partial(sig_handler))
    signal(SIGINT, partial(sig_handler))

    parser = argparse.ArgumentParser(description='Relay RFID reader/writer')
    parser.add_argument('--reader', default='', help='continuous read tags')
    parser.add_argument('--writer', default='', help='batch write tags')
    ns = parser.parse_args()

    try:
        reader = mercury.Reader('tmr:///dev/ttyUSB0')
        reader.set_region('NA2')
        reader.set_read_plan([1], 'GEN2', read_power=2600)
        if ns.reader:
            reader.start_reading(post, on_time=250, off_time=250)
            ws = RelayWebsocket()
        elif ns.writer:
            rng = ns.writer.split('-')
            for t in range(int(rng[0]), int(rng[1]) + 1):
                time.sleep(6)
                reader.write(str(t).zfill(4))
                logging.info('wrote %d' % t)
        else:
            log.debug('bad parameter?')
    except Exception as e:
{'fraction_non_alphanumeric': 0.13337165852256397, 'fraction_numerical': 0.015234262719172175, 'mean_word_length': 3.0092165898617513, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '599697', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1201, 'n_words': 288}