Files
TJWaterServer/api/s10_status.py
2022-12-17 09:23:08 +08:00

124 lines
4.4 KiB
Python

from .operation import *
LINK_STATUS_OPEN = 'OPEN'
LINK_STATUS_CLOSED = 'CLOSED'
LINK_STATUS_ACTIVE = 'ACTIVE'
def get_status_schema(name: str) -> dict[str, dict[str, Any]]:
return { 'link' : {'type': 'str' , 'optional': False , 'readonly': True },
'status' : {'type': 'str' , 'optional': True , 'readonly': False},
'setting' : {'type': 'float' , 'optional': True , 'readonly': False} }
def get_status(name: str, link: str) -> dict[str, Any]:
s = try_read(name, f"select * from status where link = '{link}'")
if s == None:
return { 'link': link, 'status': None, 'setting': None }
d = {}
d['link'] = str(s['link'])
d['status'] = str(s['status']) if s['status'] != None else None
d['setting'] = float(s['setting']) if s['setting'] != None else None
return d
class Status(object):
def __init__(self, input: dict[str, Any]) -> None:
self.type = 'status'
self.link = str(input['link'])
self.status = str(input['status']) if 'status' in input and input['status'] != None else None
self.setting = float(input['setting']) if 'setting' in input and input['setting'] != None else None
self.f_type = f"'{self.type}'"
self.f_link = f"'{self.link}'"
self.f_status = f"'{self.status}'" if self.status != None else 'null'
self.f_setting = self.setting if self.setting != None else 'null'
def as_dict(self) -> dict[str, Any]:
return { 'type': self.type, 'link': self.link, 'status': self.status, 'setting': self.setting }
def set_status_cache(name: str, cs: ChangeSet) -> DbChangeSet:
old = Status(get_status(name, cs.operations[0]['link']))
raw_new = get_status(name, cs.operations[0]['link'])
new_dict = cs.operations[0]
schema = get_status_schema(name)
for key, value in schema.items():
if key in new_dict and not value['readonly']:
raw_new[key] = new_dict[key]
new = Status(raw_new)
redo_sql = f"delete from status where link = {new.f_link};"
if new.status != None or new.setting != None:
redo_sql += f"\ninsert into status (link, status, setting) values ({new.f_link}, {new.f_status}, {new.f_setting});"
undo_sql = f"delete from status where link = {old.f_link};"
if old.status != None or old.setting != None:
undo_sql += f"\ninsert into status (link, status, setting) values ({old.f_link}, {old.f_status}, {old.f_setting});"
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_status(name: str, cs: ChangeSet) -> ChangeSet:
return execute_command(name, set_status_cache(name, cs))
class InpStatus:
def __init__(self, line: str) -> None:
tokens = line.split()
num = len(tokens)
has_desc = tokens[-1].startswith(';')
num_without_desc = (num - 1) if has_desc else num
self.link = str(tokens[0])
self.value = tokens[1].upper()
self.is_status = True
if self.value == LINK_STATUS_OPEN or self.value == LINK_STATUS_CLOSED or self.value == LINK_STATUS_ACTIVE:
self.status = str(self.value)
else:
self.setting = float(self.value)
self.is_status = False
def inp_in_status(section: list[str]) -> ChangeSet:
objs: dict[str, list[InpStatus]] = {}
for s in section:
# skip comment
if s.startswith(';'):
continue
obj = InpStatus(s)
if obj.link not in objs:
objs[obj.link] = []
objs[obj.link].append(obj)
cs = ChangeSet()
for link, values in objs.items():
obj_cs : dict[str, Any] = g_update_prefix | {'type': 'status', 'link' : link, 'status': None, 'setting': None}
for obj in values:
if obj.is_status:
obj_cs['status'] = obj.status
else:
obj_cs['setting'] = obj.setting
cs.append(obj_cs)
return cs
def inp_out_status(name: str) -> list[str]:
lines = []
objs = read_all(name, 'select * from status')
for obj in objs:
link = obj['link']
status = obj['status'] if obj['status'] != None else ''
setting = obj['setting'] if obj['setting'] != None else ''
if status != '':
lines.append(f'{link} {status}')
if setting != '':
lines.append(f'{link} {setting}')
return lines