Add scada model type to scada element

This commit is contained in:
WQY\qiong
2023-04-06 00:50:44 +08:00
parent 2e4ae1bd9f
commit 02b5ef71f2
6 changed files with 141 additions and 28 deletions

View File

@@ -127,6 +127,7 @@ from .del_cmd import clean_scada_device
from .s30_scada_device_data import get_scada_device_data_schema, get_scada_device_data, set_scada_device_data, add_scada_device_data, delete_scada_device_data
from .del_cmd import clean_scada_device_data
from .s31_scada_element import SCADA_MODEL_TYPE_JUNCTION, SCADA_MODEL_TYPE_RESERVOIR, SCADA_MODEL_TYPE_TANK, SCADA_MODEL_TYPE_PIPE, SCADA_MODEL_TYPE_PUMP, SCADA_MODEL_TYPE_VALVE
from .s31_scada_element import SCADA_ELEMENT_STATUS_OFFLINE, SCADA_ELEMENT_STATUS_ONLINE
from .s31_scada_element import get_scada_element_schema, get_scada_elements, get_scada_element, set_scada_element, add_scada_element, delete_scada_element
from .del_cmd import clean_scada_element

View File

@@ -1,5 +1,5 @@
from .database import *
from .s0_base import is_node, is_link
from .s0_base import *
SCADA_TYPE_PRESSURE = 'PRESSURE'
@@ -9,17 +9,49 @@ SCADA_TYPE_LEVEL = 'LEVEL'
SCADA_TYPE_FLOW = 'FLOW'
SCADA_MODEL_TYPE_JUNCTION = 'JUNCTION'
SCADA_MODEL_TYPE_RESERVOIR = 'RESERVOIR'
SCADA_MODEL_TYPE_TANK = 'TANK'
SCADA_MODEL_TYPE_PIPE = 'PIPE'
SCADA_MODEL_TYPE_PUMP = 'PUMP'
SCADA_MODEL_TYPE_VALVE = 'VALVE'
SCADA_ELEMENT_STATUS_OFFLINE = 'OFF'
SCADA_ELEMENT_STATUS_ONLINE = 'ON'
def _check_model_id(name: str, cs: ChangeSet) -> bool:
if 'model_id' not in cs.operations[0]:
_scada_model_types = [SCADA_MODEL_TYPE_JUNCTION, SCADA_MODEL_TYPE_RESERVOIR, SCADA_MODEL_TYPE_TANK, SCADA_MODEL_TYPE_PIPE, SCADA_MODEL_TYPE_PUMP, SCADA_MODEL_TYPE_VALVE]
def _check_model(name: str, cs: ChangeSet) -> bool:
has_model_id = 'model_id' in cs.operations[0]
has_model_type = 'model_type' in cs.operations[0]
if has_model_id and has_model_type:
pass
elif has_model_id and not has_model_type:
return False
elif not has_model_id and has_model_type:
return False
elif not has_model_id and not has_model_type:
return True
if cs.operations[0]['model_id'] == None:
return True
model_id = cs.operations[0]['model_id']
return is_node(name, model_id) or is_link(name, model_id)
_model_id = cs.operations[0]['model_id']
_model_type = cs.operations[0]['model_type']
if _model_type == SCADA_MODEL_TYPE_JUNCTION:
return is_junction(name, _model_id)
elif _model_type == SCADA_MODEL_TYPE_RESERVOIR:
return is_reservoir(name, _model_id)
elif _model_type == SCADA_MODEL_TYPE_TANK:
return is_tank(name, _model_id)
elif _model_type == SCADA_MODEL_TYPE_PIPE:
return is_pipe(name, _model_id)
elif _model_type == SCADA_MODEL_TYPE_PUMP:
return is_pump(name, _model_id)
elif _model_type == SCADA_MODEL_TYPE_VALVE:
return is_valve(name, _model_id)
return False
def get_scada_element_schema(name: str) -> dict[str, dict[str, Any]]:
@@ -28,6 +60,7 @@ def get_scada_element_schema(name: str) -> dict[str, dict[str, Any]]:
'y' : {'type': 'float' , 'optional': False , 'readonly': False},
'device_id' : {'type': 'str' , 'optional': True , 'readonly': False},
'model_id' : {'type': 'str' , 'optional': True , 'readonly': False},
'model_type' : {'type': 'str' , 'optional': True , 'readonly': False},
'status' : {'type': 'str' , 'optional': True , 'readonly': False} }
@@ -49,6 +82,7 @@ def get_scada_element(name: str, id: str) -> dict[str, Any]:
d['y'] = float(sm['y'])
d['device_id'] = str(sm['device_id']) if sm['device_id'] != None else None
d['model_id'] = str(sm['model_id']) if sm['model_id'] != None else None
d['model_type'] = str(sm['model_type']) if sm['model_type'] != None else None
d['status'] = str(sm['status'])
return d
@@ -61,6 +95,7 @@ class ScadaModel(object):
self.y = float(input['y'])
self.device_id = str(input['device_id']) if 'device_id' in input and input['device_id'] != None else None
self.model_id = str(input['model_id']) if 'model_id' in input and input['model_id'] != None else None
self.model_type = str(input['model_type']) if 'model_type' in input and input['model_type'] != None else None
self.status = str(input['status']) if 'status' in input and input['status'] != None else SCADA_ELEMENT_STATUS_OFFLINE
self.f_type = f"'{self.type}'"
@@ -69,10 +104,11 @@ class ScadaModel(object):
self.f_y = self.y
self.f_device_id = f"'{self.device_id}'" if self.device_id != None else 'null'
self.f_model_id = f"'{self.model_id}'" if self.model_id != None else 'null'
self.f_model_type = f"'{self.model_type}'" if self.model_type != 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, 'model_id': self.model_id, 'status': self.status }
return { 'type': self.type, 'id': self.id, 'x': self.x, 'y': self.y, 'device_id': self.device_id, 'model_id': self.model_id, 'model_type': self.model_type, 'status': self.status }
def as_id_dict(self) -> dict[str, Any]:
return { 'type': self.type, 'id': self.id }
@@ -89,8 +125,8 @@ def set_scada_element_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
raw_new[key] = new_dict[key]
new = ScadaModel(raw_new)
redo_sql = f"update scada_element set x = {new.f_x}, y = {new.f_y}, device_id = {new.f_device_id}, model_id = {new.f_model_id}, status = {new.f_status} where id = {new.f_id};"
undo_sql = f"update scada_element set x = {old.f_x}, y = {old.f_y}, device_id = {old.f_device_id}, model_id = {old.f_model_id}, status = {old.f_status} where id = {old.f_id};"
redo_sql = f"update scada_element set x = {new.f_x}, y = {new.f_y}, device_id = {new.f_device_id}, model_id = {new.f_model_id}, model_type = {new.f_model_type}, status = {new.f_status} where id = {new.f_id};"
undo_sql = f"update scada_element set x = {old.f_x}, y = {old.f_y}, device_id = {old.f_device_id}, model_id = {old.f_model_id}, model_type = {old.f_model_type}, 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()
@@ -101,7 +137,7 @@ def set_scada_element_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
def set_scada_element(name: str, cs: ChangeSet) -> ChangeSet:
if get_scada_element(name, cs.operations[0]['id']) == {}:
return ChangeSet()
if _check_model_id(name, cs) == False:
if _check_model(name, cs) == False:
return ChangeSet()
return execute_command(name, set_scada_element_cmd(name, cs))
@@ -109,7 +145,7 @@ def set_scada_element(name: str, cs: ChangeSet) -> ChangeSet:
def add_scada_element_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
new = ScadaModel(cs.operations[0])
redo_sql = f"insert into scada_element (id, x, y, device_id, model_id, status) values ({new.f_id}, {new.f_x}, {new.f_y}, {new.f_device_id}, {new.f_model_id}, {new.f_status});"
redo_sql = f"insert into scada_element (id, x, y, device_id, model_id, model_type, status) values ({new.f_id}, {new.f_x}, {new.f_y}, {new.f_device_id}, {new.f_model_id}, {new.f_model_type}, {new.f_status});"
undo_sql = f"delete from scada_element where id = {new.f_id};"
redo_cs = g_add_prefix | new.as_dict()
@@ -121,7 +157,7 @@ def add_scada_element_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
def add_scada_element(name: str, cs: ChangeSet) -> ChangeSet:
if get_scada_element(name, cs.operations[0]['id']) != {}:
return ChangeSet()
if _check_model_id(name, cs) == False:
if _check_model(name, cs) == False:
return ChangeSet()
return execute_command(name, add_scada_element_cmd(name, cs))
@@ -130,7 +166,7 @@ def delete_scada_element_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
old = ScadaModel(get_scada_element(name, cs.operations[0]['id']))
redo_sql = f"delete from scada_element where id = {old.f_id};"
undo_sql = f"insert into scada_element (id, x, y, device_id, model_id, status) values ({old.f_id}, {old.f_x}, {old.f_y}, {old.f_device_id}, {old.f_model_id}, {old.f_status});"
undo_sql = f"insert into scada_element (id, x, y, device_id, model_id, model_type, status) values ({old.f_id}, {old.f_x}, {old.f_y}, {old.f_device_id}, {old.f_model_id}, {old.f_model_type}, {old.f_status});"
redo_cs = g_delete_prefix | old.as_id_dict()
undo_cs = g_add_prefix | old.as_dict()