UNPKG

@totallyinformation/node-red-contrib-events

Version:
114 lines (90 loc) 4.33 kB
/** Listens for an event based on a msg.topic and outputs a copy of the msg * Destructured to make for easier and more consistent logic. * * Copyright (c) 2021 Julian Knight (Totally Information) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 'use strict' /** --- Type Defs --- * @typedef {import('../typedefs.js').runtimeRED} runtimeRED * @typedef {import('../typedefs.js').runtimeNodeConfig} runtimeNodeConfig * @typedef {import('../typedefs.js').runtimeNode} runtimeNode * typedef {import('../typedefs.js').myNode} myNode */ //#region ----- Module level variables ---- // const tiEvents = require('@totallyinformation/ti-common-event-handler') /** Main (module) variables - acts as a configuration object * that can easily be passed around. */ const mod = { /** @type {runtimeRED} Reference to the master RED instance */ RED: undefined, /** @type {string} Custom Node Name - has to match with html file and package.json `red` section */ nodeName: 'event-in', } //#endregion ----- Module level variables ---- // //#region ----- Module-level support functions ----- // /** 2) This is run when an actual instance of our node is committed to a flow * @param {runtimeNodeConfig} config The Node-RED node instance config object */ function nodeInstance(config) { // As a module-level named function, it will inherit `mod` and other module-level variables // If you need it - which you will here - or just use mod.RED if you prefer: const RED = mod.RED // Create the node instance - `this` can only be referenced AFTER here RED.nodes.createNode(this, config) // Transfer config items from the Editor panel to the runtime this.name = config.name this.topic = config.topic // Allow for and convert MQTT-style wildcards this.topic = this.topic.replace(/#/g, '**').replace(/\+/g, '*') // Add the unique prefix for these nodes (allows the event system to be reused in other nodes) const eventName = `node-red-contrib-events/${this.topic}` // Event handler const sender = (msg) => { this.send(msg) } // Create new listener for the given topic, record it so that it can be removed on close tiEvents.on(eventName, sender) /** Put things here if you need to do anything when a node instance is removed * Or if Node-RED is shutting down. * Note the use of an arrow function, ensures that the function keeps the * same `this` context and so has access to all of the node instance properties. */ this.on('close', (removed, done) => { // Remove this event listener tiEvents.removeListener(eventName, sender) done() }) /** Properties of `this` * Methods: updateWires(wires), context(), on(event,callback), emit(event,...args), removeListener(name,listener), removeAllListeners(name), close(removed) * send(msg), receive(msg), log(msg), warn(msg), error(logMessage,msg), debug(msg), trace(msg), metric(eventname, msg, metricValue), status(status) * Other: credentials, id, type, z, wires, x, y * + any props added manually from config, typically at least name and topic */ } //#endregion ----- Module-level support functions ----- // /** 1) Complete module definition for our Node. This is where things actually start. * @param {runtimeRED} RED The Node-RED runtime object */ function EventIn(RED) { // As a module-level named function, it will inherit `mod` and other module-level variables // Save a reference to the RED runtime for convenience mod.RED = RED // Register a new instance of the specified node type (2) RED.nodes.registerType(mod.nodeName, nodeInstance) } // Export the module definition (1), this is consumed by Node-RED on startup. module.exports = EventIn //EOF