SHOW:
|
|
- or go back to the newest paste.
| 1 | #!/usr/bin/env python | |
| 2 | ||
| 3 | import MySQLdb | |
| 4 | import MySQLdb.cursors | |
| 5 | ||
| 6 | - | # Information about the database |
| 6 | + | class MCDataSource(object): |
| 7 | # Information about the database | |
| 8 | - | database = "ENTS" |
| 8 | + | |
| 9 | - | table = "members" |
| 9 | + | database = "ENTS" |
| 10 | - | hostaddr = "localhost" |
| 10 | + | table = "members" |
| 11 | - | username = "mastercontrol" |
| 11 | + | hostaddr = "localhost" |
| 12 | - | # TODO: This is insecure as it can be read by anyone with access to this script, needs to be handled in another way |
| 12 | + | username = "mastercontrol" |
| 13 | - | password = "pass" |
| 13 | + | # TODO: This is insecure as it can be read by anyone with access to this script, needs to be handled in another way |
| 14 | password = "pass" | |
| 15 | - | def connect(): |
| 15 | + | conn = None |
| 16 | - | conn = MySQLdb.connect ( host = hostaddr, user = username, passwd = password, db = database, cursorclass=MySQLdb.cursors.DictCursor ) |
| 16 | + | cursor = None |
| 17 | - | cursor = conn.cursor() |
| 17 | + | |
| 18 | - | return conn, cursor |
| 18 | + | """Use to create a singleton""" |
| 19 | def __new__(cls, *args, **kwds): | |
| 20 | - | def close(conn, cursor): |
| 20 | + | """ |
| 21 | - | cursor.close() |
| 21 | + | >>> s = Singleton() |
| 22 | - | conn.close() |
| 22 | + | >>> p = Singleton() |
| 23 | >>> id(s) == id(p) | |
| 24 | - | # Go to the database and get the last unlock event for the given door number |
| 24 | + | True |
| 25 | - | # We match FobNumber on the members table with CardNum on the DoorLog table and search based on door number |
| 25 | + | """ |
| 26 | - | def getLastUnlock( doorNum ): |
| 26 | + | self = "__self__" |
| 27 | - | print "getLastUnlock connecting to db" |
| 27 | + | if not hasattr(cls, self): |
| 28 | - | conn, cursor = connect() |
| 28 | + | instance = object.__new__(cls) |
| 29 | - | #conn = MySQLdb.connect ( host = hostaddr, user = username, passwd = password, db = database, cursorclass=MySQLdb.cursors.DictCursor ) |
| 29 | + | instance.init(*args, **kwds) |
| 30 | - | #cursor = conn.cursor() #connect to users db |
| 30 | + | setattr(cls, self, instance) |
| 31 | - | print "connection cursor created" |
| 31 | + | return getattr(cls, self) |
| 32 | - | # We use an inner join because we want to find at least one match on both tables |
| 32 | + | |
| 33 | - | cursor.execute ("SELECT * from DoorLog inner join members ON members.FobNumber=DoorLog.CardNum where DoorNum = '"+str(doorNum)+"' order by EventDateTime DESC")
|
| 33 | + | def init(self, *args, **kwds): |
| 34 | - | row = cursor.fetchone() #get the record for the current cardnum |
| 34 | + | connect() |
| 35 | - | print "getLastUnlock returning row:" |
| 35 | + | |
| 36 | - | print row |
| 36 | + | def connect(): |
| 37 | - | close(conn, cursor) |
| 37 | + | self.conn = MySQLdb.connect ( host = hostaddr, user = username, passwd = password, db = database, cursorclass=MySQLdb.cursors.DictCursor ) |
| 38 | - | return row |
| 38 | + | self.cursor = conn.cursor() |
| 39 | #return conn, cursor | |
| 40 | ||
| 41 | - | def getMember( fobNumber, fobField ): |
| 41 | + | def close(conn, cursor): |
| 42 | - | #conn = MySQLdb.connect ( host = hostaddr, user = username, passwd = password, db = database, cursorclass=MySQLdb.cursors.DictCursor ) |
| 42 | + | self.cursor.close() |
| 43 | - | #cursor = conn.cursor() #connect to users db |
| 43 | + | self.conn.close() |
| 44 | - | print "getMember connecting to db" |
| 44 | + | |
| 45 | - | conn, cursor = connect() |
| 45 | + | # Go to the database and get the last unlock event for the given door number |
| 46 | - | cursor.execute ("SELECT * from members where "+fobField+" = '" + fobNumber+"'")
|
| 46 | + | # We match FobNumber on the members table with CardNum on the DoorLog table and search based on door number |
| 47 | def getLastUnlock( doorNum ): | |
| 48 | - | try: |
| 48 | + | print "getLastUnlock connecting to db" |
| 49 | - | row = cursor.fetchone() #get the record for the current cardnum |
| 49 | + | #conn, cursor = connect() |
| 50 | - | except ValueError: #Exception: |
| 50 | + | #conn = MySQLdb.connect ( host = hostaddr, user = username, passwd = password, db = database, cursorclass=MySQLdb.cursors.DictCursor ) |
| 51 | - | #no match for that user cardNum, send |
| 51 | + | #cursor = conn.cursor() #connect to users db |
| 52 | - | #server.serve_forever()back fail message |
| 52 | + | print "connection cursor created" |
| 53 | - | print "Major Failure \n" |
| 53 | + | # We use an inner join because we want to find at least one match on both tables |
| 54 | - | #print Exception |
| 54 | + | self.cursor.execute ("SELECT * from DoorLog inner join members ON members.FobNumber=DoorLog.CardNum where DoorNum = '"+str(doorNum)+"' order by EventDateTime DESC")
|
| 55 | row = self.cursor.fetchone() #get the record for the current cardnum | |
| 56 | - | #close the mysql connection |
| 56 | + | print "getLastUnlock returning row:" |
| 57 | - | close(conn, cursor) |
| 57 | + | print row |
| 58 | - | return row |
| 58 | + | #close(conn, cursor) |
| 59 | return row | |
| 60 | - | def createDoorLogFail( fobNumber, doorNumber, cmd ): |
| 60 | + | |
| 61 | - | print "createDoorLogFail connecting to db" |
| 61 | + | |
| 62 | - | conn, cursor = connect() |
| 62 | + | def getMember( fobNumber, fobField ): |
| 63 | - | cursor.execute("insert into DoorLog values ("+fobNumber+
|
| 63 | + | #conn = MySQLdb.connect ( host = hostaddr, user = username, passwd = password, db = database, cursorclass=MySQLdb.cursors.DictCursor ) |
| 64 | - | ",'"+fobNumber+" Not in database: "+cmd+ |
| 64 | + | #cursor = conn.cursor() #connect to users db |
| 65 | - | "','"+doorNumber+"',now(),NULL)") |
| 65 | + | print "getMember connecting to db" |
| 66 | - | close(conn, cursor) |
| 66 | + | #conn, cursor = connect() |
| 67 | self.cursor.execute ("SELECT * from members where "+fobField+" = '" + fobNumber+"'")
| |
| 68 | - | def createDoorLogAdmit( fobNumber, firstName, lastName, doorNumber): |
| 68 | + | |
| 69 | - | print "createDoorLogAdmit connecting to db" |
| 69 | + | try: |
| 70 | - | conn, cursor = connect() |
| 70 | + | row = self.cursor.fetchone() #get the record for the current cardnum |
| 71 | - | cursor.execute("insert into DoorLog values ("+fobNumber+
|
| 71 | + | except ValueError: #Exception: |
| 72 | - | ",'"+firstName+" "+lastName+" unlocked door "+doorNumber+ |
| 72 | + | #no match for that user cardNum, send |
| 73 | - | "','"+doorNumber+"',now(),NULL)") |
| 73 | + | #server.serve_forever()back fail message |
| 74 | - | close(conn, cursor) |
| 74 | + | print "Major Failure \n" |
| 75 | #print Exception | |
| 76 | - | def getSeconds( lastUnlock ): |
| 76 | + | |
| 77 | - | print "getSeconds connecting to db" |
| 77 | + | #close the mysql connection |
| 78 | - | conn, cursor = connect() |
| 78 | + | #close(conn, cursor) |
| 79 | - | cursor.execute("select timestampdiff(second,'"+lastUnlock+"',now()) as seconds")
|
| 79 | + | return row |
| 80 | - | seconds = cursor.fetchone()['seconds'] |
| 80 | + | |
| 81 | - | if (seconds is None): |
| 81 | + | def createDoorLogFail( fobNumber, doorNumber, cmd ): |
| 82 | - | seconds = 2000 |
| 82 | + | print "createDoorLogFail connecting to db" |
| 83 | - | close(conn, cursor) |
| 83 | + | #conn, cursor = connect() |
| 84 | - | return seconds |
| 84 | + | self.cursor.execute("insert into DoorLog values ("+fobNumber+
|
| 85 | ",'"+fobNumber+" Not in database: "+cmd+ | |
| 86 | - | def updateMember( fobNumber, fobField, lastUnlockField ): |
| 86 | + | "','"+doorNumber+"',now(),NULL)") |
| 87 | - | print "updateMember connecting to db" |
| 87 | + | #close(conn, cursor) |
| 88 | - | conn, cursor = connect() |
| 88 | + | |
| 89 | - | cursor.execute("update members set "+lastUnlockField+" = now() where "+fobField+" = '"+fobNumber+"'")
|
| 89 | + | def createDoorLogAdmit( fobNumber, firstName, lastName, doorNumber): |
| 90 | - | close(conn, cursor) |
| 90 | + | print "createDoorLogAdmit connecting to db" |
| 91 | #conn, cursor = connect() | |
| 92 | self.cursor.execute("insert into DoorLog values ("+fobNumber+
| |
| 93 | ",'"+firstName+" "+lastName+" unlocked door "+doorNumber+ | |
| 94 | "','"+doorNumber+"',now(),NULL)") | |
| 95 | #close(conn, cursor) | |
| 96 | ||
| 97 | def getSeconds( lastUnlock ): | |
| 98 | print "getSeconds connecting to db" | |
| 99 | #conn, cursor = connect() | |
| 100 | self.cursor.execute("select timestampdiff(second,'"+lastUnlock+"',now()) as seconds")
| |
| 101 | seconds = self.cursor.fetchone()['seconds'] | |
| 102 | if (seconds is None): | |
| 103 | seconds = 2000 | |
| 104 | #close(conn, cursor) | |
| 105 | return seconds | |
| 106 | ||
| 107 | def updateMember( fobNumber, fobField, lastUnlockField ): | |
| 108 | print "updateMember connecting to db" | |
| 109 | #conn, cursor = connect() | |
| 110 | self.cursor.execute("update members set "+lastUnlockField+" = now() where "+fobField+" = '"+fobNumber+"'")
| |
| 111 | #close(conn, cursor) |