Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import MySQLdb
- import MySQLdb.cursors
- class MCDataSource(object):
- # Information about the database
- database = "ENTS"
- table = "members"
- hostaddr = "localhost"
- username = "mastercontrol"
- # TODO: This is insecure as it can be read by anyone with access to this script, needs to be handled in another way
- password = "pass"
- conn = None
- cursor = None
- """Use to create a singleton"""
- def __new__(cls, *args, **kwds):
- """
- >>> s = Singleton()
- >>> p = Singleton()
- >>> id(s) == id(p)
- True
- """
- self = "__self__"
- if not hasattr(cls, self):
- instance = object.__new__(cls)
- instance.init(*args, **kwds)
- setattr(cls, self, instance)
- return getattr(cls, self)
- def init(self, *args, **kwds):
- connect()
- def connect():
- self.conn = MySQLdb.connect ( host = hostaddr, user = username, passwd = password, db = database, cursorclass=MySQLdb.cursors.DictCursor )
- self.cursor = conn.cursor()
- #return conn, cursor
- def close(conn, cursor):
- self.cursor.close()
- self.conn.close()
- # Go to the database and get the last unlock event for the given door number
- # We match FobNumber on the members table with CardNum on the DoorLog table and search based on door number
- def getLastUnlock( doorNum ):
- print "getLastUnlock connecting to db"
- #conn, cursor = connect()
- #conn = MySQLdb.connect ( host = hostaddr, user = username, passwd = password, db = database, cursorclass=MySQLdb.cursors.DictCursor )
- #cursor = conn.cursor() #connect to users db
- print "connection cursor created"
- # We use an inner join because we want to find at least one match on both tables
- self.cursor.execute ("SELECT * from DoorLog inner join members ON members.FobNumber=DoorLog.CardNum where DoorNum = '"+str(doorNum)+"' order by EventDateTime DESC")
- row = self.cursor.fetchone() #get the record for the current cardnum
- print "getLastUnlock returning row:"
- print row
- #close(conn, cursor)
- return row
- def getMember( fobNumber, fobField ):
- #conn = MySQLdb.connect ( host = hostaddr, user = username, passwd = password, db = database, cursorclass=MySQLdb.cursors.DictCursor )
- #cursor = conn.cursor() #connect to users db
- print "getMember connecting to db"
- #conn, cursor = connect()
- self.cursor.execute ("SELECT * from members where "+fobField+" = '" + fobNumber+"'")
- try:
- row = self.cursor.fetchone() #get the record for the current cardnum
- except ValueError: #Exception:
- #no match for that user cardNum, send
- #server.serve_forever()back fail message
- print "Major Failure \n"
- #print Exception
- #close the mysql connection
- #close(conn, cursor)
- return row
- def createDoorLogFail( fobNumber, doorNumber, cmd ):
- print "createDoorLogFail connecting to db"
- #conn, cursor = connect()
- self.cursor.execute("insert into DoorLog values ("+fobNumber+
- ",'"+fobNumber+" Not in database: "+cmd+
- "','"+doorNumber+"',now(),NULL)")
- #close(conn, cursor)
- def createDoorLogAdmit( fobNumber, firstName, lastName, doorNumber):
- print "createDoorLogAdmit connecting to db"
- #conn, cursor = connect()
- self.cursor.execute("insert into DoorLog values ("+fobNumber+
- ",'"+firstName+" "+lastName+" unlocked door "+doorNumber+
- "','"+doorNumber+"',now(),NULL)")
- #close(conn, cursor)
- def getSeconds( lastUnlock ):
- print "getSeconds connecting to db"
- #conn, cursor = connect()
- self.cursor.execute("select timestampdiff(second,'"+lastUnlock+"',now()) as seconds")
- seconds = self.cursor.fetchone()['seconds']
- if (seconds is None):
- seconds = 2000
- #close(conn, cursor)
- return seconds
- def updateMember( fobNumber, fobField, lastUnlockField ):
- print "updateMember connecting to db"
- #conn, cursor = connect()
- self.cursor.execute("update members set "+lastUnlockField+" = now() where "+fobField+" = '"+fobNumber+"'")
- #close(conn, cursor)
Advertisement
Add Comment
Please, Sign In to add comment