Ejemplo n.º 1
0
    def endSession(self):
        # Log the session ending.
        if self.currentSession is not None:
            DatabaseManager.sessionEnded(self.currentSession)

            # End the session.
            self.currentSession = None
            self.notify(None)
    def test_initializedDatabaseWithSessions(self):
        # Initialize and close the initial database.
        self.initialDatabase.execute(
            "CREATE TABLE Users (Id char(9),AccessType STRING);")
        self.initialDatabase.execute(
            "CREATE TABLE Sessions (Id char(9),StartTime BIGINT,EndTime BIGINT);"
        )
        self.initialDatabase.execute(
            "INSERT INTO Sessions VALUES (\"000000001\",100,105);")
        self.initialDatabase.execute(
            "INSERT INTO Sessions VALUES (\"000000002\",106,109);")
        self.initialDatabase.execute(
            "INSERT INTO Sessions VALUES (\"000000002\",109,0);")
        self.initialDatabase.commit()
        self.initialDatabase.close()

        # Create the database and assert sessions are correct.
        CuT = DatabaseManager.DatabaseManager(self.databaseFile)
        sessions = CuT.database.execute("SELECT * FROM Sessions;").fetchall()
        self.assertEqual(sessions[0], ("000000001", 100, 105),
                         "Session is incorrect.")
        self.assertEqual(sessions[1], ("000000002", 106, 109),
                         "Session is incorrect.")
        self.assertEqual(sessions[2], ("000000002", 109, -1),
                         "Session is incorrect.")
    def test_sessionEnded(self):
        # Mock the Time module.
        currentTime = 0

        class MockTimeModule:
            def getCurrentTimestamp(self):
                return currentTime

        DatabaseManager.Time = MockTimeModule()

        # Initialize and close the initial database.
        self.initialDatabase.execute(
            "CREATE TABLE Users (Id char(9),AccessType STRING);")
        self.initialDatabase.execute(
            "CREATE TABLE Sessions (Id char(9),StartTime BIGINT,EndTime BIGINT);"
        )
        self.initialDatabase.commit()
        self.initialDatabase.close()

        # Create the database, start and end sessions, and assert the sessions are correct.
        CuT = DatabaseManager.DatabaseManager(self.databaseFile)
        CuT.sessionStarted(Session.Session(User.User("000000001", 10), 5))
        currentTime = 7
        CuT.sessionEnded(Session.Session(User.User("000000001", 10), 5))
        CuT.sessionStarted(Session.Session(User.User("000000002", 10), 8))
        currentTime = 9
        CuT.sessionEnded(Session.Session(User.User("000000002", 10), 8))
        CuT.sessionStarted(Session.Session(User.User("000000001", 10), 15))
        sessions = CuT.database.execute("SELECT * FROM Sessions;").fetchall()
        self.assertEqual(sessions[0], ("000000001", 5, 7),
                         "Session is incorrect.")
        self.assertEqual(sessions[1], ("000000002", 8, 9),
                         "Session is incorrect.")
        self.assertEqual(sessions[2], ("000000001", 15, 0),
                         "Session is incorrect.")
Ejemplo n.º 4
0
    def startSession(self, user):
        # Log the session being ended if the id is changing.
        if self.currentSession is not None and self.currentSession.getUser(
        ).getId() != user.getId():
            DatabaseManager.sessionEnded(self.currentSession)

        # Set the session.
        newSession = Session.startSession(user)
        self.currentSession = newSession
        self.notify(newSession)
        DatabaseManager.sessionStarted(newSession)

        # Start a thread to expire the session.
        sessionThread = SessionThread(self)
        sessionThread.daemon = True
        sessionThread.start()
    def test_uninitializedDatabase(self):
        # Close the initial database.
        self.initialDatabase.close()

        # Create the database and assert the tables exist by running queries without errors.
        CuT = DatabaseManager.DatabaseManager(self.databaseFile)
        CuT.database.execute("SELECT * FROM Users;")
        CuT.database.execute("SELECT * FROM Sessions;")
    def test_sessionStarted(self):
        # Initialize and close the initial database.
        self.initialDatabase.execute(
            "CREATE TABLE Users (Id char(9),AccessType STRING);")
        self.initialDatabase.execute(
            "CREATE TABLE Sessions (Id char(9),StartTime BIGINT,EndTime BIGINT);"
        )
        self.initialDatabase.commit()
        self.initialDatabase.close()

        # Set the static database, start sessions, and assert the sessions are correct.
        DatabaseManager.staticDatabaseManager = DatabaseManager.DatabaseManager(
            self.databaseFile)
        DatabaseManager.sessionStarted(
            Session.Session(User.User("000000001", 10), 5))
        DatabaseManager.sessionStarted(
            Session.Session(User.User("000000002", 10), 8))
        DatabaseManager.sessionStarted(
            Session.Session(User.User("000000001", 10), 15))
        sessions = DatabaseManager.staticDatabaseManager.database.execute(
            "SELECT * FROM Sessions;").fetchall()
        self.assertEqual(sessions[0], ("000000001", 5, 0),
                         "Session is incorrect.")
        self.assertEqual(sessions[1], ("000000002", 8, 0),
                         "Session is incorrect.")
        self.assertEqual(sessions[2], ("000000001", 15, 0),
                         "Session is incorrect.")
Ejemplo n.º 7
0
    def idSwiped(self,user):
        # Swap the current user.
        lastUser = self.currentUser
        self.currentUser = user.getId()

        # Set the access type if the swiped user hasn't changed. The first swipe is used to show the current status.
        accessType = user.getAccessType()
        if self.currentUser == lastUser:
            if accessType == "UNAUTHORIZED":
                accessType = "AUTHORIZED"
            elif accessType == "AUTHORIZED":
                accessType = "ADMIN"
            else:
                accessType = "UNAUTHORIZED"
            DatabaseManager.setUserAccessType(self.currentUser,accessType)

        # Invoke the observers.
        self.notify(self.currentUser,accessType)
    def test_getUser(self):
        # Initialize and close the initial database.
        self.initialDatabase.execute(
            "CREATE TABLE Users (Id char(9),AccessType STRING);")
        self.initialDatabase.execute(
            "CREATE TABLE Sessions (Id char(9),StartTime BIGINT,EndTime BIGINT);"
        )
        self.initialDatabase.execute(
            "INSERT INTO Users VALUES (\"000000001\",\"AUTHORIZED\");")
        self.initialDatabase.execute(
            "INSERT INTO Users VALUES (\"000000002\",\"ADMIN\");")
        self.initialDatabase.commit()
        self.initialDatabase.close()

        # Set the static database and assert the users are correct.
        DatabaseManager.staticDatabaseManager = DatabaseManager.DatabaseManager(
            self.databaseFile)
        self.assertEqual(
            DatabaseManager.getUser("000000001").getId(), "000000001",
            "Id is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000001").getAccessType(), "AUTHORIZED",
            "Access type is incorrect.")
        self.assertNotEqual(
            DatabaseManager.getUser("000000001").getSessionTime(), 0,
            "Session time is zero.")
        self.assertEqual(
            DatabaseManager.getUser("000000002").getId(), "000000002",
            "Id is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000002").getAccessType(), "ADMIN",
            "Access type is incorrect.")
        self.assertNotEqual(
            DatabaseManager.getUser("000000002").getSessionTime(), 0,
            "Session time is zero.")
        self.assertEqual(
            DatabaseManager.getUser("000000003").getId(), "000000003",
            "Id is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000003").getAccessType(),
            "UNAUTHORIZED", "Access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000003").getSessionTime(), 0,
            "Session time is non-zero.")
 def setUp(self):
     DatabaseManager.staticDatabaseManager = DatabaseManager.DatabaseManager(
         ":memory:")
     DatabaseManager.setUserAccessType("000000000", "AUTHORIZED")
     DatabaseManager.setUserAccessType("000000001", "UNAUTHORIZED")
     DatabaseManager.setUserAccessType("000000002", "ADMIN")
     self.testUser = User.User("000000000", 1, "AUTHORIZED")
     self.testUnauthorizedUser = User.User("000000001", 0, "UNAUTHORIZED")
     self.testAdminUser = User.User("000000002", 1, "ADMIN")
    def test_setUserAccessType(self):
        # Initialize and close the initial database.
        self.initialDatabase.execute(
            "CREATE TABLE Users (Id char(9),AccessType STRING);")
        self.initialDatabase.execute(
            "CREATE TABLE Sessions (Id char(9),StartTime BIGINT,EndTime BIGINT);"
        )
        self.initialDatabase.commit()
        self.initialDatabase.close()

        # Set a new user type and assert the entries are correct.
        DatabaseManager.staticDatabaseManager = DatabaseManager.DatabaseManager(
            self.databaseFile)
        DatabaseManager.setUserAccessType("000000001", "AUTHORIZED")
        users = DatabaseManager.staticDatabaseManager.database.execute(
            "SELECT * FROM Users;").fetchall()
        self.assertEqual(users[0], ("000000001", "AUTHORIZED"),
                         "User is incorrect.")

        # Set a new user type and assert the entries are correct.
        DatabaseManager.setUserAccessType("000000002", "ADMIN")
        users = DatabaseManager.staticDatabaseManager.database.execute(
            "SELECT * FROM Users;").fetchall()
        self.assertEqual(users[0], ("000000001", "AUTHORIZED"),
                         "User is incorrect.")
        self.assertEqual(users[1], ("000000002", "ADMIN"),
                         "User is incorrect.")

        # Set a new user type and assert the entries are correct.
        DatabaseManager.setUserAccessType("000000001", "ADMIN")
        users = DatabaseManager.staticDatabaseManager.database.execute(
            "SELECT * FROM Users;").fetchall()
        self.assertEqual(users[0], ("000000001", "ADMIN"),
                         "User is incorrect.")
        self.assertEqual(users[1], ("000000002", "ADMIN"),
                         "User is incorrect.")

        # Set a new user type and assert the entries are correct.
        DatabaseManager.setUserAccessType("000000001", "UNAUTHORIZED")
        users = DatabaseManager.staticDatabaseManager.database.execute(
            "SELECT * FROM Users;").fetchall()
        self.assertEqual(users[0], ("000000002", "ADMIN"),
                         "User is incorrect.")
    def test_initializedDatabase(self):
        # Initialize and close the initial database.
        self.initialDatabase.execute(
            "CREATE TABLE Users (Id char(9),AccessType STRING);")
        self.initialDatabase.execute(
            "CREATE TABLE Sessions (Id char(9),StartTime BIGINT,EndTime BIGINT);"
        )
        self.initialDatabase.commit()
        self.initialDatabase.close()

        # Create the database and assert the tables exist by running queries without errors.
        CuT = DatabaseManager.DatabaseManager(self.databaseFile)
        CuT.database.execute("SELECT * FROM Users;")
        CuT.database.execute("SELECT * FROM Sessions;")
    def test_getUserAccessType(self):
        # Initialize and close the initial database.
        self.initialDatabase.execute(
            "CREATE TABLE Users (Id char(9),AccessType STRING);")
        self.initialDatabase.execute(
            "CREATE TABLE Sessions (Id char(9),StartTime BIGINT,EndTime BIGINT);"
        )
        self.initialDatabase.execute(
            "INSERT INTO Users VALUES (\"000000001\",\"AUTHORIZED\");")
        self.initialDatabase.execute(
            "INSERT INTO Users VALUES (\"000000002\",\"ADMIN\");")
        self.initialDatabase.commit()
        self.initialDatabase.close()

        # Create the database and assert the user types are correct.
        CuT = DatabaseManager.DatabaseManager(self.databaseFile)
        self.assertEqual(CuT.getUserAccessType("000000001"), "AUTHORIZED",
                         "Type is incorrect.")
        self.assertEqual(CuT.getUserAccessType("000000002"), "ADMIN",
                         "Type is incorrect.")
        self.assertEqual(CuT.getUserAccessType("000000003"), "UNAUTHORIZED",
                         "Type is incorrect.")
Ejemplo n.º 13
0
                        print("Value must be a number.")
                else:
                    if newValue not in existingConfiguration.keys(
                    ) or newValue != existingConfiguration[displayName]:
                        changesMade = True
                    existingConfiguration[displayName] = newValue
                    break

        # Save the configuration.
        if changesMade:
            with open(configurationLocation, "w", newline="\n") as file:
                file.write(json.dumps(existingConfiguration, indent=4))

    # Prompt to set up the admin ids.
    database = DatabaseManager.DatabaseManager(
        os.path.join(raspbianDrive, "home", "pi", "MachineSwipeSystem",
                     "database.sqlite"))
    changeAdminIds = True
    if len(
            database.database.execute(
                "SELECT * FROM Users WHERE AccessType = \"ADMIN\";").fetchall(
                )) > 0:
        changeAdminIdsResult = input("Update the admin ids?\n").lower().strip()
        changeAdminIds = (changeAdminIdsResult == "y"
                          or changeAdminIdsResult == "yes"
                          or changeAdminIdsResult == "t"
                          or changeAdminIdsResult == "true")

    # Change the admin ids.
    if changeAdminIds:
        # Change the existing ids.
	def idSwiped(self,id):
		StateManager.idSwiped(DatabaseManager.getUser(id))
    def test_toggleAccessTypeIdSwiped(self):
        CuT = StateManager.StateManager()

        # Set the initial state.
        state = CuT.states["ToggleAccessType"]
        CuT.setStateByName("ToggleAccessType")
        self.assertEqual(state.currentUser, None, "Initial user is incorrect.")

        # Swipe an id and assert the user and access type are correct.
        CuT.idSwiped(self.testUser)
        self.assertEqual(CuT.getState().getName(), "ToggleAccessType",
                         "State is incorrect.")
        self.assertEqual(state.currentUser, self.testUser.getId(),
                         "User is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000000").getAccessType(), "AUTHORIZED",
            "User access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000001").getAccessType(),
            "UNAUTHORIZED", "User access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000002").getAccessType(), "ADMIN",
            "User access type is incorrect.")
        self.testUser = DatabaseManager.getUser(self.testUser.getId())

        # Swipe an id and assert the user and access type are correct.
        CuT.idSwiped(self.testUser)
        self.assertEqual(CuT.getState().getName(), "ToggleAccessType",
                         "State is incorrect.")
        self.assertEqual(state.currentUser, self.testUser.getId(),
                         "User is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000000").getAccessType(), "ADMIN",
            "User access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000001").getAccessType(),
            "UNAUTHORIZED", "User access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000002").getAccessType(), "ADMIN",
            "User access type is incorrect.")
        self.testUser = DatabaseManager.getUser(self.testUser.getId())

        # Swipe an id and assert the user and access type are correct.
        CuT.idSwiped(self.testUser)
        self.assertEqual(CuT.getState().getName(), "ToggleAccessType",
                         "State is incorrect.")
        self.assertEqual(state.currentUser, self.testUser.getId(),
                         "User is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000000").getAccessType(),
            "UNAUTHORIZED", "User access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000001").getAccessType(),
            "UNAUTHORIZED", "User access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000002").getAccessType(), "ADMIN",
            "User access type is incorrect.")
        self.testUser = DatabaseManager.getUser(self.testUser.getId())

        # Swipe an id and assert the user and access type are correct.
        CuT.idSwiped(self.testUser)
        self.assertEqual(CuT.getState().getName(), "ToggleAccessType",
                         "State is incorrect.")
        self.assertEqual(state.currentUser, self.testUser.getId(),
                         "User is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000000").getAccessType(), "AUTHORIZED",
            "User access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000001").getAccessType(),
            "UNAUTHORIZED", "User access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000002").getAccessType(), "ADMIN",
            "User access type is incorrect.")
        self.testUser = DatabaseManager.getUser(self.testUser.getId())

        # Swipe an id and assert the user and access type are correct.
        CuT.idSwiped(self.testUnauthorizedUser)
        self.assertEqual(CuT.getState().getName(), "ToggleAccessType",
                         "State is incorrect.")
        self.assertEqual(state.currentUser, self.testUnauthorizedUser.getId(),
                         "User is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000000").getAccessType(), "AUTHORIZED",
            "User access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000001").getAccessType(),
            "UNAUTHORIZED", "User access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000002").getAccessType(), "ADMIN",
            "User access type is incorrect.")

        # Swipe an id and assert the user and access type are correct.
        CuT.idSwiped(self.testAdminUser)
        self.assertEqual(CuT.getState().getName(), "ToggleAccessType",
                         "State is incorrect.")
        self.assertEqual(state.currentUser, self.testAdminUser.getId(),
                         "User is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000000").getAccessType(), "AUTHORIZED",
            "User access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000001").getAccessType(),
            "UNAUTHORIZED", "User access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000002").getAccessType(), "ADMIN",
            "User access type is incorrect.")

        # Swipe an id and assert the user and access type are correct.
        CuT.idSwiped(self.testAdminUser)
        self.assertEqual(CuT.getState().getName(), "ToggleAccessType",
                         "State is incorrect.")
        self.assertEqual(state.currentUser, self.testAdminUser.getId(),
                         "User is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000000").getAccessType(), "AUTHORIZED",
            "User access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000001").getAccessType(),
            "UNAUTHORIZED", "User access type is incorrect.")
        self.assertEqual(
            DatabaseManager.getUser("000000002").getAccessType(),
            "UNAUTHORIZED", "User access type is incorrect.")
Ejemplo n.º 16
0
		newLEDColor = self.mockLEDs.getColor()
		if newLEDColor != self.lastLEDColor:
			self.lastLEDColor = newLEDColor
			self.window.drawRectangle(-310,-40,300,80,"#FFFFFF",newLEDColor)

		# Update the buzzer.
		newBuzzerState = (Time.getCurrentTimestamp() - self.mockBuzzer.lastTime) < 0.1
		if newBuzzerState != self.buzzerOn:
			self.buzzerOn = newBuzzerState
			if self.buzzerOn:
				self.window.drawRectangle(330,-130,60,60,"#FFFFFF","#00FF00")
			else:
				self.window.drawRectangle(330,-130,60,60,"#FFFFFF","#FF0000")

		# Update the display.
		self.window.update()

	"""
	Continuously updates the display.
	"""
	def startUpdateLoop(self):
		while True:
			self.updateDisplay()
			time.sleep(1/60)



if __name__ == '__main__':
	DatabaseManager.setUserAccessType("000000001","ADMIN")
	DatabaseManager.setUserAccessType("000000002","AUTHORIZED")
	Emulator()