script/dnet: pass Model data to View

This commit is contained in:
lunar-mining
2023-09-09 08:40:59 +02:00
parent 12df4958f5
commit 291f80b9b0
3 changed files with 25 additions and 61 deletions

View File

@@ -43,9 +43,10 @@ class Dnetview:
pass
data = await rpc._make_request("p2p.get_info", [])
info[name] = data
try:
self.queue.put_nowait(data)
self.queue.put_nowait(info)
except:
logging.debug("subscribe().put_nowait(): QueueFull")
@@ -55,10 +56,11 @@ class Dnetview:
while True:
data = await rpc.reader.readline()
data = json.loads(data)
info[name] = data
#logging.debug(f"events: {data}")
try:
self.queue.put_nowait(data)
self.queue.put_nowait(info)
except:
logging.debug("subscribe().putnowait(): QueueFull")
@@ -76,13 +78,22 @@ class Dnetview:
for i, node in enumerate(nodes):
rpc = JsonRpc()
subscribe = tg.create_task(self.subscribe(rpc, node['name'], node['port']))
update = tg.create_task(self.update_model())
nodes = tg.create_task(self.update_info())
async def update_model(self):
async def update_info(self):
while True:
try:
item = await self.queue.get()
logging.debug(f"result: {item}")
info = await self.queue.get()
values = list(info.values())[0]
# Update node info
if "result" in values:
self.model.update(info)
# Update event info: TODO
if "params" in values:
logging.debug("update_info(): Event detected")
self.queue.task_done()
except self.queue.is_empty():
logging.debug("update_model(): QueueEmpty")

View File

@@ -21,58 +21,10 @@ class Model:
def __init__(self):
self.nodes = {}
def update(self, new_node):
self.nodes.update(new_node)
def update(self, node):
name = list(node.keys())[0]
values = list(node.values())[0]
self.nodes[name] = values
def __repr__(self):
return f"{self.nodes}"
class NodeInfo():
def __init__(self, channels, slots):
self.node = {}
inbound = {}
outbounds = {"slots": []}
manual = {}
seed = {}
for name, channels in info.items():
channel_lookup = {}
for channel in channels:
id = channel["id"]
channel_lookup[id] = channel
for channel in channels:
if channel["session"] != "inbound":
continue
url = channel["url"]
inbound["inbound"] = url
for i, id in enumerate(slots):
if id == 0:
outbounds["slots"].append(f"{i}: none")
continue
assert id in channel_lookup
url = channel_lookup[id]["url"]
outbounds["slots"].append(f"{i}: {url}")
for channel in channels:
if channel["session"] != "seed":
continue
url = channel["url"]
seed["seed"] = url
for channel in channels:
if channel["session"] != "manual":
continue
url = channel["url"]
manual["manual"] = url
self.node[name] = [inbound, outbounds, manual,
seed]
def __repr__(self):
return f"{self.node}"

View File

@@ -20,7 +20,7 @@ import logging
import asyncio
from scroll import ScrollBar, Scrollable
from model import NodeInfo
from model import Model
event_loop = asyncio.get_event_loop()
@@ -108,7 +108,7 @@ class View():
("line","dark cyan","black","standout"),
]
def __init__(self, data=NodeInfo):
def __init__(self, data):
#logging.debug(f"dnetview init {data}")
info_text = urwid.Text("")
@@ -132,8 +132,9 @@ class View():
columns = urwid.Columns([leftbox, rightbox], focus_column=0)
self.ui = urwid.Frame(urwid.AttrWrap( columns, 'body' ))
async def update_view(self, data=NodeInfo):
async def update_view(self, data):
while True:
logging.debug(f"update_view() received model: {data}")
await asyncio.sleep(0.1)
self.service_info = urwid.Text("")