Skip to content

Commit

Permalink
Renamed "device" to "hub"
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Nov 18, 2024
1 parent 6ee7bcd commit 7e1216e
Show file tree
Hide file tree
Showing 18 changed files with 157 additions and 97 deletions.
16 changes: 8 additions & 8 deletions frontend/src/lib/MessageHandlers/WifiNetworkEventHandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { WifiNetworkEvent } from '$lib/_fbs/open-shock/serialization/local/wifi-network-event';
import { WifiNetwork as FbsWifiNetwork } from '$lib/_fbs/open-shock/serialization/types/wifi-network';
import { WifiNetworkEventType } from '$lib/_fbs/open-shock/serialization/types/wifi-network-event-type';
import { DeviceStateStore } from '$lib/stores';
import { HubStateStore } from '$lib/stores';
import { toastDelegator } from '$lib/stores/ToastDelegator';
import type { WiFiNetwork } from '$lib/types/WiFiNetwork';
import type { MessageHandler } from '.';
Expand All @@ -27,7 +27,7 @@ function handleDiscoveredEvent(fbsNetwork: FbsWifiNetwork) {
saved: fbsNetwork.saved(),
};

DeviceStateStore.setWifiNetwork(network);
HubStateStore.setWifiNetwork(network);
}
function handleUpdatedEvent(fbsNetwork: FbsWifiNetwork) {
const ssid = fbsNetwork.ssid();
Expand All @@ -47,7 +47,7 @@ function handleUpdatedEvent(fbsNetwork: FbsWifiNetwork) {
saved: fbsNetwork.saved(),
};

DeviceStateStore.setWifiNetwork(network);
HubStateStore.setWifiNetwork(network);
}
function handleLostEvent(fbsNetwork: FbsWifiNetwork) {
const bssid = fbsNetwork.bssid();
Expand All @@ -57,7 +57,7 @@ function handleLostEvent(fbsNetwork: FbsWifiNetwork) {
return;
}

DeviceStateStore.removeWifiNetwork(bssid);
HubStateStore.removeWifiNetwork(bssid);
}
function handleSavedEvent(fbsNetwork: FbsWifiNetwork) {
const ssid = fbsNetwork.ssid();
Expand All @@ -68,7 +68,7 @@ function handleSavedEvent(fbsNetwork: FbsWifiNetwork) {
return;
}

DeviceStateStore.updateWifiNetwork(bssid, (network) => {
HubStateStore.updateWifiNetwork(bssid, (network) => {
network.saved = true;
return network;
});
Expand All @@ -87,7 +87,7 @@ function handleRemovedEvent(fbsNetwork: FbsWifiNetwork) {
return;
}

DeviceStateStore.updateWifiNetwork(bssid, (network) => {
HubStateStore.updateWifiNetwork(bssid, (network) => {
network.saved = false;
return network;
});
Expand All @@ -106,7 +106,7 @@ function handleConnectedEvent(fbsNetwork: FbsWifiNetwork) {
return;
}

DeviceStateStore.setWifiConnectedBSSID(bssid);
HubStateStore.setWifiConnectedBSSID(bssid);

toastDelegator.trigger({
message: 'WiFi network connected: ' + ssid,
Expand All @@ -122,7 +122,7 @@ function handleDisconnectedEvent(fbsNetwork: FbsWifiNetwork) {
return;
}

DeviceStateStore.setWifiConnectedBSSID(null);
HubStateStore.setWifiConnectedBSSID(null);

toastDelegator.trigger({
message: 'WiFi network disconnected: ' + ssid,
Expand Down
16 changes: 8 additions & 8 deletions frontend/src/lib/MessageHandlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { HubToLocalMessagePayload } from '$lib/_fbs/open-shock/serialization/loc
import { ReadyMessage } from '$lib/_fbs/open-shock/serialization/local/ready-message';
import { WifiScanStatusMessage } from '$lib/_fbs/open-shock/serialization/local/wifi-scan-status-message';
import { ByteBuffer } from 'flatbuffers';
import { DeviceStateStore } from '$lib/stores';
import { HubStateStore } from '$lib/stores';
import { SerializeWifiScanCommand } from '$lib/Serializers/WifiScanCommand';
import { toastDelegator } from '$lib/stores/ToastDelegator';
import { SetRfTxPinCommandResult } from '$lib/_fbs/open-shock/serialization/local/set-rf-tx-pin-command-result';
Expand Down Expand Up @@ -33,9 +33,9 @@ PayloadHandlers[HubToLocalMessagePayload.ReadyMessage] = (cli, msg) => {
const payload = new ReadyMessage();
msg.payload(payload);

console.log('[WS] Connected to device, poggies: ', payload.poggies());
console.log('[WS] Connected to hub, poggies: ', payload.poggies());

DeviceStateStore.update((store) => {
HubStateStore.update((store) => {
store.wifiConnectedBSSID = payload.connectedWifi()?.bssid() || null;
store.accountLinked = payload.accountLinked();
store.config = mapConfig(payload.config());
Expand All @@ -50,7 +50,7 @@ PayloadHandlers[HubToLocalMessagePayload.ReadyMessage] = (cli, msg) => {
store.gpioValidOutputs = gpioValidOutputs;
}

console.log('[WS] Updated device state store: ', store);
console.log('[WS] Updated hub state store: ', store);

return store;
});
Expand Down Expand Up @@ -80,7 +80,7 @@ PayloadHandlers[HubToLocalMessagePayload.WifiScanStatusMessage] = (cli, msg) =>
const payload = new WifiScanStatusMessage();
msg.payload(payload);

DeviceStateStore.setWifiScanStatus(payload.status());
HubStateStore.setWifiScanStatus(payload.status());
};

PayloadHandlers[HubToLocalMessagePayload.WifiNetworkEvent] = WifiNetworkEventHandler;
Expand Down Expand Up @@ -135,7 +135,7 @@ PayloadHandlers[HubToLocalMessagePayload.SetRfTxPinCommandResult] = (cli, msg) =
const result = payload.result();

if (result == SetGPIOResultCode.Success) {
DeviceStateStore.setRfTxPin(payload.pin());
HubStateStore.setRfTxPin(payload.pin());
toastDelegator.trigger({
message: 'Changed RF TX pin to: ' + payload.pin(),
background: 'bg-green-500',
Expand Down Expand Up @@ -168,7 +168,7 @@ PayloadHandlers[HubToLocalMessagePayload.SetEstopEnabledCommandResult] = (cli, m
const success = payload.success();

if (success) {
DeviceStateStore.setEstopEnabled(payload.enabled());
HubStateStore.setEstopEnabled(payload.enabled());
toastDelegator.trigger({
message: 'Changed EStop enabled to: ' + enabled,
background: 'bg-green-500',
Expand All @@ -189,7 +189,7 @@ PayloadHandlers[HubToLocalMessagePayload.SetEstopPinCommandResult] = (cli, msg)

if (result == SetGPIOResultCode.Success) {
const gpioPin = payload.gpioPin();
DeviceStateStore.setEstopGpioPin(gpioPin);
HubStateStore.setEstopGpioPin(gpioPin);
toastDelegator.trigger({
message: 'Changed EStop pin to: ' + gpioPin,
background: 'bg-green-500',
Expand Down
17 changes: 14 additions & 3 deletions frontend/src/lib/components/GpioPinSelector.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { DeviceStateStore } from '$lib/stores';
import { HubStateStore } from '$lib/stores';
import { UsedPinsStore } from '$lib/stores/UsedPinsStore';
import { WebSocketClient } from '$lib/WebSocketClient';
Expand All @@ -10,7 +10,13 @@
let pendingPin: number | null = null;
let statusText: string = 'Loading...';
$: canSet = pendingPin !== null && pendingPin !== currentPin && pendingPin >= 0 && pendingPin <= 255 && $DeviceStateStore.gpioValidOutputs.includes(pendingPin) && !$UsedPinsStore.has(pendingPin);
$: canSet =
pendingPin !== null &&
pendingPin !== currentPin &&
pendingPin >= 0 &&
pendingPin <= 255 &&
$HubStateStore.gpioValidOutputs.includes(pendingPin) &&
!$UsedPinsStore.has(pendingPin);
$: if (currentPin !== null) {
UsedPinsStore.markPinUsed(currentPin, name);
Expand Down Expand Up @@ -40,7 +46,12 @@
<span class="text-sm text-gray-500">{statusText}</span>
</div>
<div class="flex space-x-2">
<input class="input variant-form-material" type="number" placeholder="GPIO Pin" bind:value={pendingPin} />
<input
class="input variant-form-material"
type="number"
placeholder="GPIO Pin"
bind:value={pendingPin}
/>
<button class="btn variant-filled" on:click={setGpioPin} disabled={!canSet}>Set</button>
</div>
</div>
22 changes: 15 additions & 7 deletions frontend/src/lib/components/WiFiList.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { getModalStore } from '@skeletonlabs/skeleton';
import { DeviceStateStore } from '$lib/stores';
import { HubStateStore } from '$lib/stores';
import { WebSocketClient } from '$lib/WebSocketClient';
import { WifiAuthMode } from '$lib/_fbs/open-shock/serialization/types/wifi-auth-mode';
import { WifiScanStatus } from '$lib/_fbs/open-shock/serialization/types/wifi-scan-status';
Expand All @@ -13,14 +13,16 @@
const modalStore = getModalStore();
$: scanStatus = $DeviceStateStore.wifiScanStatus;
$: scanStatus = $HubStateStore.wifiScanStatus;
$: isScanning = scanStatus === WifiScanStatus.Started || scanStatus === WifiScanStatus.InProgress;
$: connectedBSSID = $DeviceStateStore.wifiConnectedBSSID;
$: connectedBSSID = $HubStateStore.wifiConnectedBSSID;
// Sorting the groups themselves by each one's strongest network (by RSSI, higher is stronger)
// Only need to check the first network in each group, since they're already sorted by signal strength
$: strengthSortedGroups = Array.from($DeviceStateStore.wifiNetworkGroups.entries()).sort((a, b) => b[1].networks[0].rssi - a[1].networks[0].rssi);
$: strengthSortedGroups = Array.from($HubStateStore.wifiNetworkGroups.entries()).sort(
(a, b) => b[1].networks[0].rssi - a[1].networks[0].rssi
);
function wifiScan() {
const data = SerializeWifiScanCommand(!isScanning);
Expand Down Expand Up @@ -87,14 +89,20 @@
{#if netgroup.ssid}
<span class="ml-2">{netgroup.ssid}</span>
{:else}
<span class="ml-2">{netgroup.networks[0].bssid}</span><span class="text-gray-500 ml-1">(Hidden)</span>
<span class="ml-2">{netgroup.networks[0].bssid}</span><span class="text-gray-500 ml-1"
>(Hidden)</span
>
{/if}
</span>
<div class="btn-group variant-outline">
{#if netgroup.saved}
<button on:click={() => wifiConnect(netgroup)}><i class="fa fa-arrow-right text-green-500" /></button>
<button on:click={() => wifiConnect(netgroup)}
><i class="fa fa-arrow-right text-green-500" /></button
>
{:else}
<button on:click={() => wifiAuthenticate(netgroup)}><i class="fa fa-link text-green-500" /></button>
<button on:click={() => wifiAuthenticate(netgroup)}
><i class="fa fa-link text-green-500" /></button
>
{/if}
<button on:click={() => wifiSettings(netgroupKey)}><i class="fa fa-cog" /></button>
</div>
Expand Down
23 changes: 17 additions & 6 deletions frontend/src/lib/components/modals/WiFiDetails.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import { SerializeWifiNetworkSaveCommand } from '$lib/Serializers/WifiNetworkSaveCommand';
import { SerializeWifiNetworkConnectCommand } from '$lib/Serializers/WifiNetworkConnectCommand';
import { WebSocketClient } from '$lib/WebSocketClient';
import { DeviceStateStore } from '$lib/stores';
import { HubStateStore } from '$lib/stores';
import { getModalStore } from '@skeletonlabs/skeleton';
import { SerializeWifiNetworkForgetCommand } from '$lib/Serializers/WifiNetworkForgetCommand';
import { WifiAuthMode } from '$lib/_fbs/open-shock/serialization/types/wifi-auth-mode';
const modalStore = getModalStore();
export let groupKey: string;
$: group = $DeviceStateStore.wifiNetworkGroups.get(groupKey);
$: group = $HubStateStore.wifiNetworkGroups.get(groupKey);
function GetWifiAuthModeString(type: WifiAuthMode) {
switch (type) {
Expand Down Expand Up @@ -85,11 +85,17 @@
{#if group}
<div class="flex justify-between space-x-2">
<h2 class="h2">Network Info</h2>
<button class="btn-icon variant-outline" on:click={() => modalStore.close()}><i class="fa fa-xmark"></i></button>
<button class="btn-icon variant-outline" on:click={() => modalStore.close()}
><i class="fa fa-xmark"></i></button
>
</div>
<div>
{#each rows as row (row.key)}
<span class="flex justify-between"><span class="font-bold">{row.key}:</span><span class="text-gray-700 dark:text-gray-300">{row.value}</span></span>
<span class="flex justify-between"
><span class="font-bold">{row.key}:</span><span class="text-gray-700 dark:text-gray-300"
>{row.value}</span
></span
>
{/each}
</div>
<!-- Per-AP info -->
Expand Down Expand Up @@ -117,7 +123,10 @@
{#if showPasswordPrompt}
<button on:click={() => (showPasswordPrompt = false)}>Cancel</button>
{/if}
<button on:click={ConnectWiFi} disabled={showPasswordPrompt && !validPassword}><i class={'fa mr-2 text-green-500' + (group.saved ? ' fa-wifi' : ' fa-link')}></i>Connect</button>
<button on:click={ConnectWiFi} disabled={showPasswordPrompt && !validPassword}
><i class={'fa mr-2 text-green-500' + (group.saved ? ' fa-wifi' : ' fa-link')}
></i>Connect</button
>
{#if group.saved}
<button on:click={ForgetWiFi}><i class="fa fa-trash mr-2 text-red-500"></i>Forget</button>
{/if}
Expand All @@ -126,7 +135,9 @@
{:else}
<div class="flex justify-between space-x-2">
<h2 class="h2">WiFi Info</h2>
<button class="btn-icon variant-outline" on:click={() => modalStore.close()}><i class="fa fa-xmark"></i></button>
<button class="btn-icon variant-outline" on:click={() => modalStore.close()}
><i class="fa fa-xmark"></i></button
>
</div>
<div class="flex justify-center">
<i class="fa fa-spinner fa-spin"></i>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { WifiScanStatus } from '$lib/_fbs/open-shock/serialization/types/wifi-scan-status';
import type { WiFiNetwork, WiFiNetworkGroup, DeviceState } from '$lib/types';
import type { WiFiNetwork, WiFiNetworkGroup, HubState } from '$lib/types';
import { writable } from 'svelte/store';
import { WifiAuthMode } from '$lib/_fbs/open-shock/serialization/types/wifi-auth-mode';

const { subscribe, update } = writable<DeviceState>({
const { subscribe, update } = writable<HubState>({
wifiConnectedBSSID: null,
wifiScanStatus: null,
wifiNetworks: new Map<string, WiFiNetwork>(),
Expand Down Expand Up @@ -47,11 +47,11 @@ function SsidMapReducer(groups: Map<string, WiFiNetworkGroup>, [, value]: [strin
return groups;
}

function updateWifiNetworkGroups(store: DeviceState) {
function updateWifiNetworkGroups(store: HubState) {
store.wifiNetworkGroups = Array.from(store.wifiNetworks.entries()).reduce(SsidMapReducer, new Map<string, WiFiNetworkGroup>());
}

export const DeviceStateStore = {
export const HubStateStore = {
subscribe,
update,
setWifiConnectedBSSID(connectedBSSID: string | null) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/stores/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './DeviceStateStore';
export * from './HubStateStore';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { WifiScanStatus } from '$lib/_fbs/open-shock/serialization/types/wi
import type { Config } from '$lib/mappers/ConfigMapper';
import type { WiFiNetwork, WiFiNetworkGroup } from './';

export type DeviceState = {
export type HubState = {
wifiConnectedBSSID: string | null;
wifiScanStatus: WifiScanStatus | null;
wifiNetworks: Map<string, WiFiNetwork>;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './DeviceState';
export * from './HubState';
export * from './WiFiNetwork';
export * from './WiFiNetworkGroup';
29 changes: 24 additions & 5 deletions frontend/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { WebSocketClient } from '$lib/WebSocketClient';
import GpioPinSelector from '$lib/components/GpioPinSelector.svelte';
import WiFiList from '$lib/components/WiFiList.svelte';
import { DeviceStateStore } from '$lib/stores';
import { HubStateStore } from '$lib/stores';
function isValidLinkCode(str: string) {
if (typeof str != 'string') return false;
Expand Down Expand Up @@ -34,15 +34,34 @@
<div class="flex flex-col space-y-2">
<h3 class="h3">Account Linking</h3>
<div class="flex space-x-2">
<input class={'input variant-form-material ' + (linkCodeValid ? '' : 'input-error')} type="text" inputmode="numeric" pattern="[0-9]*" placeholder="Link Code" bind:value={linkCode} />
<button class="btn variant-filled" on:click={linkAccount} disabled={!linkCodeValid || linkCode.length < 6}>Link</button>
<input
class={'input variant-form-material ' + (linkCodeValid ? '' : 'input-error')}
type="text"
inputmode="numeric"
pattern="[0-9]*"
placeholder="Link Code"
bind:value={linkCode}
/>
<button
class="btn variant-filled"
on:click={linkAccount}
disabled={!linkCodeValid || linkCode.length < 6}>Link</button
>
</div>
</div>

<GpioPinSelector name="RF TX Pin" currentPin={$DeviceStateStore.config?.rf?.txPin ?? null} serializer={SerializeSetRfTxPinCommand} />
<GpioPinSelector
name="RF TX Pin"
currentPin={$HubStateStore.config?.rf?.txPin ?? null}
serializer={SerializeSetRfTxPinCommand}
/>

<!-- TODO: Add EStop Enable/Disable toggle -->

<GpioPinSelector name="EStop Pin" currentPin={$DeviceStateStore.config?.estop?.gpioPin ?? null} serializer={SerializeSetEstopPinCommand} />
<GpioPinSelector
name="EStop Pin"
currentPin={$HubStateStore.config?.estop?.gpioPin ?? null}
serializer={SerializeSetEstopPinCommand}
/>
</div>
</div>
Loading

0 comments on commit 7e1216e

Please sign in to comment.