Skip to content

Commit

Permalink
feat(nrplus): implement NR+ control messages
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Oct 30, 2023
1 parent f2773b6 commit 58db91d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
60 changes: 52 additions & 8 deletions src/NRPlusGatewayTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { RelativeTime } from './RelativeTime.js'
import { ButtonPress } from './ButtonPress.js'
import { useState } from 'preact/hooks'
import { useWebsocket } from './context/WebsocketConnection.js'
import { useMap } from './context/Map.js'
import { sortLocations } from './sortLocations.js'

export const NRPlusGatewayTile = ({ gateway }: { gateway: NRPlusGateway }) => {
const { lastUpdateTs } = useDevices()
Expand All @@ -30,9 +32,21 @@ export const NRPlusGatewayTile = ({ gateway }: { gateway: NRPlusGateway }) => {
const [deviceCode, setDeviceCode] = useState<string>(
localStorage.getItem(key) ?? '',
)
const map = useMap()
const { location } = gateway
const rankedLocations = Object.values(location ?? []).sort(sortLocations)
const deviceLocation = rankedLocations[0]

return (
<>
<Title type={'button'}>
<Title
type={'button'}
onClick={() => {
if (deviceLocation !== undefined) {
map?.center(deviceLocation)
}
}}
>
<NRPlus class="icon" />
<span class="info">
<DeviceName device={gateway} />
Expand Down Expand Up @@ -97,6 +111,17 @@ const Node = ({
}) => {
const [configure, setConfigure] = useState<boolean>(false)
const { send } = useWebsocket()
const [relay, setRelay] = useState<Record<string, string | null>>({})

const viaRelay = (id: string) => (payload: string) => {
const relayVia = relay[id]
const command = `dect beacon_rach_tx -t ${id} -d "${payload}"`
if (relayVia !== undefined)
return `dect client_rach_tx --b_name ${relayVia} -d ${JSON.stringify(
command,
)}`
return command
}

return (
<>
Expand Down Expand Up @@ -125,10 +150,25 @@ const Node = ({
<button type="button me-1" onClick={() => setConfigure(false)}>
<X strokeWidth={1} />
</button>
<button type="button me-1" onClick={() => setConfigure(false)}>
<LightbulbOff strokeWidth={2} />
</button>
<select
onChange={(e) => {
setRelay((r) => ({
...r,
[id]: (e.target as HTMLSelectElement).value,
}))
}}
>
<option selected={relay[id] === undefined}>no relay</option>
{Object.keys(gateway.state.nodes ?? {})
.filter((otherNodeId) => otherNodeId !== id)
.map((otherNodeId) => (
<option selected={otherNodeId === relay[id]}>
{otherNodeId}
</option>
))}
</select>
{[
{ name: 'REMOTE_CTRL', color: 'white' },
{ name: 'RED', color: 'RED' },
{ name: 'GREEN', color: '#18d718' },
{ name: 'BLUE', color: '#1c8afb' },
Expand All @@ -138,10 +178,12 @@ const Node = ({
type="button me-1"
style={{ color }}
onClick={() => {
setConfigure(false)
const nrplusCtrl = viaRelay(id)(`LED_${name} ON`)
console.log(`[NR+]`, nrplusCtrl)
send({
deviceId: gateway.id,
code: localStorage.getItem(`${gateway.id}:code`),
nrplusCtrl: `dect beacon_rach_tx -t ${id} -d "LED_${name} ON"`,
nrplusCtrl,
})
}}
>
Expand All @@ -151,10 +193,12 @@ const Node = ({
type="button me-1"
style={{ color }}
onClick={() => {
setConfigure(false)
const nrplusCtrl = viaRelay(id)(`LED_${name} OFF`)
console.log(`[NR+]`, nrplusCtrl)
send({
deviceId: gateway.id,
code: localStorage.getItem(`${gateway.id}:code`),
nrplusCtrl: `dect beacon_rach_tx -t ${id} -d "LED_${name} OFF"`,
nrplusCtrl,
})
}}
>
Expand Down
1 change: 1 addition & 0 deletions src/context/Devices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export type NRPlusGateway = {
id: number // e.g. 38,
networkId: number // e.g. 22
}
location?: Record<GeoLocationSource, GeoLocation>
}

export type Devices = Record<string, Device>
Expand Down
2 changes: 1 addition & 1 deletion src/context/LocationSourceLabels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export const LocationSourceLabels = {
[GeoLocationSource.WIFI]: 'Wi-Fi',
[GeoLocationSource.SCELL]: 'single-cell',
[GeoLocationSource.MCELL]: 'multi-cell',
[GeoLocationSource.fixed]: 'Fixed',
[GeoLocationSource.fixed]: 'Fixed Location',
}
21 changes: 20 additions & 1 deletion src/context/WebsocketConnection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type GeoLocation,
type Reported,
type Summary,
GeoLocationSource,
} from './Devices.js'

export const WebsocketContext = createContext<{
Expand All @@ -26,6 +27,8 @@ type Message = {
'@context': MessageContext
deviceId: string
deviceAlias?: string
// Fixed location for the device
deviceLocation?: string // e.g. 63.42115901688979,10.437200141182338
} & (
| {
'@context': MessageContext.DeviceLocation
Expand Down Expand Up @@ -110,6 +113,22 @@ export const Provider = ({ children }: { children: ComponentChildren }) => {
if ('deviceAlias' in message) {
deviceMessages.updateAlias(message.deviceId, message.deviceAlias)
}
if ('deviceLocation' in message) {
const [lat, lng] = message.deviceLocation
.split(',')
.map((s) => s.trim())
.map((s) => parseFloat(s)) as [number, number]
deviceMessages.updateLocation(
message.deviceId,
{
lat,
lng,
accuracy: 100,
source: GeoLocationSource.fixed,
},
'fixed',
)
}
})

return () => {
Expand Down Expand Up @@ -148,7 +167,7 @@ export const Provider = ({ children }: { children: ComponentChildren }) => {
value={{
connected,
send: (message) => {
console.debug(`[WS]`, { message })
console.debug(`[WS]`, message)
connection?.current?.send(
JSON.stringify({
message: 'sendmessage',
Expand Down

0 comments on commit 58db91d

Please sign in to comment.