fix device metrics & position widget
This commit is contained in:
+14
-21
@@ -1,11 +1,10 @@
|
|||||||
import type React from "react";
|
import type React from "react";
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
|
|
||||||
import { useDevice } from "@app/core/providers/useDevice.js";
|
import { useDevice } from "@app/core/providers/useDevice.js";
|
||||||
import { toMGRS } from "@app/core/utils/toMGRS.js";
|
import { toMGRS } from "@app/core/utils/toMGRS.js";
|
||||||
import { useAppStore } from "@core/stores/appStore.js";
|
import { useAppStore } from "@core/stores/appStore.js";
|
||||||
import { useDeviceStore } from "@core/stores/deviceStore.js";
|
import { useDeviceStore } from "@core/stores/deviceStore.js";
|
||||||
import { Protobuf, Types } from "@meshtastic/meshtasticjs";
|
import { Types } from "@meshtastic/meshtasticjs";
|
||||||
|
|
||||||
import { BatteryWidget } from "./Widgets/BatteryWidget.js";
|
import { BatteryWidget } from "./Widgets/BatteryWidget.js";
|
||||||
import { ConfiguringWidget } from "./Widgets/ConfiguringWidget.js";
|
import { ConfiguringWidget } from "./Widgets/ConfiguringWidget.js";
|
||||||
@@ -16,23 +15,9 @@ import { PositionWidget } from "./Widgets/PositionWidget.js";
|
|||||||
|
|
||||||
export const Sidebar = (): JSX.Element => {
|
export const Sidebar = (): JSX.Element => {
|
||||||
const { removeDevice } = useDeviceStore();
|
const { removeDevice } = useDeviceStore();
|
||||||
const { connection, hardware, nodes, status } = useDevice();
|
const { connection, hardware, nodes, status, currentMetrics } = useDevice();
|
||||||
const { selectedDevice, setSelectedDevice } = useAppStore();
|
const { selectedDevice, setSelectedDevice } = useAppStore();
|
||||||
const [telemtery, setTelemetry] = useState<Protobuf.DeviceMetrics>();
|
const myNode = nodes.find((n) => n.data.num === hardware.myNodeNum);
|
||||||
const [grid, setGrid] = useState<string>("");
|
|
||||||
const [batteryHistory, setBatteryHistory] = useState<number[]>([]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const device = nodes.find((n) => n.data.num === hardware.myNodeNum);
|
|
||||||
if (device?.deviceMetrics?.length) {
|
|
||||||
setTelemetry(device.deviceMetrics[device.deviceMetrics?.length]);
|
|
||||||
}
|
|
||||||
if (device?.data.position) {
|
|
||||||
setGrid(
|
|
||||||
toMGRS(device.data.position.latitudeI, device.data.position.longitudeI)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}, [nodes, hardware.myNodeNum]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="relative flex w-80 flex-shrink-0 flex-col gap-2 border-x border-slate-200 bg-slate-50 p-2">
|
<div className="relative flex w-80 flex-shrink-0 flex-col gap-2 border-x border-slate-200 bg-slate-50 p-2">
|
||||||
@@ -70,15 +55,23 @@ export const Sidebar = (): JSX.Element => {
|
|||||||
<div className="flex flex-col gap-3">
|
<div className="flex flex-col gap-3">
|
||||||
<NodeInfoWidget hardware={hardware} />
|
<NodeInfoWidget hardware={hardware} />
|
||||||
<BatteryWidget
|
<BatteryWidget
|
||||||
batteryLevel={telemtery?.batteryLevel ?? 0}
|
batteryLevel={currentMetrics.batteryLevel}
|
||||||
voltage={telemtery?.voltage ?? 0}
|
voltage={currentMetrics.voltage}
|
||||||
/>
|
/>
|
||||||
|
{JSON.stringify(
|
||||||
|
nodes.find((n) => n.data.num === hardware.myNodeNum)?.deviceMetrics
|
||||||
|
)}
|
||||||
<PeersWidget
|
<PeersWidget
|
||||||
peers={nodes
|
peers={nodes
|
||||||
.map((n) => n.data)
|
.map((n) => n.data)
|
||||||
.filter((n) => n.num !== hardware.myNodeNum)}
|
.filter((n) => n.num !== hardware.myNodeNum)}
|
||||||
/>
|
/>
|
||||||
<PositionWidget grid={grid} />
|
<PositionWidget
|
||||||
|
grid={toMGRS(
|
||||||
|
myNode?.data.position?.latitudeI,
|
||||||
|
myNode?.data.position?.longitudeI
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
|
||||||
<ConfiguringWidget />
|
<ConfiguringWidget />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ export interface Device {
|
|||||||
activePeer: number;
|
activePeer: number;
|
||||||
waypoints: Protobuf.Waypoint[];
|
waypoints: Protobuf.Waypoint[];
|
||||||
regionUnset: boolean;
|
regionUnset: boolean;
|
||||||
|
currentMetrics: Protobuf.DeviceMetrics;
|
||||||
|
|
||||||
setReady(ready: boolean): void;
|
setReady(ready: boolean): void;
|
||||||
setStatus: (status: Types.DeviceStatusEnum) => void;
|
setStatus: (status: Types.DeviceStatusEnum) => void;
|
||||||
@@ -104,6 +105,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
|
|||||||
activePeer: 0,
|
activePeer: 0,
|
||||||
waypoints: [],
|
waypoints: [],
|
||||||
regionUnset: false,
|
regionUnset: false,
|
||||||
|
currentMetrics: Protobuf.DeviceMetrics.create(),
|
||||||
|
|
||||||
setReady: (ready: boolean) => {
|
setReady: (ready: boolean) => {
|
||||||
set(
|
set(
|
||||||
@@ -229,6 +231,27 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
|
|||||||
if (node) {
|
if (node) {
|
||||||
switch (metrics.data.variant.oneofKind) {
|
switch (metrics.data.variant.oneofKind) {
|
||||||
case "deviceMetrics":
|
case "deviceMetrics":
|
||||||
|
if (device) {
|
||||||
|
if (metrics.data.variant.deviceMetrics.batteryLevel) {
|
||||||
|
device.currentMetrics.batteryLevel =
|
||||||
|
metrics.data.variant.deviceMetrics.batteryLevel;
|
||||||
|
}
|
||||||
|
if (metrics.data.variant.deviceMetrics.voltage) {
|
||||||
|
device.currentMetrics.voltage =
|
||||||
|
metrics.data.variant.deviceMetrics.voltage;
|
||||||
|
}
|
||||||
|
if (metrics.data.variant.deviceMetrics.airUtilTx) {
|
||||||
|
device.currentMetrics.airUtilTx =
|
||||||
|
metrics.data.variant.deviceMetrics.airUtilTx;
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
metrics.data.variant.deviceMetrics.channelUtilization
|
||||||
|
) {
|
||||||
|
device.currentMetrics.channelUtilization =
|
||||||
|
metrics.data.variant.deviceMetrics.channelUtilization;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
node.deviceMetrics.push(
|
node.deviceMetrics.push(
|
||||||
metrics.data.variant.deviceMetrics
|
metrics.data.variant.deviceMetrics
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user