gw and plot
This commit is contained in:
14
main.py
14
main.py
@ -37,10 +37,12 @@ class AppState(TypedDict):
|
||||
client: MqttClient
|
||||
message_queue: MemoryObjectReceiveStream[MqttMessage]
|
||||
task_group: TaskGroup
|
||||
history: dict[str, AwkwardArray]
|
||||
|
||||
|
||||
MQTT_BROKER: Final[str] = "192.168.2.189"
|
||||
MQTT_BROKER_PORT: Final[int] = 1883
|
||||
MAX_LENGTH = 600
|
||||
TOPIC: Final[str] = "GwData"
|
||||
NDArray = np.ndarray
|
||||
|
||||
@ -82,6 +84,7 @@ def resource(params: Any = None):
|
||||
"client": unwrap(client),
|
||||
"message_queue": rx,
|
||||
"task_group": unwrap(tg),
|
||||
"history": {},
|
||||
}
|
||||
return state
|
||||
|
||||
@ -165,9 +168,7 @@ def payload_to_hr(payload: bytes) -> int:
|
||||
def main():
|
||||
state = resource()
|
||||
logger.info("Resource created")
|
||||
history: dict[str, AwkwardArray] = {}
|
||||
|
||||
MAX_LENGTH = 500
|
||||
history = state["history"]
|
||||
|
||||
def push_new_message(message: GwMessage):
|
||||
dms = get_device_data(message)
|
||||
@ -193,9 +194,14 @@ def main():
|
||||
ak.to_parquet([history], filename)
|
||||
logger.info("Export to {}", filename)
|
||||
|
||||
export_btn = st.button(
|
||||
def on_clear():
|
||||
history.clear()
|
||||
logger.info("History cleared")
|
||||
|
||||
st.button(
|
||||
"Export", help="Export the current data to a parquet file", on_click=on_export
|
||||
)
|
||||
st.button("Clear", help="Clear the current data", on_click=on_clear)
|
||||
pannel = st.empty()
|
||||
while True:
|
||||
try:
|
||||
|
||||
@ -6,8 +6,59 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from awkward import Record as AwkwardRecord, Array as AwkwardArray\n",
|
||||
"import awkward as ak"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"FILENAME = \"export-2024-11-12-16-59-20.parquet\"\n",
|
||||
"data = ak.from_parquet(FILENAME)[0]\n",
|
||||
"display(data)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from typing import Literal\n",
|
||||
"import plotly.graph_objects as go\n",
|
||||
"import plotly.express as px\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def to_scatter(key: str, dev_history: AwkwardArray, plot_key: Literal[\"value\", \"rssi\"] = \"value\") -> go.Scatter:\n",
|
||||
" x = ak.to_numpy(dev_history[\"time\"])\n",
|
||||
" if plot_key == \"rssi\":\n",
|
||||
" y = ak.to_numpy(dev_history[\"rssi\"])\n",
|
||||
" elif plot_key == \"value\":\n",
|
||||
" y = ak.to_numpy(dev_history[\"value\"])\n",
|
||||
" else:\n",
|
||||
" raise ValueError(f\"Unknown plot_key: {plot_key}\")\n",
|
||||
" return go.Scatter(x=x, y=y, mode=\"lines+markers\", name=key)\n",
|
||||
"\n",
|
||||
"scatters: list[go.Scatter] = []\n",
|
||||
"for k in data.fields:\n",
|
||||
" val = data[k]\n",
|
||||
" scatters.append(to_scatter(k, val, \"value\"))\n",
|
||||
"\n",
|
||||
"fig = go.Figure(scatters)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"display(fig)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
@ -17,7 +68,15 @@
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.7"
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user