From e62107da0796653d624de95583b590d08b206f57 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Thu, 16 Feb 2023 21:21:21 +0800 Subject: [PATCH] Support scada_model.status in API --- api/__init__.py | 1 + api/s29_scada_model.py | 20 ++++++++++++++------ tjnetwork.py | 3 +++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 7c33afd..3dcd269 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -100,6 +100,7 @@ from .s26_labels import get_label_schema, get_label, set_label, add_label, delet from .s27_backdrop import get_backdrop_schema, get_backdrop, set_backdrop from .s29_scada_model import SCADA_TYPE_PRESSURE, SCADA_TYPE_DEMAND, SCADA_TYPE_QUALITY, SCADA_TYPE_LEVEL, SCADA_TYPE_FLOW +from .s29_scada_model import SCADA_STATUS_OFFLINE, SCADA_STATUS_ONLINE from .s29_scada_model import get_scada_model_schema, get_scada_model, set_scada_model, add_scada_model, delete_scada_model from .s30_scada_data import get_scada_data_schema, get_scada_data, set_scada_data diff --git a/api/s29_scada_model.py b/api/s29_scada_model.py index 561914d..825c05d 100644 --- a/api/s29_scada_model.py +++ b/api/s29_scada_model.py @@ -7,6 +7,10 @@ SCADA_TYPE_QUALITY = 'QUALITY' SCADA_TYPE_LEVEL = 'LEVEL' SCADA_TYPE_FLOW = 'FLOW' +SCADA_STATUS_OFFLINE = 'OFF' +SCADA_STATUS_ONLINE = 'ON' + + def _check_model_id(name: str, cs: ChangeSet) -> bool: if 'model_id' not in cs.operations[0]: return True @@ -24,7 +28,8 @@ def get_scada_model_schema(name: str) -> dict[str, dict[str, Any]]: 'device_name' : {'type': 'str' , 'optional': True , 'readonly': False}, 'address' : {'type': 'str' , 'optional': True , 'readonly': False}, 'sm_type' : {'type': 'str' , 'optional': True , 'readonly': False}, - 'model_id' : {'type': 'str' , 'optional': True , 'readonly': False} } + 'model_id' : {'type': 'str' , 'optional': True , 'readonly': False}, + 'status' : {'type': 'str' , 'optional': True , 'readonly': False} } def get_scada_model(name: str, id: str) -> dict[str, Any]: @@ -40,6 +45,7 @@ def get_scada_model(name: str, id: str) -> dict[str, Any]: d['address'] = str(sm['address']) if sm['address'] != None else None d['sm_type'] = str(sm['type']) if sm['type'] != None else None d['model_id'] = str(sm['model_id']) if sm['model_id'] != None else None + d['status'] = str(sm['status']) return d @@ -54,6 +60,7 @@ class ScadaModel(object): self.address = str(input['address']) if 'address' in input and input['address'] != None else None self.sm_type = str(input['sm_type']) if 'sm_type' in input and input['sm_type'] != None else None self.model_id = str(input['model_id']) if 'model_id' in input and input['model_id'] != None else None + self.status = str(input['status']) if 'status' in input and input['status'] != None else SCADA_STATUS_OFFLINE self.f_type = f"'{self.type}'" self.f_id = f"'{self.id}'" @@ -64,9 +71,10 @@ class ScadaModel(object): self.f_address = f"'{self.address}'" if self.address != None else 'null' self.f_sm_type = f"'{self.sm_type}'" if self.sm_type != None else 'null' self.f_model_id = f"'{self.model_id}'" if self.model_id != None else 'null' + self.f_status = f"'{self.status}'" def as_dict(self) -> dict[str, Any]: - return { 'type': self.type, 'id': self.id, 'x': self.x, 'y': self.y, 'device_id': self.device_id, 'device_name': self.device_name, 'address': self.address, 'sm_type': self.sm_type, 'model_id': self.model_id } + return { 'type': self.type, 'id': self.id, 'x': self.x, 'y': self.y, 'device_id': self.device_id, 'device_name': self.device_name, 'address': self.address, 'sm_type': self.sm_type, 'model_id': self.model_id, 'status': self.status } def as_id_dict(self) -> dict[str, Any]: return { 'type': self.type, 'id': self.id } @@ -83,8 +91,8 @@ def set_scada_model_cmd(name: str, cs: ChangeSet) -> DbChangeSet: raw_new[key] = new_dict[key] new = ScadaModel(raw_new) - redo_sql = f"update scada_model set x = {new.f_x}, y = {new.f_y}, device_id = {new.f_device_id}, device_name = {new.f_device_name}, address = {new.f_address}, type = {new.f_sm_type}, model_id = {new.f_model_id} where id = {new.f_id};" - undo_sql = f"update scada_model set x = {old.f_x}, y = {old.f_y}, device_id = {old.f_device_id}, device_name = {old.f_device_name}, address = {old.f_address}, type = {old.f_sm_type}, model_id = {old.f_model_id} where id = {old.f_id};" + redo_sql = f"update scada_model set x = {new.f_x}, y = {new.f_y}, device_id = {new.f_device_id}, device_name = {new.f_device_name}, address = {new.f_address}, type = {new.f_sm_type}, model_id = {new.f_model_id}, status = {new.f_status} where id = {new.f_id};" + undo_sql = f"update scada_model set x = {old.f_x}, y = {old.f_y}, device_id = {old.f_device_id}, device_name = {old.f_device_name}, address = {old.f_address}, type = {old.f_sm_type}, model_id = {old.f_model_id}, status = {old.f_status} where id = {old.f_id};" redo_cs = g_update_prefix | new.as_dict() undo_cs = g_update_prefix | old.as_dict() @@ -103,7 +111,7 @@ def set_scada_model(name: str, cs: ChangeSet) -> ChangeSet: def add_scada_model_cmd(name: str, cs: ChangeSet) -> DbChangeSet: new = ScadaModel(cs.operations[0]) - redo_sql = f"insert into scada_model (id, x, y, device_id, device_name, address, type, model_id) values ({new.f_id}, {new.f_x}, {new.f_y}, {new.f_device_id}, {new.f_device_name}, {new.f_address}, {new.f_sm_type}, {new.f_model_id});" + redo_sql = f"insert into scada_model (id, x, y, device_id, device_name, address, type, model_id, status) values ({new.f_id}, {new.f_x}, {new.f_y}, {new.f_device_id}, {new.f_device_name}, {new.f_address}, {new.f_sm_type}, {new.f_model_id}, {new.f_status});" undo_sql = f"delete from scada_model where id = {new.f_id};" redo_cs = g_add_prefix | new.as_dict() @@ -124,7 +132,7 @@ def delete_scada_model_cmd(name: str, cs: ChangeSet) -> DbChangeSet: old = ScadaModel(get_scada_model(name, cs.operations[0]['id'])) redo_sql = f"delete from scada_model where id = {old.f_id};" - undo_sql = f"insert into scada_model (id, x, y, device_id, device_name, address, type, model_id) values ({old.f_id}, {old.f_x}, {old.f_y}, {old.f_device_id}, {old.f_device_name}, {old.f_address}, {old.f_sm_type}, {old.f_model_id});" + undo_sql = f"insert into scada_model (id, x, y, device_id, device_name, address, type, model_id, status) values ({old.f_id}, {old.f_x}, {old.f_y}, {old.f_device_id}, {old.f_device_name}, {old.f_address}, {old.f_sm_type}, {old.f_model_id}, {old.f_status});" redo_cs = g_delete_prefix | old.as_id_dict() undo_cs = g_add_prefix | old.as_dict() diff --git a/tjnetwork.py b/tjnetwork.py index e9d4fe0..4a47c84 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -101,6 +101,9 @@ SCADA_TYPE_QUALITY = api.SCADA_TYPE_QUALITY SCADA_TYPE_LEVEL = api.SCADA_TYPE_LEVEL SCADA_TYPE_FLOW = api.SCADA_TYPE_FLOW +SCADA_STATUS_ONLINE = api.SCADA_STATUS_ONLINE +SCADA_STATUS_OFFLINE = api.SCADA_STATUS_OFFLINE + ############################################################ # project