Skip to content

Commit

Permalink
feat(wirepas): query LED state
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Feb 13, 2024
1 parent 5a29b01 commit b07e04d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 32 deletions.
53 changes: 44 additions & 9 deletions wirepas-5g-mesh-gateway/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -90,6 +101,18 @@ client.on('connect', () => {
})

let nodes: Record<string, Record<string, any>> = {}
const ledState: Record<string, Record<string, boolean>> = {}

const getColor = throttle(
async (gwId: string, node: number, color: LED_COLOR) =>
sendToGateway({
gateway: gwId,
req: getLedState({
node,
color,
}),
}),
)

client.on('message', (_, message) => {
const packetReceivedEvent =
Expand All @@ -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]: {
Expand Down Expand Up @@ -177,12 +213,9 @@ const sendToGateway = wirepasPublish({
debug: (...args) => debug(C2G, ...args),
})
const gwThingConnections: Record<string, awsMqtt.MqttClientConnection> = {}
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({
Expand All @@ -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)
39 changes: 16 additions & 23 deletions wirepas-5g-mesh-gateway/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ 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,
GREEN = 2,
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: {
Expand Down Expand Up @@ -49,35 +51,26 @@ 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]),
)

/**
* Query LED state
*
* @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
Expand Down

0 comments on commit b07e04d

Please sign in to comment.