Accept Merge Request #164: (api -> master)
Merge Request: Refine scada api Created By: @王琼钰 Accepted By: @王琼钰 URL: https://tjwater.coding.net/p/tjwatercloud/d/TJWaterServer/git/merge/164?initial=true
This commit is contained in:
@@ -104,8 +104,13 @@ 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 .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_device import SCADA_DEVICE_TYPE_PRESSURE, SCADA_DEVICE_TYPE_DEMAND, SCADA_DEVICE_TYPE_QUALITY, SCADA_DEVICE_TYPE_LEVEL, SCADA_DEVICE_TYPE_FLOW
|
||||||
from .s29_scada_model import SCADA_STATUS_OFFLINE, SCADA_STATUS_ONLINE
|
from .s29_scada_device import get_scada_device_schema, get_scada_device, set_scada_device, add_scada_device, delete_scada_device
|
||||||
from .s29_scada_model import get_scada_model_schema, get_scada_model, set_scada_model, add_scada_model, delete_scada_model
|
from .del_cmd import clean_scada_device
|
||||||
|
|
||||||
from .s30_scada_data import get_scada_data_schema, get_scada_data, set_scada_data, add_scada_data, delete_scada_data
|
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_ELEMENT_STATUS_OFFLINE, SCADA_ELEMENT_STATUS_ONLINE
|
||||||
|
from .s31_scada_element import get_scada_element_schema, get_scada_element, set_scada_element, add_scada_element, delete_scada_element
|
||||||
|
from .del_cmd import clean_scada_element
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ from .s25_vertices import set_vertex_cmd, add_vertex_cmd, delete_vertex_cmd
|
|||||||
from .s26_labels import set_label_cmd, add_label_cmd, delete_label_cmd
|
from .s26_labels import set_label_cmd, add_label_cmd, delete_label_cmd
|
||||||
from .s27_backdrop import set_backdrop_cmd
|
from .s27_backdrop import set_backdrop_cmd
|
||||||
# from .s28_end import *
|
# from .s28_end import *
|
||||||
|
from .s29_scada_device import set_scada_device_cmd, add_scada_device_cmd, delete_scada_device_cmd
|
||||||
|
from .s30_scada_device_data import set_scada_device_data_cmd, add_scada_device_data_cmd, delete_scada_device_data_cmd
|
||||||
|
from .s31_scada_element import set_scada_element_cmd, add_scada_element_cmd, delete_scada_element_cmd
|
||||||
from .del_cmd_raw import del_cascade_cmd
|
from .del_cmd_raw import del_cascade_cmd
|
||||||
|
|
||||||
|
|
||||||
@@ -96,6 +99,12 @@ def add_cmd(name: str, cs: ChangeSet) -> DbChangeSet | None:
|
|||||||
return None
|
return None
|
||||||
elif type == s28_end:
|
elif type == s28_end:
|
||||||
return None
|
return None
|
||||||
|
elif type == s29_scada_device:
|
||||||
|
return add_scada_device_cmd(name, cs)
|
||||||
|
elif type == s30_scada_device_data:
|
||||||
|
return add_scada_device_data_cmd(name, cs)
|
||||||
|
elif type == s31_scada_element:
|
||||||
|
return add_scada_element_cmd(name, cs)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -165,6 +174,12 @@ def set_cmd(name: str, cs: ChangeSet) -> DbChangeSet | None:
|
|||||||
return set_backdrop_cmd(name, cs)
|
return set_backdrop_cmd(name, cs)
|
||||||
elif type == s28_end: # end
|
elif type == s28_end: # end
|
||||||
return None
|
return None
|
||||||
|
elif type == s29_scada_device:
|
||||||
|
return set_scada_device_cmd(name, cs)
|
||||||
|
elif type == s30_scada_device_data:
|
||||||
|
return set_scada_device_data_cmd(name, cs)
|
||||||
|
elif type == s31_scada_element:
|
||||||
|
return set_scada_element_cmd(name, cs)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -234,6 +249,12 @@ def del_cmd(name: str, cs: ChangeSet) -> DbChangeSet | None:
|
|||||||
return None
|
return None
|
||||||
elif type == s28_end:
|
elif type == s28_end:
|
||||||
return None
|
return None
|
||||||
|
elif type == s29_scada_device:
|
||||||
|
return delete_scada_device_cmd(name, cs)
|
||||||
|
elif type == s30_scada_device_data:
|
||||||
|
return delete_scada_device_data_cmd(name, cs)
|
||||||
|
elif type == s31_scada_element:
|
||||||
|
return delete_scada_element_cmd(name, cs)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ from .s25_vertices import set_vertex, add_vertex, delete_vertex
|
|||||||
from .s26_labels import set_label, add_label, delete_label
|
from .s26_labels import set_label, add_label, delete_label
|
||||||
from .s27_backdrop import set_backdrop
|
from .s27_backdrop import set_backdrop
|
||||||
# from .s28_end import *
|
# from .s28_end import *
|
||||||
|
from .s29_scada_device import set_scada_device, add_scada_device, delete_scada_device
|
||||||
|
from .s30_scada_device_data import set_scada_device_data, add_scada_device_data, delete_scada_device_data
|
||||||
|
from .s31_scada_element import set_scada_element, add_scada_element, delete_scada_element
|
||||||
from .del_cmd_raw import del_cascade_cmd
|
from .del_cmd_raw import del_cascade_cmd
|
||||||
|
|
||||||
|
|
||||||
@@ -96,6 +99,12 @@ def execute_add_command(name: str, cs: ChangeSet) -> ChangeSet:
|
|||||||
return ChangeSet()
|
return ChangeSet()
|
||||||
elif type == s28_end:
|
elif type == s28_end:
|
||||||
return ChangeSet()
|
return ChangeSet()
|
||||||
|
elif type == s29_scada_device:
|
||||||
|
return add_scada_device(name, cs)
|
||||||
|
elif type == s30_scada_device_data:
|
||||||
|
return add_scada_device_data(name, cs)
|
||||||
|
elif type == s31_scada_element:
|
||||||
|
return add_scada_element(name, cs)
|
||||||
|
|
||||||
return ChangeSet()
|
return ChangeSet()
|
||||||
|
|
||||||
@@ -165,6 +174,12 @@ def execute_update_command(name: str, cs: ChangeSet) -> ChangeSet:
|
|||||||
return set_backdrop(name, cs)
|
return set_backdrop(name, cs)
|
||||||
elif type == s28_end: # end
|
elif type == s28_end: # end
|
||||||
return ChangeSet()
|
return ChangeSet()
|
||||||
|
elif type == s29_scada_device:
|
||||||
|
return set_scada_device(name, cs)
|
||||||
|
elif type == s30_scada_device_data:
|
||||||
|
return set_scada_device_data(name, cs)
|
||||||
|
elif type == s31_scada_element:
|
||||||
|
return set_scada_element(name, cs)
|
||||||
|
|
||||||
return ChangeSet()
|
return ChangeSet()
|
||||||
|
|
||||||
@@ -234,6 +249,12 @@ def execute_delete_command(name: str, cs: ChangeSet) -> ChangeSet:
|
|||||||
return ChangeSet()
|
return ChangeSet()
|
||||||
elif type == s28_end:
|
elif type == s28_end:
|
||||||
return ChangeSet()
|
return ChangeSet()
|
||||||
|
elif type == s29_scada_device:
|
||||||
|
return delete_scada_device(name, cs)
|
||||||
|
elif type == s30_scada_device_data:
|
||||||
|
return delete_scada_device_data(name, cs)
|
||||||
|
elif type == s31_scada_element:
|
||||||
|
return delete_scada_element(name, cs)
|
||||||
|
|
||||||
return ChangeSet()
|
return ChangeSet()
|
||||||
|
|
||||||
|
|||||||
@@ -180,19 +180,22 @@ def take_snapshot_for_operation(name: str, operation: int, tag: str) -> None:
|
|||||||
write(name, f"insert into snapshot_operation (id, tag) values ({operation}, '{tag}')")
|
write(name, f"insert into snapshot_operation (id, tag) values ({operation}, '{tag}')")
|
||||||
|
|
||||||
|
|
||||||
def take_snapshot_for_current_operation(name: str, tag: str) -> int | None:
|
def take_snapshot_for_current_operation(name: str, tag: str) -> None:
|
||||||
take_snapshot_for_operation(name, get_current_operation(name), tag)
|
take_snapshot_for_operation(name, get_current_operation(name), tag)
|
||||||
|
|
||||||
|
|
||||||
# deprecated ! use take_snapshot_for_current_operation instead
|
# deprecated ! use take_snapshot_for_current_operation instead
|
||||||
def take_snapshot(name: str, tag: str) -> int | None:
|
def take_snapshot(name: str, tag: str) -> None:
|
||||||
take_snapshot_for_current_operation(name, tag)
|
take_snapshot_for_current_operation(name, tag)
|
||||||
|
|
||||||
|
|
||||||
def update_snapshot(name: str, operation: int, tag: str) -> None:
|
def update_snapshot(name: str, operation: int, tag: str) -> None:
|
||||||
if tag == None or tag == '':
|
if tag == None or tag == '':
|
||||||
return None
|
return None
|
||||||
write(name, f"update snapshot_operation set tag = '{tag}' where id = {operation}")
|
if have_snapshot_for_operation(name, operation):
|
||||||
|
write(name, f"update snapshot_operation set tag = '{tag}' where id = {operation}")
|
||||||
|
else:
|
||||||
|
take_snapshot_for_operation(name, operation, tag)
|
||||||
|
|
||||||
|
|
||||||
def update_snapshot_for_current_operation(name: str, tag: str) -> None:
|
def update_snapshot_for_current_operation(name: str, tag: str) -> None:
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
from .del_cmd_raw import *
|
from .del_cmd_raw import *
|
||||||
from .batch_cmd import execute_batch_command
|
from .batch_cmd import execute_batch_command
|
||||||
|
from .s29_scada_device import clean_scada_device_cmd
|
||||||
|
from .s30_scada_device_data import clean_scada_device_data_cmd
|
||||||
|
from .s31_scada_element import clean_scada_element_cmd
|
||||||
|
|
||||||
|
|
||||||
def delete_junction_cascade(name: str, cs: ChangeSet) -> ChangeSet:
|
def delete_junction_cascade(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
@@ -48,3 +51,15 @@ def delete_curve_cascade(name: str, cs: ChangeSet) -> ChangeSet:
|
|||||||
cs.operations[0] |= { 'operation' : API_DELETE, 'type' : 'curve' }
|
cs.operations[0] |= { 'operation' : API_DELETE, 'type' : 'curve' }
|
||||||
#raw_cmd = delete_curve_cascade_batch_cmd(name, cs)
|
#raw_cmd = delete_curve_cascade_batch_cmd(name, cs)
|
||||||
return execute_batch_command(name, cs)
|
return execute_batch_command(name, cs)
|
||||||
|
|
||||||
|
|
||||||
|
def clean_scada_device(name: str) -> ChangeSet:
|
||||||
|
return execute_batch_command(name, clean_scada_device_cmd(name))
|
||||||
|
|
||||||
|
|
||||||
|
def clean_scada_device_data(name: str) -> ChangeSet:
|
||||||
|
return execute_batch_command(name, clean_scada_device_data_cmd(name))
|
||||||
|
|
||||||
|
|
||||||
|
def clean_scada_element(name: str) -> ChangeSet:
|
||||||
|
return execute_batch_command(name, clean_scada_element_cmd(name))
|
||||||
|
|||||||
120
api/s29_scada_device.py
Normal file
120
api/s29_scada_device.py
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
from .database import *
|
||||||
|
|
||||||
|
|
||||||
|
SCADA_DEVICE_TYPE_PRESSURE = 'PRESSURE'
|
||||||
|
SCADA_DEVICE_TYPE_DEMAND = 'DEMAND'
|
||||||
|
SCADA_DEVICE_TYPE_QUALITY = 'QUALITY'
|
||||||
|
SCADA_DEVICE_TYPE_LEVEL = 'LEVEL'
|
||||||
|
SCADA_DEVICE_TYPE_FLOW = 'FLOW'
|
||||||
|
|
||||||
|
|
||||||
|
def get_scada_device_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||||
|
return { 'id' : {'type': 'str', 'optional': False, 'readonly': True },
|
||||||
|
'name' : {'type': 'str', 'optional': True , 'readonly': False},
|
||||||
|
'address': {'type': 'str', 'optional': True , 'readonly': False},
|
||||||
|
'sd_type': {'type': 'str', 'optional': True , 'readonly': False}}
|
||||||
|
|
||||||
|
|
||||||
|
def get_scada_device(name: str, id: str) -> dict[str, Any]:
|
||||||
|
sm = try_read(name, f"select * from scada_device where id = '{id}'")
|
||||||
|
if sm == None:
|
||||||
|
return {}
|
||||||
|
d = {}
|
||||||
|
d['id'] = str(sm['id'])
|
||||||
|
d['name'] = str(sm['name']) if sm['name'] != None else None
|
||||||
|
d['address'] = str(sm['address']) if sm['address'] != None else None
|
||||||
|
d['sd_type'] = str(sm['type']) if sm['type'] != None else None
|
||||||
|
return d
|
||||||
|
|
||||||
|
|
||||||
|
class ScadaDevice(object):
|
||||||
|
def __init__(self, input: dict[str, Any]) -> None:
|
||||||
|
self.type = 'scada_device'
|
||||||
|
self.id = str(input['id'])
|
||||||
|
self.name = str(input['name']) if 'name' in input and input['name'] != None else None
|
||||||
|
self.address = str(input['address']) if 'address' in input and input['address'] != None else None
|
||||||
|
self.sd_type = str(input['sd_type']) if 'sd_type' in input and input['sd_type'] != None else None
|
||||||
|
|
||||||
|
self.f_type = f"'{self.type}'"
|
||||||
|
self.f_id = f"'{self.id}'"
|
||||||
|
self.f_name = f"'{self.name}'" if self.name != None else 'null'
|
||||||
|
self.f_address = f"'{self.address}'" if self.address != None else 'null'
|
||||||
|
self.f_sd_type = f"'{self.sd_type}'" if self.sd_type != None else 'null'
|
||||||
|
|
||||||
|
def as_dict(self) -> dict[str, Any]:
|
||||||
|
return { 'type': self.type, 'id': self.id, 'name': self.name, 'address': self.address, 'sd_type': self.sd_type }
|
||||||
|
|
||||||
|
def as_id_dict(self) -> dict[str, Any]:
|
||||||
|
return { 'type': self.type, 'id': self.id }
|
||||||
|
|
||||||
|
|
||||||
|
def set_scada_device_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
|
||||||
|
old = ScadaDevice(get_scada_device(name, cs.operations[0]['id']))
|
||||||
|
raw_new = get_scada_device(name, cs.operations[0]['id'])
|
||||||
|
|
||||||
|
new_dict = cs.operations[0]
|
||||||
|
schema = get_scada_device_schema(name)
|
||||||
|
for key, value in schema.items():
|
||||||
|
if key in new_dict and not value['readonly']:
|
||||||
|
raw_new[key] = new_dict[key]
|
||||||
|
new = ScadaDevice(raw_new)
|
||||||
|
|
||||||
|
redo_sql = f"update scada_device set name = {new.f_name}, address = {new.f_address}, type = {new.f_sd_type} where id = {new.f_id};"
|
||||||
|
undo_sql = f"update scada_device set name = {old.f_name}, address = {old.f_address}, type = {old.f_sd_type} where id = {old.f_id};"
|
||||||
|
|
||||||
|
redo_cs = g_update_prefix | new.as_dict()
|
||||||
|
undo_cs = g_update_prefix | old.as_dict()
|
||||||
|
|
||||||
|
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
||||||
|
|
||||||
|
|
||||||
|
def set_scada_device(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
|
if get_scada_device(name, cs.operations[0]['id']) == {}:
|
||||||
|
return ChangeSet()
|
||||||
|
return execute_command(name, set_scada_device_cmd(name, cs))
|
||||||
|
|
||||||
|
|
||||||
|
def add_scada_device_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
|
||||||
|
new = ScadaDevice(cs.operations[0])
|
||||||
|
|
||||||
|
redo_sql = f"insert into scada_device (id, name, address, type) values ({new.f_id}, {new.f_name}, {new.f_address}, {new.f_sd_type});"
|
||||||
|
undo_sql = f"delete from scada_device where id = {new.f_id};"
|
||||||
|
|
||||||
|
redo_cs = g_add_prefix | new.as_dict()
|
||||||
|
undo_cs = g_delete_prefix | new.as_id_dict()
|
||||||
|
|
||||||
|
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
||||||
|
|
||||||
|
|
||||||
|
def add_scada_device(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
|
if get_scada_device(name, cs.operations[0]['id']) != {}:
|
||||||
|
return ChangeSet()
|
||||||
|
return execute_command(name, add_scada_device_cmd(name, cs))
|
||||||
|
|
||||||
|
|
||||||
|
def delete_scada_device_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
|
||||||
|
old = ScadaDevice(get_scada_device(name, cs.operations[0]['id']))
|
||||||
|
|
||||||
|
redo_sql = f"delete from scada_device where id = {old.f_id};"
|
||||||
|
undo_sql = f"insert into scada_device (id, name, address, type) values ({old.f_id}, {old.f_name}, {old.f_address}, {old.f_sd_type});"
|
||||||
|
|
||||||
|
redo_cs = g_delete_prefix | old.as_id_dict()
|
||||||
|
undo_cs = g_add_prefix | old.as_dict()
|
||||||
|
|
||||||
|
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
||||||
|
|
||||||
|
|
||||||
|
def delete_scada_device(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
|
if get_scada_device(name, cs.operations[0]['id']) == {}:
|
||||||
|
return ChangeSet()
|
||||||
|
return execute_command(name, delete_scada_device_cmd(name, cs))
|
||||||
|
|
||||||
|
|
||||||
|
def clean_scada_device_cmd(name: str) -> ChangeSet:
|
||||||
|
cs = ChangeSet()
|
||||||
|
|
||||||
|
rows = read_all(name, 'select id from scada_device acs')
|
||||||
|
for row in rows:
|
||||||
|
cs.delete({ 'type': 'scada_device', 'id': row['id'] })
|
||||||
|
|
||||||
|
return cs
|
||||||
@@ -1,146 +0,0 @@
|
|||||||
from .database import *
|
|
||||||
from .s0_base import is_node, is_link
|
|
||||||
|
|
||||||
SCADA_TYPE_PRESSURE = 'PRESSURE'
|
|
||||||
SCADA_TYPE_DEMAND = 'DEMAND'
|
|
||||||
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
|
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
def get_scada_model_schema(name: str) -> dict[str, dict[str, Any]]:
|
|
||||||
return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True },
|
|
||||||
'x' : {'type': 'float' , 'optional': False , 'readonly': False},
|
|
||||||
'y' : {'type': 'float' , 'optional': False , 'readonly': False},
|
|
||||||
'device_id' : {'type': 'str' , 'optional': False , 'readonly': False},
|
|
||||||
'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},
|
|
||||||
'status' : {'type': 'str' , 'optional': True , 'readonly': False} }
|
|
||||||
|
|
||||||
|
|
||||||
def get_scada_model(name: str, id: str) -> dict[str, Any]:
|
|
||||||
sm = try_read(name, f"select * from scada_model where id = '{id}'")
|
|
||||||
if sm == None:
|
|
||||||
return {}
|
|
||||||
d = {}
|
|
||||||
d['id'] = str(sm['id'])
|
|
||||||
d['x'] = float(sm['x'])
|
|
||||||
d['y'] = float(sm['y'])
|
|
||||||
d['device_id'] = str(sm['device_id'])
|
|
||||||
d['device_name'] = str(sm['device_name']) if sm['device_name'] != None else None
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
class ScadaModel(object):
|
|
||||||
def __init__(self, input: dict[str, Any]) -> None:
|
|
||||||
self.type = 'scada_model'
|
|
||||||
self.id = str(input['id'])
|
|
||||||
self.x = float(input['x'])
|
|
||||||
self.y = float(input['y'])
|
|
||||||
self.device_id = str(input['device_id'])
|
|
||||||
self.device_name = str(input['device_name']) if 'device_name' in input and input['device_name'] != None else None
|
|
||||||
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}'"
|
|
||||||
self.f_x = self.x
|
|
||||||
self.f_y = self.y
|
|
||||||
self.f_device_id = f"'{self.device_id}'"
|
|
||||||
self.f_device_name = f"'{self.device_name}'" if self.device_name != None else 'null'
|
|
||||||
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, 'status': self.status }
|
|
||||||
|
|
||||||
def as_id_dict(self) -> dict[str, Any]:
|
|
||||||
return { 'type': self.type, 'id': self.id }
|
|
||||||
|
|
||||||
|
|
||||||
def set_scada_model_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
|
|
||||||
old = ScadaModel(get_scada_model(name, cs.operations[0]['id']))
|
|
||||||
raw_new = get_scada_model(name, cs.operations[0]['id'])
|
|
||||||
|
|
||||||
new_dict = cs.operations[0]
|
|
||||||
schema = get_scada_model_schema(name)
|
|
||||||
for key, value in schema.items():
|
|
||||||
if key in new_dict and not value['readonly']:
|
|
||||||
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}, 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()
|
|
||||||
|
|
||||||
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
|
||||||
|
|
||||||
|
|
||||||
def set_scada_model(name: str, cs: ChangeSet) -> ChangeSet:
|
|
||||||
if get_scada_model(name, cs.operations[0]['id']) == {}:
|
|
||||||
return ChangeSet()
|
|
||||||
if _check_model_id(name, cs) == False:
|
|
||||||
return ChangeSet()
|
|
||||||
return execute_command(name, set_scada_model_cmd(name, cs))
|
|
||||||
|
|
||||||
|
|
||||||
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, 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()
|
|
||||||
undo_cs = g_delete_prefix | new.as_id_dict()
|
|
||||||
|
|
||||||
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
|
||||||
|
|
||||||
|
|
||||||
def add_scada_model(name: str, cs: ChangeSet) -> ChangeSet:
|
|
||||||
if get_scada_model(name, cs.operations[0]['id']) != {}:
|
|
||||||
return ChangeSet()
|
|
||||||
if _check_model_id(name, cs) == False:
|
|
||||||
return ChangeSet()
|
|
||||||
return execute_command(name, add_scada_model_cmd(name, cs))
|
|
||||||
|
|
||||||
|
|
||||||
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, 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()
|
|
||||||
|
|
||||||
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
|
||||||
|
|
||||||
|
|
||||||
def delete_scada_model(name: str, cs: ChangeSet) -> ChangeSet:
|
|
||||||
if get_scada_model(name, cs.operations[0]['id']) == {}:
|
|
||||||
return ChangeSet()
|
|
||||||
return execute_command(name, delete_scada_model_cmd(name, cs))
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
from .database import *
|
|
||||||
|
|
||||||
|
|
||||||
def get_scada_data_schema(name: str) -> dict[str, dict[str, Any]]:
|
|
||||||
return { 'device_id' : {'type': 'str' , 'optional': False , 'readonly': True },
|
|
||||||
'data' : {'type': 'list' , 'optional': False , 'readonly': False,
|
|
||||||
'element': { 'time' : {'type': 'str' , 'optional': False , 'readonly': False },
|
|
||||||
'value' : {'type': 'float' , 'optional': False , 'readonly': False } }}}
|
|
||||||
|
|
||||||
|
|
||||||
def get_scada_data(name: str, device_id: str) -> dict[str, Any]:
|
|
||||||
sds = read_all(name, f"select * from scada_data where device_id = '{device_id}' order by time")
|
|
||||||
ds = []
|
|
||||||
for r in sds:
|
|
||||||
ds.append({ 'time': str(r['time']), 'value': float(r['value']) })
|
|
||||||
return { 'device_id': device_id, 'data': ds }
|
|
||||||
|
|
||||||
|
|
||||||
def set_scada_data_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
|
|
||||||
device_id = cs.operations[0]['device_id']
|
|
||||||
|
|
||||||
old = get_scada_data(name, device_id)
|
|
||||||
new = { 'device_id': device_id, 'data': [] }
|
|
||||||
|
|
||||||
f_device_id = f"'{device_id}'"
|
|
||||||
|
|
||||||
# TODO: transaction ?
|
|
||||||
redo_sql = f"delete from scada_data where device_id = {f_device_id};"
|
|
||||||
for tv in cs.operations[0]['data']:
|
|
||||||
time, value = str(tv['time']), float(tv['value'])
|
|
||||||
f_time, f_value = f"'{time}'", value
|
|
||||||
redo_sql += f"\ninsert into scada_data (device_id, time, value) values ({f_device_id}, {f_time}, {f_value});"
|
|
||||||
new['data'].append({ 'time': time, 'value': value })
|
|
||||||
|
|
||||||
undo_sql = f"delete from scada_data where device_id = {f_device_id};"
|
|
||||||
for tv in old['data']:
|
|
||||||
time, value = str(tv['time']), float(tv['value'])
|
|
||||||
f_time, f_value = f"'{time}'", value
|
|
||||||
undo_sql += f"\ninsert into scada_data (device_id, time, value) values ({f_device_id}, {f_time}, {f_value});"
|
|
||||||
|
|
||||||
redo_cs = { 'type': 'scada_data' } | new
|
|
||||||
undo_cs = { 'type': 'scada_data' } | old
|
|
||||||
|
|
||||||
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
|
||||||
|
|
||||||
|
|
||||||
def set_scada_data(name: str, cs: ChangeSet) -> ChangeSet:
|
|
||||||
result = set_scada_data_cmd(name, cs)
|
|
||||||
result.redo_cs[0] |= g_update_prefix
|
|
||||||
result.undo_cs[0] |= g_update_prefix
|
|
||||||
return execute_command(name, result)
|
|
||||||
|
|
||||||
|
|
||||||
def add_scada_data_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
|
|
||||||
values = cs.operations[0]
|
|
||||||
device_id = values['device_id']
|
|
||||||
time = values['time']
|
|
||||||
value = float(values['value'])
|
|
||||||
|
|
||||||
redo_sql = f"insert into scada_data (device_id, time, value) values ('{device_id}', '{time}', {value});"
|
|
||||||
undo_sql = f"delete from scada_data where device_id = '{device_id}' and time = '{time}';"
|
|
||||||
redo_cs = g_add_prefix | { 'type': 'scada_data', 'device_id': device_id, 'time': time, 'value': value }
|
|
||||||
undo_cs = g_delete_prefix | { 'type': 'scada_data', 'device_id': device_id, 'time': time }
|
|
||||||
|
|
||||||
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
|
||||||
|
|
||||||
|
|
||||||
def add_scada_data(name: str, cs: ChangeSet) -> ChangeSet:
|
|
||||||
row = try_read(name, f"select * from scada_data where device_id = '{cs.operations[0]['device_id']}' and time = '{cs.operations[0]['time']}'")
|
|
||||||
if row != None:
|
|
||||||
return ChangeSet()
|
|
||||||
return execute_command(name, add_scada_data_cmd(name, cs))
|
|
||||||
|
|
||||||
|
|
||||||
def delete_scada_data_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
|
|
||||||
values = cs.operations[0]
|
|
||||||
device_id = values['device_id']
|
|
||||||
time = values['time']
|
|
||||||
value = float(read(name, f"select * from scada_data where device_id = '{device_id}' and time = '{time}'")['value'])
|
|
||||||
|
|
||||||
redo_sql = f"delete from scada_data where device_id = '{device_id}' and time = '{time}';"
|
|
||||||
undo_sql = f"insert into scada_data (device_id, time, value) values ('{device_id}', '{time}', {value});"
|
|
||||||
redo_cs = g_delete_prefix | { 'type': 'scada_data', 'device_id': device_id, 'time': time }
|
|
||||||
undo_cs = g_add_prefix | { 'type': 'scada_data', 'device_id': device_id, 'time': time, 'value': value }
|
|
||||||
|
|
||||||
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
|
||||||
|
|
||||||
|
|
||||||
def delete_scada_data(name: str, cs: ChangeSet) -> ChangeSet:
|
|
||||||
row = try_read(name, f"select * from scada_data where device_id = '{cs.operations[0]['device_id']}' and time = '{cs.operations[0]['time']}'")
|
|
||||||
if row == None:
|
|
||||||
return ChangeSet()
|
|
||||||
return execute_command(name, delete_scada_data_cmd(name, cs))
|
|
||||||
100
api/s30_scada_device_data.py
Normal file
100
api/s30_scada_device_data.py
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
from .database import *
|
||||||
|
|
||||||
|
|
||||||
|
def get_scada_device_data_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||||
|
return { 'device_id' : {'type': 'str' , 'optional': False , 'readonly': True },
|
||||||
|
'data' : {'type': 'list' , 'optional': False , 'readonly': False,
|
||||||
|
'element': { 'time' : {'type': 'str' , 'optional': False , 'readonly': False },
|
||||||
|
'value' : {'type': 'float' , 'optional': False , 'readonly': False } }}}
|
||||||
|
|
||||||
|
|
||||||
|
def get_scada_device_data(name: str, device_id: str) -> dict[str, Any]:
|
||||||
|
sds = read_all(name, f"select * from scada_device_data where device_id = '{device_id}' order by time")
|
||||||
|
ds = []
|
||||||
|
for r in sds:
|
||||||
|
ds.append({ 'time': str(r['time']), 'value': float(r['value']) })
|
||||||
|
return { 'device_id': device_id, 'data': ds }
|
||||||
|
|
||||||
|
|
||||||
|
def set_scada_device_data_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
|
||||||
|
device_id = cs.operations[0]['device_id']
|
||||||
|
|
||||||
|
old = get_scada_device_data(name, device_id)
|
||||||
|
new = { 'device_id': device_id, 'data': [] }
|
||||||
|
|
||||||
|
f_device_id = f"'{device_id}'"
|
||||||
|
|
||||||
|
# TODO: transaction ?
|
||||||
|
redo_sql = f"delete from scada_device_data where device_id = {f_device_id};"
|
||||||
|
for tv in cs.operations[0]['data']:
|
||||||
|
time, value = str(tv['time']), float(tv['value'])
|
||||||
|
f_time, f_value = f"'{time}'", value
|
||||||
|
redo_sql += f"\ninsert into scada_device_data (device_id, time, value) values ({f_device_id}, {f_time}, {f_value});"
|
||||||
|
new['data'].append({ 'time': time, 'value': value })
|
||||||
|
|
||||||
|
undo_sql = f"delete from scada_device_data where device_id = {f_device_id};"
|
||||||
|
for tv in old['data']:
|
||||||
|
time, value = str(tv['time']), float(tv['value'])
|
||||||
|
f_time, f_value = f"'{time}'", value
|
||||||
|
undo_sql += f"\ninsert into scada_device_data (device_id, time, value) values ({f_device_id}, {f_time}, {f_value});"
|
||||||
|
|
||||||
|
redo_cs = g_update_prefix | { 'type': 'scada_device_data' } | new
|
||||||
|
undo_cs = g_update_prefix | { 'type': 'scada_device_data' } | old
|
||||||
|
|
||||||
|
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
||||||
|
|
||||||
|
|
||||||
|
def set_scada_device_data(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
|
return execute_command(name, set_scada_device_data_cmd(name, cs))
|
||||||
|
|
||||||
|
|
||||||
|
def add_scada_device_data_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
|
||||||
|
values = cs.operations[0]
|
||||||
|
device_id = values['device_id']
|
||||||
|
time = values['time']
|
||||||
|
value = float(values['value'])
|
||||||
|
|
||||||
|
redo_sql = f"insert into scada_device_data (device_id, time, value) values ('{device_id}', '{time}', {value});"
|
||||||
|
undo_sql = f"delete from scada_device_data where device_id = '{device_id}' and time = '{time}';"
|
||||||
|
redo_cs = g_add_prefix | { 'type': 'scada_device_data', 'device_id': device_id, 'time': time, 'value': value }
|
||||||
|
undo_cs = g_delete_prefix | { 'type': 'scada_device_data', 'device_id': device_id, 'time': time }
|
||||||
|
|
||||||
|
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
||||||
|
|
||||||
|
|
||||||
|
def add_scada_device_data(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
|
row = try_read(name, f"select * from scada_device_data where device_id = '{cs.operations[0]['device_id']}' and time = '{cs.operations[0]['time']}'")
|
||||||
|
if row != None:
|
||||||
|
return ChangeSet()
|
||||||
|
return execute_command(name, add_scada_device_data_cmd(name, cs))
|
||||||
|
|
||||||
|
|
||||||
|
def delete_scada_device_data_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
|
||||||
|
values = cs.operations[0]
|
||||||
|
device_id = values['device_id']
|
||||||
|
time = values['time']
|
||||||
|
value = float(read(name, f"select * from scada_device_data where device_id = '{device_id}' and time = '{time}'")['value'])
|
||||||
|
|
||||||
|
redo_sql = f"delete from scada_device_data where device_id = '{device_id}' and time = '{time}';"
|
||||||
|
undo_sql = f"insert into scada_device_data (device_id, time, value) values ('{device_id}', '{time}', {value});"
|
||||||
|
redo_cs = g_delete_prefix | { 'type': 'scada_device_data', 'device_id': device_id, 'time': time }
|
||||||
|
undo_cs = g_add_prefix | { 'type': 'scada_device_data', 'device_id': device_id, 'time': time, 'value': value }
|
||||||
|
|
||||||
|
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
||||||
|
|
||||||
|
|
||||||
|
def delete_scada_device_data(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
|
row = try_read(name, f"select * from scada_device_data where device_id = '{cs.operations[0]['device_id']}' and time = '{cs.operations[0]['time']}'")
|
||||||
|
if row == None:
|
||||||
|
return ChangeSet()
|
||||||
|
return execute_command(name, delete_scada_device_data_cmd(name, cs))
|
||||||
|
|
||||||
|
|
||||||
|
def clean_scada_device_data_cmd(name: str) -> ChangeSet:
|
||||||
|
cs = ChangeSet()
|
||||||
|
|
||||||
|
rows = read_all(name, 'select distinct device_id from scada_device_data acs')
|
||||||
|
for row in rows:
|
||||||
|
cs.update({ 'type': 'scada_device_data', 'device_id': row['device_id'], 'data': [] })
|
||||||
|
|
||||||
|
return cs
|
||||||
146
api/s31_scada_element.py
Normal file
146
api/s31_scada_element.py
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
from .database import *
|
||||||
|
from .s0_base import is_node, is_link
|
||||||
|
|
||||||
|
|
||||||
|
SCADA_TYPE_PRESSURE = 'PRESSURE'
|
||||||
|
SCADA_TYPE_DEMAND = 'DEMAND'
|
||||||
|
SCADA_TYPE_QUALITY = 'QUALITY'
|
||||||
|
SCADA_TYPE_LEVEL = 'LEVEL'
|
||||||
|
SCADA_TYPE_FLOW = 'FLOW'
|
||||||
|
|
||||||
|
|
||||||
|
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]:
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
def get_scada_element_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||||
|
return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True },
|
||||||
|
'x' : {'type': 'float' , 'optional': False , 'readonly': False},
|
||||||
|
'y' : {'type': 'float' , 'optional': False , 'readonly': False},
|
||||||
|
'device_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_element(name: str, id: str) -> dict[str, Any]:
|
||||||
|
sm = try_read(name, f"select * from scada_element where id = '{id}'")
|
||||||
|
if sm == None:
|
||||||
|
return {}
|
||||||
|
d = {}
|
||||||
|
d['id'] = str(sm['id'])
|
||||||
|
d['x'] = float(sm['x'])
|
||||||
|
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['status'] = str(sm['status'])
|
||||||
|
return d
|
||||||
|
|
||||||
|
|
||||||
|
class ScadaModel(object):
|
||||||
|
def __init__(self, input: dict[str, Any]) -> None:
|
||||||
|
self.type = 'scada_element'
|
||||||
|
self.id = str(input['id'])
|
||||||
|
self.x = float(input['x'])
|
||||||
|
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.status = str(input['status']) if 'status' in input and input['status'] != None else SCADA_ELEMENT_STATUS_OFFLINE
|
||||||
|
|
||||||
|
self.f_type = f"'{self.type}'"
|
||||||
|
self.f_id = f"'{self.id}'"
|
||||||
|
self.f_x = self.x
|
||||||
|
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_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 }
|
||||||
|
|
||||||
|
def as_id_dict(self) -> dict[str, Any]:
|
||||||
|
return { 'type': self.type, 'id': self.id }
|
||||||
|
|
||||||
|
|
||||||
|
def set_scada_element_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
|
||||||
|
old = ScadaModel(get_scada_element(name, cs.operations[0]['id']))
|
||||||
|
raw_new = get_scada_element(name, cs.operations[0]['id'])
|
||||||
|
|
||||||
|
new_dict = cs.operations[0]
|
||||||
|
schema = get_scada_element_schema(name)
|
||||||
|
for key, value in schema.items():
|
||||||
|
if key in new_dict and not value['readonly']:
|
||||||
|
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_cs = g_update_prefix | new.as_dict()
|
||||||
|
undo_cs = g_update_prefix | old.as_dict()
|
||||||
|
|
||||||
|
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
||||||
|
return ChangeSet()
|
||||||
|
return execute_command(name, set_scada_element_cmd(name, cs))
|
||||||
|
|
||||||
|
|
||||||
|
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});"
|
||||||
|
undo_sql = f"delete from scada_element where id = {new.f_id};"
|
||||||
|
|
||||||
|
redo_cs = g_add_prefix | new.as_dict()
|
||||||
|
undo_cs = g_delete_prefix | new.as_id_dict()
|
||||||
|
|
||||||
|
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
||||||
|
return ChangeSet()
|
||||||
|
return execute_command(name, add_scada_element_cmd(name, cs))
|
||||||
|
|
||||||
|
|
||||||
|
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});"
|
||||||
|
|
||||||
|
redo_cs = g_delete_prefix | old.as_id_dict()
|
||||||
|
undo_cs = g_add_prefix | old.as_dict()
|
||||||
|
|
||||||
|
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
|
||||||
|
|
||||||
|
|
||||||
|
def delete_scada_element(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
|
if get_scada_element(name, cs.operations[0]['id']) == {}:
|
||||||
|
return ChangeSet()
|
||||||
|
return execute_command(name, delete_scada_element_cmd(name, cs))
|
||||||
|
|
||||||
|
|
||||||
|
def clean_scada_element_cmd(name: str) -> ChangeSet:
|
||||||
|
cs = ChangeSet()
|
||||||
|
|
||||||
|
rows = read_all(name, 'select id from scada_element acs')
|
||||||
|
for row in rows:
|
||||||
|
cs.delete({ 'type': 'scada_element', 'id': row['id'] })
|
||||||
|
|
||||||
|
return cs
|
||||||
@@ -29,3 +29,6 @@ s25_vertex = 'vertex'
|
|||||||
s26_label = 'label'
|
s26_label = 'label'
|
||||||
s27_backdrop = 'backdrop'
|
s27_backdrop = 'backdrop'
|
||||||
s28_end = 'end'
|
s28_end = 'end'
|
||||||
|
s29_scada_device = 'scada_device'
|
||||||
|
s30_scada_device_data = 'scada_device_data'
|
||||||
|
s31_scada_element = 'scada_element'
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ files = [
|
|||||||
#'nanjing',
|
#'nanjing',
|
||||||
#'net3',
|
#'net3',
|
||||||
#'zj',
|
#'zj',
|
||||||
'suzhouhe',
|
#'suzhouhe',
|
||||||
]
|
]
|
||||||
|
|
||||||
def inp2db():
|
def inp2db():
|
||||||
@@ -20,6 +20,7 @@ def db2inp():
|
|||||||
dump_inp(file, f'./db_inp/{file}.db.inp')
|
dump_inp(file, f'./db_inp/{file}.db.inp')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
inp2db()
|
#inp2db()
|
||||||
#db2inp()
|
#db2inp()
|
||||||
#print(run_inp('net3'))
|
#print(run_inp('net3'))
|
||||||
|
pass
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
from .epanet import run_project, run_inp, dump_output
|
from .epanet2 import run_project, run_inp, dump_output
|
||||||
@@ -7,7 +7,7 @@ from typing import Any
|
|||||||
sys.path.append("..")
|
sys.path.append("..")
|
||||||
from api import project
|
from api import project
|
||||||
from api import parser
|
from api import parser
|
||||||
from .error_code import *
|
from .epanet2_ec import *
|
||||||
|
|
||||||
def _verify_platform():
|
def _verify_platform():
|
||||||
_platform = platform.system()
|
_platform = platform.system()
|
||||||
9
script/sql/create/29.scada_device.sql
Normal file
9
script/sql/create/29.scada_device.sql
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
create type scada_device_type as enum ('PRESSURE', 'DEMAND', 'QUALITY', 'LEVEL', 'FLOW');
|
||||||
|
|
||||||
|
create table scada_device
|
||||||
|
(
|
||||||
|
id text primary key
|
||||||
|
, name text
|
||||||
|
, address text
|
||||||
|
, type scada_device_type
|
||||||
|
);
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
create type scada_type as enum ('PRESSURE', 'DEMAND', 'QUALITY', 'LEVEL', 'FLOW');
|
|
||||||
create type scada_status as enum ('OFF', 'ON');
|
|
||||||
|
|
||||||
create table scada_model
|
|
||||||
(
|
|
||||||
id text primary key
|
|
||||||
, x numeric not null
|
|
||||||
, y numeric not null
|
|
||||||
, device_id text not null unique
|
|
||||||
, device_name text
|
|
||||||
, address text
|
|
||||||
, type scada_type
|
|
||||||
, model_id varchar(32) -- add constraint in API
|
|
||||||
, status scada_status not null default 'OFF'
|
|
||||||
);
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
create table scada_data
|
create table scada_device_data
|
||||||
(
|
(
|
||||||
device_id text not null references scada_model(device_id)
|
device_id text not null references scada_device(id)
|
||||||
, time timestamp not null
|
, time timestamp not null
|
||||||
, value numeric not null
|
, value numeric not null
|
||||||
, primary key (device_id, time)
|
, primary key (device_id, time)
|
||||||
11
script/sql/create/31.scada_element.sql
Normal file
11
script/sql/create/31.scada_element.sql
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
create type scada_element_status as enum ('OFF', 'ON');
|
||||||
|
|
||||||
|
create table scada_element
|
||||||
|
(
|
||||||
|
id text primary key
|
||||||
|
, x numeric not null
|
||||||
|
, y numeric not null
|
||||||
|
, device_id text references scada_device(id)
|
||||||
|
, model_id varchar(32) -- add constraint in API
|
||||||
|
, status scada_element_status not null default 'OFF'
|
||||||
|
);
|
||||||
3
script/sql/drop/29.scada_device.sql
Normal file
3
script/sql/drop/29.scada_device.sql
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
drop table if exists scada_device;
|
||||||
|
|
||||||
|
drop type if exists scada_device_type;
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
drop table if exists scada_model;
|
|
||||||
|
|
||||||
drop type if exists scada_status;
|
|
||||||
|
|
||||||
drop type if exists scada_type;
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
drop table if exists scada_data;
|
|
||||||
1
script/sql/drop/30.scada_device_data.sql
Normal file
1
script/sql/drop/30.scada_device_data.sql
Normal file
@@ -0,0 +1 @@
|
|||||||
|
drop table if exists scada_device_data;
|
||||||
3
script/sql/drop/31.scada_element.sql
Normal file
3
script/sql/drop/31.scada_element.sql
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
drop table if exists scada_element;
|
||||||
|
|
||||||
|
drop type if exists scada_element_status;
|
||||||
@@ -30,15 +30,17 @@ sql_create = [
|
|||||||
"sql/create/26.labels.sql",
|
"sql/create/26.labels.sql",
|
||||||
"sql/create/27.backdrop.sql",
|
"sql/create/27.backdrop.sql",
|
||||||
"sql/create/28.end.sql",
|
"sql/create/28.end.sql",
|
||||||
"sql/create/29.scada_model.sql",
|
"sql/create/29.scada_device.sql",
|
||||||
"sql/create/30.scada_data.sql",
|
"sql/create/30.scada_device_data.sql",
|
||||||
|
"sql/create/31.scada_element.sql",
|
||||||
"sql/create/operation.sql"
|
"sql/create/operation.sql"
|
||||||
]
|
]
|
||||||
|
|
||||||
sql_drop = [
|
sql_drop = [
|
||||||
"sql/drop/operation.sql",
|
"sql/drop/operation.sql",
|
||||||
"sql/drop/30.scada_data.sql",
|
"sql/drop/31.scada_element.sql",
|
||||||
"sql/drop/29.scada_model.sql",
|
"sql/drop/30.scada_device_data.sql",
|
||||||
|
"sql/drop/29.scada_device.sql",
|
||||||
"sql/drop/28.end.sql",
|
"sql/drop/28.end.sql",
|
||||||
"sql/drop/27.backdrop.sql",
|
"sql/drop/27.backdrop.sql",
|
||||||
"sql/drop/26.labels.sql",
|
"sql/drop/26.labels.sql",
|
||||||
|
|||||||
1111
test_tjnetwork.py
1111
test_tjnetwork.py
File diff suppressed because it is too large
Load Diff
96
tjnetwork.py
96
tjnetwork.py
@@ -95,14 +95,14 @@ OPTION_QUALITY_CHEMICAL = api.OPTION_QUALITY_CHEMICAL
|
|||||||
OPTION_QUALITY_AGE = api.OPTION_QUALITY_AGE
|
OPTION_QUALITY_AGE = api.OPTION_QUALITY_AGE
|
||||||
OPTION_QUALITY_TRACE = api.OPTION_QUALITY_TRACE
|
OPTION_QUALITY_TRACE = api.OPTION_QUALITY_TRACE
|
||||||
|
|
||||||
SCADA_TYPE_PRESSURE = api.SCADA_TYPE_PRESSURE
|
SCADA_DEVICE_TYPE_PRESSURE = api.SCADA_DEVICE_TYPE_PRESSURE
|
||||||
SCADA_TYPE_DEMAND = api.SCADA_TYPE_DEMAND
|
SCADA_DEVICE_TYPE_DEMAND = api.SCADA_DEVICE_TYPE_DEMAND
|
||||||
SCADA_TYPE_QUALITY = api.SCADA_TYPE_QUALITY
|
SCADA_DEVICE_TYPE_QUALITY = api.SCADA_DEVICE_TYPE_QUALITY
|
||||||
SCADA_TYPE_LEVEL = api.SCADA_TYPE_LEVEL
|
SCADA_DEVICE_TYPE_LEVEL = api.SCADA_DEVICE_TYPE_LEVEL
|
||||||
SCADA_TYPE_FLOW = api.SCADA_TYPE_FLOW
|
SCADA_DEVICE_TYPE_FLOW = api.SCADA_DEVICE_TYPE_FLOW
|
||||||
|
|
||||||
SCADA_STATUS_ONLINE = api.SCADA_STATUS_ONLINE
|
SCADA_ELEMENT_STATUS_ONLINE = api.SCADA_ELEMENT_STATUS_ONLINE
|
||||||
SCADA_STATUS_OFFLINE = api.SCADA_STATUS_OFFLINE
|
SCADA_ELEMENT_STATUS_OFFLINE = api.SCADA_ELEMENT_STATUS_OFFLINE
|
||||||
|
|
||||||
|
|
||||||
############################################################
|
############################################################
|
||||||
@@ -185,11 +185,11 @@ def have_snapshot_for_current_operation(name: str) -> bool:
|
|||||||
def take_snapshot_for_operation(name: str, operation: int, tag: str) -> None:
|
def take_snapshot_for_operation(name: str, operation: int, tag: str) -> None:
|
||||||
return api.take_snapshot_for_operation(name, operation, tag)
|
return api.take_snapshot_for_operation(name, operation, tag)
|
||||||
|
|
||||||
def take_snapshot_for_current_operation(name: str, tag: str) -> int | None:
|
def take_snapshot_for_current_operation(name: str, tag: str) -> None:
|
||||||
return api.take_snapshot_for_current_operation(name, tag)
|
return api.take_snapshot_for_current_operation(name, tag)
|
||||||
|
|
||||||
# deprecated ! use take_snapshot_for_current_operation instead
|
# deprecated ! use take_snapshot_for_current_operation instead
|
||||||
def take_snapshot(name: str, tag: str) -> int | None:
|
def take_snapshot(name: str, tag: str) -> None:
|
||||||
return api.take_snapshot(name, tag)
|
return api.take_snapshot(name, tag)
|
||||||
|
|
||||||
def update_snapshot(name: str, operation: int, tag: str) -> None:
|
def update_snapshot(name: str, operation: int, tag: str) -> None:
|
||||||
@@ -773,43 +773,73 @@ def set_backdrop(name: str, cs: ChangeSet) -> ChangeSet:
|
|||||||
|
|
||||||
|
|
||||||
############################################################
|
############################################################
|
||||||
# scada_model 29
|
# scada_device 29
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
def get_scada_model_schema(name: str) -> dict[str, dict[str, Any]]:
|
def get_scada_device_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||||
return api.get_scada_model_schema(name)
|
return api.get_scada_device_schema(name)
|
||||||
|
|
||||||
def get_scada_model(name: str, id: str) -> dict[str, Any]:
|
def get_scada_device(name: str, id: str) -> dict[str, Any]:
|
||||||
return api.get_scada_model(name, id)
|
return api.get_scada_device(name, id)
|
||||||
|
|
||||||
def set_scada_model(name: str, cs: ChangeSet) -> ChangeSet:
|
def set_scada_device(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
return api.set_scada_model(name, cs)
|
return api.set_scada_device(name, cs)
|
||||||
|
|
||||||
# example: add_scada_model(p, ChangeSet({'id': 'sm', 'x': 0.0, 'y': 10.0, 'device_id': 'sm_device'}))
|
def add_scada_device(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
def add_scada_model(name: str, cs: ChangeSet) -> ChangeSet:
|
return api.add_scada_device(name, cs)
|
||||||
return api.add_scada_model(name, cs)
|
|
||||||
|
|
||||||
def delete_scada_model(name: str, cs: ChangeSet) -> ChangeSet:
|
def delete_scada_device(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
return api.delete_scada_model(name, cs)
|
return api.delete_scada_device(name, cs)
|
||||||
|
|
||||||
|
def clean_scada_device(name: str) -> ChangeSet:
|
||||||
|
return api.clean_scada_device(name)
|
||||||
|
|
||||||
|
|
||||||
############################################################
|
############################################################
|
||||||
# scada_data 30
|
# scada_device_data 30
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
def get_scada_data_schema(name: str) -> dict[str, dict[str, Any]]:
|
def get_scada_device_data_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||||
return api.get_scada_data_schema(name)
|
return api.get_scada_device_data_schema(name)
|
||||||
|
|
||||||
def get_scada_data(name: str, device_id: str) -> dict[str, Any]:
|
def get_scada_device_data(name: str, device_id: str) -> dict[str, Any]:
|
||||||
return api.get_scada_data(name, device_id)
|
return api.get_scada_device_data(name, device_id)
|
||||||
|
|
||||||
# example: set_scada_data(p, ChangeSet({'device_id': 'sm_device', 'data': [{ 'time': '2023-02-10 00:02:22', 'value': 100.0 }, { 'time': '2023-02-10 00:03:22', 'value': 200.0 }]}))
|
# example: set_scada_device_data(p, ChangeSet({'device_id': 'sm_device', 'data': [{ 'time': '2023-02-10 00:02:22', 'value': 100.0 }, { 'time': '2023-02-10 00:03:22', 'value': 200.0 }]}))
|
||||||
# time format must be 'YYYY-MM-DD HH:MM:SS'
|
# time format must be 'YYYY-MM-DD HH:MM:SS'
|
||||||
def set_scada_data(name: str, cs: ChangeSet) -> ChangeSet:
|
def set_scada_device_data(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
return api.set_scada_data(name, cs)
|
return api.set_scada_device_data(name, cs)
|
||||||
|
|
||||||
def add_scada_data(name: str, cs: ChangeSet) -> ChangeSet:
|
# example: add_scada_device_data(p, ChangeSet({'device_id': 'sm_device', 'time': '2023-02-10 00:02:22', 'value': 100.0}))
|
||||||
return api.add_scada_data(name, cs)
|
def add_scada_device_data(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
|
return api.add_scada_device_data(name, cs)
|
||||||
|
|
||||||
def delete_scada_data(name: str, cs: ChangeSet) -> ChangeSet:
|
# example: delete_scada_device_data(p, ChangeSet({'device_id': 'sm_device', 'time': '2023-02-12 00:02:22'}))
|
||||||
return api.delete_scada_data(name, cs)
|
def delete_scada_device_data(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
|
return api.delete_scada_device_data(name, cs)
|
||||||
|
|
||||||
|
def clean_scada_device_data(name: str) -> ChangeSet:
|
||||||
|
return api.clean_scada_device_data(name)
|
||||||
|
|
||||||
|
|
||||||
|
############################################################
|
||||||
|
# scada_element 31
|
||||||
|
############################################################
|
||||||
|
|
||||||
|
def get_scada_element_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||||
|
return api.get_scada_element_schema(name)
|
||||||
|
|
||||||
|
def get_scada_element(name: str, id: str) -> dict[str, Any]:
|
||||||
|
return api.get_scada_element(name, id)
|
||||||
|
|
||||||
|
def set_scada_element(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
|
return api.set_scada_element(name, cs)
|
||||||
|
|
||||||
|
def add_scada_element(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
|
return api.add_scada_element(name, cs)
|
||||||
|
|
||||||
|
def delete_scada_element(name: str, cs: ChangeSet) -> ChangeSet:
|
||||||
|
return api.delete_scada_element(name, cs)
|
||||||
|
|
||||||
|
def clean_scada_element(name: str) -> ChangeSet:
|
||||||
|
return api.clean_scada_element(name)
|
||||||
|
|||||||
Reference in New Issue
Block a user