From 5099a88adc2bfc265a65f8865613fb82b9d2843d Mon Sep 17 00:00:00 2001 From: Kristian Heljas Date: Fri, 19 Mar 2021 03:16:08 +0200 Subject: [PATCH] feat(nodes): added state, restart and logs nodes --- src/nodes/bridge/logs.html | 58 ++++++++++++++++++++++++++++++++ src/nodes/bridge/logs.ts | 17 ++++++++++ src/nodes/bridge/restart.html | 46 ++++++++++++++++++++++++++ src/nodes/bridge/restart.ts | 19 +++++++++++ src/nodes/bridge/state.html | 62 +++++++++++++++++++++++++++++++++++ src/nodes/bridge/state.ts | 24 ++++++++++++++ 6 files changed, 226 insertions(+) create mode 100644 src/nodes/bridge/logs.html create mode 100644 src/nodes/bridge/logs.ts create mode 100644 src/nodes/bridge/restart.html create mode 100644 src/nodes/bridge/restart.ts create mode 100644 src/nodes/bridge/state.html create mode 100644 src/nodes/bridge/state.ts diff --git a/src/nodes/bridge/logs.html b/src/nodes/bridge/logs.html new file mode 100644 index 0000000..7401693 --- /dev/null +++ b/src/nodes/bridge/logs.html @@ -0,0 +1,58 @@ + + + + + diff --git a/src/nodes/bridge/logs.ts b/src/nodes/bridge/logs.ts new file mode 100644 index 0000000..47b486b --- /dev/null +++ b/src/nodes/bridge/logs.ts @@ -0,0 +1,17 @@ +import type { NodeAPI } from 'node-red'; +import { Z2mNode } from '../../core/z2m-node'; + +class Z2mEventNode extends Z2mNode { + setup(): void { + this.z2m.subscribe('bridge/logging', 0, this.onLogMessage.bind(this), this); + } + + onLogMessage(topic: string, buffer: Buffer) { + const { level, message: payload } = JSON.parse(buffer.toString()); + this.send({ topic, level, payload }); + } +} + +export = (RED: NodeAPI): void => { + Z2mEventNode.register(RED, 'z2m-bridge-logs'); +}; diff --git a/src/nodes/bridge/restart.html b/src/nodes/bridge/restart.html new file mode 100644 index 0000000..58d0ac8 --- /dev/null +++ b/src/nodes/bridge/restart.html @@ -0,0 +1,46 @@ + + + + + diff --git a/src/nodes/bridge/restart.ts b/src/nodes/bridge/restart.ts new file mode 100644 index 0000000..0431c18 --- /dev/null +++ b/src/nodes/bridge/restart.ts @@ -0,0 +1,19 @@ +import type { NodeAPI } from 'node-red'; +import { Z2mNode } from '../../core/z2m-node'; + +class Z2mRestartNode extends Z2mNode { + setup(): void { + this.on('input', () => { + this.z2m.restart(); + this.status({ + shape: 'ring', + fill: 'yellow', + text: 'restart requested', + }); + }); + } +} + +export = (RED: NodeAPI): void => { + Z2mRestartNode.register(RED, 'z2m-bridge-restart'); +}; diff --git a/src/nodes/bridge/state.html b/src/nodes/bridge/state.html new file mode 100644 index 0000000..5ea5e6c --- /dev/null +++ b/src/nodes/bridge/state.html @@ -0,0 +1,62 @@ + + + + + diff --git a/src/nodes/bridge/state.ts b/src/nodes/bridge/state.ts new file mode 100644 index 0000000..cb68567 --- /dev/null +++ b/src/nodes/bridge/state.ts @@ -0,0 +1,24 @@ +import type { IPublishPacket } from 'mqtt'; +import type { NodeAPI } from 'node-red'; +import { Z2mNode } from '../../core/z2m-node'; + +interface BridgeStateConfig { + initial: boolean; +} + +class BridgeStateNode extends Z2mNode { + setup(): void { + this.z2m.subscribe('bridge/state', 1, this.onStateMessage.bind(this), this); + } + + onStateMessage(topic: string, buffer: Buffer, { retain }: IPublishPacket) { + if (!retain || this.config.initial) { + const payload = buffer.toString(); + this.send({ topic, payload, retain }); + } + } +} + +export = (RED: NodeAPI): void => { + BridgeStateNode.register(RED, 'z2m-bridge-state'); +};