forked from iti-framework/iTi-Flask
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.6 KiB
Python
163 lines
4.6 KiB
Python
from apiflask import APIBlueprint
|
|
from iti.applications.extensions import db, sys_log
|
|
from iti.applications.common.utils import success, page_schema, page
|
|
from iti.applications.models import (
|
|
IotDevice,
|
|
IotDeviceSchema,
|
|
)
|
|
from .schemas.device import (
|
|
DeviceQuery,
|
|
DeviceAddRequest,
|
|
DeviceUpdateRequest,
|
|
)
|
|
from iti.applications.common import ModelFilter
|
|
from iti.applications.common.exceptions.biz_exp import BizException
|
|
from flask_jwt_extended import jwt_required
|
|
from sqlalchemy import select, delete, exists
|
|
from sqlalchemy.sql.functions import func
|
|
from sqlalchemy.orm import noload
|
|
from iti.applications.common import permission
|
|
|
|
|
|
bp = APIBlueprint("iot_device", __name__, url_prefix="/device", tag="设备管理")
|
|
|
|
@bp.get("/list")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:device:list")
|
|
@bp.input(DeviceQuery.Schema(partial=True), location="query")
|
|
@bp.output(IotDeviceSchema(many=True))
|
|
def list_device(query_data: DeviceQuery):
|
|
"""
|
|
获取设备列表
|
|
"""
|
|
|
|
return success(get_list(query_data))
|
|
|
|
|
|
@bp.get("/page")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:device:list")
|
|
@bp.input(DeviceQuery.Schema(partial=True), location="query")
|
|
@bp.output(page_schema(IotDeviceSchema(many=True)))
|
|
def page_device(query_data: DeviceQuery):
|
|
"""
|
|
分页获取设备列表
|
|
"""
|
|
|
|
return page(get_page(query_data))
|
|
|
|
|
|
@bp.post("/add")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:device:add")
|
|
@bp.input(DeviceAddRequest, location="json")
|
|
def add_device(json_data: dict):
|
|
"""
|
|
添加设备信息
|
|
"""
|
|
|
|
device = IotDevice(**json_data)
|
|
device.status = 0
|
|
db.session.add(device)
|
|
db.session.commit()
|
|
return success()
|
|
|
|
|
|
@bp.put("/<int:id>")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:device:update")
|
|
@bp.input(DeviceUpdateRequest(partial=True), location="json")
|
|
def update_device(id: int, json_data: dict):
|
|
"""
|
|
更新设备信息
|
|
"""
|
|
|
|
device = db.session.scalar(select(IotDevice).filter_by(id=id))
|
|
if not device:
|
|
raise BizException("设备信息不存在")
|
|
for key, value in json_data.items():
|
|
if value is not None:
|
|
setattr(device, key, value)
|
|
|
|
db.session.commit()
|
|
|
|
return success()
|
|
|
|
@bp.delete("/<int:id>")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:device:delete")
|
|
def delete_device(id: int):
|
|
"""
|
|
删除设备信息
|
|
"""
|
|
|
|
device = db.session.scalar(select(IotDevice).filter_by(id=id))
|
|
if not device:
|
|
raise BizException("设备不存在")
|
|
|
|
# 删除设备
|
|
db.session.delete(device)
|
|
db.session.commit()
|
|
return success()
|
|
|
|
@bp.get("/count")
|
|
@jwt_required()
|
|
@bp.doc(security="JWT")
|
|
@permission("iot:device:list")
|
|
def count_device():
|
|
"""
|
|
统计设备数量
|
|
"""
|
|
|
|
countData = {}
|
|
deviceReady = db.session.query(func.count(IotDevice.id).label('number')).filter_by(status=1).first().number
|
|
deviceUnready = db.session.query(func.count(IotDevice.id).label('number')).filter_by(status=0).first().number
|
|
deviceFix = db.session.query(func.count(IotDevice.id).label('number')).filter_by(status=2).first().number
|
|
countData["ready"] = deviceReady
|
|
countData["unReady"] = deviceUnready
|
|
countData["fix"] = deviceFix
|
|
countData["total"] = deviceReady + deviceUnready + deviceFix
|
|
|
|
return success(countData)
|
|
|
|
def get_page(query_data: DeviceQuery):
|
|
"""
|
|
获取设备信息分页
|
|
"""
|
|
query = select(IotDevice).order_by(IotDevice.created_at.desc())
|
|
if query_data.keyword:
|
|
kw = ModelFilter.escape_like(query_data.keyword)
|
|
query = query.filter(
|
|
IotDevice.device_name.like(f"%{kw}%")
|
|
| IotDevice.device_number.like(f"%{kw}%")
|
|
)
|
|
if query_data.workshop_id:
|
|
query = query.filter(IotDevice.workshop_id == query_data.workshop_id)
|
|
if query_data.status is not None:
|
|
query = query.filter(IotDevice.status == query_data.status)
|
|
|
|
return db.paginate(query, page=query_data.page, per_page=query_data.size)
|
|
|
|
|
|
def get_list(query_data: DeviceQuery):
|
|
"""
|
|
获取设备列表
|
|
"""
|
|
query = select(IotDevice).options(noload(IotDevice.workshop)).order_by(IotDevice.created_at.desc())
|
|
if query_data.keyword:
|
|
kw = ModelFilter.escape_like(query_data.keyword)
|
|
query = query.filter(
|
|
IotDevice.device_name.like(f"%{kw}%")
|
|
| IotDevice.device_number.like(f"%{kw}%")
|
|
)
|
|
if query_data.workshop_id:
|
|
query = query.filter(IotDevice.workshop_id == query_data.workshop_id)
|
|
if query_data.status is not None:
|
|
query = query.filter(IotDevice.status == query_data.status)
|
|
|
|
return db.session.scalars(query).all() |