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'); +};