logger instead of print, prettier sample
This commit is contained in:
parent
794bac9231
commit
58c98b0119
2 changed files with 15 additions and 11 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import json
|
||||||
import panel
|
import panel
|
||||||
|
|
||||||
from watt42_viewlib import attach_w42_state
|
from watt42_viewlib import attach_w42_state
|
||||||
|
|
@ -5,12 +6,12 @@ from watt42_viewlib import attach_w42_state
|
||||||
w42_state = panel.rx(None)
|
w42_state = panel.rx(None)
|
||||||
attach_w42_state(rx_var=w42_state, system_id="fb2b91ce-383e-4356-96b3-b6405dacb353")
|
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_as_text = panel.bind(lambda s: f"W42 State:\n\n```\n{json.dumps(s, indent=2)}\n```\n\nReplace this with some awesome visuals", w42_state)
|
||||||
|
|
||||||
state_pane = panel.pane.Markdown(state_as_text, sizing_mode='stretch_width')
|
state_pane = panel.pane.Markdown(state_as_text, sizing_mode='stretch_width')
|
||||||
|
|
||||||
_ = panel.template.FastListTemplate(
|
_ = panel.template.FastListTemplate(
|
||||||
title="Sample W42 View App",
|
title="Sample W42 View App",
|
||||||
sidebar=[panel.pane.Markdown("This is a sample sidebar.")],
|
sidebar=[panel.pane.Markdown("This is a sample sidebar.")],
|
||||||
main=[panel.pane.Markdown("Welcome to the main content area!"), state_pane],
|
main=[panel.pane.Markdown("# Welcome to the Main Content Area"), state_pane],
|
||||||
).servable()
|
).servable()
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
import panel
|
import panel
|
||||||
|
|
||||||
from websockets import ConnectionClosed
|
from websockets import ConnectionClosed
|
||||||
from websockets.asyncio.client import connect
|
from websockets.asyncio.client import connect
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def attach_w42_state(rx_var: panel.rx, system_id: str):
|
def attach_w42_state(rx_var: panel.rx, system_id: str):
|
||||||
|
|
||||||
WS_URL = "ws://localhost:8000/ws/systems" # TODO: make configurable
|
WS_URL = "ws://localhost:8000/ws/systems" # TODO: make configurable
|
||||||
|
|
@ -13,40 +16,40 @@ def attach_w42_state(rx_var: panel.rx, system_id: str):
|
||||||
|
|
||||||
async def subscribe_to_system():
|
async def subscribe_to_system():
|
||||||
|
|
||||||
print(f"Starting connection to {WS_URL} for system_id {system_id}")
|
logger.info(f"Starting connection to {WS_URL} for system_id {system_id}")
|
||||||
|
|
||||||
async for websocket in connect(WS_URL):
|
async for websocket in connect(WS_URL):
|
||||||
try:
|
try:
|
||||||
print(f"Connected to {WS_URL}")
|
logger.info(f"Connected to {WS_URL}")
|
||||||
send_response = await websocket.send(json.dumps({
|
send_response = await websocket.send(json.dumps({
|
||||||
"action": "subscribe",
|
"action": "subscribe",
|
||||||
"system_id": system_id
|
"system_id": system_id
|
||||||
}))
|
}))
|
||||||
print(f"Subscribed to system {system_id}, waiting for messages..., send_response={send_response}")
|
logger.info(f"Subscribed to system {system_id}, waiting for messages..., send_response={send_response}")
|
||||||
async for message in websocket:
|
async for message in websocket:
|
||||||
as_json = json.loads(message)
|
as_json = json.loads(message)
|
||||||
rx_var.rx.value = as_json['change']['state']
|
rx_var.rx.value = as_json['change']['state']
|
||||||
except ConnectionClosed:
|
except ConnectionClosed:
|
||||||
if must_reconnect:
|
if must_reconnect:
|
||||||
print("connection closed, retrying...")
|
logger.info("connection closed, retrying...")
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
print("Not retrying connection.")
|
logger.info("Not retrying connection.")
|
||||||
return
|
return
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"connection error, will retry: {e}")
|
logger.info(f"connection error, will retry: {e}")
|
||||||
await asyncio.sleep(2)
|
await asyncio.sleep(2)
|
||||||
|
|
||||||
print(f"Attaching W42 state for system_id {system_id}")
|
logger.info(f"Attaching W42 state for system_id {system_id}")
|
||||||
panel.state.execute(subscribe_to_system)
|
panel.state.execute(subscribe_to_system)
|
||||||
|
|
||||||
async def session_destroyed():
|
async def session_destroyed():
|
||||||
print("Stop retrying connection (session destroyed)")
|
logger.info("Stop retrying connection (session destroyed)")
|
||||||
nonlocal must_reconnect
|
nonlocal must_reconnect
|
||||||
must_reconnect = False
|
must_reconnect = False
|
||||||
|
|
||||||
def on_session_destroyed(session_id):
|
def on_session_destroyed(session_id):
|
||||||
print(f"Session {session_id} destroyed")
|
logger.info(f"Session {session_id} destroyed")
|
||||||
panel.state.execute(session_destroyed)
|
panel.state.execute(session_destroyed)
|
||||||
|
|
||||||
panel.state.on_session_destroyed(on_session_destroyed)
|
panel.state.on_session_destroyed(on_session_destroyed)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue