You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
7.0 KiB
165 lines
7.0 KiB
""" 柜体抽屉信息初始化命令
|
|
|
|
NOTE: 此命令代码基于http://git.yanei-iot.com:600/rms-edition/rms-config.git的develop分支中: ./deploy/rmsinstall生成的配置文件做为输入参数,
|
|
解析输入的json文件, 将其中配置好的柜体的抽屉参数提取出来并在目标数据库中进行柜体的配置.
|
|
|
|
前置条件1: 默认新增终端, 柜体等记录, 不考虑对老记录的更改覆盖, 除非在配置中指定了ID值, 其他复杂情况均不考虑, 最好的情况是数据库中不存在任何
|
|
Terminal, Cabinet, Hikvision, DrawerBoard, Drawer的记录, 其他表基本记录要存在(因为是固化的).
|
|
"""
|
|
|
|
import sys
|
|
import json
|
|
import textwrap
|
|
|
|
from models import *
|
|
|
|
|
|
def _load_config(filename):
|
|
_v = {}
|
|
try:
|
|
with open(filename, encoding="utf8") as fd:
|
|
_v = json.load(fd)
|
|
except:
|
|
pass
|
|
return _v
|
|
|
|
|
|
# 型号到cabient params的映射(同rms_config项目下的statuses进行关联映射(注意, 这里没有中控, 也不需要中控)
|
|
terminal_type2parasms = {
|
|
'Eagle9000': '{"type":2,"temperature":[4,-20],"all_temperature":-13,"temp_out":5,"heat_config":9,"fan_config":2,"defrost_config":9,"light_config":2,"filterExpire":"2025-01-08","defrosWarning":9,"door":true}',
|
|
'RC1800': '{"type":1,"temperature":[10],"all_temperature":"10","temp_out":5,"heat_config":0,"fan_config":3,"defrost_config":33,"light_config":2,"filterExpire":"2025-01-08","defrosWarning":9,"minute":30,"door":true}',
|
|
'RC1600': '{"type":3,"temperature":[],"all_temperature":"6","temp_out":10,"heat_config":0,"fan_config":5,"defrost_config":0,"light_config":2,"filterExpire":"2023-08-11","defrosWarning":9,"minute":30,"door":true}',
|
|
'MINI90': '{"type":3,"temperature":[],"all_temperature":"3","temp_out":10,"heat_config":10,"fan_config":0,"defrost_config":9,"light_config":2,"filterExpire":"2025-01-08","defrosWarning":9,"door":true}',
|
|
'RMST-V2': '{"type":3,"temperature":[],"all_temperature":"-13","temp_out":10,"heat_config":10,"fan_config":0,"defrost_config":9,"light_config":2,"filterExpire":"2023-08-11","defrosWarning":9,"door":true}',
|
|
}
|
|
|
|
|
|
async def _update_one_cabinet(config_info, cabinet_info, archive, user, terminal):
|
|
cabinet_id = cabinet_info.get("CABINET_ID")
|
|
terminal_type = config_info.get("TERMINAL_TYPE")
|
|
if cabinet_id:
|
|
cabinet = await Cabinet.get_or_none(id=cabinet_id)
|
|
if not cabinet:
|
|
print("--未在数据库中找到{cabinet_id}对应的cabinet记录, 请更改配置文件或确认数据库数据")
|
|
sys.exit(-1)
|
|
|
|
cabinet.mac_addr = cabinet_info["MAC"]
|
|
cabinet.ip = cabinet_info["IP"]
|
|
cabinet.label = cabinet_info["LABEL"]
|
|
cabinet.location = cabinet_info["LOCATION"]
|
|
cabinet.matrix = cabinet_info["MATRIX"]
|
|
cabinet.archive_id = archive.id
|
|
await cabinet.save()
|
|
else:
|
|
cabinet = await Cabinet.create(
|
|
**{
|
|
'terminal_id': terminal.id,
|
|
'mac_addr': cabinet_info['MAC'],
|
|
'type': 1,
|
|
'ip': cabinet_info['IP'],
|
|
'label': cabinet_info['LABEL'],
|
|
'location': cabinet_info['LOCATION'],
|
|
'matrix': cabinet_info['MATRIX'],
|
|
'user_id': user.id,
|
|
'archive_id': archive.id,
|
|
'params': terminal_type2parasms.get(terminal_type, terminal_type2parasms["Eagle9000"]),
|
|
}
|
|
)
|
|
|
|
# 先清理相关的drawer_boards和drawers, 再创建新的记录
|
|
drawer_boards = await DrawerBoard.filter(cabinet_id=cabinet.id).all()
|
|
for _drawer_board in drawer_boards:
|
|
await _drawer_board.delete()
|
|
drawers = await DrawerBoard.filter(cabinet_id=cabinet.id).all()
|
|
for _drawer_board in drawer_boards:
|
|
await _drawer_board.delete()
|
|
name2board = {item.name: item for item in await Board.all()}
|
|
drawer_info = cabinet_info["DRAWER_INFO"]
|
|
for _lineno, board_info in drawer_info.items():
|
|
board = name2board[board_info["BOARD"]]
|
|
drawer = await Drawer.create(
|
|
**{
|
|
'line_no': _lineno,
|
|
'label': board_info['DRAWER_LABEL'],
|
|
'rank': board_info['DRAWER_RANK'],
|
|
'cabinet_id': cabinet.id,
|
|
}
|
|
)
|
|
await DrawerBoard.create(
|
|
**{
|
|
'line_no': _lineno,
|
|
'label': board_info['LABEL'],
|
|
'capacity': board_info['CAPACITY'],
|
|
'board_id': board.id,
|
|
'cabinet_id': cabinet.id,
|
|
'drawer_id': drawer.id,
|
|
}
|
|
)
|
|
|
|
# hikvision配置
|
|
hikvision = await Hikvision.filter(terminal_id=terminal.id, cabinet_id=cabinet.id).first()
|
|
if not hikvision:
|
|
hikvision = await Hikvision.create(
|
|
**{
|
|
'terminal_id': terminal.id,
|
|
'cabinet_id': cabinet.id,
|
|
'ip': cabinet_info['IP'],
|
|
'channel': cabinet_info['CHANNEL'],
|
|
'name': cabinet_info['LABEL'],
|
|
}
|
|
)
|
|
else:
|
|
hikvision.channel = cabinet_info['CHANNEL']
|
|
hikvision.name = cabinet_info['LABEL']
|
|
hikvision.ip = cabinet_info['IP']
|
|
await hikvision.save()
|
|
|
|
|
|
async def command_add_terminal_by_config(config_file):
|
|
"""解析传入的config_file配置文件, 完成terminal, cabinet, drawer_boards, drawers, boards表的更新工作"""
|
|
from conf import setting
|
|
|
|
# 载入配置
|
|
config_info = _load_config(config_file)
|
|
|
|
# terminal配置
|
|
terminal_count = await Terminal.filter().count()
|
|
terminal_id = config_info.get("CABINET_TERMINAL_ID")
|
|
if not terminal_count:
|
|
default_terminal = await Terminal.get_or_none(id=setting.TERMINAL_ID)
|
|
info = {
|
|
'name': config_info['CABINET_TERMINAL_NAME'],
|
|
'title': config_info['CABINET_TERMINAL_TITLE'],
|
|
'login_count': 1,
|
|
'index_of_user': 1,
|
|
}
|
|
if not default_terminal:
|
|
info['id'] = setting.TERMINAL_ID
|
|
terminal = await Terminal.create(**info)
|
|
else:
|
|
if terminal_id:
|
|
terminal = await Terminal.get_or_none(id=terminal_id)
|
|
if not terminal:
|
|
print("--未在数据库中找到{terminal_id}对应的terminal记录, 请更改配置文件或确认数据库数据")
|
|
sys.exit(-1)
|
|
else:
|
|
terminal = await Terminal.first()
|
|
|
|
terminal.name = config_info["CABINET_TERMINAL_NAME"]
|
|
terminal.title = config_info["CABINET_TERMINAL_TITLE"]
|
|
await terminal.save()
|
|
|
|
# archive
|
|
archive = await Archive.filter(name=config_info["CABINET_ARCHIVE_NAME"]).first()
|
|
user = await User.filter(username="admin").first()
|
|
|
|
# main cabinet
|
|
main_info = config_info["CABINET_MAIN"]
|
|
if not config_info.get("CABINET_MAIN_SKIP"):
|
|
await _update_one_cabinet(config_info, main_info, archive, user, terminal)
|
|
|
|
# other cabinet
|
|
for _index in range(config_info["AUXILIARY_NUM"]):
|
|
cabinet_info = config_info[f'CABINET_SLAVE_{_index + 1}']
|
|
await _update_one_cabinet(config_info, cabinet_info, archive, user, terminal)
|