From 5c68c552e20f60e1e0e31b6d6f24351d37eea199 Mon Sep 17 00:00:00 2001 From: Sacha Weatherstone Date: Sat, 29 Oct 2022 22:36:05 +1000 Subject: [PATCH] Fix manual positions --- .../PageComponents/Config/Position.tsx | 62 ++++++++++++------- src/components/form/Input.tsx | 1 + src/validation/config/position.ts | 8 +-- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/components/PageComponents/Config/Position.tsx b/src/components/PageComponents/Config/Position.tsx index 4413f9de..5ff90468 100644 --- a/src/components/PageComponents/Config/Position.tsx +++ b/src/components/PageComponents/Config/Position.tsx @@ -14,7 +14,10 @@ import { classValidatorResolver } from "@hookform/resolvers/class-validator"; import { Protobuf } from "@meshtastic/meshtasticjs"; export const Position = (): JSX.Element => { - const { config, connection } = useDevice(); + const { config, connection, nodes, hardware } = useDevice(); + + const myNode = nodes.find((n) => n.data.num === hardware.myNodeNum); + const { register, handleSubmit, @@ -22,7 +25,12 @@ export const Position = (): JSX.Element => { reset, control, } = useForm({ - defaultValues: config.position, + defaultValues: { + fixedAlt: myNode?.data.position?.altitude, + fixedLat: (myNode?.data.position?.latitudeI ?? 0) / 1e7, + fixedLng: (myNode?.data.position?.longitudeI ?? 0) / 1e7, + ...config.position, + }, resolver: classValidatorResolver(PositionValidation), }); @@ -33,12 +41,22 @@ export const Position = (): JSX.Element => { }); useEffect(() => { - reset(config.position); - }, [reset, config.position]); + reset({ + fixedAlt: myNode?.data.position?.altitude, + fixedLat: (myNode?.data.position?.latitudeI ?? 0) / 1e7, + fixedLng: (myNode?.data.position?.longitudeI ?? 0) / 1e7, + ...config.position, + }); + }, [reset, config.position, myNode?.data.position]); const onSubmit = handleSubmit((data) => { const { fixedAlt, fixedLat, fixedLng, ...rest } = data; + const configHasChanged = !Protobuf.Config_PositionConfig.equals( + config.position, + Protobuf.Config_PositionConfig.create(rest) + ); + if (connection) { void toast.promise( connection.sendPacket( @@ -66,25 +84,27 @@ export const Position = (): JSX.Element => { error: "No response received", } ); - void toast.promise( - connection.setConfig( - { - payloadVariant: { - oneofKind: "position", - position: rest, + if (configHasChanged) { + void toast.promise( + connection.setConfig( + { + payloadVariant: { + oneofKind: "position", + position: rest, + }, }, - }, - async () => { - reset({ ...data }); - await Promise.resolve(); + async () => { + reset({ ...data }); + await Promise.resolve(); + } + ), + { + loading: "Saving...", + success: "Saved Position Config, Restarting Node", + error: "No response received", } - ), - { - loading: "Saving...", - success: "Saved Position Config, Restarting Node", - error: "No response received", - } - ); + ); + } } }); diff --git a/src/components/form/Input.tsx b/src/components/form/Input.tsx index 64b29c9f..baeaed6b 100644 --- a/src/components/form/Input.tsx +++ b/src/components/form/Input.tsx @@ -47,6 +47,7 @@ export const Input = forwardRef(function Input( disabled ? "cursor-not-allowed bg-orange-50 text-orange-200" : "" }`} disabled={disabled} + step="any" {...rest} /> {suffix && ( diff --git a/src/validation/config/position.ts b/src/validation/config/position.ts index 5f00bf30..8dbf71f4 100644 --- a/src/validation/config/position.ts +++ b/src/validation/config/position.ts @@ -1,4 +1,4 @@ -import { IsBoolean, IsInt } from "class-validator"; +import { IsBoolean, IsInt, IsNumber } from "class-validator"; import type { Protobuf } from "@meshtastic/meshtasticjs"; @@ -25,12 +25,12 @@ export class PositionValidation implements Protobuf.Config_PositionConfig { positionFlags: number; // fixed position fields - @IsInt() + @IsNumber() fixedAlt: number; - @IsInt() + @IsNumber() fixedLat: number; - @IsInt() + @IsNumber() fixedLng: number; }