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.
124 lines
3.6 KiB
124 lines
3.6 KiB
# -*- coding:utf-8 -*-
|
|
"""
|
|
@Created on : 2023/5/22 13:55
|
|
@Author: hxl
|
|
@Des: 柜体管理
|
|
"""
|
|
import uuid
|
|
|
|
import httpx
|
|
from fastapi import APIRouter
|
|
from fastapi.encoders import jsonable_encoder
|
|
from pydantic import BaseModel
|
|
from tortoise.expressions import Q
|
|
|
|
from conf import setting
|
|
from helper import respond_to
|
|
from helper.log import logger_wrapper
|
|
from helper.cabinet import calculate_today_data
|
|
from models import Cabinet, Terminal
|
|
|
|
router = APIRouter(prefix='/cabinets')
|
|
|
|
|
|
class ProfileRegister(BaseModel):
|
|
mac_addr: str
|
|
ip: str
|
|
|
|
|
|
class ProfileEdit(BaseModel):
|
|
label: str = None
|
|
location: str = None
|
|
matrix: str = None
|
|
|
|
|
|
@router.get('', summary='柜体管理列表')
|
|
async def index(page_no: int = 1, page_size: int = 100, keyword: str = None):
|
|
"""
|
|
柜体管理列表
|
|
:param page_no: ProfileEdit
|
|
:param page_size: ProfileEdit
|
|
:param keyword: ProfileEdit
|
|
:return:
|
|
"""
|
|
terminal = await Terminal.get(id=setting.TERMINAL_ID)
|
|
query = Cabinet.filter(terminal_id=terminal.id)
|
|
if keyword:
|
|
query = query.filter(Q(username__istartswith=keyword) | Q(name__startswith=keyword))
|
|
|
|
offset = (page_no - 1) * page_size
|
|
count = await query.count()
|
|
cabinets = await query.limit(page_size).offset(offset).order_by('-created_at')
|
|
result = list()
|
|
for cabinet in jsonable_encoder(cabinets):
|
|
cabinet["today"] = await calculate_today_data(cabinet.get("id", ""))
|
|
result.append(cabinet)
|
|
return respond_to(data=dict(count=count, cabinets=result))
|
|
|
|
|
|
@router.post('/register', summary='柜体注册(下位机)')
|
|
async def create(model: ProfileRegister):
|
|
"""
|
|
柜体注册(下位机)
|
|
:param model: ProfileEdit
|
|
:return:
|
|
"""
|
|
|
|
ter = await Terminal.get(id=uuid.UUID(setting.TERMINAL_ID))
|
|
all_objs = await Cabinet.filter(terminal=ter)
|
|
for o in all_objs:
|
|
# Ip相同Mac地址不同返回提示
|
|
if o.ip == model.ip and o.mac_addr != model.mac_addr:
|
|
return respond_to(code=409, desc='This IP already exists')
|
|
cabinet_obj = await Cabinet.get_or_none(mac_addr=model.mac_addr)
|
|
# 有则更新无责创建
|
|
if cabinet_obj:
|
|
cabinet_obj.ip = model.ip
|
|
await cabinet_obj.save()
|
|
else:
|
|
await Cabinet.create(**model.dict(), terminal=ter)
|
|
return respond_to(desc='success')
|
|
|
|
|
|
@router.put('/{cabinet_id}', summary='柜体信息编辑')
|
|
@logger_wrapper
|
|
async def update(cabinet_id: str, model: ProfileEdit):
|
|
"""
|
|
柜体信息编辑
|
|
:param cabinet_id: str
|
|
:param model: ProfileEdit
|
|
:return:
|
|
"""
|
|
cabinet = await Cabinet.get(id=cabinet_id)
|
|
# 更新记录中存在的属性
|
|
for attr, value in model.dict(exclude_unset=True).items():
|
|
setattr(cabinet, attr, value)
|
|
# 保存更改
|
|
await cabinet.save()
|
|
return respond_to(200, desc=f"{cabinet.label}柜体信息修改成功")
|
|
|
|
|
|
@router.patch('/{cabinet_id}', summary='柜体参数编辑')
|
|
async def update(cabinet_id: str, params: dict):
|
|
"""
|
|
柜体参数编辑
|
|
:param cabinet_id: str
|
|
:param params: dict
|
|
:return:
|
|
"""
|
|
cabinet = await Cabinet.get(id=cabinet_id)
|
|
url = f'http://{cabinet.ip}/api/cabinet/v1/setting'
|
|
async with httpx.AsyncClient() as client:
|
|
try:
|
|
response = await client.post(url, json=params)
|
|
except Exception:
|
|
return respond_to(code=400, desc='下位机通讯失败')
|
|
|
|
if response.json()['code'] != 200:
|
|
return respond_to(code=405, desc='参数有误')
|
|
# 柜体参数 增加是否有门的配置 需要提前写死
|
|
params["door"] = cabinet.params.get("door")
|
|
cabinet.params = params
|
|
await cabinet.save()
|
|
return respond_to()
|