This commit is contained in:
2026-01-18 19:32:43 +09:00
parent 5b2915d1df
commit a6f8136b32
2 changed files with 63 additions and 34 deletions

View File

@@ -14,6 +14,7 @@ class caser:
Создает объект контроллера, к которому прикрепленна информация о существующем
локере.
connection - объект активного соединения с контроллером
device_hwid - pid USB конвертера(необязательно USB)
пример: "PID=1A86:7523"
logger - logging.getLogger() object
@@ -34,31 +35,43 @@ class caser:
self.baudrate = baudrate
self.controller_capacity: int
self.logger = logger
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.connection : serial.Serial
# Определение базовых констант/команд
self.default_consts = {True: 17,
False: 0,
"INFO": 51,
"READ_HEADER": 128,
"RESPONSE_LENGHT": 5,
"MANIPULATE_HEADER": 138}
def find_comport(self, some_open_ports : list):
'''Находит и возвращает корректный comport (объект)'''
for port in some_open_ports:
logger.debug(f'{port.location}, {port.description}, {port.hwid}')
self.logger.debug(f'{port.location}, {port.description}, {port.hwid}')
if self.device_hwid in port.hwid:
logger.info(f"Found the correct port: {port.device}")
self.logger.info(f"Found the correct port: {port.device}")
return str(port.device)
def set_controller_capacity(self, capacity: int):
'''Устанавливает количество доступных замков'''
#сделать автоматом
if capacity:
self.controller_capacity = capacity
else:
self.controller_capacity = 18
def set_port_device(self, port_device=None):
'''Устанавливает com port'''
if port_device == None:
self.comport = self.find_comport(some_open_ports)
if port_device is None:
self.comport = self.find_comport(serial.tools.list_ports.comports())
else:
self.comport = port_device
@@ -70,7 +83,7 @@ class caser:
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 }")
self.logger.info(f"Connected to {active_comport.name }")
# 1 - header(8Ahex) (138dec), 2 - controller number(01), 3 - cell number(04), 4 - command(11), 5 - checksum(9E)
# *4 - (11) - open, (00) - close
@@ -91,7 +104,7 @@ class caser:
def read_controller(self, cell_number : int, command=51, header=128):
active_comport = serial.Serial(self.comport, baudrate=self.baudrate)
logger.info(f"Connected to {active_comport.name }")
self.logger.info(f"Connected to {active_comport.name }")
data = [header.to_bytes(1, 'little'),
int(self.controller_number, 16).to_bytes(1, 'little'),
cell_number.to_bytes(1, 'little'),
@@ -107,6 +120,14 @@ class caser:
active_comport.write(data_string)
return active_comport.read(5)
def READ_ALL_LOCKS(self):
# try:
return self.send_command(0, self.default_consts["INFO"],
5,
header=self.default_consts["READ_HEADER"])
# except Exception as error:
# return str(error)
def OPEN_ALL_LOCKS(self, confirm : bool):
'''и возвращает состояние всех замков'''
if confirm:
@@ -137,31 +158,3 @@ class caser:
def watchdog(self):
pass
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))

36
main.py Normal file
View File

@@ -0,0 +1,36 @@
import lib.OCer as OCer
import logging
import configparser
import serial
import socket
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 = OCer.caser(device_hwid="PID=1A86:7523", baudrate=9600,
config=config, logger=logger)
zxc.set_controller_number(1)
zxc.set_port_device()
zxc.set_controller_capacity(18)
# добавь *aargs
zxc.READ_ALL_LOCKS()
# print(zxc.read_controller(1))
# print(zxc.send_command(1, 17))
# print(zxc.read_controller(1))