def lcdThread(threadName, delay): LCD.lcd_init() while 1: time.sleep(delay) #print "%s: %s" % (threadName, time.ctime(time.time()) ) LCD.lcd_string(userName,LCD.LCD_LINE_1) LCD.lcd_string(time.ctime(time.time()),LCD.LCD_LINE_2) LCD.lcd_cleanup()
def taskLCD(): while 1: #print "in taskLCD" time.sleep(0.3) if not LcdQ.empty(): LcdText = LcdQ.get() print "-------------------------------LCD Display:", LcdText LCD.lcd_string(LcdText[0],LCD.LCD_LINE_1) LCD.lcd_string(LcdText[1],LCD.LCD_LINE_2)
def event_login(badgeCode): global currentUser,currentBadge, currentUserID, currentUserTime,globalDeviceName,configOptions if currentBadge == badgeCode: currentUserTime = time.time() + ( 100 * 60 ) return v = requestAccess(badgeCode) # if the server returned that we have more than 0 min left on device # then we have access. if v[2] > 0: logging.info("Access granted for %s granted with time %s" % (badgeCode, v) ) GPIO.output( configOptions['pin_relay'], GPIO.HIGH) currentUser = v[0] currentUserID = v[3] currentUserTime = time.time() + ( v[2] * 60 ) globalDeviceName = v[1] currentBadge = badgeCode led(False,True,False) else: if currentUser: logging.info("Access denied for %s but %s already logged in" % (badgeCode, currentUser)) return logging.info("Access denied for %s " % badgeCode ) led(True,False,False) LCD.lcd_string("Access Denied" ,LCD.LCD_LINE_1) LCD.lcd_string("Take the class" ,LCD.LCD_LINE_2) time.sleep(2) led(False,False,True) LCD.lcd_string("Scan Badge" ,LCD.LCD_LINE_1) LCD.lcd_string("To Login" ,LCD.LCD_LINE_2) GPIO.output( configOptions['pin_relay'], GPIO.LOW )
def resetButtonThread(threadName, delay): RESET_PIN = 16 resetPinVal = GPIO.LOW GPIO.setup(RESET_PIN, GPIO.IN) print "%s: %s" % (threadName, time.ctime(time.time()) ) while 1: time.sleep(delay) resetPinVal = GPIO.input(RESET_PIN) #print "%s: %s %i" % ("RESETING pin", time.ctime(time.time()), resetPinVal ) if resetPinVal == GPIO.HIGH: print "%s: %s %i" % ("RESETING", time.ctime(time.time()), resetPinVal ) LCD.lcd_string("RESETTING",LCD.LCD_LINE_1) LCD.lcd_string(time.ctime(time.time()),LCD.LCD_LINE_2) a_lock.release() resetPinVal = GPIO.LOW
def badgeScanThread(threadName, delay): retcode = SCAN.scanInit() if retcode == "SUCCESS": print "%s: %s" % (threadName, time.ctime(time.time()) ) else: print "FAILED ScanInit %s/n%s" % (retcode, time.ctime(time.time()) ) LCD.lcd_string(retcode,LCD.LCD_LINE_1) LCD.lcd_string(time.ctime(time.time()),LCD.LCD_LINE_2) while 1: time.sleep(delay) badgeId = SCAN.watchPort() # wait here until a badge is scanned userName = DB.queryUserNameFromBadgeId(badgeId) print "badgeScanThread %s: %s" % (userName.text, badgeId ) LCD.lcd_string(userName.text,LCD.LCD_LINE_1) LCD.lcd_string(time.ctime(time.time()),LCD.LCD_LINE_2) a_lock.release()
def deviceAccessControlThread(threadName, delay): LOCK_PIN = 17 a_lock.acquire(1) GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers GPIO.setup(LOCK_PIN, GPIO.OUT) # E while 1: print "LOCK_PIN FALSE %s: %s" % (threadName, time.ctime(time.time()) ) GPIO.output(LOCK_PIN, False) a_lock.acquire() tstart=time.time() print "LOCK_PIN TRUE %s: %s" % (threadName, time.ctime(tstart) ) GPIO.output(LOCK_PIN, True) while (time.time() < delay+tstart): timeremaining = time.ctime((delay+tstart)-time.time() )[14:20] LCD.lcd_string(timeremaining,LCD.LCD_LINE_2) print "LOCK_PIN TRUE %s: %s" % (threadName, timeremaining ) time.sleep(0.5) LCD.lcd_string('WELCOME' ,LCD.LCD_LINE_1) LCD.lcd_string(time.ctime(time.time()) ,LCD.LCD_LINE_2)
def loop(): global currentUserTime, currentUser, configOptions, marioMode, currentTrainerId , currentTrainerCode while True: time.sleep(.01) if (not marioMode) and currentUser: LCD.lcd_string(currentUser,LCD.LCD_LINE_1) LCD.lcd_string( str( int(math.floor( (currentUserTime - time.time())/3600 ))) + ":" + str( int(math.floor( ((currentUserTime - time.time())/60)%60 ))) + ":" + str( int(math.floor( (currentUserTime - time.time())%60 ))) ,LCD.LCD_LINE_2) if currentUserTime - time.time() < 300: led(True,False,True) # if the user runs out of time, log them out if currentUser != False and currentUserTime < time.time(): event_logout() continue # if the user logs out with the logout button log them out if GPIO.input( configOptions['pin_logout'] ) == GPIO.HIGH: holdDownCount=0 marioMode = False # if the logout button is held down for over 2 seconds # you enter mario mode and any badge scanned after this # will become authorized to use this device. while GPIO.input( configOptions['pin_logout'] ) == GPIO.HIGH: time.sleep(.1) holdDownCount = holdDownCount + 1 if holdDownCount > 25 and currentUser: marioMode = True currentTrainerId = currentUserID currentTrainerCode = currentBadge LCD.lcd_string("Mario Mode" ,LCD.LCD_LINE_1) LCD.lcd_string("Activated" ,LCD.LCD_LINE_2) while GPIO.input( configOptions['pin_logout'] ) == GPIO.HIGH: time.sleep(.1) break if not marioMode: event_logout() time.sleep(.2) continue # if the serial port has data read it. if serialConnection.inWaiting() > 1: badgeCode = serialConnection.readline().strip()[-12:] serialConnection.flushInput() serialConnection.flushOutput() # if mario mode is active, then register this badge on the machine if marioMode: # don't re-register the trainer if she scans her badge again if badgeCode == currentTrainerCode: continue # contact the server and register this new badge on this equipment url = "%s/admin/marioStar/%s/%s/%s/%s" % (configOptions['server'], currentTrainerId, currentTrainerCode, configOptions['deviceID'], badgeCode) logging.debug("calling server:" + url) LCD.lcd_string("Calling Server" ,LCD.LCD_LINE_1) LCD.lcd_string("Please Wait..." ,LCD.LCD_LINE_2) try: re = requests.get(url, timeout=5) if re.text == "true": LCD.lcd_string("Register of" ,LCD.LCD_LINE_1) LCD.lcd_string(badgeCode ,LCD.LCD_LINE_2) else: LCD.lcd_string("!!FAILED!!" ,LCD.LCD_LINE_1) LCD.lcd_string(badgeCode ,LCD.LCD_LINE_2) except Exception as e: logging.debug("Error talking to server: %s" % str(e)) LCD.lcd_string("Error Talking" ,LCD.LCD_LINE_1) LCD.lcd_string("To Server" ,LCD.LCD_LINE_2) time.sleep(2) logging.debug("server response:" + re.text) # otherwise just do a normal login else: try: data = event_login(badgeCode) except Exception as e: logging.debug("Error logging in: %s" % str(e)) LCD.lcd_string("Error" ,LCD.LCD_LINE_1) LCD.lcd_string("Logging in" ,LCD.LCD_LINE_2) time.sleep(2) continue
def event_logout(): global configOptions, currentBadge, currentUser,currentUserID, marioMode, currentTrainerId, currentTrainerCode isMachineRunning = False currentBadge = False currentTrainerId = False currentTrainerCode = False marioMode = False # the loop makes sure the machine is powered down all the way # before logging the user off while True: # if the current pin is not high, break out of the loop if not GPIO.input( configOptions['pin_current_sense'] ) == GPIO.HIGH: break #check if machine is running, if so, flag machine running status and prevent shutdown while GPIO.input( configOptions['pin_current_sense'] ) == GPIO.HIGH: isMachineRunning = True LCD.lcd_string("Waiting for" ,LCD.LCD_LINE_1) LCD.lcd_string("Machine to Stop" ,LCD.LCD_LINE_2) time.sleep(1) # If logout was attempted while machine is running, delay for coast time (seconds) defined in config file if isMachineRunning == True: LCD.lcd_string("Machine" ,LCD.LCD_LINE_1) LCD.lcd_string("Coasting Down" ,LCD.LCD_LINE_2) time.sleep(configOptions['logout_coast_time'] ) isMachineRunning = False if currentUser: # tell the server we have logged out url = "%s/device/%s/logout/%s" % (configOptions['server'], configOptions['deviceID'], currentUserID) logging.debug("calling server:" + url) re = requests.get(url, timeout=5) logging.debug("server response:" + re.text) logging.info("%s logged out" % currentUser ) GPIO.output( configOptions['pin_relay'], GPIO.LOW ) currentUser = False currentUserTime = 0 currentUserID = False else: currentUserTime = 0 #LCD Waiting Status LCD.lcd_string("Scan Badge" ,LCD.LCD_LINE_1) LCD.lcd_string("To Login" ,LCD.LCD_LINE_2) led(False,False,True) #Blue LED
GPIO.output( configOptions['pin_relay'], GPIO.LOW ) GPIO.output(configOptions['pin_led_r'], False) GPIO.output(configOptions['pin_led_g'], False) GPIO.output(configOptions['pin_led_b'], False) # configure the serial port if not os.path.exists(configOptions['serialPortName']): logging.fatal("Unable to find serial port %s" % configOptions['serialPortName'] ) sys.exit(1) serialConnection = serial.Serial( configOptions['serialPortName'], configOptions['serialPortSpeed'] ) serialConnection.flushInput() serialConnection.flushOutput() LCD.lcd_init() LCD.lcd_string("Scan Badge" ,LCD.LCD_LINE_1) LCD.lcd_string("To Login" ,LCD.LCD_LINE_2) led(False,False,True) # End Initialize ## def requestAccess(badgeCode): global configOptions url = "%s/device/%s/code/%s" % (configOptions['server'], configOptions['deviceID'], badgeCode) logging.debug("calling server:" + url) serverResponse = requests.get(url, timeout=5) data = serverResponse.json() username = data['username']
def startLCD(): LCD.lcd_init() print "%s: %s" % ("WELCOME", time.ctime(time.time()) ) LCD.lcd_string("WELCOME",LCD.LCD_LINE_1) LCD.lcd_string(time.ctime(time.time()),LCD.LCD_LINE_2)