Added logging of button presses

master
Davide Bongiovanni 8 years ago
parent 9c7bf52e8f
commit 2e59b80ed1

1
.gitignore vendored

@ -5,3 +5,4 @@ admin.json
cards/ cards/
history.json history.json
*.pyc *.pyc
tags

@ -6,100 +6,103 @@ import subprocess
pin_config = [] pin_config = []
def btnBlink(): def btnBlink():
for i in range(3): for i in range(3):
GPIO.output(pin_config['fridge_red_LED'], GPIO.HIGH) GPIO.output(pin_config['fridge_red_LED'], GPIO.HIGH)
time.sleep(0.02) time.sleep(0.02)
GPIO.output(pin_config['fridge_grn_LED'], GPIO.HIGH) GPIO.output(pin_config['fridge_grn_LED'], GPIO.HIGH)
time.sleep(0.02) time.sleep(0.02)
GPIO.output(pin_config['tools_red_LED'], GPIO.HIGH) GPIO.output(pin_config['tools_red_LED'], GPIO.HIGH)
time.sleep(0.02) time.sleep(0.02)
GPIO.output(pin_config['tools_grn_LED'], GPIO.HIGH) GPIO.output(pin_config['tools_grn_LED'], GPIO.HIGH)
time.sleep(0.02) time.sleep(0.02)
GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW) GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW)
time.sleep(0.02) time.sleep(0.02)
GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW) GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW)
time.sleep(0.02) time.sleep(0.02)
GPIO.output(pin_config['tools_red_LED'], GPIO.LOW) GPIO.output(pin_config['tools_red_LED'], GPIO.LOW)
time.sleep(0.02) time.sleep(0.02)
GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW) GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW)
time.sleep(0.02) time.sleep(0.02)
if __name__ == '__main__': if __name__ == '__main__':
with open('pin_config.json') as pin_config_stream: with open('pin_config.json') as pin_config_stream:
pin_config = json.load(pin_config_stream) pin_config = json.load(pin_config_stream)
if pin_config['mode'] == 'BOARD': if pin_config['mode'] == 'BOARD':
GPIO.setmode(GPIO.BOARD) GPIO.setmode(GPIO.BOARD)
else: else:
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_config['fridge_btn'], GPIO.IN, pull_up_down=GPIO.PUD_UP) 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['tools_btn'], GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(pin_config['fridge_red_LED'], GPIO.OUT) GPIO.setup(pin_config['fridge_red_LED'], GPIO.OUT)
GPIO.setup(pin_config['fridge_grn_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_red_LED'], GPIO.OUT)
GPIO.setup(pin_config['tools_grn_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['fridge_grn_LED'], GPIO.LOW)
GPIO.output(pin_config['tools_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['fridge_red_LED'], GPIO.LOW)
GPIO.output(pin_config['tools_red_LED'], GPIO.LOW) GPIO.output(pin_config['tools_red_LED'], GPIO.LOW)
while True: while True:
print "Enter card ID:" print "Enter card ID:"
cardID_input = keyboard.record(until='enter') cardID_input = keyboard.record(until='enter')
cardID = keyboard.get_typed_strings(cardID_input)[0] cardID = keyboard.get_typed_strings(cardID_input)[0]
event = {} event = {}
event["time"] = time.time() event["time"] = time.time()
event["cardID"] = cardID event["cardID"] = cardID
event["name"] = '' event["name"] = ''
try: event["action"] = ''
with open('cards/' + cardID + '.json') as card_data: try:
data = json.load(card_data) with open('cards/' + cardID + '.json') as card_data:
event["name"] = data['name'] data = json.load(card_data)
if data['secretTools']: event["name"] = data['name']
GPIO.output(pin_config['tools_grn_LED'], GPIO.HIGH) if data['secretTools']:
else: GPIO.output(pin_config['tools_grn_LED'], GPIO.HIGH)
GPIO.output(pin_config['tools_red_LED'], GPIO.HIGH) else:
if data['fridge']: GPIO.output(pin_config['tools_red_LED'], GPIO.HIGH)
GPIO.output(pin_config['fridge_grn_LED'], GPIO.HIGH) if data['fridge']:
else: GPIO.output(pin_config['fridge_grn_LED'], GPIO.HIGH)
GPIO.output(pin_config['fridge_red_LED'], GPIO.HIGH) else:
timeout = time.time() + 5 # 5 seconds timeout GPIO.output(pin_config['fridge_red_LED'], GPIO.HIGH)
wait = True timeout = time.time() + 5 # 5 seconds timeout
while wait: wait = True
if data['secretTools'] and GPIO.input(pin_config['tools_btn']) == 0: while wait:
GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW) if data['secretTools'] and GPIO.input(pin_config['tools_btn']) == 0:
GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW) GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW)
GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW) GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW)
GPIO.output(pin_config['tools_red_LED'], GPIO.LOW) GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW)
wait = False GPIO.output(pin_config['tools_red_LED'], GPIO.LOW)
# Open tools event['action'] = 'tools'
subprocess.call("/home/pi/ELAB-RFID-I2C/RPi/i2c_challenge 0x30", shell=True) wait = False
if data['fridge'] and GPIO.input(pin_config['fridge_btn']) == 0: # Open tools
GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW) subprocess.call("/home/pi/ELAB-RFID-I2C/RPi/i2c_challenge 0x30", shell=True)
GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW) if data['fridge'] and GPIO.input(pin_config['fridge_btn']) == 0:
GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW) GPIO.output(pin_config['fridge_grn_LED'], GPIO.LOW)
GPIO.output(pin_config['tools_red_LED'], GPIO.LOW) GPIO.output(pin_config['tools_grn_LED'], GPIO.LOW)
wait = False GPIO.output(pin_config['fridge_red_LED'], GPIO.LOW)
# Open fridge GPIO.output(pin_config['tools_red_LED'], GPIO.LOW)
subprocess.call("/home/pi/ELAB-RFID-I2C/RPi/i2c_challenge 0x20", shell=True) event['action'] = 'fridge'
wait = False
# Open fridge
subprocess.call("/home/pi/ELAB-RFID-I2C/RPi/i2c_challenge 0x20", shell=True)
if time.time() > timeout: if time.time() > timeout:
GPIO.output(pin_config['fridge_grn_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['tools_grn_LED'], GPIO.LOW)
GPIO.output(pin_config['fridge_red_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['tools_red_LED'], GPIO.LOW)
wait = False wait = False
except: except:
btnBlink() btnBlink()
print "Card not registered." print "Card not registered."
history = [] history = []
try: try:
with open('history.json', 'r') as json_data: with open('history.json', 'r') as json_data:
history = json.load(json_data) history = json.load(json_data)
except: except:
print 'Initializing new history file.' print 'Initializing new history file.'
finally: finally:
history.append(event) history.append(event)
with open('history.json', 'w') as outfile: with open('history.json', 'w') as outfile:
json.dump(history, outfile) json.dump(history, outfile)

Loading…
Cancel
Save