diff --git a/wirepas-5g-mesh-gateway/gateway.ts b/wirepas-5g-mesh-gateway/gateway.ts index 0b831ce..9f3b764 100644 --- a/wirepas-5g-mesh-gateway/gateway.ts +++ b/wirepas-5g-mesh-gateway/gateway.ts @@ -16,10 +16,21 @@ import { import { merge } from 'lodash-es' import { decodePayload } from './decodePayload.js' import { cloudToGateway } from './cloudToGateway.js' -import { LED_COLOR, setLEDColor, wirepasPublish } from './publish.js' +import { + LED_COLOR, + LED_STATE, + getLedState, + setLEDColor, + wirepasPublish, +} from './publish.js' import chalk from 'chalk' import pThrottle from 'p-throttle' +const throttle = pThrottle({ + limit: 1, + interval: 250, +}) + const { region, accessKeyId, secretAccessKey, gatewayEndpoint } = fromEnv({ region: 'GATEWAY_REGION', gatewayEndpoint: 'GATEWAY_MQTT_ENDPOINT', @@ -90,6 +101,18 @@ client.on('connect', () => { }) let nodes: Record> = {} +const ledState: Record> = {} + +const getColor = throttle( + async (gwId: string, node: number, color: LED_COLOR) => + sendToGateway({ + gateway: gwId, + req: getLedState({ + node, + color, + }), + }), +) client.on('message', (_, message) => { const packetReceivedEvent = @@ -116,6 +139,19 @@ client.on('message', (_, message) => { return } + // Query LED state of node + if (ledState[gwId]?.[sourceAddress] === undefined) { + ledState[gwId] = { + ...(ledState[gwId] ?? {}), + [sourceAddress]: true, + } + void Promise.all([ + getColor(gwId, sourceAddress, LED_COLOR.RED), + getColor(gwId, sourceAddress, LED_COLOR.GREEN), + getColor(gwId, sourceAddress, LED_COLOR.BLUE), + ]) + } + nodes[gwId] = merge( { [sourceAddress]: { @@ -177,12 +213,9 @@ const sendToGateway = wirepasPublish({ debug: (...args) => debug(C2G, ...args), }) const gwThingConnections: Record = {} -const throttle = pThrottle({ - limit: 1, - interval: 250, -}) + const updateColor = throttle( - async (gwId: string, node: number, color: LED_COLOR, ledState: boolean) => + async (gwId: string, node: number, color: LED_COLOR, ledState: LED_STATE) => sendToGateway({ gateway: gwId, req: setLEDColor({ @@ -204,13 +237,15 @@ for (const gwId of Object.keys(existingGws)) { const { r, g, b } = payload.led const updates = [] if (r !== undefined) - updates.push(updateColor(gwId, node, LED_COLOR.RED, r)) + updates.push(updateColor(gwId, node, LED_COLOR.RED, toState(r))) if (g !== undefined) - updates.push(updateColor(gwId, node, LED_COLOR.GREEN, g)) + updates.push(updateColor(gwId, node, LED_COLOR.GREEN, toState(g))) if (b !== undefined) - updates.push(updateColor(gwId, node, LED_COLOR.BLUE, b)) + updates.push(updateColor(gwId, node, LED_COLOR.BLUE, toState(b))) await Promise.all(updates) } } }) } + +const toState = (state: boolean) => (state ? LED_STATE.ON : LED_STATE.OFF) diff --git a/wirepas-5g-mesh-gateway/publish.ts b/wirepas-5g-mesh-gateway/publish.ts index 2d7a6be..32bc50d 100644 --- a/wirepas-5g-mesh-gateway/publish.ts +++ b/wirepas-5g-mesh-gateway/publish.ts @@ -8,10 +8,6 @@ enum ExampleAppMessages { LED_STATE_GET = 130, } -// FIXME: change to 5 later -// For now keep the LED ID as “0” (current boards do not manage this but for the demo it will be “5”. -const LED_ID = 0 - export enum LED_COLOR { RED = 0, BLUE = 1, @@ -19,6 +15,12 @@ export enum LED_COLOR { ALL = 255, } +// LED state (Decimal value 0 => switch off, 1 => switch on) +export enum LED_STATE { + OFF = 0, + ON = 1, +} + const sendPacket = (node: number, payload: Buffer): GenericMessage => ({ wirepas: { sendPacketReq: { @@ -49,18 +51,11 @@ export const setLEDColor = ({ }: { node: number color: LED_COLOR - ledState: boolean + ledState: LED_STATE }): GenericMessage => sendPacket( node, - Buffer.from([ - // LED state set message - ExampleAppMessages.LED_STATE_SET, - // LED identifier (Decimal value 0 is the first user available LED on the node) - color, - // LED state (Decimal value 0 => switch off, 1 => switch on) - ledState ? 1 : 0, - ]), + Buffer.from([ExampleAppMessages.LED_STATE_SET, color, ledState]), ) /** @@ -68,16 +63,14 @@ export const setLEDColor = ({ * * @see https://github.com/wirepas/wm-sdk/tree/v1.4.0/source/example_apps/evaluation_app#led-state-get-request-message */ -export const getLedState = ({ node }: { node: number }): GenericMessage => - sendPacket( - node, - Buffer.from([ - // LED state set message - ExampleAppMessages.LED_STATE_GET, - // LED identifier (Decimal value 0 is the first user available LED on the node) - LED_ID, - ]), - ) +export const getLedState = ({ + node, + color, +}: { + node: number + color: LED_COLOR +}): GenericMessage => + sendPacket(node, Buffer.from([ExampleAppMessages.LED_STATE_GET, color])) /** * Publish a message to the Wirepas 5G Mesh Gateway