Support to get all tags
This commit is contained in:
@@ -53,7 +53,7 @@ from .s7_valves import get_valve_schema, add_valve, get_valve, set_valve
|
||||
from .del_cmd import delete_valve_cascade
|
||||
|
||||
from .s8_tags import TAG_TYPE_NODE, TAG_TYPE_LINK
|
||||
from .s8_tags import get_tag_schema, get_tag, set_tag
|
||||
from .s8_tags import get_tag_schema, get_tags, get_tag, set_tag
|
||||
|
||||
from .s9_demands import get_demand_schema, get_demand, set_demand
|
||||
|
||||
|
||||
@@ -9,14 +9,25 @@ def get_tag_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
'tag' : {'type': 'str' , 'optional': True , 'readonly': False},}
|
||||
|
||||
|
||||
def get_tags(name: str) -> list[dict[str, Any]]:
|
||||
results: list[dict[str, Any]] = []
|
||||
rows = read_all(name, f"select * from tags_node")
|
||||
for row in rows:
|
||||
tag = str(row['tag']) if row['tag'] != None else None
|
||||
results.append({ 't_type': TAG_TYPE_NODE, 'id': str(row['id']), 'tag': tag })
|
||||
rows = read_all(name, f"select * from tags_link")
|
||||
for row in rows:
|
||||
tag = str(row['tag']) if row['tag'] != None else None
|
||||
results.append({ 't_type': TAG_TYPE_LINK, 'id': str(row['id']), 'tag': tag })
|
||||
return results
|
||||
|
||||
|
||||
def get_tag(name: str, t_type: str, id: str) -> dict[str, Any]:
|
||||
t = None
|
||||
if t_type == TAG_TYPE_NODE:
|
||||
t = try_read(name, f"select * from tags_node where id = '{id}'")
|
||||
elif t_type == TAG_TYPE_LINK:
|
||||
t = try_read(name, f"select * from tags_link where id = '{id}'")
|
||||
else:
|
||||
raise Exception('Only support NODE and Link')
|
||||
if t == None:
|
||||
return { 't_type': t_type, 'id': id, 'tag': None }
|
||||
d = {}
|
||||
|
||||
@@ -1998,10 +1998,19 @@ class TestApi:
|
||||
t = get_tag(p, TAG_TYPE_NODE, 'j1')
|
||||
assert t['tag'] == 'j1t'
|
||||
|
||||
tags = get_tags(p)
|
||||
assert len(tags) == 1
|
||||
assert tags[0]['t_type'] == TAG_TYPE_NODE
|
||||
assert tags[0]['id'] == 'j1'
|
||||
assert tags[0]['tag'] == 'j1t'
|
||||
|
||||
set_tag(p, ChangeSet({'t_type': TAG_TYPE_NODE, 'id': 'j1', 'tag': None }))
|
||||
t = get_tag(p, TAG_TYPE_NODE, 'j1')
|
||||
assert t['tag'] == None
|
||||
|
||||
tags = get_tags(p)
|
||||
assert len(tags) == 0
|
||||
|
||||
t = get_tag(p, TAG_TYPE_NODE, 'j2')
|
||||
assert t['tag'] == None
|
||||
set_tag(p, ChangeSet({'t_type': TAG_TYPE_NODE, 'id': 'j2', 'tag': 'j2t' }))
|
||||
@@ -2014,6 +2023,15 @@ class TestApi:
|
||||
t = get_tag(p, TAG_TYPE_LINK, 'p0')
|
||||
assert t['tag'] == 'p0t'
|
||||
|
||||
tags = get_tags(p)
|
||||
assert len(tags) == 2
|
||||
assert tags[0]['t_type'] == TAG_TYPE_NODE
|
||||
assert tags[0]['id'] == 'j2'
|
||||
assert tags[0]['tag'] == 'j2t'
|
||||
assert tags[1]['t_type'] == TAG_TYPE_LINK
|
||||
assert tags[1]['id'] == 'p0'
|
||||
assert tags[1]['tag'] == 'p0t'
|
||||
|
||||
self.leave(p)
|
||||
|
||||
|
||||
|
||||
@@ -492,6 +492,9 @@ def delete_valve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def get_tag_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
return api.get_tag_schema(name)
|
||||
|
||||
def get_tags(name: str) -> list[dict[str, Any]]:
|
||||
return api.get_tags(name)
|
||||
|
||||
def get_tag(name: str, t_type: str, id: str) -> dict[str, Any]:
|
||||
return api.get_tag(name, t_type, id)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user