-
Notifications
You must be signed in to change notification settings - Fork 303
Telemetry UI #1361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Hunter275
wants to merge
3
commits into
meshtastic:main
Choose a base branch
from
Hunter275:telemetry-ui
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Telemetry UI #1361
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
apps/web/src/components/PageComponents/Telemetry/Battery.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { useWaitForConfig } from "@app/core/hooks/useWaitForConfig"; | ||
| import { | ||
| type BluetoothValidation, | ||
| BluetoothValidationSchema, | ||
| } from "@app/validation/config/bluetooth.ts"; | ||
| import { | ||
| DynamicForm, | ||
| type DynamicFormFormInit, | ||
| } from "@components/Form/DynamicForm.tsx"; | ||
| import { useDevice } from "@core/stores"; | ||
| import { Protobuf } from "@meshtastic/sdk"; | ||
| import { useConfigEditor, useSignal } from "@meshtastic/sdk-react"; | ||
| import { useTranslation } from "react-i18next"; | ||
|
|
||
| interface BluetoothConfigProps { | ||
| onFormInit: DynamicFormFormInit<BluetoothValidation>; | ||
| } | ||
|
|
||
| const EMPTY_RADIO_SIGNAL = { | ||
| value: {} as { bluetooth?: Protobuf.Config.Config_BluetoothConfig }, | ||
| peek: () => ({}) as { bluetooth?: Protobuf.Config.Config_BluetoothConfig }, | ||
| subscribe: () => () => {}, | ||
| } as const; | ||
|
|
||
| export const Bluetooth = ({ onFormInit }: BluetoothConfigProps) => { | ||
| useWaitForConfig({ configCase: "bluetooth" }); | ||
|
|
||
| const { config, getEffectiveConfig } = useDevice(); | ||
| const editor = useConfigEditor(); | ||
| const radio = useSignal(editor?.radio ?? EMPTY_RADIO_SIGNAL); | ||
| const effective = | ||
| radio.bluetooth ?? | ||
| (getEffectiveConfig("bluetooth") as | ||
| | Protobuf.Config.Config_BluetoothConfig | ||
| | undefined); | ||
|
|
||
| const { t } = useTranslation("config"); | ||
|
|
||
| const onSubmit = (data: BluetoothValidation) => { | ||
| if (!editor) return; | ||
| editor.setRadioSection( | ||
| "bluetooth", | ||
| data as unknown as Protobuf.Config.Config_BluetoothConfig, | ||
| ); | ||
| }; | ||
|
|
||
| return ( | ||
| <DynamicForm<BluetoothValidation> | ||
| onSubmit={onSubmit} | ||
| onFormInit={onFormInit} | ||
| validationSchema={BluetoothValidationSchema} | ||
| defaultValues={config.bluetooth} | ||
| values={effective} | ||
| fieldGroups={[ | ||
| { | ||
| label: t("bluetooth.bluetoothConfig.label"), | ||
| description: t("bluetooth.bluetoothConfig.description"), | ||
| notes: t("bluetooth.note"), | ||
| fields: [ | ||
| { | ||
| type: "toggle", | ||
| name: "enabled", | ||
| label: t("bluetooth.enabled.label"), | ||
| description: t("bluetooth.enabled.description"), | ||
| }, | ||
| { | ||
| type: "select", | ||
| name: "mode", | ||
| label: t("bluetooth.pairingMode.label"), | ||
| description: t("bluetooth.pairingMode.description"), | ||
| properties: { | ||
| enumValue: Protobuf.Config.Config_BluetoothConfig_PairingMode, | ||
| formatEnumName: true, | ||
| }, | ||
| }, | ||
| { | ||
| type: "number", | ||
| name: "fixedPin", | ||
| label: t("bluetooth.pin.label"), | ||
| description: t("bluetooth.pin.description"), | ||
| }, | ||
| ], | ||
| }, | ||
| ]} | ||
| /> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { createLogger, type MeshClient } from "@meshtastic/sdk"; | ||
| import type { | ||
| NodeMetricSample, | ||
| NodeMetricsRetentionPolicy, | ||
| SqlocalNodeMetricsRepository, | ||
| } from "@meshtastic/sdk-storage-sqlocal/nodeMetrics"; | ||
|
|
||
| const log = createLogger("nodeMetricsRecorder"); | ||
|
|
||
| export interface NodeMetricsRecorderOptions { | ||
| retention?: NodeMetricsRetentionPolicy; | ||
| /** Run a prune pass after roughly this many appended samples. */ | ||
| pruneEvery?: number; | ||
| } | ||
|
|
||
| /** | ||
| * Records per-node metrics (SNR, hops away, last heard) — the node-level | ||
| * values shown on the Nodes page that Telemetry packets don't carry — into the | ||
| * given repository, so the Telemetry page can chart them for every node. | ||
| * | ||
| * SNR is sampled from every inbound mesh packet (dense signal history); hops | ||
| * away / last heard come from NodeInfo broadcasts. Returns a teardown that | ||
| * detaches the subscriptions. | ||
| */ | ||
| export function attachNodeMetricsRecorder( | ||
| client: MeshClient, | ||
| repo: SqlocalNodeMetricsRepository, | ||
| options: NodeMetricsRecorderOptions = {}, | ||
| ): () => void { | ||
| const retention = options.retention; | ||
| const pruneEvery = options.pruneEvery ?? 128; | ||
| let sincePrune = 0; | ||
|
|
||
| const record = (samples: NodeMetricSample[]): void => { | ||
| if (samples.length === 0) return; | ||
| repo | ||
| .appendBatch(samples) | ||
| .then(() => { | ||
| sincePrune += samples.length; | ||
| if (retention && sincePrune >= pruneEvery) { | ||
| sincePrune = 0; | ||
| return repo.prune(retention); | ||
| } | ||
| }) | ||
| .catch((e: unknown) => { | ||
| log.warn("node metric persist failed", { | ||
| error: (e as Error)?.message, | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| const unsubMesh = client.events.onMeshPacket.subscribe((packet) => { | ||
| if (!packet.from) return; | ||
| const time = | ||
| packet.rxTime > 0 ? new Date(packet.rxTime * 1000) : new Date(); | ||
| if (Number.isFinite(packet.rxSnr)) { | ||
| record([ | ||
| { nodeNum: packet.from, metric: "snr", time, value: packet.rxSnr }, | ||
| ]); | ||
| } | ||
| }); | ||
|
|
||
| const unsubNodeInfo = client.events.onNodeInfoPacket.subscribe((info) => { | ||
| if (!info.num) return; | ||
| const time = | ||
| info.lastHeard > 0 ? new Date(info.lastHeard * 1000) : new Date(); | ||
| const samples: NodeMetricSample[] = []; | ||
| // NodeInfo.snr is 0 when unset; only record a genuine measurement. | ||
| if (Number.isFinite(info.snr) && info.snr !== 0) { | ||
| samples.push({ nodeNum: info.num, metric: "snr", time, value: info.snr }); | ||
| } | ||
| if (typeof info.hopsAway === "number") { | ||
| samples.push({ | ||
| nodeNum: info.num, | ||
| metric: "hopsAway", | ||
| time, | ||
| value: info.hopsAway, | ||
| }); | ||
| } | ||
| if (info.lastHeard > 0) { | ||
| samples.push({ | ||
| nodeNum: info.num, | ||
| metric: "lastHeard", | ||
| time, | ||
| value: info.lastHeard, | ||
| }); | ||
| } | ||
| record(samples); | ||
| }); | ||
|
|
||
| return () => { | ||
| unsubMesh(); | ||
| unsubNodeInfo(); | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: meshtastic/web
Length of output: 9816
🏁 Script executed:
Repository: meshtastic/web
Length of output: 10314
🌐 Web query:
Zod conditional required field requiredIf exact match mode conditional optional based on another field value💡 Result:
Zod does not have a native "requiredIf" method [1][2]. To enforce that a field is required based on the value of another field, you should use either
superRefine(or.checkin newer versions) for cross-field validation, or adiscriminatedUnionfor stronger type inference [3][4][5]. 1. Using superRefine/check (Recommended for general conditional logic) This approach allows you to inspect the entire object and add custom issues to specific fields when conditions are not met [4][6]. const schema = z.object({ conditionField: z.boolean, dependentField: z.string.optional, }).superRefine((data, ctx) => { if (data.conditionField &&!data.dependentField) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: "dependentField is required when conditionField is true", path: ["dependentField"], }); } }); 2. Using Discriminated Union (Recommended for stronger types) If your conditional logic relies on literal values, a discriminated union is preferred because it creates distinct types based on the discriminator field [3][5]. const schema = z.discriminatedUnion("type", [ z.object({ type: z.literal("A"), field: z.string }), z.object({ type: z.literal("B"), field: z.string.optional }), ]); 3. Controlling when validation runs By default, refinements may only run if the base schema is valid [2][7]. If you need your custom validation to run even when base fields fail (e.g., to report all errors at once), you can use thewhenparameter insuperRefineor.refine, which accepts a callback to determine if the refinement should execute [8][7].Citations:
🏁 Script executed:
Repository: meshtastic/web
Length of output: 3656
Require
fixedPinonly forFIXED_PIN.BluetoothValidationSchemarequires a six-digit PIN for every Bluetooth mode, but the protobuf reservesfixedPinforFIXED_PIN;RANDOM_PIN/NO_PINcannot be submitted without an ignored value. Make the PIN optional except whenmode === FIXED_PIN, and apply the same schema UI behavior in the Bluetooth Settings form.🤖 Prompt for AI Agents