def getInsertedValue(): sql = '''SELECT * FROM message_log AS log ORDER BY log.id desc LIMIT 1 ''' DataBaseUtilities.sendDataToFrontendFromDb(sqlStatement=sql)
def __handleIncomingMessage(ardCon, incomingByteArray): encryptionHandler = EncryptionHandler() decryptedMessage = encryptionHandler.decryptByteArray(incomingByteArray) if decryptedMessage != None: print("Decrpyted Message:", decryptedMessage) print("partnerId", ardCon.partnerId) DataBaseUtilities.insertMessageAndSender("mockup", decryptedMessage)
def createDataBase(): sql = '''CREATE TABLE IF NOT EXISTS message_log( id INTEGER PRIMARY KEY AUTOINCREMENT , sender TEXT NOT NULL, time TEXT NOT NULL, date TEXT NOT NULL, message TEXT NOT NULL ) ''' DataBaseUtilities.insertIntoDb(sqlInsertStatement=sql)
def loadMoreEntrys(startID): sql = ''' SELECT * FROM ( SELECT * FROM message_log AS log WHERE log.id < ? ORDER BY id DESC LIMIT 20) ORDER BY id ASC ''' DataBaseUtilities.sendDataToFrontendFromDb(sql, startID)
def getInitialLoad(): sql = ''' SELECT * FROM ( SELECT * FROM message_log AS log ORDER BY log.id DESC LIMIT 20 ) ORDER BY id ASC ''' DataBaseUtilities.sendDataToFrontendFromDb(sqlStatement=sql)
def main(): data = UniversalUtilities.read_in_stdin_json() if data[DictIndex.LOAD.value] == Action.INITIAL.value: createDataBase() createSettingsTable() elif data[DictIndex.LOAD.value] == Action.INSERT.value: message, sender = data[DictIndex.MESSAGE.value], data[DictIndex.SENDER.value] DataBaseUtilities.insertMessageAndSender(sender=sender, message=message) try: UniversalUtilities.connectAndSendTo_6200(message=message) except ConnectionError as e: UniversalUtilities.sendErrorMessageToFrontend(e)
def _getKeyHandlerFromDb(self): '''Fetches the DiffieHellman JSON from the settings table and returns the decoded class''' statement = ''' SELECT value FROM settings WHERE key = 'keyHandlerJson' ''' results = DataBaseUtilities.getValuesFromDb(statement) if len(results) == 0: return self._createAndSaveKeyHandler() return jsonpickle.decode(results[0]["value"])
def databaseState(lastMessageId, partnerID): sql = '''SELECT log.id FROM message_log AS log ORDER BY log.id desc LIMIT 1 ''' newMessageId = DataBaseUtilities.getOneValueFromDb( sql) # newest message-id in the database # calculate how many messages arrived since the last refresh of the frontend counterNewMessages = newMessageId['id'] - lastMessageId if counterNewMessages > 0: # returns the messages, which arrived since the last refresh of the frontend sql = ''' SELECT * FROM ( SELECT * FROM message_log AS log ORDER BY log.id desc LIMIT ? ) ORDER BY id ASC ''' newData = DataBaseUtilities.getValuesFromDb(sql, counterNewMessages) return True, newData else: return False, None
def createSettingsTable(): sql = '''CREATE TABLE IF NOT EXISTS settings( key TEXT PRIMARY KEY, value TEXT NOT NULL ) ''' DataBaseUtilities.insertIntoDb(sqlInsertStatement=sql)
def _insertKeyHandlerIntoDb(self, keyHandler): '''Encodes a DiffieHellman class as JSON and saves it as a string into settings table''' keyHandlerJson = jsonpickle.encode(keyHandler) statement = '''INSERT OR REPLACE INTO settings (key, value) VALUES('keyHandlerJson', ?)''' DataBaseUtilities.insertIntoDb(statement, keyHandlerJson)
def sendDefaults(): sql = '''SELECT * FROM settings''' DataBaseUtilities.sendDataToFrontendFromDb(sqlStatement=sql)
def insertThemeOnly(theme): sqlInsert = '''INSERT OR REPLACE INTO settings (key, value) VALUES('theme', ?)''' DataBaseUtilities.insertIntoDb(sqlInsert, str(theme))
def insertDefaults(uuid, theme): sqlInsert = '''INSERT OR REPLACE INTO settings (key, value) VALUES('uuid', ?), ('theme', ?)''' DataBaseUtilities.insertIntoDb(sqlInsert, str(uuid), str(theme))
def getThemeFromSettings(): sql = '''SELECT value FROM settings WHERE key='theme' ''' return DataBaseUtilities.getOneValueFromDb(sqlStatement=sql)