gw and plot
This commit is contained in:
14
main.py
14
main.py
@ -37,10 +37,12 @@ class AppState(TypedDict):
|
|||||||
client: MqttClient
|
client: MqttClient
|
||||||
message_queue: MemoryObjectReceiveStream[MqttMessage]
|
message_queue: MemoryObjectReceiveStream[MqttMessage]
|
||||||
task_group: TaskGroup
|
task_group: TaskGroup
|
||||||
|
history: dict[str, AwkwardArray]
|
||||||
|
|
||||||
|
|
||||||
MQTT_BROKER: Final[str] = "192.168.2.189"
|
MQTT_BROKER: Final[str] = "192.168.2.189"
|
||||||
MQTT_BROKER_PORT: Final[int] = 1883
|
MQTT_BROKER_PORT: Final[int] = 1883
|
||||||
|
MAX_LENGTH = 600
|
||||||
TOPIC: Final[str] = "GwData"
|
TOPIC: Final[str] = "GwData"
|
||||||
NDArray = np.ndarray
|
NDArray = np.ndarray
|
||||||
|
|
||||||
@ -82,6 +84,7 @@ def resource(params: Any = None):
|
|||||||
"client": unwrap(client),
|
"client": unwrap(client),
|
||||||
"message_queue": rx,
|
"message_queue": rx,
|
||||||
"task_group": unwrap(tg),
|
"task_group": unwrap(tg),
|
||||||
|
"history": {},
|
||||||
}
|
}
|
||||||
return state
|
return state
|
||||||
|
|
||||||
@ -165,9 +168,7 @@ def payload_to_hr(payload: bytes) -> int:
|
|||||||
def main():
|
def main():
|
||||||
state = resource()
|
state = resource()
|
||||||
logger.info("Resource created")
|
logger.info("Resource created")
|
||||||
history: dict[str, AwkwardArray] = {}
|
history = state["history"]
|
||||||
|
|
||||||
MAX_LENGTH = 500
|
|
||||||
|
|
||||||
def push_new_message(message: GwMessage):
|
def push_new_message(message: GwMessage):
|
||||||
dms = get_device_data(message)
|
dms = get_device_data(message)
|
||||||
@ -193,9 +194,14 @@ def main():
|
|||||||
ak.to_parquet([history], filename)
|
ak.to_parquet([history], filename)
|
||||||
logger.info("Export to {}", 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
|
"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()
|
pannel = st.empty()
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@ -6,8 +6,59 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
"from awkward import Record as AwkwardRecord, Array as AwkwardArray\n",
|
||||||
"import awkward as ak"
|
"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": {
|
"metadata": {
|
||||||
@ -17,7 +68,15 @@
|
|||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.12.7"
|
"version": "3.12.7"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user