# Path: thingsboard_gateway/storage/sqlite/database_connector.py
# class DatabaseConnector:
#     def __init__(self, settings: StorageSettings):
#         self.data_file_path = settings.data_folder_path
#         self.connection: Optional[Connection] = None
#         self.lock = RLock()
# 
#     def connect(self):
#         """
#         Create database file in path from settings
#         """
#         try:
#             self.connection = connect(self.data_file_path, check_same_thread=False)
#         except Exception as e:
#             log.exception(e)
# 
#     def commit(self):
#         """
#         Commit changes
#         """
#         log.debug("Committing changes to DB")
#         try:
#             with self.lock:
#                 self.connection.commit()
# 
#         except Exception as e:
#             log.exception(e)
# 
#     def execute(self, *args):
#         """
#         Execute changes
#         """
#         # log.debug("Execute %s to DB", str(args))
#         try:
#             with self.lock:
#                 return self.connection.execute(*args)
#         except sqlite3.ProgrammingError:
#             pass
#         except Exception as e:
#             log.exception(e)
# 
#     def rollback(self):
#         """
#         Rollback changes after exception
#         """
#         log.debug("Rollback transaction")
#         try:
#             with self.lock:
#                 self.connection.rollback()
# 
#         except Exception as e:
#             log.exception(e)
# 
#     def close(self):
#         """
#         Closes database file
#         """
#         try:
#             with self.lock:
#                 self.connection.close()
# 
#         except Exception as e:
#             log.exception(e)
# 
#     def get_cursor(self):
#         try:
#             return self.connection.cursor()
# 
#         except Exception as e:
#             log.exception(e)
# 
# Path: thingsboard_gateway/storage/sqlite/database_action_type.py
# class DatabaseActionType(Enum):
#     WRITE_DATA_STORAGE = auto()  # Writes do not require a response on the request
# 
# Path: thingsboard_gateway/storage/sqlite/storage_settings.py
# class StorageSettings:
#     def __init__(self, config):
#         self.data_folder_path = config.get("data_file_path", "./")
#         self.messages_ttl_check_in_hours = config.get('messages_ttl_check_in_hours', 1) * 3600
#         self.messages_ttl_in_days = config.get('messages_ttl_in_days', 7)


from os.path import exists
from time import time, sleep
from logging import getLogger
from threading import Thread
from queue import Queue
from thingsboard_gateway.storage.sqlite.database_connector import DatabaseConnector
from thingsboard_gateway.storage.sqlite.database_action_type import DatabaseActionType
from thingsboard_gateway.storage.sqlite.storage_settings import StorageSettings
import datetime
        self.msg_counter = 0
        self.start()

    def init_table(self):
        try:
            self.db.execute('''CREATE TABLE IF NOT EXISTS messages (timestamp INTEGER, message TEXT); ''')
            self.db.commit()
        except Exception as e:
            self.db.rollback()
            log.exception(e)

    def run(self):
        while True:
            self.process()

            sleep(.2)

    def process(self):
        try:
            if time() - self.__last_msg_check >= self.settings.messages_ttl_check_in_hours:
                self.__last_msg_check = time()
                self.delete_data_lte(self.settings.messages_ttl_in_days)

            # Signalization so that we can spam call process()
            if not self.__stopped and self.processQueue:
                while self.processQueue.qsize() > 0:

                    req = self.processQueue.get()

                    log.debug("Processing %s" % req.type)

# Path: thingsboard_gateway/storage/sqlite/database_connector.py
# class DatabaseConnector:
#     def __init__(self, settings: StorageSettings):
#         self.data_file_path = settings.data_folder_path
#         self.connection: Optional[Connection] = None
#         self.lock = RLock()
# 
#     def connect(self):
#         """
#         Create database file in path from settings
#         """
#         try:
#             self.connection = connect(self.data_file_path, check_same_thread=False)
#         except Exception as e:
#             log.exception(e)
# 
#     def commit(self):
#         """
#         Commit changes
#         """
#         log.debug("Committing changes to DB")
#         try:
#             with self.lock:
#                 self.connection.commit()
# 
#         except Exception as e:
#             log.exception(e)
# 
#     def execute(self, *args):
#         """
#         Execute changes
#         """
#         # log.debug("Execute %s to DB", str(args))
#         try:
#             with self.lock:
#                 return self.connection.execute(*args)
#         except sqlite3.ProgrammingError:
#             pass
#         except Exception as e:
#             log.exception(e)
# 
#     def rollback(self):
#         """
#         Rollback changes after exception
#         """
#         log.debug("Rollback transaction")
#         try:
#             with self.lock:
#                 self.connection.rollback()
# 
#         except Exception as e:
#             log.exception(e)
# 
#     def close(self):
#         """
#         Closes database file
#         """
#         try:
#             with self.lock:
#                 self.connection.close()
# 
#         except Exception as e:
#             log.exception(e)
# 
#     def get_cursor(self):
#         try:
#             return self.connection.cursor()
# 
#         except Exception as e:
#             log.exception(e)
# 
# Path: thingsboard_gateway/storage/sqlite/database_action_type.py
# class DatabaseActionType(Enum):
#     WRITE_DATA_STORAGE = auto()  # Writes do not require a response on the request
# 
# Path: thingsboard_gateway/storage/sqlite/storage_settings.py
# class StorageSettings:
#     def __init__(self, config):
#         self.data_folder_path = config.get("data_file_path", "./")
#         self.messages_ttl_check_in_hours = config.get('messages_ttl_check_in_hours', 1) * 3600
#         self.messages_ttl_in_days = config.get('messages_ttl_in_days', 7)


# Path: thingsboard_gateway/storage/sqlite/database.py
from os.path import exists
from time import time, sleep
from logging import getLogger
from threading import Thread
from queue import Queue
from thingsboard_gateway.storage.sqlite.database_connector import DatabaseConnector
from thingsboard_gateway.storage.sqlite.database_action_type import DatabaseActionType
from thingsboard_gateway.storage.sqlite.storage_settings import StorageSettings
import datetime

        self.msg_counter = 0
        self.start()

    def init_table(self):
        try:
            self.db.execute('''CREATE TABLE IF NOT EXISTS messages (timestamp INTEGER, message TEXT); ''')
            self.db.commit()
        except Exception as e:
            self.db.rollback()
            log.exception(e)

    def run(self):
        while True:
            self.process()

            sleep(.2)

    def process(self):
        try:
            if time() - self.__last_msg_check >= self.settings.messages_ttl_check_in_hours:
                self.__last_msg_check = time()
                self.delete_data_lte(self.settings.messages_ttl_in_days)

            # Signalization so that we can spam call process()
            if not self.__stopped and self.processQueue:
                while self.processQueue.qsize() > 0:

                    req = self.processQueue.get()

                    log.debug("Processing %s" % req.type)

                    if req.type is DatabaseActionType.WRITE_DATA_STORAGE: