diff --git a/iti/applications/models/iot/iot_device.py b/iti/applications/models/iot/iot_device.py index ade536b..51cbd39 100644 --- a/iti/applications/models/iot/iot_device.py +++ b/iti/applications/models/iot/iot_device.py @@ -56,8 +56,5 @@ class IotDeviceSimpleSchema(BaseSchema): class Meta: name = "IotDevice" - id = Integer() device_name = String() - device_number = String() - #关系 - workshop = Nested("IotWorkshopSimpleSchema") \ No newline at end of file + device_number = String() \ No newline at end of file diff --git a/iti/applications/models/iot/iot_endpoint.py b/iti/applications/models/iot/iot_endpoint.py index faba33d..1bf253a 100644 --- a/iti/applications/models/iot/iot_endpoint.py +++ b/iti/applications/models/iot/iot_endpoint.py @@ -16,6 +16,7 @@ class IotEndpoint(db.Model, TimeModelMixin): autoincrement=True, 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_name = db.Column(db.String(255), nullable=False, unique=True, 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:在线") 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( "IotDevice", primaryjoin="foreign(IotEndpoint.device_id) == IotDevice.id", @@ -41,6 +46,7 @@ class IotEndpointSchema(BaseSchema): name = "IotEndpoint" id = Integer() + workshop_id = Integer() device_id = Integer() endpoint_name = String() endpoint_number = String() @@ -54,6 +60,7 @@ class IotEndpointSchema(BaseSchema): created_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") @@ -64,8 +71,5 @@ class IotEndpointSimpleSchema(BaseSchema): class Meta: name = "IotEndpoint" - id = Integer() endpoint_name = String() - endpoint_number = String() - #关系 - device = Nested("IotDeviceSimpleSchema") \ No newline at end of file + endpoint_number = String() \ No newline at end of file diff --git a/iti/applications/models/iot/iot_node.py b/iti/applications/models/iot/iot_node.py index e90e174..087d8c0 100644 --- a/iti/applications/models/iot/iot_node.py +++ b/iti/applications/models/iot/iot_node.py @@ -16,6 +16,8 @@ class IotNode(db.Model, TimeModelMixin): autoincrement=True, 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") title = db.Column(db.String(255), nullable=False, unique=True, comment="节点ID") 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="方法节点") 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( "IotEndpoint", primaryjoin="foreign(IotNode.endpoint_id) == IotEndpoint.id", @@ -43,6 +53,8 @@ class IotNodeSchema(BaseSchema): name = "IotNode" id = Integer() + workshop_id = Integer() + device_id = Integer() endpoint_id = Integer() title = String() mark = String() @@ -58,4 +70,6 @@ class IotNodeSchema(BaseSchema): created_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") \ No newline at end of file diff --git a/iti/applications/models/iot/iot_workshop.py b/iti/applications/models/iot/iot_workshop.py index 1f0dbd3..b681c31 100644 --- a/iti/applications/models/iot/iot_workshop.py +++ b/iti/applications/models/iot/iot_workshop.py @@ -47,6 +47,5 @@ class IotWorkshopSimpleSchema(BaseSchema): class Meta: name = "IotWorkshop" - id = Integer() workshop_name = String() workshop_number = String() \ No newline at end of file diff --git a/iti/applications/routes/iot/endpoint_ctl.py b/iti/applications/routes/iot/endpoint_ctl.py index 60e357f..bdd1d52 100644 --- a/iti/applications/routes/iot/endpoint_ctl.py +++ b/iti/applications/routes/iot/endpoint_ctl.py @@ -2,6 +2,7 @@ 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, IotEndpoint, IotEndpointSchema, ) @@ -59,6 +60,10 @@ def add_endpoint(json_data: dict): """ 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 db.session.add(endpoint) 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: raise BizException("采集端信息不存在") 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: raise BizException("采集端不存在") @@ -117,6 +128,8 @@ def get_page(query_data: EndpointQuery): ) if 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: 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: kw = ModelFilter.escape_like(query_data.keyword) query = query.filter( @@ -136,6 +149,8 @@ def get_list(query_data: EndpointQuery): ) if 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: query = query.filter(IotEndpoint.status == query_data.status) diff --git a/iti/applications/routes/iot/node_ctl.py b/iti/applications/routes/iot/node_ctl.py index 2b8ffe6..aaf3818 100644 --- a/iti/applications/routes/iot/node_ctl.py +++ b/iti/applications/routes/iot/node_ctl.py @@ -2,6 +2,7 @@ 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 ( + IotEndpoint, IotNode, IotNodeSchema, ) @@ -59,6 +60,14 @@ def add_node(json_data: dict): """ 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 db.session.add(node) 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: raise BizException("节点信息不存在") 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: raise BizException("采集节点不存在") @@ -112,6 +127,10 @@ def get_page(query_data: NodeQuery): query = select(IotNode).order_by(IotNode.created_at.desc()) if 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: 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: 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: query = query.filter(IotNode.status == query_data.status) return db.session.scalars(query).all() \ No newline at end of file diff --git a/iti/applications/routes/iot/schemas/endpoint.py b/iti/applications/routes/iot/schemas/endpoint.py index c9889d2..c36b66e 100644 --- a/iti/applications/routes/iot/schemas/endpoint.py +++ b/iti/applications/routes/iot/schemas/endpoint.py @@ -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( default=None, metadata={ @@ -44,19 +51,16 @@ class EndpointAddRequest(BaseSchema): """ device_id = fields.Integer( - required=False, + required=True, metadata={"example": 1, "description": "设备ID"}, - load_default=None, ) endpoint_name = fields.String( - required=False, + required=True, metadata={"example": "采集端名称", "descriptrion": "采集端名称"}, - load_default=None, ) endpoint_number = fields.String( - required=False, + required=True, metadata={"example": "采集端编号", "description": "采集端编号"}, - load_default=None, ) description = fields.String( required=False, @@ -64,14 +68,12 @@ class EndpointAddRequest(BaseSchema): load_default=None, ) ip = fields.String( - required=False, + required=True, metadata={"example": "采集端IP", "description": "采集端IP"}, - load_default=None, ) port = fields.String( - required=False, + required=True, metadata={"example": "采集端端口", "description": "采集端端口"}, - load_default=None, ) brand_name = fields.String( required=False, @@ -91,19 +93,16 @@ class EndpointUpdateRequest(BaseSchema): """ device_id = fields.Integer( - required=False, + required=True, metadata={"example": 1, "description": "设备ID"}, - load_default=None, ) endpoint_name = fields.String( - required=False, + required=True, metadata={"example": "采集端名称", "descriptrion": "采集端名称"}, - load_default=None, ) endpoint_number = fields.String( - required=False, + required=True, metadata={"example": "采集端编号", "description": "采集端编号"}, - load_default=None, ) description = fields.String( required=False, @@ -111,14 +110,12 @@ class EndpointUpdateRequest(BaseSchema): load_default=None, ) ip = fields.String( - required=False, + required=True, metadata={"example": "采集端IP", "description": "采集端IP"}, - load_default=None, ) port = fields.String( - required=False, + required=True, metadata={"example": "采集端端口", "description": "采集端端口"}, - load_default=None, ) brand_name = fields.String( required=False, diff --git a/iti/applications/routes/iot/schemas/node.py b/iti/applications/routes/iot/schemas/node.py index 39c41b4..e62f5d1 100644 --- a/iti/applications/routes/iot/schemas/node.py +++ b/iti/applications/routes/iot/schemas/node.py @@ -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( default=None, metadata={ @@ -35,14 +49,12 @@ class NodeAddRequest(BaseSchema): """ endpoint_id = fields.Integer( - required=False, + required=True, metadata={"example": 1, "description": "采集端ID"}, - load_default=None, ) title = fields.String( - required=False, + required=True, metadata={"example": "节点ID", "descriptrion": "节点ID"}, - load_default=None, ) mark = fields.String( required=False, @@ -55,14 +67,12 @@ class NodeAddRequest(BaseSchema): load_default=1, ) tag_label = fields.String( - required=False, + required=True, metadata={"example": "变量别名", "description": "变量别名"}, - load_default=None, ) data_type = fields.String( - required=False, + required=True, metadata={"example": "text", "description": "值类型 text: 文本 int: 整型 float: 浮点型 boolean:布尔型"}, - load_default=None, ) is_warning = fields.Integer( required=False, @@ -97,14 +107,12 @@ class NodeUpdateRequest(BaseSchema): """ endpoint_id = fields.Integer( - required=False, + required=True, metadata={"example": 1, "description": "采集端ID"}, - load_default=None, ) title = fields.String( - required=False, + required=True, metadata={"example": "节点ID", "descriptrion": "节点ID"}, - load_default=None, ) mark = fields.String( required=False, @@ -117,14 +125,12 @@ class NodeUpdateRequest(BaseSchema): load_default=1, ) tag_label = fields.String( - required=False, + required=True, metadata={"example": "变量别名", "description": "变量别名"}, - load_default=None, ) data_type = fields.String( - required=False, + required=True, metadata={"example": "text", "description": "值类型 text: 文本 int: 整型 float: 浮点型 boolean:布尔型"}, - load_default=None, ) is_warning = fields.Integer( required=False,