class RFIDObject: def __init__(self): self.InitRFID() def InitRFID(self): try: self.RFIDEnabled=False self.rfid = RFID() self.rfid.setOnAttachHandler(self.RFIDAttached) self.rfid.setOnDetachHandler(self.RFIDDetached) self.rfid.setOnTagHandler(self.RFIDNewTag) self.rfid.openPhidget() print ('here') except RuntimeError as e: self.Log("Runtime Exception creating RFID: %s" % e.details) self.Log("Exiting....") exit(1) def RFIDAttached(self,e): self.RFIDEnabled=True print "Found RFID" self.rfid.setAntennaOn(True) def RFIDDetached(self,e): self.RFIDEnabled=False print "RFID Detached" def RFIDNewTag(self,e): self.RFIDButtonHit=True print "RFID tag %s"%e.tag
def __init__(self): # Create an RFID object try: self.rfid = RFID() self.user_manager = UserManager() except RuntimeError as e: Logger.info("RFID: Phidget Runtime Exception: %s" % e.details) Logger.info("RFID: Phidget Exiting....") exit(1) #Main Phiget Program Code try: #logging example, uncomment to generate a log file #rfid.enableLogging(PhidgetLogLevel.PHIDGET_LOG_VERBOSE, "phidgetlog.log") self.rfid.setOnAttachHandler(self.rfidAttached) self.rfid.setOnDetachHandler(self.rfidDetached) self.rfid.setOnErrorhandler(self.rfidError) self.rfid.setOnOutputChangeHandler(self.rfidOutputChanged) self.rfid.setOnTagHandler(self.rfidTagGained) self.rfid.setOnTagLostHandler(self.rfidTagLost) except PhidgetException as e: Logger.exception("RFID: Phidget Exception %i: %s" % (e.code, e.details)) Logger.exception("RFID: Exiting....") exit(1) Logger.info("RFID: Opening phidget object....") try: self.rfid.openPhidget() except PhidgetException as e: Logger.info("RFID: Phidget Exception %i: %s" % (e.code, e.details)) Logger.info("RFID: Exiting....") exit(1) Logger.info("RFID: Waiting for attach....") try: self.rfid.waitForAttach(10000) except PhidgetException as e: Logger.exception("RFID: Phidget Exception %i: %s" % (e.code, e.details)) try: self.rfid.closePhidget() except PhidgetException as e: Logger.exception("RFID: Phidget Exception %i: %s" % (e.code, e.details)) Logger.exception("RFID: Exiting....") exit(1) Logger.exception("RFID: Exiting....") exit(1) else: self.displayDeviceInfo() Logger.info("RFID: Turning on the RFID antenna....") self.rfid.setAntennaOn(True)
def __init__(self, config): self.config = config self.store = PersistentStore(self.config['dbfilename']) self.spotify_session = SpotifyAsyncSessionManager(useragent="SpotifyThings") self.spotify_session.player_state_changed += self.__player_state_changed self.spotify_session.current_track_changed += self.__current_track_changed self.spotify_session.playback_progress += self.__playback_progress_event self.spotify_session.playqueue_changed += self.__playqueue_changed_event self.spotify_session.login_state_changed += self.__login_state_changed_event self.spotify_session.password_blob_changed += self.__login_password_blob_changed self.wsregistry = WebSocketRegistry(); self.wsregistry.on_new_client += self.__on_new_client self.webapp = WebApplication(self) self.asyncioloop = AsyncIOLoop() self.attached_rfid_readers = dict() self.rfid = RFID() self.rfid.setOnAttachHandler(self.rfidAttached) self.rfid.setOnDetachHandler(self.rfidDetached) self.rfid.setOnErrorhandler(self.rfidError) self.rfid.setOnTagHandler(self.rfidTagGained) self.rfid.setOnTagLostHandler(self.rfidTagLost)
def InitRFID(self): try: self.RFIDEnabled=False self.rfid = RFID() self.rfid.setOnAttachHandler(self.RFIDAttached) self.rfid.setOnDetachHandler(self.RFIDDetached) self.rfid.setOnTagHandler(self.RFIDNewTag) self.rfid.openPhidget() print ('here') except RuntimeError as e: self.Log("Runtime Exception creating RFID: %s" % e.details) self.Log("Exiting....") exit(1)
def __init__(self): self.last_tag=0 try: self.rfid = RFID() self.rfid.setOnAttachHandler(self.rfidAttached) self.rfid.setOnDetachHandler(self.rfidDetached) self.rfid.setOnErrorhandler(self.rfidError) self.rfid.setOnOutputChangeHandler(self.rfidOutputChanged) self.rfid.setOnTagHandler(self.rfidTagGained) self.rfid.setOnTagLostHandler(self.rfidTagLost) self.rfid.openPhidget() self.rfid.waitForAttach(10000) except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) exit(1)
def __init__(self): print("INIT: RFID") try: self.rfid = RFID() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) try: self.rfid.setOnAttachHandler(self.rfidAttached) self.rfid.setOnDetachHandler(self.rfidDetached) self.rfid.setOnErrorhandler(self.rfidError) self.rfid.setOnOutputChangeHandler(self.rfidOutputChanged) self.rfid.setOnTagHandler(self.rfidTagGained) self.rfid.setOnTagLostHandler(self.rfidTagLost) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) #print("Opening phidget object....") try: self.rfid.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) #print("Waiting for attach....") try: self.rfid.waitForAttach(100000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: self.rfid.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1)
def init_rfid(): try: if debug: print "Initializing RFID object..." rfid = RFID() rfid.setOnErrorhandler(rfidError) rfid.setOnTagHandler(rfidTagGained) rfid.setOnTagLostHandler(rfidTagLost) rfid.openPhidget() if debug: print "RFID object initialized!" print "Waiting to attach RFID reader..." rfid.waitForAttach(10000) if debug: print "RFID reader attached!" rfid.setAntennaOn(True) return rfid except RuntimeError as e: print "Runtime error: %s stopping!" % e.details exit(1) except PhidgetException as e: print "Phidget error: %i, %s" % (e.code, e.details) exit(1)
def AttachRFID(databasepath, serialNumber): def onAttachHandler(event): logString = "RFID Attached " + str(event.device.getSerialNum()) #print(logString) DisplayAttachedDeviceInfo(event.device) def onDetachHandler(event): logString = "RFID Detached " + str(event.device.getSerialNum()) #print(logString) DisplayDetachedDeviceInfo(event.device) event.device.closePhidget() def onErrorHandler(event): logString = "RFID Error " + str(event.device.getSerialNum()) + ", Error: " + event.description #print(logString) DisplayErrorDeviceInfo(event) def onServerConnectHandler(event): logString = "RFID Server Connect " + str(event.device.getSerialNum()) #print(logString) def onServerDisconnectHandler(event): logString = "RFID Server Disconnect " + str(event.device.getSerialNum()) #print(logString) def outputChangeHandler(event): logString = "RFID Changed " + str(event.device.getSerialNum()) #print(logString) try: conn = sqlite3.connect(databasepath) conn.execute("INSERT INTO RFID_OUTPUTCHANGE VALUES(NULL, DateTime('now'), ?, ?, ?)", (event.device.getSerialNum(), event.index, event.value)) conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] def tagHandler(event): logString = "RFID Changed " + str(event.device.getSerialNum()) #print(logString) try: conn = sqlite3.connect(databasepath) conn.execute("INSERT INTO RFID_TAG VALUES(NULL, DateTime('now'), ?, ?)", (event.device.getSerialNum(), event.tag)) conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] def tagLostHandler(event): logString = "RFID Changed " + str(event.device.getSerialNum()) #print(logString) try: conn = sqlite3.connect(databasepath) conn.execute("INSERT INTO RFID_TAGLOST VALUES(NULL, DateTime('now'), ?, ?)", (event.device.getSerialNum(), event.tag)) conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] def accelerationChangeHandler(event): logString = "RFID Changed " + str(event.device.getSerialNum()) #print(logString) try: conn = sqlite3.connect(databasepath) conn.execute("INSERT INTO ACCELEROMETER_CHANGE VALUES(NULL, DateTime('now'), ?, ?, ?)", (event.device.getSerialNum(), event.index, event.acceleration)) conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] try: p = RFID() p.setOnAttachHandler(onAttachHandler) p.setOnDetachHandler(onDetachHandler) p.setOnErrorhandler(onErrorHandler) p.setOnServerConnectHandler(onServerConnectHandler) p.setOnServerDisconnectHandler(onServerDisconnectHandler) p.setOnOutputChangeHandler(outputChangeHandler) p.setOnTagHandler (tagHandler) p.setOnTagLostHandler (tagLostHandler) p.openPhidget(serialNumber) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting...") exit(1)
def AttachRFID(databasepath, serialNumber): def onAttachHandler(event): logString = "RFID Attached " + str(event.device.getSerialNum()) #print(logString) DisplayAttachedDeviceInfo(event.device) def onDetachHandler(event): logString = "RFID Detached " + str(event.device.getSerialNum()) #print(logString) DisplayDetachedDeviceInfo(event.device) event.device.closePhidget() def onErrorHandler(event): logString = "RFID Error " + str( event.device.getSerialNum()) + ", Error: " + event.description #print(logString) DisplayErrorDeviceInfo(event) def onServerConnectHandler(event): logString = "RFID Server Connect " + str(event.device.getSerialNum()) #print(logString) def onServerDisconnectHandler(event): logString = "RFID Server Disconnect " + str( event.device.getSerialNum()) #print(logString) def outputChangeHandler(event): logString = "RFID Changed " + str(event.device.getSerialNum()) #print(logString) try: conn = sqlite3.connect(databasepath) conn.execute( "INSERT INTO RFID_OUTPUTCHANGE VALUES(NULL, DateTime('now'), ?, ?, ?)", (event.device.getSerialNum(), event.index, event.value)) conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] def tagHandler(event): logString = "RFID Changed " + str(event.device.getSerialNum()) #print(logString) try: conn = sqlite3.connect(databasepath) conn.execute( "INSERT INTO RFID_TAG VALUES(NULL, DateTime('now'), ?, ?)", (event.device.getSerialNum(), event.tag)) conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] def tagLostHandler(event): logString = "RFID Changed " + str(event.device.getSerialNum()) #print(logString) try: conn = sqlite3.connect(databasepath) conn.execute( "INSERT INTO RFID_TAGLOST VALUES(NULL, DateTime('now'), ?, ?)", (event.device.getSerialNum(), event.tag)) conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] def accelerationChangeHandler(event): logString = "RFID Changed " + str(event.device.getSerialNum()) #print(logString) try: conn = sqlite3.connect(databasepath) conn.execute( "INSERT INTO ACCELEROMETER_CHANGE VALUES(NULL, DateTime('now'), ?, ?, ?)", (event.device.getSerialNum(), event.index, event.acceleration)) conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] try: p = RFID() p.setOnAttachHandler(onAttachHandler) p.setOnDetachHandler(onDetachHandler) p.setOnErrorhandler(onErrorHandler) p.setOnServerConnectHandler(onServerConnectHandler) p.setOnServerDisconnectHandler(onServerDisconnectHandler) p.setOnOutputChangeHandler(outputChangeHandler) p.setOnTagHandler(tagHandler) p.setOnTagLostHandler(tagLostHandler) p.openPhidget(serialNumber) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting...") exit(1)
__author__ = 'Adam Stelmack' __version__ = '2.1.8' __date__ = 'May 17 2010' #Basic imports from ctypes import * import sys #Phidget specific imports from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException from Phidgets.Events.Events import AttachEventArgs, DetachEventArgs, ErrorEventArgs, OutputChangeEventArgs, TagEventArgs from Phidgets.Devices.RFID import RFID, RFIDTagProtocol from Phidgets.Phidget import PhidgetLogLevel #Create an RFID object try: rfid = RFID() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) #Information Display Function def displayDeviceInfo(): print( "|------------|----------------------------------|--------------|------------|" ) print( "|- Attached -|- Type -|- Serial No. -|- Version -|" ) print(
#Basic imports from ctypes import * import sys #Phidget specific imports from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException from Phidgets.Events.Events import AttachEventArgs, DetachEventArgs, ErrorEventArgs, OutputChangeEventArgs, TagEventArgs from Phidgets.Devices.RFID import RFID, RFIDTagProtocol from Phidgets.Phidget import PhidgetLogLevel import pyautogui import time #Create an RFID object try: rfid = RFID() except RuntimeError as e: rfid.log(PhidgetLogLevel.PHIDGET_LOG_INFO, None, "Runtime Exception: %s" % e.details) rfid.log(PhidgetLogLevel.PHIDGET_LOG_INFO, None, "Exiting....") exit(1) #Information Display Function def displayDeviceInfo(): rfid.log(PhidgetLogLevel.PHIDGET_LOG_INFO, None, "|------------|----------------------------------|--------------|------------|") rfid.log(PhidgetLogLevel.PHIDGET_LOG_INFO, None, "|- Attached -|- Type -|- Serial No. -|- Version -|") rfid.log(PhidgetLogLevel.PHIDGET_LOG_INFO, None, "|------------|----------------------------------|--------------|------------|") rfid.log(PhidgetLogLevel.PHIDGET_LOG_INFO, None, "|- %8s -|- %30s -|- %10d -|- %8d -|" % (rfid.isAttached(), rfid.getDeviceName(), rfid.getSerialNum(), rfid.getDeviceVersion())) rfid.log(PhidgetLogLevel.PHIDGET_LOG_INFO, None, "|------------|----------------------------------|--------------|------------|") rfid.log(PhidgetLogLevel.PHIDGET_LOG_INFO, None, "Number of outputs: %i -- Antenna Status: %s -- Onboard LED Status: %s" % (rfid.getOutputCount(), rfid.getAntennaOn(), rfid.getLEDOn()))
__author__ = 'Adam Stelmack' __version__ = '2.1.8' __date__ = 'May 17 2010' #Basic imports from ctypes import * import sys import serial #Phidget specific imports from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException from Phidgets.Events.Events import AttachEventArgs, DetachEventArgs, ErrorEventArgs, OutputChangeEventArgs, TagEventArgs from Phidgets.Devices.RFID import RFID #Create an RFID object try: rfid = RFID() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) #Information Display Function def displayDeviceInfo(): print("|------------|----------------------------------|--------------|------------|") print("|- Attached -|- Type -|- Serial No. -|- Version -|") print("|------------|----------------------------------|--------------|------------|") print("|- %8s -|- %30s -|- %10d -|- %8d -|" % (rfid.isAttached(), rfid.getDeviceName(), rfid.getSerialNum(), rfid.getDeviceVersion())) print("|------------|----------------------------------|--------------|------------|") print("Number of outputs: %i -- Antenna Status: %s -- Onboard LED Status: %s" % (rfid.getOutputCount(), rfid.getAntennaOn(), rfid.getLEDOn())) #Event Handler Callback Functions
class AppContainer(object): def __init__(self, config): self.config = config self.store = PersistentStore(self.config['dbfilename']) self.spotify_session = SpotifyAsyncSessionManager(useragent="SpotifyThings") self.spotify_session.player_state_changed += self.__player_state_changed self.spotify_session.current_track_changed += self.__current_track_changed self.spotify_session.playback_progress += self.__playback_progress_event self.spotify_session.playqueue_changed += self.__playqueue_changed_event self.spotify_session.login_state_changed += self.__login_state_changed_event self.spotify_session.password_blob_changed += self.__login_password_blob_changed self.wsregistry = WebSocketRegistry(); self.wsregistry.on_new_client += self.__on_new_client self.webapp = WebApplication(self) self.asyncioloop = AsyncIOLoop() self.attached_rfid_readers = dict() self.rfid = RFID() self.rfid.setOnAttachHandler(self.rfidAttached) self.rfid.setOnDetachHandler(self.rfidDetached) self.rfid.setOnErrorhandler(self.rfidError) self.rfid.setOnTagHandler(self.rfidTagGained) self.rfid.setOnTagLostHandler(self.rfidTagLost) # player events def __player_state_changed(self, state): self.wsregistry.broadcast('app', PlayerStateMessage(state)) def __current_track_changed(self, track): self.wsregistry.broadcast('app', PlayerCurrentTrackMessage(track)) def __playback_progress_event(self, playback_time): self.wsregistry.broadcast('app', PlayerPlaybackProgressMessage(int(playback_time))) def __playqueue_changed_event(self): self.wsregistry.broadcast('app', PlayerQueueModifiedMessage()) # app event def __login_state_changed_event(self, is_logged_in, login_error, current_user): self.wsregistry.broadcast('app', SpotifyLoginStateMessage(is_logged_in, login_error, current_user)) def __login_password_blob_changed(self, username, password_blob): self.store.set_config('spotify.username', username) self.store.set_config('spotify.password_blob', password_blob) self.wsregistry.broadcast('app', SpotifyCredentialStoredMessage(self.store.get_config('spotify.password_blob') is not None)) # websocket events def __on_new_client(self, wstype, ws): def spotify_state_handler(current_track, track_playback_time, player_state): # send player state ws.write_message(PlayerStateMessage(player_state)) # send current track ws.write_message(PlayerCurrentTrackMessage(current_track)) # send playback progress ws.write_message(PlayerPlaybackProgressMessage(int(track_playback_time))) def spotify_login_state_handler(is_logged_in, login_error, current_user): # send login state message ws.write_message(SpotifyLoginStateMessage(is_logged_in, login_error, current_user)) if wstype == "app": # request spotify state self.spotify_session.get_player_state(spotify_state_handler) # request login state self.spotify_session.get_login_state(spotify_login_state_handler) # send message with currently attched rfid readers ws.write_message(RfidReaderUpdatedMessage(self.attached_rfid_readers)) # send message with credentials stored status ws.write_message(SpotifyCredentialStoredMessage(self.store.get_config('spotify.password_blob') is not None)) # rfid events def rfidAttached(self, e): device = e.device device.setAntennaOn(True) # add to list of attached readers reader = RfidReader( device.getDeviceName(), device.getDeviceVersion(), device.getSerialNum()) self.attached_rfid_readers[reader.serial_no] = reader # notify clients self.wsregistry.broadcast('app', RfidReaderUpdatedMessage(self.attached_rfid_readers)) logger.info('RFID reader %s Attached', device.getSerialNum()) def rfidDetached(self, e): device = e.device serial_no = device.getSerialNum() # remove from attached list del self.attached_rfid_readers[serial_no] # notify clients self.wsregistry.broadcast('app', RfidReaderUpdatedMessage(self.attached_rfid_readers)) logger.info('RFID reader %s Deteched', device.getSerialNum()) def rfidError(self, e): try: source = e.device logger.error("RFID %i: Phidget Error %i: %s", source.getSerialNum(), e.eCode, e.description) except PhidgetException as e: logger.exception('Phidget Exception %i: %s', e.code, e.details) def rfidTagGained(self, e): source = e.device self.rfid.setLEDOn(1) logger.debug('RFID %i: Tag Read: %s', source.getSerialNum(), e.tag) # get existing mapping, if any tagmapping = self.store.find_by_tagid(e.tag) # broadcast to any listeners any_listeners = self.wsregistry.broadcast('rfid', RfidTagReadMessage(e.tag, tagmapping)) # no listeners and resource was mapped, play resource if not any_listeners and tagmapping: self.spotify_session.play_link(tagmapping.spotifylink, None) def rfidTagLost(self, e): source = e.device self.rfid.setLEDOn(0) logger.debug('RFID %i: Tag Lost: %s', source.getSerialNum(), e.tag) def forget_spotify_credentials(self): # delete from store self.store.delete_configs(['spotify.username', 'spotify.password_blob']) # notify clients self.wsregistry.broadcast('app', SpotifyCredentialStoredMessage(self.store.get_config('spotify.password_blob') is not None)) def start(self): logger.info('Starting appcontainer') # initialize store logger.info('Initializing store') self.store.initialize() # start spotify logger.info('Starting spotify session') self.spotify_session.start() # login using stored credentials username = self.store.get_config('spotify.username') password_blob = self.store.get_config('spotify.password_blob') if username and password_blob: logger.info('Logging in with username %s', username) self.spotify_session.login(username, None, False, password_blob) # start webapp listen_port = self.config['listen_port'] logger.info('Starting web application, listening on port %s', listen_port) self.webapp.listen(listen_port) self.asyncioloop.start() # start rfid logger.info('Initializing RFID reader') self.rfid.openPhidget() logger.info('Appcontainer started') def stop(self): logger.info('Stopping appcontainer') # stop rfid logger.info('Closing RFID reader') self.rfid.closePhidget() # stop webapp logger.info('Stopping web application') self.asyncioloop.stop() # stop spotify logger.info('Stopping Spotify session') self.spotify_session.stop() logger.info('Appcontainer stopped')
class Reader: ''' Phidgets 125khz RFID Reader/Writer ''' def __init__(self): # Create an RFID object try: self.rfid = RFID() self.user_manager = UserManager() except RuntimeError as e: Logger.info("RFID: Phidget Runtime Exception: %s" % e.details) Logger.info("RFID: Phidget Exiting....") exit(1) #Main Phiget Program Code try: #logging example, uncomment to generate a log file #rfid.enableLogging(PhidgetLogLevel.PHIDGET_LOG_VERBOSE, "phidgetlog.log") self.rfid.setOnAttachHandler(self.rfidAttached) self.rfid.setOnDetachHandler(self.rfidDetached) self.rfid.setOnErrorhandler(self.rfidError) self.rfid.setOnOutputChangeHandler(self.rfidOutputChanged) self.rfid.setOnTagHandler(self.rfidTagGained) self.rfid.setOnTagLostHandler(self.rfidTagLost) except PhidgetException as e: Logger.exception("RFID: Phidget Exception %i: %s" % (e.code, e.details)) Logger.exception("RFID: Exiting....") exit(1) Logger.info("RFID: Opening phidget object....") try: self.rfid.openPhidget() except PhidgetException as e: Logger.info("RFID: Phidget Exception %i: %s" % (e.code, e.details)) Logger.info("RFID: Exiting....") exit(1) Logger.info("RFID: Waiting for attach....") try: self.rfid.waitForAttach(10000) except PhidgetException as e: Logger.exception("RFID: Phidget Exception %i: %s" % (e.code, e.details)) try: self.rfid.closePhidget() except PhidgetException as e: Logger.exception("RFID: Phidget Exception %i: %s" % (e.code, e.details)) Logger.exception("RFID: Exiting....") exit(1) Logger.exception("RFID: Exiting....") exit(1) else: self.displayDeviceInfo() Logger.info("RFID: Turning on the RFID antenna....") self.rfid.setAntennaOn(True) # Information Display Function def displayDeviceInfo(self): Logger.info("RFID: |------------|----------------------------------|--------------|------------|") Logger.info("RFID: |- Attached -|- Type -|- Serial No. -|- Version -|") Logger.info("RFID: |------------|----------------------------------|--------------|------------|") Logger.info("RFID: |- %8s -|- %30s -|- %10d -|- %8d -|" % (self.rfid.isAttached(), self.rfid.getDeviceName(), self.rfid.getSerialNum(), self.rfid.getDeviceVersion())) Logger.info("RFID: |------------|----------------------------------|--------------|------------|") Logger.info("RFID: Number of outputs: %i -- Antenna Status: %s -- Onboard LED Status: %s" % (self.rfid.getOutputCount(), self.rfid.getAntennaOn(), self.rfid.getLEDOn())) #Event Handler Callback Functions def rfidAttached(self, e): self.attached = e.device Logger.info("RFID: %i Attached!" % (self.attached.getSerialNum())) def rfidDetached(self, e): self.detached = e.device Logger.info("RFID: %i Detached!" % (self.detached.getSerialNum())) def rfidError(self, e): try: source = e.device Logger.exception("RFID: %i Phidget Error %i: %s" % (self.source.getSerialNum(), e.eCode, e.description)) except PhidgetException as e: Logger.exception(("RFID: Phidget Exception %i: %s" % (e.code, e.details))) def rfidOutputChanged(self, e): self.source = e.device Logger.info("RFID: %i Output %i State: %s" % (self.source.getSerialNum(), e.index, e.state)) def rfidTagGained(self, e): self.rfid.setLEDOn(1) Logger.info("RFID: Tag gained: {}".format(e.tag)) self.user_manager.tag_gained = e.tag # this sets up the UserManager.on_tag_gained() to be called def rfidTagLost(self, e): self.rfid.setLEDOn(0) Logger.info("RFID: Tag lost: {}".format(e.tag))
# Basic imports from ctypes import * import sys # Phidget specific imports from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException from Phidgets.Events.Events import AttachEventArgs, DetachEventArgs, ErrorEventArgs, OutputChangeEventArgs, TagEventArgs from Phidgets.Devices.RFID import RFID, RFIDTagProtocol from Phidgets.Phidget import PhidgetLogLevel import pyautogui import time # Create an RFID object try: rfid = RFID() except RuntimeError as e: rfid.log(PhidgetLogLevel.PHIDGET_LOG_INFO, None, "Runtime Exception: %s" % e.details) rfid.log(PhidgetLogLevel.PHIDGET_LOG_INFO, None, "Exiting....") exit(1) # Information Display Function def displayDeviceInfo(): rfid.log( PhidgetLogLevel.PHIDGET_LOG_INFO, None, "|------------|----------------------------------|--------------|------------|", ) rfid.log( PhidgetLogLevel.PHIDGET_LOG_INFO,
class RFID_Controller: def __init__(self): self.last_tag=0 try: self.rfid = RFID() self.rfid.setOnAttachHandler(self.rfidAttached) self.rfid.setOnDetachHandler(self.rfidDetached) self.rfid.setOnErrorhandler(self.rfidError) self.rfid.setOnOutputChangeHandler(self.rfidOutputChanged) self.rfid.setOnTagHandler(self.rfidTagGained) self.rfid.setOnTagLostHandler(self.rfidTagLost) self.rfid.openPhidget() self.rfid.waitForAttach(10000) except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) exit(1) def rfidAttached(self,e): attached = e.device print("RFID %i Attached!" % (attached.getSerialNum())) def rfidDetached(self,e): detached = e.device print("RFID %i Detached!" % (detached.getSerialNum())) def rfidError(self,e): try: source = e.device print("RFID %i: Phidget Error %i: %s" % (source.getSerialNum(), e.eCode, e.description)) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) def rfidOutputChanged(self,e): source = e.device print("RFID %i: Output %i State: %s" % (source.getSerialNum(), e.index, e.state)) def rfidTagGained(self,e): source = e.device self.last_tag=e.tag self.rfid.setLEDOn(1) def rfidTagLost(self,e): source = e.device self.rfid.setLEDOn(0) def displayDeviceInfo(self): print("|------------|----------------------------------|--------------|------------|") print("|- Attached -|- Type -|- Serial No. -|- Version -|") print("|------------|----------------------------------|--------------|------------|") print("|- %8s -|- %30s -|- %10d -|- %8d -|" % (self.rfid.isAttached(), self.rfid.getDeviceName(), self.rfid.getSerialNum(), self.rfid.getDeviceVersion())) print("|------------|----------------------------------|--------------|------------|") print("Number of outputs: %i -- Antenna Status: %s -- Onboard LED Status: %s" % (self.rfid.getOutputCount(), self.rfid.getAntennaOn(), self.rfid.getLEDOn())) def end_program(self): try: self.rfid.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Done.") exit(0)
class csss_rfid: onTagCallback = None rfid = None rfid_tags = { '01022EA8F2':'execA', '1000033B7F':'execB', '01022FA703':'member' } def __init__(self): print("INIT: RFID") try: self.rfid = RFID() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) try: self.rfid.setOnAttachHandler(self.rfidAttached) self.rfid.setOnDetachHandler(self.rfidDetached) self.rfid.setOnErrorhandler(self.rfidError) self.rfid.setOnOutputChangeHandler(self.rfidOutputChanged) self.rfid.setOnTagHandler(self.rfidTagGained) self.rfid.setOnTagLostHandler(self.rfidTagLost) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) #print("Opening phidget object....") try: self.rfid.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) #print("Waiting for attach....") try: self.rfid.waitForAttach(100000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: self.rfid.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) def setOnTagCallback(self, func): self.onTagCallback = func def displayDeviceInfo(): print("|------------|----------------------------------|--------------|------------|") print("|- Attached -|- Type -|- Serial No. -|- Version -|") print("|------------|----------------------------------|--------------|------------|") print("|- %8s -|- %30s -|- %10d -|- %8d -|" % (rfid.isAttached(), rfid.getDeviceName(), rfid.getSerialNum(), rfid.getDeviceVersion())) print("|------------|----------------------------------|--------------|------------|") print("Antenna Status: %s " % (rfid.getAntennaOn())) #Event Handler Callback Functions def rfidAttached(self, e): attached = e.device print("RFID %i Attached!" % (attached.getSerialNum())) def rfidDetached(self, e): detached = e.device print("RFID %i Detached!" % (detached.getSerialNum())) def rfidError(self, e): try: source = e.device print("RFID %i: Phidget Error %i: %s" % (source.getSerialNum(), e.eCode, e.description)) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) def rfidOutputChanged(self, e): source = e.device print("RFID %i: Output %i State: %s" % (source.getSerialNum(), e.index, e.state)) def rfidTagGained(self, e): source = e.device id = e.tag if id in self.rfid_tags: id = self.rfid_tags[id] self.onTagCallback(id) def rfidTagLost(self, e): source = e.device def close(self): try: self.rfid.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1)