Browse Source

Fix manual positions

pull/55/head
Sacha Weatherstone 4 years ago
parent
commit
5c68c552e2
No known key found for this signature in database GPG Key ID: 7AB2D7E206124B31
  1. 62
      src/components/PageComponents/Config/Position.tsx
  2. 1
      src/components/form/Input.tsx
  3. 8
      src/validation/config/position.ts

62
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<PositionValidation>({
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",
}
);
);
}
}
});

1
src/components/form/Input.tsx

@ -47,6 +47,7 @@ export const Input = forwardRef<HTMLInputElement, InputProps>(function Input(
disabled ? "cursor-not-allowed bg-orange-50 text-orange-200" : ""
}`}
disabled={disabled}
step="any"
{...rest}
/>
{suffix && (

8
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;
}

Loading…
Cancel
Save