Created
May 8, 2013 15:10
-
-
Save jessedobbelaere/37e7e60c5c8cf47ed3a6 to your computer and use it in GitHub Desktop.
Check-in script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from time import sleep | |
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate | |
import requests | |
import os | |
# Variables | |
baseURL = 'http://127.0.0.1' | |
# Initialize the LCD plate. | |
lcd = Adafruit_CharLCDPlate() | |
lcd.clear() | |
lcd.backlight(lcd.OFF) | |
# Build initial status array with userID's and their status | |
r = requests.get(baseURL + '/api/users') | |
users = r.json() | |
statusArr = {} | |
for user in users['content']: | |
if user['status'] is None: | |
statusArr[int(user['ID'])] = 0 | |
else: | |
statusArr[int(user['ID'])] = int(user['status']) | |
# Check every 3 seconds for new changes | |
def pollCheckinsAndDisplay(): | |
checkForChanges() | |
sleep(3) | |
# Check user api for changes in checkin/checkout | |
def checkForChanges(): | |
r = requests.get(baseURL + '/api/users') | |
users = r.json() | |
for user in users['content']: | |
if user['status'] is not None: | |
# print('Current status: ' + user['status']) | |
# print('User ID: ' + user['ID']) | |
# previousStatus = statusArr[int(user['ID'])] | |
# print('Previous status: ' + str(previousStatus)) | |
if int(user['status']) != statusArr[int(user['ID'])]: | |
print('User with id=' + user['ID'] + ' changed status...') | |
showUserCheckinOnDisplay(user) | |
sleep(3) | |
lcd.clear() | |
lcd.backlight(lcd.OFF) | |
# Show checkin on display when user status changes | |
def showUserCheckinOnDisplay(user): | |
lcd.clear() | |
if int(user['status']) == 1: | |
lcd.backlight(lcd.GREEN) | |
lcd.message(user['username'] + '\nchecked-in') | |
statusArr[int(user['ID'])] = 1 | |
elif int(user['status']) == 0: | |
lcd.backlight(lcd.RED) | |
lcd.message(user['username'] + '\nchecked-out') | |
statusArr[int(user['ID'])] = 0 | |
else: | |
print('error') | |
# Poll for nfc cards | |
def pollNFC(): | |
f = os.popen("/home/pi/libnfc/libnfc-1.6.0-rc1/examples/nfc-poll") | |
for i in f.readlines(): | |
if "UID" in i: | |
uidStr = i.strip().replace(" ", "").split(":") | |
uid = uidStr[1] | |
print('NFC card with UID found: ' + uid) | |
doCheckinByUID(uid) | |
def doCheckinByUID(uid): | |
# Do post request | |
r = requests.post(baseURL + '/api/users/' + uid + '/checkins') | |
if int(r.status_code) == 200: | |
print('Succesfully checked user in/out!') | |
# Retrieve user info and display it on screen | |
r = requests.get(baseURL + '/api/users/' + str(uid)) | |
userInfo = r.json() | |
user = userInfo['content'] | |
# Display on LCD | |
if int(user['status']) == 1: | |
lcd.backlight(lcd.GREEN) | |
lcd.message(user['username'] + '\nchecked-in') | |
statusArr[int(user['ID'])] = 1 | |
elif int(user['status']) == 0: | |
lcd.backlight(lcd.RED) | |
lcd.message(user['username'] + '\nchecked-out') | |
statusArr[int(user['ID'])] = 0 | |
else: | |
print('Something went wrong...') | |
sleep(3) | |
lcd.clear() | |
lcd.backlight(lcd.OFF) | |
# Infinite loop | |
while True: | |
#pollCheckinsAndDisplay() # Poll the api for new checkins and display them on screen (depricated) | |
pollNFC() # Poll for NFC cards and do checkin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment