From 375001acd625d556f3592aa51ffe80c982d3295f Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Sat, 12 Nov 2022 16:21:49 +0800 Subject: [PATCH] Parse [STATUS] --- api/s10_status.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/api/s10_status.py b/api/s10_status.py index fbe2aec..987b43d 100644 --- a/api/s10_status.py +++ b/api/s10_status.py @@ -66,3 +66,58 @@ def set_status_cache(name: str, cs: ChangeSet) -> SqlChangeSet: 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] + 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 = {'operation': API_UPDATE, '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_valve(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