From cad3891fb93f3be28e7b892fbd576ad6a8422307 Mon Sep 17 00:00:00 2001 From: Vladislav Date: Sat, 28 Oct 2023 11:30:52 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D0=BE?= =?UTF-8?q?=D1=82=D0=BA=D1=80=D1=8B=D1=82=D0=B8=D0=B5=20=D1=81=D0=BE=D0=B5?= =?UTF-8?q?=D0=B4=D0=B8=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=20=D0=B1?= =?UTF-8?q?=D0=B4=20=D0=BA=D0=B0=D0=B6=D0=B4=D1=8B=D0=B9=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B7=20=D0=BF=D1=80=D0=B8=20=D0=BE=D0=B1=D1=80=D0=B0=D1=89?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=BA=20=D0=B1=D0=B4,=20=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B5=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=8D=D1=82=D0=B0=D0=BF=D0=B5=20=D0=B8=D0=BD=D0=B8?= =?UTF-8?q?=D1=86=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- endpoint.py | 55 ++++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/endpoint.py b/endpoint.py index ccceab2..cda168b 100644 --- a/endpoint.py +++ b/endpoint.py @@ -5,11 +5,9 @@ from datetime import datetime, timedelta from threading import Thread import journal - # SUPPORTED_REGISTER_TYPES = ['D', 'M'] SUPPORTED_REGISTER_TYPES = ['D'] - LOG_TYPE_ON_SCAN = 'on-scan' LOG_TYPE_ON_CHANGE = 'on-change' @@ -109,7 +107,6 @@ class Endpoint(Thread): self.ranges = parse_ranges(config) # база данных - self.connection = None self.db_table = config['database']['db-table'] self.db_col_names = '' for rn in self.ranges: @@ -134,14 +131,21 @@ class Endpoint(Thread): return values def __read_last_db_regs(self): - with self.connection.cursor() as cursor: - cursor.execute(f"select {self.db_col_names} from `{self.db_table}`" - f"order by `{self.config['database']['db-datetime-col']}` desc limit 1;") - row = cursor.fetchall() - if len(row) != 0: - return row[0] - else: - return None + with connect( + host=self.config["database"]["host"], + port=self.config["database"]["port"], + user=self.config["database"]["db-user"], + password=self.config["database"]["db-password"], + database=self.config["database"]["db-name"], + ) as connection: + with connection.cursor() as cursor: + cursor.execute(f"select {self.db_col_names} from `{self.db_table}`" + f"order by `{self.config['database']['db-datetime-col']}` desc limit 1;") + row = cursor.fetchall() + if len(row) != 0: + return row[0] + else: + return None def __insert_db_row(self, values): vals_str = "" @@ -151,10 +155,17 @@ class Endpoint(Thread): else: vals_str += f"{v}" - query = f"insert into `{self.db_table}` ({self.db_col_names}) values ({vals_str});" - journal.log(f"Executing query: {query}") - with self.connection.cursor() as cursor: - cursor.execute(query) + with connect( + host=self.config["database"]["host"], + port=self.config["database"]["port"], + user=self.config["database"]["db-user"], + password=self.config["database"]["db-password"], + database=self.config["database"]["db-name"] + ) as connection: + query = f"insert into `{self.db_table}` ({self.db_col_names}) values ({vals_str});" + journal.log(f"Executing query: {query}") + with connection.cursor() as cursor: + cursor.execute(query) def __endless_loop(self): # if self.log_type == 'on-change': @@ -174,6 +185,7 @@ class Endpoint(Thread): while True: try: row = self.__mb_read_all_regs() + if self.log_type == LOG_TYPE_ON_SCAN: self.__insert_db_row(row) last_row = row @@ -210,15 +222,6 @@ class Endpoint(Thread): port=self.config["modbus"]["port"], debug=self.config["modbus"]["debug"], auto_open=True) - with connect( - host=self.config["database"]["host"], - port=self.config["database"]["port"], - user=self.config["database"]["db-user"], - password=self.config["database"]["db-password"], - database=self.config["database"]["db-name"] - ) as _connection: - self.connection = _connection - - # запуск бесконечного цикла - self.__endless_loop() + # запуск бесконечного цикла + self.__endless_loop()