YoungJules

MCData

Nov 7th, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. # Class handling MasterControl Data
  4.  
  5. # Author: YoungJules with thanks to Matt and the guys at ENTS for the original MasterControl concept.
  6.  
  7. # Class is a singleton and takes care of making a connection to the db and creating a cursor
  8. # This is part of a pluggable architecture which simplifies use of different databases and other data sources
  9. # To enable another database in MasterControl (e.g. postgreSQL)
  10. #   Clone MasterControlMySQL.py to MasterControlPostgres.py
  11. #       Adjust imports
  12. #       Adjust database connection params in connect()
  13. #       Adjust SQL statements where required (e.g. postgreSQL may have different syntax for joins...)
  14. #       Remove MasterControlMySQL.py from application path and rerun RFIDServer
  15. # Using other data sources (flat files, excel...)
  16. #   Follow pattern as above and adjust subroutines to get data from other datasource/update other datasource
  17. #   Ensure routines returning a 'row' return an appropriate array of data
  18. #       If you don't need to close anything at exit, just create a close() with pass, and similar for connect()
  19.  
  20. # Example usage:
  21. #   row = MCData().getMember( '117877', 'FobNumber' )
  22.  
  23. # Available functions:
  24. #   getMember( fob number, fob field name )
  25. #   getLastUnlock( door number )
  26. #   createDoorLogFail( fob number, door number, command )
  27. #   createDoorLogAdmit( fob number, first name, last name, door number )
  28. #   getSeconds( last unlock )
  29. #   updateMember( fob number, fob field name, last unlock field name )
  30.  
  31. # Specific imports for MySQL, your mileage may vary...
  32. import MySQLdb
  33. import MySQLdb.cursors
  34.  
  35. class MCData(object):
  36.     conn = None
  37.     cursor = None
  38.  
  39.     """Use to create a singleton"""
  40.     def __new__(cls, *args, **kwds):
  41.         """
  42.         >>> s = MCData()
  43.         >>> p = MCData()
  44.         >>> id(s) == id(p)
  45.         True
  46.         """
  47.         self = "__self__"
  48.         if not hasattr(cls, self):
  49.             instance = object.__new__(cls)
  50.             instance.init(*args, **kwds)
  51.             setattr(cls, self, instance)
  52.         return getattr(cls, self)
  53.  
  54.     def init(self, *args, **kwds):
  55.         self.connect()
  56.  
  57.     def connect( self ):
  58.         # Information about the database
  59.         database = "ENTS"
  60.         table = "members"
  61.         hostaddr = "localhost"
  62.         username = "mastercontrol"
  63.         # TODO: This is insecure as it can be read by anyone with access to this script, needs to be handled in another way
  64.         password = "pass"
  65.         self.conn = MySQLdb.connect ( host = hostaddr, user = username, passwd = password, db = database, cursorclass=MySQLdb.cursors.DictCursor )
  66.             self.cursor = self.conn.cursor()
  67.  
  68.     def close( self ):
  69.         self.cursor.close()
  70.         self.conn.close()
  71.  
  72.     # Go to the database and get the last unlock event for the given door number
  73.     # We match FobNumber on the members table with CardNum on the DoorLog table and search based on door number
  74.     def getLastUnlock( self, doorNum ):
  75.         # We use an inner join because we want to find at least one match on both tables
  76.             self.cursor.execute ("SELECT * from DoorLog inner join members ON members.FobNumber=DoorLog.CardNum where DoorNum = '"+str(doorNum)+"' order by EventDateTime DESC")
  77.             row = self.cursor.fetchone()                 #get the record for the current cardnum
  78.         #print "getLastUnlock returning row:"        
  79.         #print row
  80.             return row
  81.  
  82.     def getMember( self, fobNumber, fobField ):
  83.             self.cursor.execute ("SELECT * from members where "+fobField+" = '" + fobNumber+"'")
  84.             try:
  85.                 row = self.cursor.fetchone()                 #get the record for the current cardnum
  86.             except ValueError:              #Exception:
  87.                     #TODO: Something better than just printing a message!
  88.                     print "Major Failure \n"
  89.         return row
  90.  
  91.     def createDoorLogFail( self, fobNumber, doorNumber, cmd ):
  92.         self.cursor.execute("insert into DoorLog values ("+fobNumber+
  93.             ",'"+fobNumber+" Not in database: "+cmd+
  94.             "','"+doorNumber+"',now(),NULL)")
  95.  
  96.     def createDoorLogAdmit( self, fobNumber, firstName, lastName, doorNumber):
  97.             self.cursor.execute("insert into DoorLog values ("+fobNumber+
  98.             ",'"+firstName+" "+lastName+" unlocked door "+doorNumber+
  99.             "','"+doorNumber+"',now(),NULL)")
  100.  
  101.     def getSeconds( self, lastUnlock ):
  102.         self.cursor.execute("select timestampdiff(second,'"+lastUnlock+"',now()) as seconds")
  103.             seconds = self.cursor.fetchone()['seconds']
  104.         if (seconds is None):
  105.             seconds = 2000
  106.         return seconds
  107.  
  108.     def updateMember( self, fobNumber, fobField, lastUnlockField ):
  109.         self.cursor.execute("update members set "+lastUnlockField+" =  now() where "+fobField+" = '"+fobNumber+"'")
Advertisement
Add Comment
Please, Sign In to add comment