diff --git a/bot/main.py b/bot/main.py index ee39145..bc20127 100644 --- a/bot/main.py +++ b/bot/main.py @@ -62,5 +62,7 @@ class ChatBot(): def start(self): self.sub_modules = {"clock" : self.commands.clock.Clock(self.db, self.modules["clock"], self.api_art)} self.add_commands() - self.telegram.start_polling() + self.telegram.start_polling( + poll_interval=0.2, + ) # self.telegram.idle() diff --git a/launcher.py b/launcher.py index 30af97c..16f7749 100644 --- a/launcher.py +++ b/launcher.py @@ -12,7 +12,7 @@ class Launcher: def __init__(self, **modules): """""" self.persistence = p_io.PersistentDict("persistence/prst.json") - self.db = p_out.DBLogging + self.db = p_out.DBConnector() logger.info(self.__class__.__name__ + " initialized") @@ -31,7 +31,7 @@ class Launcher: logger.info("Starting module "+ module.__class__.__name__) module.modules = self.modules module.persistence = self.persistence - module.db = self.db() # newly initialized for every module + module.db = self.db # pooled ie multithreaded module.start() diff --git a/persistence/models.py b/persistence/models.py index 64c3dac..f37cc17 100644 --- a/persistence/models.py +++ b/persistence/models.py @@ -1,17 +1,28 @@ from peewee import * -import datetime +# from playhouse.pool import PooledMySQLDatabase +from playhouse.shortcuts import ReconnectMixin import logging logger = logging.getLogger(__name__) -from threading import Thread from . import keys dbk = keys.db_keys -db = MySQLDatabase(dbk["name"], user=dbk["username"], password=dbk["password"], host=dbk["url"], port=dbk["port"], autorollback=True) + +class ReconnectDataBase(ReconnectMixin, MySQLDatabase): + pass +db = ReconnectDataBase( + dbk["name"], + user=dbk["username"], + password=dbk["password"], + host=dbk["url"], + port=dbk["port"], + autorollback=True +) + class DBModel(Model): # specific to the above DB class Meta: @@ -28,6 +39,13 @@ class DBModel(Model): logger.error(e) # db.atomic().rollback() + def get(self, *query, **filters): + try: + return super().get(*query, **filters) + except: + logger.error("Error while 'getting' from db.") + print(query, filters) + class Metric(DBModel): time = DateTimeField() @@ -40,19 +58,24 @@ class SensorMetric(Metric): temperature = IntegerField() humidity = IntegerField() luminosity = IntegerField() + default = {"temperature": 100, "humidity": 100, "luminosity": 100} + class ChatMetric(Metric): read = BooleanField() send = BooleanField() execute = BooleanField() + default = {"read": False, "send": False, "execute": False} class ErrorMetric(Metric): # same as above error = TextField() + default = {"error": "SQL connection broke off"} class List(DBModel): name = CharField(unique=True) - content = TextField() # unlimited length, use to serialise list into \ No newline at end of file + content = TextField() # unlimited length, use to serialise list into + default = {"content": "SQL connection broke off"} \ No newline at end of file diff --git a/persistence/p_out.py b/persistence/p_out.py index 6bcf64f..6181b62 100644 --- a/persistence/p_out.py +++ b/persistence/p_out.py @@ -1,7 +1,7 @@ from . import models -class DBLogging: +class DBConnector: """Create a connection to a remote database and log some quantities that will be visualized otherwhere""" def __init__(self): self.db = models.db @@ -16,42 +16,3 @@ class DBLogging: def create_tables(self): self.db.create_tables([self.sensors, self.chats, self.errors, self.lists]) - - - - - - - - - - - - - - - - -# writin to the db gets handled through the model directly - -# create_tables() -# # read from json, excel, txt ... whatever -# now = dt.datetime.timestamp(dt.datetime.now()) - - -# for i in range(1000): -# with db: -# sensor_data = SensorMetric.create( -# time = now + i, -# temperature = 23, -# humidity = 30 + randint(0,20), -# luminosity = 1 -# ) -# chat = ChatMetric( -# time = now + i, -# activity = "Hello world" -# ) -# errors = ErrorMetric( -# time = now + i, -# error = "Could not load module" -# ) \ No newline at end of file