diff --git a/.gitignore b/.gitignore index 6cf7488..d13c3db 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ sftp-config.json admin.json cards/ history.json -*.pyc \ No newline at end of file +*.pyc +tags \ No newline at end of file diff --git a/cardlistener.py b/cardlistener.py index 06b2ea8..9121e30 100755 --- a/cardlistener.py +++ b/cardlistener.py @@ -6,100 +6,103 @@ import subprocess pin_config = [] def btnBlink(): - for i in range(3): - GPIO.output(pin_config['fridge_red_LED'], GPIO.HIGH) - time.sleep(0.02) - GPIO.output(pin_config['fridge_grn_LED'], GPIO.HIGH) - time.sleep(0.02) - GPIO.output(pin_config['tools_red_LED'], GPIO.HIGH) - time.sleep(0.02) - GPIO.output(pin_config['tools_grn_LED'], GPIO.HIGH) - time.sleep(0.02) - GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW) - time.sleep(0.02) - GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW) - time.sleep(0.02) - GPIO.output(pin_config['tools_red_LED'], GPIO.LOW) - time.sleep(0.02) - GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW) - time.sleep(0.02) + for i in range(3): + GPIO.output(pin_config['fridge_red_LED'], GPIO.HIGH) + time.sleep(0.02) + GPIO.output(pin_config['fridge_grn_LED'], GPIO.HIGH) + time.sleep(0.02) + GPIO.output(pin_config['tools_red_LED'], GPIO.HIGH) + time.sleep(0.02) + GPIO.output(pin_config['tools_grn_LED'], GPIO.HIGH) + time.sleep(0.02) + GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW) + time.sleep(0.02) + GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW) + time.sleep(0.02) + GPIO.output(pin_config['tools_red_LED'], GPIO.LOW) + time.sleep(0.02) + GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW) + time.sleep(0.02) if __name__ == '__main__': - with open('pin_config.json') as pin_config_stream: - pin_config = json.load(pin_config_stream) - if pin_config['mode'] == 'BOARD': - GPIO.setmode(GPIO.BOARD) - else: - GPIO.setmode(GPIO.BCM) - GPIO.setup(pin_config['fridge_btn'], GPIO.IN, pull_up_down=GPIO.PUD_UP) - GPIO.setup(pin_config['tools_btn'], GPIO.IN, pull_up_down=GPIO.PUD_UP) - GPIO.setup(pin_config['fridge_red_LED'], GPIO.OUT) - GPIO.setup(pin_config['fridge_grn_LED'], GPIO.OUT) - GPIO.setup(pin_config['tools_red_LED'], GPIO.OUT) - GPIO.setup(pin_config['tools_grn_LED'], GPIO.OUT) + with open('pin_config.json') as pin_config_stream: + pin_config = json.load(pin_config_stream) + if pin_config['mode'] == 'BOARD': + GPIO.setmode(GPIO.BOARD) + else: + GPIO.setmode(GPIO.BCM) + GPIO.setup(pin_config['fridge_btn'], GPIO.IN, pull_up_down=GPIO.PUD_UP) + GPIO.setup(pin_config['tools_btn'], GPIO.IN, pull_up_down=GPIO.PUD_UP) + GPIO.setup(pin_config['fridge_red_LED'], GPIO.OUT) + GPIO.setup(pin_config['fridge_grn_LED'], GPIO.OUT) + GPIO.setup(pin_config['tools_red_LED'], GPIO.OUT) + GPIO.setup(pin_config['tools_grn_LED'], GPIO.OUT) - GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW) - GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW) - GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW) - GPIO.output(pin_config['tools_red_LED'], GPIO.LOW) + GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW) + GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW) + GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW) + GPIO.output(pin_config['tools_red_LED'], GPIO.LOW) - while True: - print "Enter card ID:" - cardID_input = keyboard.record(until='enter') - cardID = keyboard.get_typed_strings(cardID_input)[0] - event = {} - event["time"] = time.time() - event["cardID"] = cardID - event["name"] = '' - try: - with open('cards/' + cardID + '.json') as card_data: - data = json.load(card_data) - event["name"] = data['name'] - if data['secretTools']: - GPIO.output(pin_config['tools_grn_LED'], GPIO.HIGH) - else: - GPIO.output(pin_config['tools_red_LED'], GPIO.HIGH) - if data['fridge']: - GPIO.output(pin_config['fridge_grn_LED'], GPIO.HIGH) - else: - GPIO.output(pin_config['fridge_red_LED'], GPIO.HIGH) - timeout = time.time() + 5 # 5 seconds timeout - wait = True - while wait: - if data['secretTools'] and GPIO.input(pin_config['tools_btn']) == 0: - GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW) - GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW) - GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW) - GPIO.output(pin_config['tools_red_LED'], GPIO.LOW) - wait = False - # Open tools - subprocess.call("/home/pi/ELAB-RFID-I2C/RPi/i2c_challenge 0x30", shell=True) - if data['fridge'] and GPIO.input(pin_config['fridge_btn']) == 0: - GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW) - GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW) - GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW) - GPIO.output(pin_config['tools_red_LED'], GPIO.LOW) - wait = False - # Open fridge - subprocess.call("/home/pi/ELAB-RFID-I2C/RPi/i2c_challenge 0x20", shell=True) + while True: + print "Enter card ID:" + cardID_input = keyboard.record(until='enter') + cardID = keyboard.get_typed_strings(cardID_input)[0] + event = {} + event["time"] = time.time() + event["cardID"] = cardID + event["name"] = '' + event["action"] = '' + try: + with open('cards/' + cardID + '.json') as card_data: + data = json.load(card_data) + event["name"] = data['name'] + if data['secretTools']: + GPIO.output(pin_config['tools_grn_LED'], GPIO.HIGH) + else: + GPIO.output(pin_config['tools_red_LED'], GPIO.HIGH) + if data['fridge']: + GPIO.output(pin_config['fridge_grn_LED'], GPIO.HIGH) + else: + GPIO.output(pin_config['fridge_red_LED'], GPIO.HIGH) + timeout = time.time() + 5 # 5 seconds timeout + wait = True + while wait: + if data['secretTools'] and GPIO.input(pin_config['tools_btn']) == 0: + GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW) + GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW) + GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW) + GPIO.output(pin_config['tools_red_LED'], GPIO.LOW) + event['action'] = 'tools' + wait = False + # Open tools + subprocess.call("/home/pi/ELAB-RFID-I2C/RPi/i2c_challenge 0x30", shell=True) + if data['fridge'] and GPIO.input(pin_config['fridge_btn']) == 0: + GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW) + GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW) + GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW) + GPIO.output(pin_config['tools_red_LED'], GPIO.LOW) + event['action'] = 'fridge' + wait = False + # Open fridge + subprocess.call("/home/pi/ELAB-RFID-I2C/RPi/i2c_challenge 0x20", shell=True) - if time.time() > timeout: - GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW) - GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW) - GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW) - GPIO.output(pin_config['tools_red_LED'], GPIO.LOW) - wait = False - except: - btnBlink() - print "Card not registered." - history = [] - try: - with open('history.json', 'r') as json_data: - history = json.load(json_data) - except: - print 'Initializing new history file.' - finally: - history.append(event) - with open('history.json', 'w') as outfile: - json.dump(history, outfile) - + if time.time() > timeout: + GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW) + GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW) + GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW) + GPIO.output(pin_config['tools_red_LED'], GPIO.LOW) + wait = False + except: + btnBlink() + print "Card not registered." + history = [] + try: + with open('history.json', 'r') as json_data: + history = json.load(json_data) + except: + print 'Initializing new history file.' + finally: + history.append(event) + with open('history.json', 'w') as outfile: + json.dump(history, outfile) +