Compare commits
No commits in common. "0ac7db0b0ddcbbc22493fc46f7344bb3a18b3198" and "b3656ffc05c81942f0e11ba65db5ba2c25f6b287" have entirely different histories.
0ac7db0b0d
...
b3656ffc05
33
endpoint.py
33
endpoint.py
@ -5,9 +5,11 @@ 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'
|
||||
|
||||
@ -107,6 +109,7 @@ 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:
|
||||
@ -131,14 +134,7 @@ class Endpoint(Thread):
|
||||
return values
|
||||
|
||||
def __read_last_db_regs(self):
|
||||
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:
|
||||
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()
|
||||
@ -155,16 +151,9 @@ class Endpoint(Thread):
|
||||
else:
|
||||
vals_str += f"{v}"
|
||||
|
||||
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:
|
||||
with self.connection.cursor() as cursor:
|
||||
cursor.execute(query)
|
||||
|
||||
def __endless_loop(self):
|
||||
@ -185,7 +174,6 @@ 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
|
||||
@ -206,7 +194,7 @@ class Endpoint(Thread):
|
||||
break
|
||||
except Exception as ex:
|
||||
journal.log(f"Exception: {ex}")
|
||||
time.sleep(1) # небольшая задержка, чтоб не спамить запросами
|
||||
time.sleep(0.1) # небольшая задержка, чтоб не спамить запросами
|
||||
need_time = datetime.now() + scan_rate
|
||||
|
||||
# вычислим время до следующего опроса и подождем
|
||||
@ -222,6 +210,15 @@ 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()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user