You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.6 KiB

import time, json
import RPi.GPIO as GPIO
import keyboard
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)
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)
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"] = ''
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)