reconnect and connection cleanup working
This commit is contained in:
parent
2fba3350ec
commit
794bac9231
2 changed files with 66 additions and 3 deletions
16
sample.py
Normal file
16
sample.py
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import panel
|
||||||
|
|
||||||
|
from watt42_viewlib import attach_w42_state
|
||||||
|
|
||||||
|
w42_state = panel.rx(None)
|
||||||
|
attach_w42_state(rx_var=w42_state, system_id="fb2b91ce-383e-4356-96b3-b6405dacb353")
|
||||||
|
|
||||||
|
state_as_text = panel.bind(lambda s: f"State is {s}", w42_state)
|
||||||
|
|
||||||
|
state_pane = panel.pane.Markdown(state_as_text, sizing_mode='stretch_width')
|
||||||
|
|
||||||
|
_ = panel.template.FastListTemplate(
|
||||||
|
title="Sample W42 View App",
|
||||||
|
sidebar=[panel.pane.Markdown("This is a sample sidebar.")],
|
||||||
|
main=[panel.pane.Markdown("Welcome to the main content area!"), state_pane],
|
||||||
|
).servable()
|
||||||
|
|
@ -1,5 +1,52 @@
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
|
import panel
|
||||||
|
|
||||||
print(f"hello from viewlib")
|
from websockets import ConnectionClosed
|
||||||
|
from websockets.asyncio.client import connect
|
||||||
|
|
||||||
def greet(name: str) -> str:
|
def attach_w42_state(rx_var: panel.rx, system_id: str):
|
||||||
return f"Hello, {name}!"
|
|
||||||
|
WS_URL = "ws://localhost:8000/ws/systems" # TODO: make configurable
|
||||||
|
|
||||||
|
must_reconnect = True
|
||||||
|
|
||||||
|
async def subscribe_to_system():
|
||||||
|
|
||||||
|
print(f"Starting connection to {WS_URL} for system_id {system_id}")
|
||||||
|
|
||||||
|
async for websocket in connect(WS_URL):
|
||||||
|
try:
|
||||||
|
print(f"Connected to {WS_URL}")
|
||||||
|
send_response = await websocket.send(json.dumps({
|
||||||
|
"action": "subscribe",
|
||||||
|
"system_id": system_id
|
||||||
|
}))
|
||||||
|
print(f"Subscribed to system {system_id}, waiting for messages..., send_response={send_response}")
|
||||||
|
async for message in websocket:
|
||||||
|
as_json = json.loads(message)
|
||||||
|
rx_var.rx.value = as_json['change']['state']
|
||||||
|
except ConnectionClosed:
|
||||||
|
if must_reconnect:
|
||||||
|
print("connection closed, retrying...")
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
print("Not retrying connection.")
|
||||||
|
return
|
||||||
|
except Exception as e:
|
||||||
|
print(f"connection error, will retry: {e}")
|
||||||
|
await asyncio.sleep(2)
|
||||||
|
|
||||||
|
print(f"Attaching W42 state for system_id {system_id}")
|
||||||
|
panel.state.execute(subscribe_to_system)
|
||||||
|
|
||||||
|
async def session_destroyed():
|
||||||
|
print("Stop retrying connection (session destroyed)")
|
||||||
|
nonlocal must_reconnect
|
||||||
|
must_reconnect = False
|
||||||
|
|
||||||
|
def on_session_destroyed(session_id):
|
||||||
|
print(f"Session {session_id} destroyed")
|
||||||
|
panel.state.execute(session_destroyed)
|
||||||
|
|
||||||
|
panel.state.on_session_destroyed(on_session_destroyed)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue