From 287b13c529ef7832b584d747eb28f001d78838b3 Mon Sep 17 00:00:00 2001 From: crosstyan Date: Tue, 12 Nov 2024 17:11:32 +0800 Subject: [PATCH] gw and plot --- main.py | 14 ++++++++--- parse_exported.ipynb | 59 ++++++++++++++++++++++++++++++++++++++++++++ run.sh | 1 + 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100755 run.sh diff --git a/main.py b/main.py index 2b032b1..934a634 100644 --- a/main.py +++ b/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: diff --git a/parse_exported.ipynb b/parse_exported.ipynb index 7c7c7b3..f25dcbc 100644 --- a/parse_exported.ipynb +++ b/parse_exported.ipynb @@ -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" } }, diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..086a355 --- /dev/null +++ b/run.sh @@ -0,0 +1 @@ +python -m streamlit run main.py \ No newline at end of file