Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import MySQLdb
- import MySQLdb.cursors
- # 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"
- def connect():
- conn = MySQLdb.connect ( host = hostaddr, user = username, passwd = password, db = database, cursorclass=MySQLdb.cursors.DictCursor )
- cursor = conn.cursor()
- return conn, cursor
- def close(conn, cursor):
- cursor.close()
- 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
- cursor.execute ("SELECT * from DoorLog inner join members ON members.FobNumber=DoorLog.CardNum where DoorNum = '"+str(doorNum)+"' order by EventDateTime DESC")
- row = 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()
- cursor.execute ("SELECT * from members where "+fobField+" = '" + fobNumber+"'")
- try:
- row = 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()
- 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()
- 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()
- cursor.execute("select timestampdiff(second,'"+lastUnlock+"',now()) as seconds")
- seconds = 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()
- cursor.execute("update members set "+lastUnlockField+" = now() where "+fobField+" = '"+fobNumber+"'")
- close(conn, cursor)
Advertisement
Add Comment
Please, Sign In to add comment