Accept Merge Request #135: (api -> master)
Merge Request: Add scada API Created By: @王琼钰 Accepted By: @王琼钰 URL: https://tjwater.coding.net/p/tjwatercloud/d/TJWaterServer/git/merge/135
This commit is contained in:
@@ -98,3 +98,8 @@ from .s25_vertices import get_vertex_schema, get_vertex, set_vertex, add_vertex,
|
||||
from .s26_labels import get_label_schema, get_label, set_label, add_label, delete_label
|
||||
|
||||
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 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
|
||||
|
||||
138
api/s29_scada_model.py
Normal file
138
api/s29_scada_model.py
Normal file
@@ -0,0 +1,138 @@
|
||||
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'
|
||||
|
||||
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} }
|
||||
|
||||
|
||||
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
|
||||
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.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'
|
||||
|
||||
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 }
|
||||
|
||||
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} 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_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) 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});"
|
||||
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) 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});"
|
||||
|
||||
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))
|
||||
51
api/s30_scada_data.py
Normal file
51
api/s30_scada_data.py
Normal file
@@ -0,0 +1,51 @@
|
||||
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)
|
||||
13
script/sql/create/29.scada_model.sql
Normal file
13
script/sql/create/29.scada_model.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
create type scada_type as enum ('PRESSURE', 'DEMAND', 'QUALITY', 'LEVEL', 'FLOW');
|
||||
|
||||
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
|
||||
);
|
||||
7
script/sql/create/30.scada_data.sql
Normal file
7
script/sql/create/30.scada_data.sql
Normal file
@@ -0,0 +1,7 @@
|
||||
create table scada_data
|
||||
(
|
||||
device_id text not null references scada_model(device_id)
|
||||
, time timestamp not null
|
||||
, value numeric not null
|
||||
, primary key (device_id, time)
|
||||
);
|
||||
3
script/sql/drop/29.scada_model.sql
Normal file
3
script/sql/drop/29.scada_model.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
drop table if exists scada_model;
|
||||
|
||||
drop type if exists scada_type;
|
||||
1
script/sql/drop/30.scada_data.sql
Normal file
1
script/sql/drop/30.scada_data.sql
Normal file
@@ -0,0 +1 @@
|
||||
drop table if exists scada_data;
|
||||
@@ -30,11 +30,15 @@ sql_create = [
|
||||
"sql/create/26.labels.sql",
|
||||
"sql/create/27.backdrop.sql",
|
||||
"sql/create/28.end.sql",
|
||||
"sql/create/29.scada_model.sql",
|
||||
"sql/create/30.scada_data.sql",
|
||||
"sql/create/operation.sql"
|
||||
]
|
||||
|
||||
sql_drop = [
|
||||
"sql/drop/operation.sql",
|
||||
"sql/drop/30.scada_data.sql",
|
||||
"sql/drop/29.scada_model.sql",
|
||||
"sql/drop/28.end.sql",
|
||||
"sql/drop/27.backdrop.sql",
|
||||
"sql/drop/26.labels.sql",
|
||||
|
||||
@@ -3650,5 +3650,320 @@ class TestApi:
|
||||
self.leave(p)
|
||||
|
||||
|
||||
# 28 end
|
||||
|
||||
|
||||
# 29 scada_model
|
||||
|
||||
|
||||
def test_scada_model(self):
|
||||
p = 'test_scada_model'
|
||||
self.enter(p)
|
||||
|
||||
add_junction(p, ChangeSet({'id': 'j1', 'x': 0.0, 'y': 10.0, 'elevation': 20.0}))
|
||||
add_junction(p, ChangeSet({'id': 'j2', 'x': 0.0, 'y': 10.0, 'elevation': 20.0}))
|
||||
add_pipe(p, ChangeSet({'id': 'p1', 'node1': 'j1', 'node2': 'j2', 'length': 100.0, 'diameter': 10.0, 'roughness': 0.1, 'minor_loss': 0.5, 'status': PIPE_STATUS_OPEN }))
|
||||
|
||||
sm = get_scada_model(p, 'sm')
|
||||
assert sm == {}
|
||||
|
||||
add_scada_model(p, ChangeSet({'id': 'sm', 'x': 0.0, 'y': 10.0, 'device_id': 'sm_device'}))
|
||||
sm = get_scada_model(p, 'sm')
|
||||
assert sm['id'] == 'sm'
|
||||
assert sm['x'] == 0.0
|
||||
assert sm['y'] == 10.0
|
||||
assert sm['device_id'] == 'sm_device'
|
||||
assert sm['device_name'] == None
|
||||
assert sm['address'] == None
|
||||
assert sm['sm_type'] == None
|
||||
assert sm['model_id'] == None
|
||||
|
||||
add_scada_model(p, ChangeSet({'id': 'sm0', 'x': 0.0, 'y': 10.0, 'device_id': 'sm_device_0', 'device_name': 'sm_device_name', 'address': 'xxx', 'sm_type': SCADA_TYPE_PRESSURE, 'model_id': 'j0'}))
|
||||
sm0 = get_scada_model(p, 'sm0')
|
||||
assert sm0 == {}
|
||||
|
||||
add_scada_model(p, ChangeSet({'id': 'sm0', 'x': 0.0, 'y': 10.0, 'device_id': 'sm_device_0', 'device_name': 'sm_device_name', 'address': 'xxx', 'sm_type': SCADA_TYPE_PRESSURE, 'model_id': 'p0'}))
|
||||
sm0 = get_scada_model(p, 'sm0')
|
||||
assert sm0 == {}
|
||||
|
||||
add_scada_model(p, ChangeSet({'id': 'sm1', 'x': 0.0, 'y': 10.0, 'device_id': 'sm_device_1', 'device_name': 'sm_device_name', 'address': 'xxx', 'sm_type': SCADA_TYPE_PRESSURE, 'model_id': 'j1'}))
|
||||
sm1 = get_scada_model(p, 'sm1')
|
||||
assert sm1['id'] == 'sm1'
|
||||
assert sm1['x'] == 0.0
|
||||
assert sm1['y'] == 10.0
|
||||
assert sm1['device_id'] == 'sm_device_1'
|
||||
assert sm1['device_name'] == 'sm_device_name'
|
||||
assert sm1['address'] == 'xxx'
|
||||
assert sm1['sm_type'] == SCADA_TYPE_PRESSURE
|
||||
assert sm1['model_id'] == 'j1'
|
||||
|
||||
add_scada_model(p, ChangeSet({'id': 'sm2', 'x': 0.0, 'y': 10.0, 'device_id': 'sm_device_2', 'device_name': 'sm_device_name', 'address': 'xxx', 'sm_type': SCADA_TYPE_PRESSURE, 'model_id': 'p1'}))
|
||||
sm2 = get_scada_model(p, 'sm2')
|
||||
assert sm2['id'] == 'sm2'
|
||||
assert sm2['x'] == 0.0
|
||||
assert sm2['y'] == 10.0
|
||||
assert sm2['device_id'] == 'sm_device_2'
|
||||
assert sm2['device_name'] == 'sm_device_name'
|
||||
assert sm2['address'] == 'xxx'
|
||||
assert sm2['sm_type'] == SCADA_TYPE_PRESSURE
|
||||
assert sm2['model_id'] == 'p1'
|
||||
|
||||
set_scada_model(p, ChangeSet({'id': 'sm', 'device_name': 'sm_device_name', 'address': 'xxx', 'sm_type': SCADA_TYPE_PRESSURE, 'model_id': 'j0'}))
|
||||
sm = get_scada_model(p, 'sm')
|
||||
assert sm['id'] == 'sm'
|
||||
assert sm['x'] == 0.0
|
||||
assert sm['y'] == 10.0
|
||||
assert sm['device_id'] == 'sm_device'
|
||||
assert sm['device_name'] == None
|
||||
assert sm['address'] == None
|
||||
assert sm['sm_type'] == None
|
||||
assert sm['model_id'] == None
|
||||
|
||||
set_scada_model(p, ChangeSet({'id': 'sm', 'device_name': 'sm_device_name', 'address': 'xxx', 'sm_type': SCADA_TYPE_PRESSURE, 'model_id': 'j1'}))
|
||||
sm = get_scada_model(p, 'sm')
|
||||
assert sm['id'] == 'sm'
|
||||
assert sm['x'] == 0.0
|
||||
assert sm['y'] == 10.0
|
||||
assert sm['device_id'] == 'sm_device'
|
||||
assert sm['device_name'] == 'sm_device_name'
|
||||
assert sm['address'] == 'xxx'
|
||||
assert sm['sm_type'] == SCADA_TYPE_PRESSURE
|
||||
assert sm['model_id'] == 'j1'
|
||||
|
||||
delete_scada_model(p, ChangeSet({'id': 'sm'}))
|
||||
sm = get_scada_model(p, 'sm')
|
||||
assert sm == {}
|
||||
|
||||
delete_scada_model(p, ChangeSet({'id': 'sm1'}))
|
||||
sm1 = get_scada_model(p, 'sm1')
|
||||
assert sm1 == {}
|
||||
|
||||
delete_scada_model(p, ChangeSet({'id': 'sm2'}))
|
||||
sm2 = get_scada_model(p, 'sm2')
|
||||
assert sm2 == {}
|
||||
|
||||
self.leave(p)
|
||||
|
||||
|
||||
def test_scada_model_op(self):
|
||||
p = 'test_scada_model_op'
|
||||
|
||||
self.enter(p)
|
||||
|
||||
add_junction(p, ChangeSet({'id': 'j1', 'x': 0.0, 'y': 10.0, 'elevation': 20.0}))
|
||||
add_junction(p, ChangeSet({'id': 'j2', 'x': 0.0, 'y': 10.0, 'elevation': 20.0}))
|
||||
|
||||
cs = add_scada_model(p, ChangeSet({'id': 'sm', 'x': 0.0, 'y': 10.0, 'device_id': 'sm_device', 'device_name': 'sm_device_name', 'address': 'xxx', 'sm_type': SCADA_TYPE_PRESSURE, 'model_id': 'j1'})).operations[0]
|
||||
assert cs['operation'] == API_ADD
|
||||
assert cs['type'] == 'scada_model'
|
||||
assert cs['id'] == 'sm'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 10.0
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert cs['device_name'] == 'sm_device_name'
|
||||
assert cs['address'] == 'xxx'
|
||||
assert cs['sm_type'] == SCADA_TYPE_PRESSURE
|
||||
assert cs['model_id'] == 'j1'
|
||||
|
||||
cs = execute_undo(p).operations[0]
|
||||
assert cs['operation'] == API_DELETE
|
||||
assert cs['type'] == 'scada_model'
|
||||
assert cs['id'] == 'sm'
|
||||
|
||||
cs = execute_redo(p).operations[0]
|
||||
assert cs['operation'] == API_ADD
|
||||
assert cs['type'] == 'scada_model'
|
||||
assert cs['id'] == 'sm'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 10.0
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert cs['device_name'] == 'sm_device_name'
|
||||
assert cs['address'] == 'xxx'
|
||||
assert cs['sm_type'] == SCADA_TYPE_PRESSURE
|
||||
assert cs['model_id'] == 'j1'
|
||||
|
||||
cs = set_scada_model(p, ChangeSet({'id': 'sm', 'device_name': 'sm_device_name_', 'address': 'xxx_', 'sm_type': SCADA_TYPE_DEMAND, 'model_id': 'j2'})).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'scada_model'
|
||||
assert cs['id'] == 'sm'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 10.0
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert cs['device_name'] == 'sm_device_name_'
|
||||
assert cs['address'] == 'xxx_'
|
||||
assert cs['sm_type'] == SCADA_TYPE_DEMAND
|
||||
assert cs['model_id'] == 'j2'
|
||||
|
||||
cs = execute_undo(p).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'scada_model'
|
||||
assert cs['id'] == 'sm'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 10.0
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert cs['device_name'] == 'sm_device_name'
|
||||
assert cs['address'] == 'xxx'
|
||||
assert cs['sm_type'] == SCADA_TYPE_PRESSURE
|
||||
assert cs['model_id'] == 'j1'
|
||||
|
||||
cs = execute_redo(p).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'scada_model'
|
||||
assert cs['id'] == 'sm'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 10.0
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert cs['device_name'] == 'sm_device_name_'
|
||||
assert cs['address'] == 'xxx_'
|
||||
assert cs['sm_type'] == SCADA_TYPE_DEMAND
|
||||
assert cs['model_id'] == 'j2'
|
||||
|
||||
cs = delete_scada_model(p, ChangeSet({'id': 'sm'})).operations[0]
|
||||
assert cs['operation'] == API_DELETE
|
||||
assert cs['type'] == 'scada_model'
|
||||
assert cs['id'] == 'sm'
|
||||
|
||||
cs = execute_undo(p).operations[0]
|
||||
assert cs['operation'] == API_ADD
|
||||
assert cs['type'] == 'scada_model'
|
||||
assert cs['id'] == 'sm'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 10.0
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert cs['device_name'] == 'sm_device_name_'
|
||||
assert cs['address'] == 'xxx_'
|
||||
assert cs['sm_type'] == SCADA_TYPE_DEMAND
|
||||
assert cs['model_id'] == 'j2'
|
||||
|
||||
cs = execute_redo(p).operations[0]
|
||||
assert cs['operation'] == API_DELETE
|
||||
assert cs['type'] == 'scada_model'
|
||||
assert cs['id'] == 'sm'
|
||||
|
||||
self.leave(p)
|
||||
|
||||
|
||||
# 30 scada_data
|
||||
|
||||
|
||||
def test_scada_data(self):
|
||||
p = 'test_scada_data'
|
||||
self.enter(p)
|
||||
|
||||
add_junction(p, ChangeSet({'id': 'j1', 'x': 0.0, 'y': 10.0, 'elevation': 20.0}))
|
||||
|
||||
add_scada_model(p, ChangeSet({'id': 'sm', 'x': 0.0, 'y': 10.0, 'device_id': 'sm_device', 'device_name': 'sm_device_name', 'address': 'xxx', 'sm_type': SCADA_TYPE_PRESSURE, 'model_id': 'j1'}))
|
||||
|
||||
sa = get_scada_data(p, 'sm_device')
|
||||
assert sa['device_id'] == 'sm_device'
|
||||
assert sa['data'] == []
|
||||
|
||||
set_scada_data(p, ChangeSet({'device_id': 'sm_device', 'data': [{ 'time': '2023-02-10 00:02:22', 'value': 100.0 }]}))
|
||||
sa = get_scada_data(p, 'sm_device')
|
||||
assert sa['device_id'] == 'sm_device'
|
||||
assert len(sa['data']) == 1
|
||||
assert sa['data'][0]['time'] == '2023-02-10 00:02:22'
|
||||
assert sa['data'][0]['value'] == 100.0
|
||||
|
||||
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 }]}))
|
||||
sa = get_scada_data(p, 'sm_device')
|
||||
assert sa['device_id'] == 'sm_device'
|
||||
assert len(sa['data']) == 2
|
||||
assert sa['data'][0]['time'] == '2023-02-10 00:02:22'
|
||||
assert sa['data'][0]['value'] == 100.0
|
||||
assert sa['data'][1]['time'] == '2023-02-10 00:03:22'
|
||||
assert sa['data'][1]['value'] == 200.0
|
||||
|
||||
set_scada_data(p, ChangeSet({'device_id': 'sm_device', 'data': []}))
|
||||
sa = get_scada_data(p, 'sm_device')
|
||||
assert sa['device_id'] == 'sm_device'
|
||||
assert sa['data'] == []
|
||||
|
||||
self.leave(p)
|
||||
|
||||
|
||||
def test_scada_data_op(self):
|
||||
p = 'test_scada_data_op'
|
||||
self.enter(p)
|
||||
|
||||
add_junction(p, ChangeSet({'id': 'j1', 'x': 0.0, 'y': 10.0, 'elevation': 20.0}))
|
||||
|
||||
add_scada_model(p, ChangeSet({'id': 'sm', 'x': 0.0, 'y': 10.0, 'device_id': 'sm_device', 'device_name': 'sm_device_name', 'address': 'xxx', 'sm_type': SCADA_TYPE_PRESSURE, 'model_id': 'j1'}))
|
||||
|
||||
cs = set_scada_data(p, ChangeSet({'device_id': 'sm_device', 'data': [{ 'time': '2023-02-10 00:02:22', 'value': 100.0 }]})).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'scada_data'
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert len(cs['data']) == 1
|
||||
assert cs['data'][0]['time'] == '2023-02-10 00:02:22'
|
||||
assert cs['data'][0]['value'] == 100.0
|
||||
|
||||
cs = execute_undo(p).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'scada_data'
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert cs['data'] == []
|
||||
|
||||
cs = execute_redo(p).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'scada_data'
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert len(cs['data']) == 1
|
||||
assert cs['data'][0]['time'] == '2023-02-10 00:02:22'
|
||||
assert cs['data'][0]['value'] == 100.0
|
||||
|
||||
cs = 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 }]})).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'scada_data'
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert len(cs['data']) == 2
|
||||
assert cs['data'][0]['time'] == '2023-02-10 00:02:22'
|
||||
assert cs['data'][0]['value'] == 100.0
|
||||
assert cs['data'][1]['time'] == '2023-02-10 00:03:22'
|
||||
assert cs['data'][1]['value'] == 200.0
|
||||
|
||||
cs = execute_undo(p).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'scada_data'
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert len(cs['data']) == 1
|
||||
assert cs['data'][0]['time'] == '2023-02-10 00:02:22'
|
||||
assert cs['data'][0]['value'] == 100.0
|
||||
|
||||
cs = execute_redo(p).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'scada_data'
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert len(cs['data']) == 2
|
||||
assert cs['data'][0]['time'] == '2023-02-10 00:02:22'
|
||||
assert cs['data'][0]['value'] == 100.0
|
||||
assert cs['data'][1]['time'] == '2023-02-10 00:03:22'
|
||||
assert cs['data'][1]['value'] == 200.0
|
||||
|
||||
cs = set_scada_data(p, ChangeSet({'device_id': 'sm_device', 'data': []})).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'scada_data'
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert cs['data'] == []
|
||||
|
||||
cs = execute_undo(p).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'scada_data'
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert len(cs['data']) == 2
|
||||
assert cs['data'][0]['time'] == '2023-02-10 00:02:22'
|
||||
assert cs['data'][0]['value'] == 100.0
|
||||
assert cs['data'][1]['time'] == '2023-02-10 00:03:22'
|
||||
assert cs['data'][1]['value'] == 200.0
|
||||
|
||||
cs = execute_redo(p).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'scada_data'
|
||||
assert cs['device_id'] == 'sm_device'
|
||||
assert cs['data'] == []
|
||||
|
||||
self.leave(p)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main()
|
||||
|
||||
43
tjnetwork.py
43
tjnetwork.py
@@ -95,6 +95,12 @@ OPTION_QUALITY_CHEMICAL = api.OPTION_QUALITY_CHEMICAL
|
||||
OPTION_QUALITY_AGE = api.OPTION_QUALITY_AGE
|
||||
OPTION_QUALITY_TRACE = api.OPTION_QUALITY_TRACE
|
||||
|
||||
SCADA_TYPE_PRESSURE = api.SCADA_TYPE_PRESSURE
|
||||
SCADA_TYPE_DEMAND = api.SCADA_TYPE_DEMAND
|
||||
SCADA_TYPE_QUALITY = api.SCADA_TYPE_QUALITY
|
||||
SCADA_TYPE_LEVEL = api.SCADA_TYPE_LEVEL
|
||||
SCADA_TYPE_FLOW = api.SCADA_TYPE_FLOW
|
||||
|
||||
|
||||
############################################################
|
||||
# project
|
||||
@@ -721,3 +727,40 @@ def set_backdrop(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
############################################################
|
||||
# end 28.[END]
|
||||
############################################################
|
||||
|
||||
|
||||
############################################################
|
||||
# scada_model 29
|
||||
############################################################
|
||||
|
||||
def get_scada_model_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
return api.get_scada_model_schema(name)
|
||||
|
||||
def get_scada_model(name: str, id: str) -> dict[str, Any]:
|
||||
return api.get_scada_model(name, id)
|
||||
|
||||
def set_scada_model(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return api.set_scada_model(name, cs)
|
||||
|
||||
# example: add_scada_model(p, ChangeSet({'id': 'sm', 'x': 0.0, 'y': 10.0, 'device_id': 'sm_device'}))
|
||||
def add_scada_model(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return api.add_scada_model(name, cs)
|
||||
|
||||
def delete_scada_model(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return api.delete_scada_model(name, cs)
|
||||
|
||||
|
||||
############################################################
|
||||
# scada_data 30
|
||||
############################################################
|
||||
|
||||
def get_scada_data_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
return api.get_scada_data_schema(name)
|
||||
|
||||
def get_scada_data(name: str, device_id: str) -> dict[str, Any]:
|
||||
return api.get_scada_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 }]}))
|
||||
# time format must be 'YYYY-MM-DD HH:MM:SS'
|
||||
def set_scada_data(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return api.set_scada_data(name, cs)
|
||||
|
||||
Reference in New Issue
Block a user