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"; import { Protobuf } from "@meshtastic/meshtasticjs";
export const Position = (): JSX.Element => { 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 { const {
register, register,
handleSubmit, handleSubmit,
@ -22,7 +25,12 @@ export const Position = (): JSX.Element => {
reset, reset,
control, control,
} = useForm<PositionValidation>({ } = 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), resolver: classValidatorResolver(PositionValidation),
}); });
@ -33,12 +41,22 @@ export const Position = (): JSX.Element => {
}); });
useEffect(() => { useEffect(() => {
reset(config.position); reset({
}, [reset, config.position]); 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 onSubmit = handleSubmit((data) => {
const { fixedAlt, fixedLat, fixedLng, ...rest } = data; const { fixedAlt, fixedLat, fixedLng, ...rest } = data;
const configHasChanged = !Protobuf.Config_PositionConfig.equals(
config.position,
Protobuf.Config_PositionConfig.create(rest)
);
if (connection) { if (connection) {
void toast.promise( void toast.promise(
connection.sendPacket( connection.sendPacket(
@ -66,25 +84,27 @@ export const Position = (): JSX.Element => {
error: "No response received", error: "No response received",
} }
); );
void toast.promise( if (configHasChanged) {
connection.setConfig( void toast.promise(
{ connection.setConfig(
payloadVariant: { {
oneofKind: "position", payloadVariant: {
position: rest, oneofKind: "position",
position: rest,
},
}, },
}, async () => {
async () => { reset({ ...data });
reset({ ...data }); await Promise.resolve();
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 ? "cursor-not-allowed bg-orange-50 text-orange-200" : ""
}`} }`}
disabled={disabled} disabled={disabled}
step="any"
{...rest} {...rest}
/> />
{suffix && ( {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"; import type { Protobuf } from "@meshtastic/meshtasticjs";
@ -25,12 +25,12 @@ export class PositionValidation implements Protobuf.Config_PositionConfig {
positionFlags: number; positionFlags: number;
// fixed position fields // fixed position fields
@IsInt() @IsNumber()
fixedAlt: number; fixedAlt: number;
@IsInt() @IsNumber()
fixedLat: number; fixedLat: number;
@IsInt() @IsNumber()
fixedLng: number; fixedLng: number;
} }

Loading…
Cancel
Save