From 1520847813a054218fae2a545fab65252194fa94 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Fri, 10 Feb 2023 00:46:16 +0800 Subject: [PATCH 1/9] Add table scada_model --- script/sql/create/29.scada_model.sql | 13 +++++++++++++ script/sql/drop/29.scada_model.sql | 3 +++ 2 files changed, 16 insertions(+) create mode 100644 script/sql/create/29.scada_model.sql create mode 100644 script/sql/drop/29.scada_model.sql diff --git a/script/sql/create/29.scada_model.sql b/script/sql/create/29.scada_model.sql new file mode 100644 index 0000000..397683d --- /dev/null +++ b/script/sql/create/29.scada_model.sql @@ -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 +); diff --git a/script/sql/drop/29.scada_model.sql b/script/sql/drop/29.scada_model.sql new file mode 100644 index 0000000..262077a --- /dev/null +++ b/script/sql/drop/29.scada_model.sql @@ -0,0 +1,3 @@ +drop table if exists scada_model; + +drop type if exists scada_type; From de6e4284d7d682c549d2797762a47256bfe132be Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Fri, 10 Feb 2023 00:46:32 +0800 Subject: [PATCH 2/9] Add table scada_data --- script/sql/create/30.scada_data.sql | 7 +++++++ script/sql/drop/30.scada_data.sql | 1 + 2 files changed, 8 insertions(+) create mode 100644 script/sql/create/30.scada_data.sql create mode 100644 script/sql/drop/30.scada_data.sql diff --git a/script/sql/create/30.scada_data.sql b/script/sql/create/30.scada_data.sql new file mode 100644 index 0000000..02c1643 --- /dev/null +++ b/script/sql/create/30.scada_data.sql @@ -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) +); diff --git a/script/sql/drop/30.scada_data.sql b/script/sql/drop/30.scada_data.sql new file mode 100644 index 0000000..daa81a0 --- /dev/null +++ b/script/sql/drop/30.scada_data.sql @@ -0,0 +1 @@ +drop table if exists scada_data; From 1c5de25694423b2ef15f1718f31132948a453cfe Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Fri, 10 Feb 2023 00:47:04 +0800 Subject: [PATCH 3/9] Deploy scada table --- script/template.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/script/template.py b/script/template.py index e6e34a8..da61652 100644 --- a/script/template.py +++ b/script/template.py @@ -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", From e18a2a529e4fe5be32f2d47980a3f5c31b18a07a Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Fri, 10 Feb 2023 00:47:37 +0800 Subject: [PATCH 4/9] Add scada_model api --- api/__init__.py | 3 + api/s29_scada_model.py | 138 +++++++++++++++++++++++++++++++++++++++++ tjnetwork.py | 27 ++++++++ 3 files changed, 168 insertions(+) create mode 100644 api/s29_scada_model.py diff --git a/api/__init__.py b/api/__init__.py index 3eaedbf..cb32a38 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -98,3 +98,6 @@ 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 \ No newline at end of file diff --git a/api/s29_scada_model.py b/api/s29_scada_model.py new file mode 100644 index 0000000..561914d --- /dev/null +++ b/api/s29_scada_model.py @@ -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)) diff --git a/tjnetwork.py b/tjnetwork.py index 39c4740..161ab8c 100644 --- a/tjnetwork.py +++ b/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,24 @@ 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) From 14c57bb31333bd89bf73f89a9b87838b01280960 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Fri, 10 Feb 2023 00:47:53 +0800 Subject: [PATCH 5/9] Add scada_model test --- test_tjnetwork.py | 193 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index c2c92c3..eb0eab7 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -3650,5 +3650,198 @@ 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) + + if __name__ == '__main__': pytest.main() From 5edd986ce635071da9e2da1a775dea4399000680 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Fri, 10 Feb 2023 01:44:29 +0800 Subject: [PATCH 6/9] Add scada_data api --- api/__init__.py | 4 +++- api/s30_scada_data.py | 51 +++++++++++++++++++++++++++++++++++++++++++ tjnetwork.py | 15 +++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 api/s30_scada_data.py diff --git a/api/__init__.py b/api/__init__.py index cb32a38..7c33afd 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -100,4 +100,6 @@ 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 get_scada_model_schema, get_scada_model, set_scada_model, add_scada_model, delete_scada_model \ No newline at end of file +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/s30_scada_data.py b/api/s30_scada_data.py new file mode 100644 index 0000000..e222131 --- /dev/null +++ b/api/s30_scada_data.py @@ -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) diff --git a/tjnetwork.py b/tjnetwork.py index 161ab8c..00c9abb 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -748,3 +748,18 @@ def add_scada_model(name: str, cs: ChangeSet) -> ChangeSet: def delete_scada_model(name: str, cs: ChangeSet) -> ChangeSet: return api.delete_scada_model(name, cs) + + +############################################################ +# scada_data 29 +############################################################ + +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 }]})) +def set_scada_data(name: str, cs: ChangeSet) -> ChangeSet: + return api.set_scada_data(name, cs) From 62a1303b8f50a095fd1467ae3cda1a0cd7025f45 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Fri, 10 Feb 2023 01:45:23 +0800 Subject: [PATCH 7/9] Add scada_data test --- test_tjnetwork.py | 122 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index eb0eab7..e8191a4 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -3843,5 +3843,127 @@ class TestApi: 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() From e5f67af36bdd7fcfe7f9d6e8de5d17c7e579bb29 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Fri, 10 Feb 2023 01:47:19 +0800 Subject: [PATCH 8/9] Add comment for iso time format --- tjnetwork.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tjnetwork.py b/tjnetwork.py index 00c9abb..bdfbf8a 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -761,5 +761,6 @@ 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) From 8c21ebb48c09139ea7e7c82f1f9d65e728252181 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Fri, 10 Feb 2023 01:47:46 +0800 Subject: [PATCH 9/9] Fix order --- tjnetwork.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tjnetwork.py b/tjnetwork.py index bdfbf8a..e9d4fe0 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -751,7 +751,7 @@ def delete_scada_model(name: str, cs: ChangeSet) -> ChangeSet: ############################################################ -# scada_data 29 +# scada_data 30 ############################################################ def get_scada_data_schema(name: str) -> dict[str, dict[str, Any]]: