From 6ba4dc2ed7b04be757cab18532ca8ff36eb0c9f6 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Thu, 16 Feb 2023 21:20:21 +0800 Subject: [PATCH 1/6] Add 'status' field to scada_model --- script/sql/create/29.scada_model.sql | 2 ++ script/sql/drop/29.scada_model.sql | 2 ++ 2 files changed, 4 insertions(+) diff --git a/script/sql/create/29.scada_model.sql b/script/sql/create/29.scada_model.sql index 397683d..1460b26 100644 --- a/script/sql/create/29.scada_model.sql +++ b/script/sql/create/29.scada_model.sql @@ -1,4 +1,5 @@ create type scada_type as enum ('PRESSURE', 'DEMAND', 'QUALITY', 'LEVEL', 'FLOW'); +create type scada_status as enum ('OFF', 'ON'); create table scada_model ( @@ -10,4 +11,5 @@ create table scada_model , address text , type scada_type , model_id varchar(32) -- add constraint in API +, status scada_status not null default 'OFF' ); diff --git a/script/sql/drop/29.scada_model.sql b/script/sql/drop/29.scada_model.sql index 262077a..fe44555 100644 --- a/script/sql/drop/29.scada_model.sql +++ b/script/sql/drop/29.scada_model.sql @@ -1,3 +1,5 @@ drop table if exists scada_model; +drop type if exists scada_status; + drop type if exists scada_type; From e62107da0796653d624de95583b590d08b206f57 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Thu, 16 Feb 2023 21:21:21 +0800 Subject: [PATCH 2/6] Support scada_model.status in API --- api/__init__.py | 1 + api/s29_scada_model.py | 20 ++++++++++++++------ tjnetwork.py | 3 +++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 7c33afd..3dcd269 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -100,6 +100,7 @@ from .s26_labels import get_label_schema, get_label, set_label, add_label, delet from .s27_backdrop import get_backdrop_schema, get_backdrop, set_backdrop from .s29_scada_model import SCADA_TYPE_PRESSURE, SCADA_TYPE_DEMAND, SCADA_TYPE_QUALITY, SCADA_TYPE_LEVEL, SCADA_TYPE_FLOW +from .s29_scada_model import SCADA_STATUS_OFFLINE, SCADA_STATUS_ONLINE from .s29_scada_model import get_scada_model_schema, get_scada_model, set_scada_model, add_scada_model, delete_scada_model from .s30_scada_data import get_scada_data_schema, get_scada_data, set_scada_data diff --git a/api/s29_scada_model.py b/api/s29_scada_model.py index 561914d..825c05d 100644 --- a/api/s29_scada_model.py +++ b/api/s29_scada_model.py @@ -7,6 +7,10 @@ SCADA_TYPE_QUALITY = 'QUALITY' SCADA_TYPE_LEVEL = 'LEVEL' SCADA_TYPE_FLOW = 'FLOW' +SCADA_STATUS_OFFLINE = 'OFF' +SCADA_STATUS_ONLINE = 'ON' + + def _check_model_id(name: str, cs: ChangeSet) -> bool: if 'model_id' not in cs.operations[0]: return True @@ -24,7 +28,8 @@ def get_scada_model_schema(name: str) -> dict[str, dict[str, Any]]: 'device_name' : {'type': 'str' , 'optional': True , 'readonly': False}, 'address' : {'type': 'str' , 'optional': True , 'readonly': False}, 'sm_type' : {'type': 'str' , 'optional': True , 'readonly': False}, - 'model_id' : {'type': 'str' , 'optional': True , 'readonly': False} } + 'model_id' : {'type': 'str' , 'optional': True , 'readonly': False}, + 'status' : {'type': 'str' , 'optional': True , 'readonly': False} } def get_scada_model(name: str, id: str) -> dict[str, Any]: @@ -40,6 +45,7 @@ def get_scada_model(name: str, id: str) -> dict[str, Any]: d['address'] = str(sm['address']) if sm['address'] != None else None d['sm_type'] = str(sm['type']) if sm['type'] != None else None d['model_id'] = str(sm['model_id']) if sm['model_id'] != None else None + d['status'] = str(sm['status']) return d @@ -54,6 +60,7 @@ class ScadaModel(object): self.address = str(input['address']) if 'address' in input and input['address'] != None else None self.sm_type = str(input['sm_type']) if 'sm_type' in input and input['sm_type'] != None else None self.model_id = str(input['model_id']) if 'model_id' in input and input['model_id'] != None else None + self.status = str(input['status']) if 'status' in input and input['status'] != None else SCADA_STATUS_OFFLINE self.f_type = f"'{self.type}'" self.f_id = f"'{self.id}'" @@ -64,9 +71,10 @@ class ScadaModel(object): self.f_address = f"'{self.address}'" if self.address != None else 'null' self.f_sm_type = f"'{self.sm_type}'" if self.sm_type != None else 'null' self.f_model_id = f"'{self.model_id}'" if self.model_id != None else 'null' + self.f_status = f"'{self.status}'" def as_dict(self) -> dict[str, Any]: - return { 'type': self.type, 'id': self.id, 'x': self.x, 'y': self.y, 'device_id': self.device_id, 'device_name': self.device_name, 'address': self.address, 'sm_type': self.sm_type, 'model_id': self.model_id } + return { 'type': self.type, 'id': self.id, 'x': self.x, 'y': self.y, 'device_id': self.device_id, 'device_name': self.device_name, 'address': self.address, 'sm_type': self.sm_type, 'model_id': self.model_id, 'status': self.status } def as_id_dict(self) -> dict[str, Any]: return { 'type': self.type, 'id': self.id } @@ -83,8 +91,8 @@ def set_scada_model_cmd(name: str, cs: ChangeSet) -> DbChangeSet: raw_new[key] = new_dict[key] new = ScadaModel(raw_new) - redo_sql = f"update scada_model set x = {new.f_x}, y = {new.f_y}, device_id = {new.f_device_id}, device_name = {new.f_device_name}, address = {new.f_address}, type = {new.f_sm_type}, model_id = {new.f_model_id} where id = {new.f_id};" - undo_sql = f"update scada_model set x = {old.f_x}, y = {old.f_y}, device_id = {old.f_device_id}, device_name = {old.f_device_name}, address = {old.f_address}, type = {old.f_sm_type}, model_id = {old.f_model_id} where id = {old.f_id};" + redo_sql = f"update scada_model set x = {new.f_x}, y = {new.f_y}, device_id = {new.f_device_id}, device_name = {new.f_device_name}, address = {new.f_address}, type = {new.f_sm_type}, model_id = {new.f_model_id}, status = {new.f_status} where id = {new.f_id};" + undo_sql = f"update scada_model set x = {old.f_x}, y = {old.f_y}, device_id = {old.f_device_id}, device_name = {old.f_device_name}, address = {old.f_address}, type = {old.f_sm_type}, model_id = {old.f_model_id}, status = {old.f_status} where id = {old.f_id};" redo_cs = g_update_prefix | new.as_dict() undo_cs = g_update_prefix | old.as_dict() @@ -103,7 +111,7 @@ def set_scada_model(name: str, cs: ChangeSet) -> ChangeSet: def add_scada_model_cmd(name: str, cs: ChangeSet) -> DbChangeSet: new = ScadaModel(cs.operations[0]) - redo_sql = f"insert into scada_model (id, x, y, device_id, device_name, address, type, model_id) values ({new.f_id}, {new.f_x}, {new.f_y}, {new.f_device_id}, {new.f_device_name}, {new.f_address}, {new.f_sm_type}, {new.f_model_id});" + redo_sql = f"insert into scada_model (id, x, y, device_id, device_name, address, type, model_id, status) values ({new.f_id}, {new.f_x}, {new.f_y}, {new.f_device_id}, {new.f_device_name}, {new.f_address}, {new.f_sm_type}, {new.f_model_id}, {new.f_status});" undo_sql = f"delete from scada_model where id = {new.f_id};" redo_cs = g_add_prefix | new.as_dict() @@ -124,7 +132,7 @@ def delete_scada_model_cmd(name: str, cs: ChangeSet) -> DbChangeSet: old = ScadaModel(get_scada_model(name, cs.operations[0]['id'])) redo_sql = f"delete from scada_model where id = {old.f_id};" - undo_sql = f"insert into scada_model (id, x, y, device_id, device_name, address, type, model_id) values ({old.f_id}, {old.f_x}, {old.f_y}, {old.f_device_id}, {old.f_device_name}, {old.f_address}, {old.f_sm_type}, {old.f_model_id});" + undo_sql = f"insert into scada_model (id, x, y, device_id, device_name, address, type, model_id, status) values ({old.f_id}, {old.f_x}, {old.f_y}, {old.f_device_id}, {old.f_device_name}, {old.f_address}, {old.f_sm_type}, {old.f_model_id}, {old.f_status});" redo_cs = g_delete_prefix | old.as_id_dict() undo_cs = g_add_prefix | old.as_dict() diff --git a/tjnetwork.py b/tjnetwork.py index e9d4fe0..4a47c84 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -101,6 +101,9 @@ SCADA_TYPE_QUALITY = api.SCADA_TYPE_QUALITY SCADA_TYPE_LEVEL = api.SCADA_TYPE_LEVEL SCADA_TYPE_FLOW = api.SCADA_TYPE_FLOW +SCADA_STATUS_ONLINE = api.SCADA_STATUS_ONLINE +SCADA_STATUS_OFFLINE = api.SCADA_STATUS_OFFLINE + ############################################################ # project From 93f5321c89d0611ef635ad33ea091ed01ab0b09a Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Thu, 16 Feb 2023 21:21:37 +0800 Subject: [PATCH 3/6] Test scada_model.status --- test_tjnetwork.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index e8191a4..8697c92 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -3677,6 +3677,7 @@ class TestApi: assert sm['address'] == None assert sm['sm_type'] == None assert sm['model_id'] == None + assert sm['status'] == SCADA_STATUS_OFFLINE 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') @@ -3696,8 +3697,9 @@ class TestApi: assert sm1['address'] == 'xxx' assert sm1['sm_type'] == SCADA_TYPE_PRESSURE assert sm1['model_id'] == 'j1' + assert sm1['status'] == SCADA_STATUS_OFFLINE - 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'})) + 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', 'status': SCADA_STATUS_ONLINE})) sm2 = get_scada_model(p, 'sm2') assert sm2['id'] == 'sm2' assert sm2['x'] == 0.0 @@ -3707,8 +3709,9 @@ class TestApi: assert sm2['address'] == 'xxx' assert sm2['sm_type'] == SCADA_TYPE_PRESSURE assert sm2['model_id'] == 'p1' + assert sm2['status'] == SCADA_STATUS_ONLINE - set_scada_model(p, ChangeSet({'id': 'sm', 'device_name': 'sm_device_name', 'address': 'xxx', 'sm_type': SCADA_TYPE_PRESSURE, 'model_id': 'j0'})) + set_scada_model(p, ChangeSet({'id': 'sm', 'device_name': 'sm_device_name', 'address': 'xxx', 'sm_type': SCADA_TYPE_PRESSURE, 'model_id': 'j0', 'status': SCADA_STATUS_ONLINE})) sm = get_scada_model(p, 'sm') assert sm['id'] == 'sm' assert sm['x'] == 0.0 @@ -3718,8 +3721,9 @@ class TestApi: assert sm['address'] == None assert sm['sm_type'] == None assert sm['model_id'] == None + assert sm['status'] == SCADA_STATUS_OFFLINE - set_scada_model(p, ChangeSet({'id': 'sm', 'device_name': 'sm_device_name', 'address': 'xxx', 'sm_type': SCADA_TYPE_PRESSURE, 'model_id': 'j1'})) + set_scada_model(p, ChangeSet({'id': 'sm', 'device_name': 'sm_device_name', 'address': 'xxx', 'sm_type': SCADA_TYPE_PRESSURE, 'model_id': 'j1', 'status': SCADA_STATUS_ONLINE})) sm = get_scada_model(p, 'sm') assert sm['id'] == 'sm' assert sm['x'] == 0.0 @@ -3729,6 +3733,7 @@ class TestApi: assert sm['address'] == 'xxx' assert sm['sm_type'] == SCADA_TYPE_PRESSURE assert sm['model_id'] == 'j1' + assert sm['status'] == SCADA_STATUS_ONLINE delete_scada_model(p, ChangeSet({'id': 'sm'})) sm = get_scada_model(p, 'sm') @@ -3753,7 +3758,7 @@ class TestApi: 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] + 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', 'status': SCADA_STATUS_ONLINE})).operations[0] assert cs['operation'] == API_ADD assert cs['type'] == 'scada_model' assert cs['id'] == 'sm' @@ -3764,6 +3769,7 @@ class TestApi: assert cs['address'] == 'xxx' assert cs['sm_type'] == SCADA_TYPE_PRESSURE assert cs['model_id'] == 'j1' + assert cs['status'] == SCADA_STATUS_ONLINE cs = execute_undo(p).operations[0] assert cs['operation'] == API_DELETE @@ -3781,8 +3787,9 @@ class TestApi: assert cs['address'] == 'xxx' assert cs['sm_type'] == SCADA_TYPE_PRESSURE assert cs['model_id'] == 'j1' + assert cs['status'] == SCADA_STATUS_ONLINE - 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] + cs = set_scada_model(p, ChangeSet({'id': 'sm', 'device_name': 'sm_device_name_', 'address': 'xxx_', 'sm_type': SCADA_TYPE_DEMAND, 'model_id': 'j2', 'status': SCADA_STATUS_OFFLINE})).operations[0] assert cs['operation'] == API_UPDATE assert cs['type'] == 'scada_model' assert cs['id'] == 'sm' @@ -3793,6 +3800,7 @@ class TestApi: assert cs['address'] == 'xxx_' assert cs['sm_type'] == SCADA_TYPE_DEMAND assert cs['model_id'] == 'j2' + assert cs['status'] == SCADA_STATUS_OFFLINE cs = execute_undo(p).operations[0] assert cs['operation'] == API_UPDATE @@ -3805,6 +3813,7 @@ class TestApi: assert cs['address'] == 'xxx' assert cs['sm_type'] == SCADA_TYPE_PRESSURE assert cs['model_id'] == 'j1' + assert cs['status'] == SCADA_STATUS_ONLINE cs = execute_redo(p).operations[0] assert cs['operation'] == API_UPDATE @@ -3817,6 +3826,7 @@ class TestApi: assert cs['address'] == 'xxx_' assert cs['sm_type'] == SCADA_TYPE_DEMAND assert cs['model_id'] == 'j2' + assert cs['status'] == SCADA_STATUS_OFFLINE cs = delete_scada_model(p, ChangeSet({'id': 'sm'})).operations[0] assert cs['operation'] == API_DELETE @@ -3834,6 +3844,7 @@ class TestApi: assert cs['address'] == 'xxx_' assert cs['sm_type'] == SCADA_TYPE_DEMAND assert cs['model_id'] == 'j2' + assert cs['status'] == SCADA_STATUS_OFFLINE cs = execute_redo(p).operations[0] assert cs['operation'] == API_DELETE From 11a5bb43330c3b5144bfd3bbbf6f6db51e8df749 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Thu, 16 Feb 2023 22:14:07 +0800 Subject: [PATCH 4/6] Add more api for scada_data --- api/__init__.py | 2 +- api/s30_scada_data.py | 42 ++++++++++++++++++++++++++++++++++++++++++ tjnetwork.py | 6 ++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/api/__init__.py b/api/__init__.py index 3dcd269..bdd2505 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -103,4 +103,4 @@ from .s29_scada_model import SCADA_TYPE_PRESSURE, SCADA_TYPE_DEMAND, SCADA_TYPE_ from .s29_scada_model import SCADA_STATUS_OFFLINE, SCADA_STATUS_ONLINE from .s29_scada_model import get_scada_model_schema, get_scada_model, set_scada_model, add_scada_model, delete_scada_model -from .s30_scada_data import get_scada_data_schema, get_scada_data, set_scada_data +from .s30_scada_data import get_scada_data_schema, get_scada_data, set_scada_data, add_scada_data, delete_scada_data diff --git a/api/s30_scada_data.py b/api/s30_scada_data.py index e222131..14f8f8b 100644 --- a/api/s30_scada_data.py +++ b/api/s30_scada_data.py @@ -49,3 +49,45 @@ def set_scada_data(name: str, cs: ChangeSet) -> ChangeSet: 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)) diff --git a/tjnetwork.py b/tjnetwork.py index 4a47c84..ffbba3d 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -767,3 +767,9 @@ def get_scada_data(name: str, device_id: str) -> dict[str, Any]: # 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) + +def add_scada_data(name: str, cs: ChangeSet) -> ChangeSet: + return api.add_scada_data(name, cs) + +def delete_scada_data(name: str, cs: ChangeSet) -> ChangeSet: + return api.delete_scada_data(name, cs) From 3c7903e74231283a0062c2baefbac34cb0f01750 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Thu, 16 Feb 2023 22:14:31 +0800 Subject: [PATCH 5/6] Test more api for scada_data --- test_tjnetwork.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index 8697c92..18a4310 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -3885,6 +3885,46 @@ class TestApi: assert sa['data'][1]['time'] == '2023-02-10 00:03:22' assert sa['data'][1]['value'] == 200.0 + add_scada_data(p, ChangeSet({'device_id': 'sm_device', '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']) == 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 + + add_scada_data(p, ChangeSet({'device_id': 'sm_device', 'time': '2023-02-11 00:02:22', 'value': 100.0})) + sa = get_scada_data(p, 'sm_device') + assert sa['device_id'] == 'sm_device' + assert len(sa['data']) == 3 + 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 + assert sa['data'][2]['time'] == '2023-02-11 00:02:22' + assert sa['data'][2]['value'] == 100.0 + + delete_scada_data(p, ChangeSet({'device_id': 'sm_device', 'time': '2023-02-12 00:02:22'})) + sa = get_scada_data(p, 'sm_device') + assert sa['device_id'] == 'sm_device' + assert len(sa['data']) == 3 + 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 + assert sa['data'][2]['time'] == '2023-02-11 00:02:22' + assert sa['data'][2]['value'] == 100.0 + + delete_scada_data(p, ChangeSet({'device_id': 'sm_device', 'time': '2023-02-11 00:02:22'})) + 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' @@ -3951,6 +3991,51 @@ class TestApi: assert cs['data'][1]['time'] == '2023-02-10 00:03:22' assert cs['data'][1]['value'] == 200.0 + cs = add_scada_data(p, ChangeSet({'device_id': 'sm_device', 'time': '2023-02-10 00:02:22', 'value': 100.0})) + assert len(cs.operations) == 0 + + cs = add_scada_data(p, ChangeSet({'device_id': 'sm_device', 'time': '2023-02-11 00:02:22', 'value': 100.0})).operations[0] + assert cs['operation'] == API_ADD + assert cs['type'] == 'scada_data' + assert cs['device_id'] == 'sm_device' + assert cs['time'] == '2023-02-11 00:02:22' + assert cs['value'] == 100.0 + + cs = execute_undo(p).operations[0] + assert cs['operation'] == API_DELETE + assert cs['type'] == 'scada_data' + assert cs['device_id'] == 'sm_device' + assert cs['time'] == '2023-02-11 00:02:22' + + cs = execute_redo(p).operations[0] + assert cs['operation'] == API_ADD + assert cs['type'] == 'scada_data' + assert cs['device_id'] == 'sm_device' + assert cs['time'] == '2023-02-11 00:02:22' + assert cs['value'] == 100.0 + + cs = delete_scada_data(p, ChangeSet({'device_id': 'sm_device', 'time': '2023-02-12 00:02:22'})) + assert len(cs.operations) == 0 + + cs = delete_scada_data(p, ChangeSet({'device_id': 'sm_device', 'time': '2023-02-11 00:02:22'})).operations[0] + assert cs['operation'] == API_DELETE + assert cs['type'] == 'scada_data' + assert cs['device_id'] == 'sm_device' + assert cs['time'] == '2023-02-11 00:02:22' + + cs = execute_undo(p).operations[0] + assert cs['operation'] == API_ADD + assert cs['type'] == 'scada_data' + assert cs['device_id'] == 'sm_device' + assert cs['time'] == '2023-02-11 00:02:22' + assert cs['value'] == 100.0 + + cs = execute_redo(p).operations[0] + assert cs['operation'] == API_DELETE + assert cs['type'] == 'scada_data' + assert cs['device_id'] == 'sm_device' + assert cs['time'] == '2023-02-11 00:02:22' + cs = set_scada_data(p, ChangeSet({'device_id': 'sm_device', 'data': []})).operations[0] assert cs['operation'] == API_UPDATE assert cs['type'] == 'scada_data' From 8f55b21275e87beb19d961cb9c3fe932a1d262ca Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Thu, 16 Feb 2023 22:18:51 +0800 Subject: [PATCH 6/6] Support to set restore operation --- api/__init__.py | 2 +- api/database.py | 4 ++++ api/parser.py | 2 +- tjnetwork.py | 3 +++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index bdd2505..f9e7c95 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -9,7 +9,7 @@ from .database import ChangeSet from .database import get_current_operation from .database import execute_undo, execute_redo from .database import have_snapshot, take_snapshot, pick_snapshot -from .database import pick_operation, sync_with_server, get_restore_operation +from .database import pick_operation, sync_with_server, get_restore_operation, set_restore_operation from .batch_cmd import execute_batch_command from .batch_cmds import execute_batch_commands diff --git a/api/database.py b/api/database.py index f2cba43..6a2d167 100644 --- a/api/database.py +++ b/api/database.py @@ -278,3 +278,7 @@ def sync_with_server(name: str, operation: int) -> ChangeSet: def get_restore_operation(name: str) -> int: return read(name, f'select * from restore_operation')['id'] + + +def set_restore_operation(name: str, operation: int) -> None: + write(name, f'update restore_operation set id = {operation}') diff --git a/api/parser.py b/api/parser.py index 8187882..2b3b009 100644 --- a/api/parser.py +++ b/api/parser.py @@ -229,7 +229,7 @@ def read_inp(project: str, inp: str): execute_batch_commands(project, _read_inp(inp)) op = get_current_operation(project) - write(project, f'update restore_operation set id = {op}') + set_restore_operation(project, op) def dump_inp(project: str, inp: str): diff --git a/tjnetwork.py b/tjnetwork.py index ffbba3d..06c6b45 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -193,6 +193,9 @@ def execute_batch_commands(name: str, cs: ChangeSet) -> ChangeSet: def get_restore_operation(name: str) -> int: return api.get_restore_operation(name) +def set_restore_operation(name: str, operation: int) -> None: + return api.set_restore_operation(name, operation) + ############################################################ # type