Browse Source

Initial commit

pull/82/head
Adam McQuilkin 3 years ago
parent
commit
f111181f0b
  1. 29
      src/components/PageComponents/Config/User.tsx
  2. 120
      src/core/stores/deviceStore.ts
  3. 25
      src/pages/Config/DeviceConfig.tsx

29
src/components/PageComponents/Config/User.tsx

@ -13,39 +13,26 @@ import { classValidatorResolver } from "@hookform/resolvers/class-validator";
import { Protobuf } from "@meshtastic/meshtasticjs"; import { Protobuf } from "@meshtastic/meshtasticjs";
export const User = (): JSX.Element => { export const User = (): JSX.Element => {
const { hardware, nodes, connection } = useDevice(); const { hardware, nodes, connection, setWorkingOwner } = useDevice();
const myNode = nodes.find((n) => n.data.num === hardware.myNodeNum); const myNode = nodes.find((n) => n.data.num === hardware.myNodeNum);
const { register, handleSubmit, reset, control } = useForm<UserValidation>({ const { register, handleSubmit, reset, control } = useForm<UserValidation>({
defaultValues: myNode?.data.user, defaultValues: myNode?.data.user,
resolver: classValidatorResolver(UserValidation) resolver: classValidatorResolver(UserValidation),
}); });
useEffect(() => { useEffect(() => {
reset({ reset(myNode?.data.user);
longName: myNode?.data.user?.longName,
shortName: myNode?.data.user?.shortName,
isLicensed: myNode?.data.user?.isLicensed
});
}, [reset, myNode]); }, [reset, myNode]);
const onSubmit = handleSubmit((data) => { const onSubmit = handleSubmit((data) => {
if (connection && myNode?.data.user) { if (connection && myNode?.data.user) {
void toast.promise( setWorkingOwner(
connection new Protobuf.User({
.setOwner( ...myNode.data.user,
new Protobuf.User({ ...data,
...myNode.data.user, })
...data
})
)
.then(() => reset({ ...data })),
{
loading: "Saving...",
success: "Saved User, Restarting Node",
error: "No response received"
}
); );
} }
}); });

120
src/core/stores/deviceStore.ts

@ -60,6 +60,7 @@ export interface Device {
channels: Channel[]; channels: Channel[];
config: Protobuf.LocalConfig; config: Protobuf.LocalConfig;
moduleConfig: Protobuf.LocalModuleConfig; moduleConfig: Protobuf.LocalModuleConfig;
workingOwner: Protobuf.User;
workingConfig: Protobuf.Config[]; workingConfig: Protobuf.Config[];
workingModuleConfig: Protobuf.ModuleConfig[]; workingModuleConfig: Protobuf.ModuleConfig[];
hardware: Protobuf.MyNodeInfo; hardware: Protobuf.MyNodeInfo;
@ -84,6 +85,7 @@ export interface Device {
setStatus: (status: Types.DeviceStatusEnum) => void; setStatus: (status: Types.DeviceStatusEnum) => void;
setConfig: (config: Protobuf.Config) => void; setConfig: (config: Protobuf.Config) => void;
setModuleConfig: (config: Protobuf.ModuleConfig) => void; setModuleConfig: (config: Protobuf.ModuleConfig) => void;
setWorkingOwner: (owner: Protobuf.User) => void;
setWorkingConfig: (config: Protobuf.Config) => void; setWorkingConfig: (config: Protobuf.Config) => void;
setWorkingModuleConfig: (config: Protobuf.ModuleConfig) => void; setWorkingModuleConfig: (config: Protobuf.ModuleConfig) => void;
setHardware: (hardware: Protobuf.MyNodeInfo) => void; setHardware: (hardware: Protobuf.MyNodeInfo) => void;
@ -101,12 +103,12 @@ export interface Device {
addMessage: (message: MessageWithState) => void; addMessage: (message: MessageWithState) => void;
addWaypointMessage: (message: WaypointIDWithState) => void; addWaypointMessage: (message: WaypointIDWithState) => void;
addDeviceMetadataMessage: ( addDeviceMetadataMessage: (
metadata: Types.PacketMetadata<Protobuf.DeviceMetadata> metadata: Types.PacketMetadata<Protobuf.DeviceMetadata>,
) => void; ) => void;
setMessageState: ( setMessageState: (
channelIndex: number, channelIndex: number,
messageId: number, messageId: number,
state: MessageState state: MessageState,
) => void; ) => void;
setDialogOpen: (dialog: DialogVariant, open: boolean) => void; setDialogOpen: (dialog: DialogVariant, open: boolean) => void;
processPacket: (data: processPacketParams) => void; processPacket: (data: processPacketParams) => void;
@ -136,6 +138,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
status: Types.DeviceStatusEnum.DEVICE_DISCONNECTED, status: Types.DeviceStatusEnum.DEVICE_DISCONNECTED,
channels: [], channels: [],
config: new Protobuf.LocalConfig(), config: new Protobuf.LocalConfig(),
workingOwner: new Protobuf.User(),
moduleConfig: new Protobuf.LocalModuleConfig(), moduleConfig: new Protobuf.LocalModuleConfig(),
workingConfig: [], workingConfig: [],
workingModuleConfig: [], workingModuleConfig: [],
@ -152,7 +155,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
import: false, import: false,
QR: false, QR: false,
shutdown: false, shutdown: false,
reboot: false reboot: false,
}, },
pendingSettingsChanges: false, pendingSettingsChanges: false,
messageDraft: "", messageDraft: "",
@ -164,7 +167,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
if (device) { if (device) {
device.ready = ready; device.ready = ready;
} }
}) }),
); );
}, },
setStatus: (status: Types.DeviceStatusEnum) => { setStatus: (status: Types.DeviceStatusEnum) => {
@ -174,7 +177,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
if (device) { if (device) {
device.status = status; device.status = status;
} }
}) }),
); );
}, },
setConfig: (config: Protobuf.Config) => { setConfig: (config: Protobuf.Config) => {
@ -203,14 +206,14 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
device.config.lora = config.payloadVariant.value; device.config.lora = config.payloadVariant.value;
device.regionUnset = device.regionUnset =
config.payloadVariant.value.region === config.payloadVariant.value.region ===
Protobuf.Config_LoRaConfig_RegionCode.UNSET; Protobuf.Config_LoRaConfig_RegionCode.UNSET;
break; break;
case "bluetooth": case "bluetooth":
device.config.bluetooth = config.payloadVariant.value; device.config.bluetooth = config.payloadVariant.value;
break; break;
} }
} }
}) }),
); );
}, },
setModuleConfig: (config: Protobuf.ModuleConfig) => { setModuleConfig: (config: Protobuf.ModuleConfig) => {
@ -250,9 +253,20 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
device.moduleConfig.audio = config.payloadVariant.value; device.moduleConfig.audio = config.payloadVariant.value;
} }
} }
}) }),
); );
}, },
setWorkingOwner: (owner: Protobuf.User) => {
set(produce<DeviceState>((draft) => {
const device = draft.devices.get(id);
if (!device) {
return;
}
device.workingOwner = owner;
}));
},
setWorkingConfig: (config: Protobuf.Config) => { setWorkingConfig: (config: Protobuf.Config) => {
set( set(
produce<DeviceState>((draft) => { produce<DeviceState>((draft) => {
@ -261,14 +275,14 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
return; return;
} }
const workingConfigIndex = device?.workingConfig.findIndex( const workingConfigIndex = device?.workingConfig.findIndex(
(wc) => wc.payloadVariant.case === config.payloadVariant.case (wc) => wc.payloadVariant.case === config.payloadVariant.case,
); );
if (workingConfigIndex !== -1) { if (workingConfigIndex !== -1) {
device.workingConfig[workingConfigIndex] = config; device.workingConfig[workingConfigIndex] = config;
} else { } else {
device?.workingConfig.push(config); device?.workingConfig.push(config);
} }
}) }),
); );
}, },
setWorkingModuleConfig: (moduleConfig: Protobuf.ModuleConfig) => { setWorkingModuleConfig: (moduleConfig: Protobuf.ModuleConfig) => {
@ -278,11 +292,11 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
if (!device) { if (!device) {
return; return;
} }
const workingModuleConfigIndex = const workingModuleConfigIndex = device?.workingModuleConfig
device?.workingModuleConfig.findIndex( .findIndex(
(wmc) => (wmc) =>
wmc.payloadVariant.case === wmc.payloadVariant.case ===
moduleConfig.payloadVariant.case moduleConfig.payloadVariant.case,
); );
if (workingModuleConfigIndex !== -1) { if (workingModuleConfigIndex !== -1) {
device.workingModuleConfig[workingModuleConfigIndex] = device.workingModuleConfig[workingModuleConfigIndex] =
@ -290,7 +304,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
} else { } else {
device?.workingModuleConfig.push(moduleConfig); device?.workingModuleConfig.push(moduleConfig);
} }
}) }),
); );
}, },
setHardware: (hardware: Protobuf.MyNodeInfo) => { setHardware: (hardware: Protobuf.MyNodeInfo) => {
@ -300,7 +314,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
if (device) { if (device) {
device.hardware = hardware; device.hardware = hardware;
} }
}) }),
); );
}, },
setMetrics: (metrics: Types.PacketMetadata<Protobuf.Telemetry>) => { setMetrics: (metrics: Types.PacketMetadata<Protobuf.Telemetry>) => {
@ -308,7 +322,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
produce<DeviceState>((draft) => { produce<DeviceState>((draft) => {
const device = draft.devices.get(id); const device = draft.devices.get(id);
let node = device?.nodes.find( let node = device?.nodes.find(
(n) => n.data.num === metrics.from (n) => n.data.num === metrics.from,
); );
if (node) { if (node) {
switch (metrics.data.variant.case) { switch (metrics.data.variant.case) {
@ -333,18 +347,18 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
} }
node.deviceMetrics.push({ node.deviceMetrics.push({
metric: metrics.data.variant.value, metric: metrics.data.variant.value,
timestamp: metrics.rxTime timestamp: metrics.rxTime,
}); });
break; break;
case "environmentMetrics": case "environmentMetrics":
node.environmentMetrics.push({ node.environmentMetrics.push({
metric: metrics.data.variant.value, metric: metrics.data.variant.value,
timestamp: metrics.rxTime timestamp: metrics.rxTime,
}); });
break; break;
} }
} }
}) }),
); );
}, },
setActivePage: (page) => { setActivePage: (page) => {
@ -354,7 +368,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
if (device) { if (device) {
device.activePage = page; device.activePage = page;
} }
}) }),
); );
}, },
setPendingSettingsChanges: (state) => { setPendingSettingsChanges: (state) => {
@ -364,7 +378,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
if (device) { if (device) {
device.pendingSettingsChanges = state; device.pendingSettingsChanges = state;
} }
}) }),
); );
}, },
addChannel: (channel: Channel) => { addChannel: (channel: Channel) => {
@ -373,7 +387,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
const device = draft.devices.get(id); const device = draft.devices.get(id);
if (device) { if (device) {
const channelIndex = device.channels.findIndex( const channelIndex = device.channels.findIndex(
(c) => c.config.index === channel.config.index (c) => c.config.index === channel.config.index,
); );
if (channelIndex !== -1) { if (channelIndex !== -1) {
const messages = device.channels[channelIndex].messages; const messages = device.channels[channelIndex].messages;
@ -383,7 +397,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
device.channels.push(channel); device.channels.push(channel);
} }
} }
}) }),
); );
}, },
addWaypoint: (waypoint: Protobuf.Waypoint) => { addWaypoint: (waypoint: Protobuf.Waypoint) => {
@ -392,7 +406,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
const device = draft.devices.get(id); const device = draft.devices.get(id);
if (device) { if (device) {
const waypointIndex = device.waypoints.findIndex( const waypointIndex = device.waypoints.findIndex(
(wp) => wp.id === waypoint.id (wp) => wp.id === waypoint.id,
); );
if (waypointIndex !== -1) { if (waypointIndex !== -1) {
@ -401,7 +415,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
device.waypoints.push(waypoint); device.waypoints.push(waypoint);
} }
} }
}) }),
); );
}, },
addNodeInfo: (nodeInfo) => { addNodeInfo: (nodeInfo) => {
@ -409,7 +423,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
produce<DeviceState>((draft) => { produce<DeviceState>((draft) => {
const device = draft.devices.get(id); const device = draft.devices.get(id);
const node = device?.nodes.find( const node = device?.nodes.find(
(node) => node.data.num === nodeInfo.num (node) => node.data.num === nodeInfo.num,
); );
if (node) { if (node) {
node.data = nodeInfo; node.data = nodeInfo;
@ -418,10 +432,10 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
data: nodeInfo, data: nodeInfo,
metadata: undefined, metadata: undefined,
deviceMetrics: [], deviceMetrics: [],
environmentMetrics: [] environmentMetrics: [],
}); });
} }
}) }),
); );
}, },
setPeerInfoOpen: (open) => { setPeerInfoOpen: (open) => {
@ -431,7 +445,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
if (device) { if (device) {
device.peerInfoOpen = open; device.peerInfoOpen = open;
} }
}) }),
); );
}, },
setActivePeer: (peer) => { setActivePeer: (peer) => {
@ -441,7 +455,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
if (device) { if (device) {
device.activePeer = peer; device.activePeer = peer;
} }
}) }),
); );
}, },
addUser: (user) => { addUser: (user) => {
@ -449,12 +463,12 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
produce<DeviceState>((draft) => { produce<DeviceState>((draft) => {
const device = draft.devices.get(id); const device = draft.devices.get(id);
const node = device?.nodes.find( const node = device?.nodes.find(
(node) => node.data.num === user.from (node) => node.data.num === user.from,
); );
if (node) { if (node) {
node.data.user = user.data; node.data.user = user.data;
} }
}) }),
); );
}, },
addPosition: (position) => { addPosition: (position) => {
@ -462,12 +476,12 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
produce<DeviceState>((draft) => { produce<DeviceState>((draft) => {
const device = draft.devices.get(id); const device = draft.devices.get(id);
const node = device?.nodes.find( const node = device?.nodes.find(
(node) => node.data.num === position.from (node) => node.data.num === position.from,
); );
if (node) { if (node) {
node.data.position = position.data; node.data.position = position.data;
} }
}) }),
); );
}, },
addConnection: (connection) => { addConnection: (connection) => {
@ -477,7 +491,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
if (device) { if (device) {
device.connection = connection; device.connection = connection;
} }
}) }),
); );
}, },
addMessage: (message) => { addMessage: (message) => {
@ -487,7 +501,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
device?.channels device?.channels
.find((ch) => ch.config.index === message.channel) .find((ch) => ch.config.index === message.channel)
?.messages.push(message); ?.messages.push(message);
}) }),
); );
}, },
addWaypointMessage: (waypointID) => { addWaypointMessage: (waypointID) => {
@ -497,7 +511,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
device?.channels device?.channels
.find((ch) => ch.config.index === waypointID.channel) .find((ch) => ch.config.index === waypointID.channel)
?.messages.push(waypointID); ?.messages.push(waypointID);
}) }),
); );
}, },
addDeviceMetadataMessage: (metadata) => { addDeviceMetadataMessage: (metadata) => {
@ -505,33 +519,33 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
produce<DeviceState>((draft) => { produce<DeviceState>((draft) => {
const device = draft.devices.get(id); const device = draft.devices.get(id);
const node = device?.nodes.find( const node = device?.nodes.find(
(n) => n.data.num === metadata.from (n) => n.data.num === metadata.from,
); );
if (node) { if (node) {
node.metadata = metadata.data; node.metadata = metadata.data;
} }
}) }),
); );
}, },
setMessageState: ( setMessageState: (
channelIndex: number, channelIndex: number,
messageId: number, messageId: number,
state: MessageState state: MessageState,
) => { ) => {
set( set(
produce<DeviceState>((draft) => { produce<DeviceState>((draft) => {
const device = draft.devices.get(id); const device = draft.devices.get(id);
const channel = device?.channels.find( const channel = device?.channels.find(
(ch) => ch.config.index === channelIndex (ch) => ch.config.index === channelIndex,
); );
const message = channel?.messages.find( const message = channel?.messages.find(
(msg) => msg.id === messageId (msg) => msg.id === messageId,
); );
if (message) { if (message) {
message.state = state; message.state = state;
} }
}) }),
); );
}, },
setDialogOpen: (dialog: DialogVariant, open: boolean) => { setDialogOpen: (dialog: DialogVariant, open: boolean) => {
@ -542,7 +556,7 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
return; return;
} }
device.dialog[dialog] = open; device.dialog[dialog] = open;
}) }),
); );
}, },
processPacket(data: processPacketParams) { processPacket(data: processPacketParams) {
@ -558,21 +572,21 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
data: new Protobuf.NodeInfo({ data: new Protobuf.NodeInfo({
num: data.from, num: data.from,
lastHeard: data.time, lastHeard: data.time,
snr: data.snr snr: data.snr,
}), }),
metadata: undefined, metadata: undefined,
deviceMetrics: [], deviceMetrics: [],
environmentMetrics: [] environmentMetrics: [],
}); });
return; return;
} else { } else {
node.data = { node.data = {
...node.data, ...node.data,
lastHeard: data.time, lastHeard: data.time,
snr: data.snr snr: data.snr,
}; };
} }
}) }),
); );
}, },
setMessageDraft: (message: string) => { setMessageDraft: (message: string) => {
@ -582,11 +596,11 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
if (device) { if (device) {
device.messageDraft = message; device.messageDraft = message;
} }
}) }),
); );
} },
}); });
}) }),
); );
const device = get().devices.get(id); const device = get().devices.get(id);
@ -600,13 +614,13 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
set( set(
produce<DeviceState>((draft) => { produce<DeviceState>((draft) => {
draft.devices.delete(id); draft.devices.delete(id);
}) }),
); );
}, },
getDevices: () => Array.from(get().devices.values()), getDevices: () => Array.from(get().devices.values()),
getDevice: (id) => get().devices.get(id) getDevice: (id) => get().devices.get(id),
})); }));
export const DeviceContext = createContext<Device | undefined>(undefined); export const DeviceContext = createContext<Device | undefined>(undefined);

25
src/pages/Config/DeviceConfig.tsx

@ -16,42 +16,42 @@ import { NavBar } from "@app/Nav/NavBar.js";
import { VerticalTabbedContent } from "@app/components/generic/VerticalTabbedContent.js"; import { VerticalTabbedContent } from "@app/components/generic/VerticalTabbedContent.js";
export const DeviceConfig = (): JSX.Element => { export const DeviceConfig = (): JSX.Element => {
const { hardware, workingConfig, connection } = useDevice(); const { hardware, workingConfig, workingOwner, connection } = useDevice();
const tabs = [ const tabs = [
{ {
label: "User", label: "User",
element: User element: User,
}, },
{ {
label: "Device", label: "Device",
element: Device element: Device,
}, },
{ {
label: "Position", label: "Position",
element: Position element: Position,
}, },
{ {
label: "Power", label: "Power",
element: Power element: Power,
}, },
{ {
label: "Network", label: "Network",
element: Network, element: Network,
disabled: !hardware.hasWifi disabled: !hardware.hasWifi,
}, },
{ {
label: "Display", label: "Display",
element: Display element: Display,
}, },
{ {
label: "LoRa", label: "LoRa",
element: LoRa element: LoRa,
}, },
{ {
label: "Bluetooth", label: "Bluetooth",
element: Bluetooth element: Bluetooth,
} },
]; ];
return ( return (
@ -65,9 +65,10 @@ export const DeviceConfig = (): JSX.Element => {
workingConfig.map(async (config) => { workingConfig.map(async (config) => {
await connection?.setConfig(config); await connection?.setConfig(config);
}); });
connection?.setOwner(workingOwner);
await connection?.commitEditSettings(); await connection?.commitEditSettings();
} },
} },
]} ]}
/> />

Loading…
Cancel
Save