关联调整

iot
DESKTOP-1JS6RSM\Admin 3 months ago
parent 60bf0ffaf1
commit 8a15f1990f

@ -56,8 +56,5 @@ class IotDeviceSimpleSchema(BaseSchema):
class Meta: class Meta:
name = "IotDevice" name = "IotDevice"
id = Integer()
device_name = String() device_name = String()
device_number = String() device_number = String()
#关系
workshop = Nested("IotWorkshopSimpleSchema")

@ -16,6 +16,7 @@ class IotEndpoint(db.Model, TimeModelMixin):
autoincrement=True, autoincrement=True,
comment="标识", comment="标识",
) )
workshop_id = db.Column(db.Integer, nullable=False, default=0, unique=True, comment="车间ID")
device_id = db.Column(db.Integer, nullable=False, default=0, unique=True, comment="设备ID") device_id = db.Column(db.Integer, nullable=False, default=0, unique=True, comment="设备ID")
endpoint_name = db.Column(db.String(255), nullable=False, unique=True, comment="采集端名称") endpoint_name = db.Column(db.String(255), nullable=False, unique=True, comment="采集端名称")
endpoint_number = db.Column(db.String(20), nullable=False, comment="采集端编号") endpoint_number = db.Column(db.String(20), nullable=False, comment="采集端编号")
@ -27,6 +28,10 @@ class IotEndpoint(db.Model, TimeModelMixin):
is_online = db.Column(db.Integer, nullable=False, default=0, comment="在线状态 0:离线 1:在线") is_online = db.Column(db.Integer, nullable=False, default=0, comment="在线状态 0:离线 1:在线")
status = db.Column(db.Integer, nullable=False, default=0, comment="状态 0:停用 1:运行中 2:维修中") status = db.Column(db.Integer, nullable=False, default=0, comment="状态 0:停用 1:运行中 2:维修中")
#关系 #关系
workshop = db.relationship(
"IotWorkshop",
primaryjoin="foreign(IotEndpoint.workshop_id) == IotWorkshop.id",
)
device = db.relationship( device = db.relationship(
"IotDevice", "IotDevice",
primaryjoin="foreign(IotEndpoint.device_id) == IotDevice.id", primaryjoin="foreign(IotEndpoint.device_id) == IotDevice.id",
@ -41,6 +46,7 @@ class IotEndpointSchema(BaseSchema):
name = "IotEndpoint" name = "IotEndpoint"
id = Integer() id = Integer()
workshop_id = Integer()
device_id = Integer() device_id = Integer()
endpoint_name = String() endpoint_name = String()
endpoint_number = String() endpoint_number = String()
@ -54,6 +60,7 @@ class IotEndpointSchema(BaseSchema):
created_at = DateTime(format="%Y-%m-%d %H:%M:%S") created_at = DateTime(format="%Y-%m-%d %H:%M:%S")
updated_at = DateTime(format="%Y-%m-%d %H:%M:%S") updated_at = DateTime(format="%Y-%m-%d %H:%M:%S")
#关系 #关系
workshop = Nested("IotWorkshopSimpleSchema")
device = Nested("IotDeviceSimpleSchema") device = Nested("IotDeviceSimpleSchema")
@ -64,8 +71,5 @@ class IotEndpointSimpleSchema(BaseSchema):
class Meta: class Meta:
name = "IotEndpoint" name = "IotEndpoint"
id = Integer()
endpoint_name = String() endpoint_name = String()
endpoint_number = String() endpoint_number = String()
#关系
device = Nested("IotDeviceSimpleSchema")

@ -16,6 +16,8 @@ class IotNode(db.Model, TimeModelMixin):
autoincrement=True, autoincrement=True,
comment="标识", comment="标识",
) )
workshop_id = db.Column(db.Integer, nullable=False, default=0, unique=True, comment="车间ID")
device_id = db.Column(db.Integer, nullable=False, default=0, unique=True, comment="设备ID")
endpoint_id = db.Column(db.Integer, nullable=False, default=0, unique=True, comment="采集端ID") endpoint_id = db.Column(db.Integer, nullable=False, default=0, unique=True, comment="采集端ID")
title = db.Column(db.String(255), nullable=False, unique=True, comment="节点ID") title = db.Column(db.String(255), nullable=False, unique=True, comment="节点ID")
mark = db.Column(db.String(255), nullable=False, comment="采集标识") mark = db.Column(db.String(255), nullable=False, comment="采集标识")
@ -29,6 +31,14 @@ class IotNode(db.Model, TimeModelMixin):
method_content = db.Column(db.String(255), nullable=False, comment="方法节点") method_content = db.Column(db.String(255), nullable=False, comment="方法节点")
status = db.Column(db.Integer, nullable=False, default=0, comment="状态 0:禁用 1:启用") status = db.Column(db.Integer, nullable=False, default=0, comment="状态 0:禁用 1:启用")
#关系 #关系
workshop = db.relationship(
"IotWorkshop",
primaryjoin="foreign(IotNode.workshop_id) == IotWorkshop.id",
)
device = db.relationship(
"IotDevice",
primaryjoin="foreign(IotNode.device_id) == IotDevice.id",
)
endpoint = db.relationship( endpoint = db.relationship(
"IotEndpoint", "IotEndpoint",
primaryjoin="foreign(IotNode.endpoint_id) == IotEndpoint.id", primaryjoin="foreign(IotNode.endpoint_id) == IotEndpoint.id",
@ -43,6 +53,8 @@ class IotNodeSchema(BaseSchema):
name = "IotNode" name = "IotNode"
id = Integer() id = Integer()
workshop_id = Integer()
device_id = Integer()
endpoint_id = Integer() endpoint_id = Integer()
title = String() title = String()
mark = String() mark = String()
@ -58,4 +70,6 @@ class IotNodeSchema(BaseSchema):
created_at = DateTime(format="%Y-%m-%d %H:%M:%S") created_at = DateTime(format="%Y-%m-%d %H:%M:%S")
updated_at = DateTime(format="%Y-%m-%d %H:%M:%S") updated_at = DateTime(format="%Y-%m-%d %H:%M:%S")
#关系 #关系
workshop = Nested("IotWorkshopSimpleSchema")
device = Nested("IotDeviceSimpleSchema")
endpoint = Nested("IotEndpointSimpleSchema") endpoint = Nested("IotEndpointSimpleSchema")

@ -47,6 +47,5 @@ class IotWorkshopSimpleSchema(BaseSchema):
class Meta: class Meta:
name = "IotWorkshop" name = "IotWorkshop"
id = Integer()
workshop_name = String() workshop_name = String()
workshop_number = String() workshop_number = String()

@ -2,6 +2,7 @@ from apiflask import APIBlueprint
from iti.applications.extensions import db, sys_log from iti.applications.extensions import db, sys_log
from iti.applications.common.utils import success, page_schema, page from iti.applications.common.utils import success, page_schema, page
from iti.applications.models import ( from iti.applications.models import (
IotDevice,
IotEndpoint, IotEndpoint,
IotEndpointSchema, IotEndpointSchema,
) )
@ -59,6 +60,10 @@ def add_endpoint(json_data: dict):
""" """
endpoint = IotEndpoint(**json_data) endpoint = IotEndpoint(**json_data)
device = db.session.scalar(select(IotDevice).options(noload(IotDevice.workshop)).filter_by(id = endpoint.device_id))
if not device:
raise BizException("设备信息不存在")
endpoint.workshop_id = device.workshop_id
endpoint.status = 0 endpoint.status = 0
db.session.add(endpoint) db.session.add(endpoint)
db.session.commit() db.session.commit()
@ -75,7 +80,10 @@ def update_endpoint(id: int, json_data: dict):
更新采集端信息 更新采集端信息
""" """
endpoint = db.session.scalar(select(IotEndpoint).filter_by(id=id)) endpoint = db.session.scalar(
select(IotEndpoint)
.options(noload(IotEndpoint.workshop), noload(IotEndpoint.device))
.filter_by(id=id))
if not endpoint: if not endpoint:
raise BizException("采集端信息不存在") raise BizException("采集端信息不存在")
for key, value in json_data.items(): for key, value in json_data.items():
@ -95,7 +103,10 @@ def delete_endpoint(id: int):
删除采集端信息 删除采集端信息
""" """
endpoint = db.session.scalar(select(IotEndpoint).filter_by(id=id)) endpoint = db.session.scalar(
select(IotEndpoint)
.options(noload(IotEndpoint.workshop, IotEndpoint.device))
.filter_by(id=id))
if not endpoint: if not endpoint:
raise BizException("采集端不存在") raise BizException("采集端不存在")
@ -117,6 +128,8 @@ def get_page(query_data: EndpointQuery):
) )
if query_data.device_id: if query_data.device_id:
query = query.filter(IotEndpoint.device_id == query_data.device_id) query = query.filter(IotEndpoint.device_id == query_data.device_id)
elif query_data.workshop_id:
query = query.filter(IotEndpoint.workshop_id == query_data.workshop_id)
if query_data.status is not None: if query_data.status is not None:
query = query.filter(IotEndpoint.status == query_data.status) query = query.filter(IotEndpoint.status == query_data.status)
@ -127,7 +140,7 @@ def get_list(query_data: EndpointQuery):
""" """
获取采集端信息列表 获取采集端信息列表
""" """
query = select(IotEndpoint).options(noload(IotEndpoint.device)).order_by(IotEndpoint.created_at.desc()) query = select(IotEndpoint).options(noload(IotEndpoint.device), noload(IotEndpoint.workshop)).order_by(IotEndpoint.created_at.desc())
if query_data.keyword: if query_data.keyword:
kw = ModelFilter.escape_like(query_data.keyword) kw = ModelFilter.escape_like(query_data.keyword)
query = query.filter( query = query.filter(
@ -136,6 +149,8 @@ def get_list(query_data: EndpointQuery):
) )
if query_data.device_id: if query_data.device_id:
query = query.filter(IotEndpoint.device_id == query_data.device_id) query = query.filter(IotEndpoint.device_id == query_data.device_id)
elif query_data.workshop_id:
query = query.filter(IotEndpoint.workshop_id == query_data.workshop_id)
if query_data.status is not None: if query_data.status is not None:
query = query.filter(IotEndpoint.status == query_data.status) query = query.filter(IotEndpoint.status == query_data.status)

@ -2,6 +2,7 @@ from apiflask import APIBlueprint
from iti.applications.extensions import db, sys_log from iti.applications.extensions import db, sys_log
from iti.applications.common.utils import success, page_schema, page from iti.applications.common.utils import success, page_schema, page
from iti.applications.models import ( from iti.applications.models import (
IotEndpoint,
IotNode, IotNode,
IotNodeSchema, IotNodeSchema,
) )
@ -59,6 +60,14 @@ def add_node(json_data: dict):
""" """
node = IotNode(**json_data) node = IotNode(**json_data)
endpoint = db.session.scalar(
select(IotEndpoint)
.options(noload(IotEndpoint.workshop), noload(IotEndpoint.device))
.filter_by(id=node.endpoint_id))
if not endpoint:
raise BizException("采集端信息不存在")
node.device_id = endpoint.device_id
node.workshop_id = endpoint.workshop_id
node.status = 0 node.status = 0
db.session.add(node) db.session.add(node)
db.session.commit() db.session.commit()
@ -75,7 +84,10 @@ def update_node(id: int, json_data: dict):
更新采集节点信息 更新采集节点信息
""" """
node = db.session.scalar(select(IotNode).filter_by(id=id)) node = db.session.scalar(
select(IotNode)
.options(noload(IotNode.workshop), noload(IotNode.device), noload(IotNode.endpoint))
.filter_by(id=id))
if not node: if not node:
raise BizException("节点信息不存在") raise BizException("节点信息不存在")
for key, value in json_data.items(): for key, value in json_data.items():
@ -95,7 +107,10 @@ def delete_node(id: int):
删除采集节点信息 删除采集节点信息
""" """
node = db.session.scalar(select(IotNode).filter_by(id=id)) node = db.session.scalar(
select(IotNode)
.options(noload(IotNode.workshop), noload(IotNode.device), noload(IotNode.endpoint))
.filter_by(id=id))
if not node: if not node:
raise BizException("采集节点不存在") raise BizException("采集节点不存在")
@ -112,6 +127,10 @@ def get_page(query_data: NodeQuery):
query = select(IotNode).order_by(IotNode.created_at.desc()) query = select(IotNode).order_by(IotNode.created_at.desc())
if query_data.endpoint_id: if query_data.endpoint_id:
query = query.filter(IotNode.endpoint_id == query_data.endpoint_id) query = query.filter(IotNode.endpoint_id == query_data.endpoint_id)
elif query_data.device_id:
query = query.filter(IotNode.device_id == query_data.device_id)
elif query_data.workshop_id:
query = query.filter(IotNode.workshop_id == query_data.workshop_id)
if query_data.status is not None: if query_data.status is not None:
query = query.filter(IotNode.status == query_data.status) query = query.filter(IotNode.status == query_data.status)
@ -122,9 +141,13 @@ def get_list(query_data: NodeQuery):
""" """
获取采集节点列表 获取采集节点列表
""" """
query = select(IotNode).options(noload(IotNode.endpoint)).order_by(IotNode.created_at.desc()) query = select(IotNode).options(noload(IotNode.workshop), noload(IotNode.device), noload(IotNode.endpoint)).order_by(IotNode.created_at.desc())
if query_data.endpoint_id: if query_data.endpoint_id:
query = query.filter(IotNode.endpoint_id == query_data.endpoint_id) query = query.filter(IotNode.endpoint_id == query_data.endpoint_id)
elif query_data.device_id:
query = query.filter(IotNode.device_id == query_data.device_id)
elif query_data.workshop_id:
query = query.filter(IotNode.workshop_id == query_data.workshop_id)
if query_data.status is not None: if query_data.status is not None:
query = query.filter(IotNode.status == query_data.status) query = query.filter(IotNode.status == query_data.status)
return db.session.scalars(query).all() return db.session.scalars(query).all()

@ -21,6 +21,13 @@ class EndpointQuery(Pagination):
}, },
}, },
) )
workshop_id: int = field(
default=None,
metadata={
"required": False,
"metadata": {"example": 1, "description": "车间ID"},
},
)
device_id: int = field( device_id: int = field(
default=None, default=None,
metadata={ metadata={
@ -44,19 +51,16 @@ class EndpointAddRequest(BaseSchema):
""" """
device_id = fields.Integer( device_id = fields.Integer(
required=False, required=True,
metadata={"example": 1, "description": "设备ID"}, metadata={"example": 1, "description": "设备ID"},
load_default=None,
) )
endpoint_name = fields.String( endpoint_name = fields.String(
required=False, required=True,
metadata={"example": "采集端名称", "descriptrion": "采集端名称"}, metadata={"example": "采集端名称", "descriptrion": "采集端名称"},
load_default=None,
) )
endpoint_number = fields.String( endpoint_number = fields.String(
required=False, required=True,
metadata={"example": "采集端编号", "description": "采集端编号"}, metadata={"example": "采集端编号", "description": "采集端编号"},
load_default=None,
) )
description = fields.String( description = fields.String(
required=False, required=False,
@ -64,14 +68,12 @@ class EndpointAddRequest(BaseSchema):
load_default=None, load_default=None,
) )
ip = fields.String( ip = fields.String(
required=False, required=True,
metadata={"example": "采集端IP", "description": "采集端IP"}, metadata={"example": "采集端IP", "description": "采集端IP"},
load_default=None,
) )
port = fields.String( port = fields.String(
required=False, required=True,
metadata={"example": "采集端端口", "description": "采集端端口"}, metadata={"example": "采集端端口", "description": "采集端端口"},
load_default=None,
) )
brand_name = fields.String( brand_name = fields.String(
required=False, required=False,
@ -91,19 +93,16 @@ class EndpointUpdateRequest(BaseSchema):
""" """
device_id = fields.Integer( device_id = fields.Integer(
required=False, required=True,
metadata={"example": 1, "description": "设备ID"}, metadata={"example": 1, "description": "设备ID"},
load_default=None,
) )
endpoint_name = fields.String( endpoint_name = fields.String(
required=False, required=True,
metadata={"example": "采集端名称", "descriptrion": "采集端名称"}, metadata={"example": "采集端名称", "descriptrion": "采集端名称"},
load_default=None,
) )
endpoint_number = fields.String( endpoint_number = fields.String(
required=False, required=True,
metadata={"example": "采集端编号", "description": "采集端编号"}, metadata={"example": "采集端编号", "description": "采集端编号"},
load_default=None,
) )
description = fields.String( description = fields.String(
required=False, required=False,
@ -111,14 +110,12 @@ class EndpointUpdateRequest(BaseSchema):
load_default=None, load_default=None,
) )
ip = fields.String( ip = fields.String(
required=False, required=True,
metadata={"example": "采集端IP", "description": "采集端IP"}, metadata={"example": "采集端IP", "description": "采集端IP"},
load_default=None,
) )
port = fields.String( port = fields.String(
required=False, required=True,
metadata={"example": "采集端端口", "description": "采集端端口"}, metadata={"example": "采集端端口", "description": "采集端端口"},
load_default=None,
) )
brand_name = fields.String( brand_name = fields.String(
required=False, required=False,

@ -12,6 +12,20 @@ class NodeQuery(Pagination):
节点信息查询请求 节点信息查询请求
""" """
workshop_id: int = field(
default=None,
metadata={
"required": False,
"metadata": {"example": 1, "description": "车间ID"},
},
)
device_id: int = field(
default=None,
metadata={
"required": False,
"metadata": {"example": 1, "description": "设备ID"},
},
)
endpoint_id: int = field( endpoint_id: int = field(
default=None, default=None,
metadata={ metadata={
@ -35,14 +49,12 @@ class NodeAddRequest(BaseSchema):
""" """
endpoint_id = fields.Integer( endpoint_id = fields.Integer(
required=False, required=True,
metadata={"example": 1, "description": "采集端ID"}, metadata={"example": 1, "description": "采集端ID"},
load_default=None,
) )
title = fields.String( title = fields.String(
required=False, required=True,
metadata={"example": "节点ID", "descriptrion": "节点ID"}, metadata={"example": "节点ID", "descriptrion": "节点ID"},
load_default=None,
) )
mark = fields.String( mark = fields.String(
required=False, required=False,
@ -55,14 +67,12 @@ class NodeAddRequest(BaseSchema):
load_default=1, load_default=1,
) )
tag_label = fields.String( tag_label = fields.String(
required=False, required=True,
metadata={"example": "变量别名", "description": "变量别名"}, metadata={"example": "变量别名", "description": "变量别名"},
load_default=None,
) )
data_type = fields.String( data_type = fields.String(
required=False, required=True,
metadata={"example": "text", "description": "值类型 text: 文本 int: 整型 float: 浮点型 boolean:布尔型"}, metadata={"example": "text", "description": "值类型 text: 文本 int: 整型 float: 浮点型 boolean:布尔型"},
load_default=None,
) )
is_warning = fields.Integer( is_warning = fields.Integer(
required=False, required=False,
@ -97,14 +107,12 @@ class NodeUpdateRequest(BaseSchema):
""" """
endpoint_id = fields.Integer( endpoint_id = fields.Integer(
required=False, required=True,
metadata={"example": 1, "description": "采集端ID"}, metadata={"example": 1, "description": "采集端ID"},
load_default=None,
) )
title = fields.String( title = fields.String(
required=False, required=True,
metadata={"example": "节点ID", "descriptrion": "节点ID"}, metadata={"example": "节点ID", "descriptrion": "节点ID"},
load_default=None,
) )
mark = fields.String( mark = fields.String(
required=False, required=False,
@ -117,14 +125,12 @@ class NodeUpdateRequest(BaseSchema):
load_default=1, load_default=1,
) )
tag_label = fields.String( tag_label = fields.String(
required=False, required=True,
metadata={"example": "变量别名", "description": "变量别名"}, metadata={"example": "变量别名", "description": "变量别名"},
load_default=None,
) )
data_type = fields.String( data_type = fields.String(
required=False, required=True,
metadata={"example": "text", "description": "值类型 text: 文本 int: 整型 float: 浮点型 boolean:布尔型"}, metadata={"example": "text", "description": "值类型 text: 文本 int: 整型 float: 浮点型 boolean:布尔型"},
load_default=None,
) )
is_warning = fields.Integer( is_warning = fields.Integer(
required=False, required=False,

Loading…
Cancel
Save