From e1babdc737580b9ba64dd25fd6d293e4b4193f1d Mon Sep 17 00:00:00 2001 From: valoka Date: Tue, 23 Dec 2025 22:41:16 +0900 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE:=20=D1=88=D0=B0=D0=B1=D0=BB=D0=BE=D0=BD=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BE=D1=82=D0=BA=D1=80=D1=8B=D1=82=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B2=D1=81=D0=B5=D1=85=20=D0=B7=D0=B0=D0=BC=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2,=20=D0=BD=D0=B5=D1=81=D0=BA=D0=BE=D0=BB=D1=8C=D0=BA?= =?UTF-8?q?=D0=BE=20"=D1=83=D0=B4=D0=BE=D0=B1=D0=BD=D1=8B=D1=85"=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D0=B5=D0=B9=20=D0=B8=20if=20=5F=5Fmain=5F=5F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.ini | 3 +- main.py | 91 ++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/config.ini b/config.ini index 5181ffb..2bb6e19 100644 --- a/config.ini +++ b/config.ini @@ -1,4 +1,5 @@ [DEFAULT] device_hwid = PID=1A86:7523 baudrate = 9600 -debug = false \ No newline at end of file +debug = false +controller_capacity = 18 \ No newline at end of file diff --git a/main.py b/main.py index 9c68e84..c614e93 100644 --- a/main.py +++ b/main.py @@ -5,28 +5,10 @@ import logging import socket import lib.lomka as lomka -#consts&confs -config = configparser.ConfigParser() -#bc_calculator = lomka.BCCCalculator() -some_open_ports = serial.tools.list_ports.comports() - -#load config file -config.read('config.ini') -# device_hwid = config['DEFAULT']['device_hwid'] -# baudrate = config['DEFAULT']['baudrate'] - -#log section -logging.basicConfig( - level=logging.INFO if config['DEFAULT']['debug'] == "false" else logging.DEBUG, - format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', - handlers=[ - logging.FileHandler('lastest.log'), - logging.StreamHandler()]) -logger = logging.getLogger(socket.gethostname()) - class caser: #добавить по умолчанию с конфигом - def __init__(self, device_hwid, config, baudrate=9600, + def __init__(self, device_hwid, config, + logger=logging.getLogger(socket.gethostname()), baudrate=9600, bc_calculator=lomka.BCCCalculator, config_name="config.ini"): ''' Создает объект контроллера, к которому прикрепленна информация о существующем @@ -34,24 +16,35 @@ class caser: device_hwid - pid USB конвертера(необязательно USB) пример: "PID=1A86:7523" + logger - logging.getLogger() object baudrate - скорость соединения bc_calculator - метод вычисления хеш-суммы (должен возвращать один байт в int) + config_name - имя до файла конфигурации ''' try: config.read(config_name) self.device_hwid = config['DEFAULT']['device_hwid'] self.baudrate = config['DEFAULT']['baudrate'] + self.controller_capacity = config['DEFAULT']['controller_capacity'] + # узнать какое исключение вызывается конкретно, ибо нужно проверять каждое по отдельности except: self.device_hwid = device_hwid self.baudrate = baudrate + self.controller_capacity: int self.bc_calculator = bc_calculator # xor_bcc method(returns int) self.comport: str # только адрес self.active_comport = serial.tools.list_ports.comports()[0] # объект порта (dummy) self.controller_number: bytes = 1 + # Определение базовых констант/команд + self.default_consts = {True: 17, + False: 0, + "READ_HEADER": 128, + "MANIPULATE_HEADER": 138} + def find_comport(self, some_open_ports : list): '''Находит и возвращает корректный comport (объект)''' @@ -60,6 +53,11 @@ class caser: if self.device_hwid in port.hwid: logger.info(f"Found the correct port: {port.device}") return str(port.device) + + def set_controller_capacity(self, capacity: int): + '''Устанавливает количество доступных замков''' + + def set_port_device(self, port_device=None): '''Устанавливает com port''' @@ -74,7 +72,8 @@ class caser: self.controller_number = hex(controller_number) - def send_command(self, cell_number : int, command : int, header=138): + def send_command(self, cell_number : int, command : int, + bytes_count=5, header=138): active_comport = serial.Serial(self.comport, baudrate=self.baudrate) logger.info(f"Connected to {active_comport.name }") @@ -93,7 +92,7 @@ class caser: data_string += checksum.to_bytes(1, 'little') active_comport.write(data_string) - return active_comport.read(5) + return active_comport.read(bytes_count) def read_controller(self, cell_number : int, command=51, header=128): active_comport = serial.Serial(self.comport, baudrate=self.baudrate) @@ -112,12 +111,44 @@ class caser: data_string += checksum.to_bytes(1, 'little') active_comport.write(data_string) return active_comport.read(5) + + def OPEN_ALL_LOCKS(self, confirm : bool): + '''и возвращает состояние всех замков''' + if confirm: + try: + return self.send_command(0, self.default_consts[True], + self.default_consts["MANIPULATE_HEADER"], + bytes_count=self.controller_capacity) + except Exception as error: + return str(error) + else: + return 1 -zxc = caser(device_hwid="PID=1A86:7523", baudrate=9600, - config=config) -zxc.set_controller_number(1) -zxc.set_port_device() -# добавь *aargs -print(zxc.read_controller(1)) -print(zxc.send_command(1, 17)) -print(zxc.read_controller(1)) \ No newline at end of file +if __name__ == "__main__": + #consts&confs + config = configparser.ConfigParser() + #bc_calculator = lomka.BCCCalculator() + some_open_ports = serial.tools.list_ports.comports() + + #load config file + config.read('config.ini') + # device_hwid = config['DEFAULT']['device_hwid'] + # baudrate = config['DEFAULT']['baudrate'] + + #log section + logging.basicConfig( + level=logging.INFO if config['DEFAULT']['debug'] == "false" else logging.DEBUG, + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + handlers=[ + logging.FileHandler('lastest.log'), + logging.StreamHandler()]) + logger = logging.getLogger(socket.gethostname()) + + zxc = caser(device_hwid="PID=1A86:7523", baudrate=9600, + config=config, logger=logger) + zxc.set_controller_number(1) + zxc.set_port_device() + # добавь *aargs + print(zxc.read_controller(1)) + print(zxc.send_command(1, 17)) + print(zxc.read_controller(1)) \ No newline at end of file