UNPKG

nixfilter-mqtt

Version:

Filters for publishing and subscribing to MQTT topics, similar to "mosquitto_pub" and "mosquitto_sub"

43 lines (37 loc) 1.14 kB
#!/usr/bin/env node 'use strict'; var nixfilter, utils; // Require the "nixfilter" module nixfilter = require('nixfilter'); // Require the "./utils" module utils = require('./utils'); // Define the filter and register it on the module nixfilter.filter(module, { description: 'Publish messages to a MQTT topic', output_writer: null, add_arguments: function(argument_parser) { utils.add_common_arguments(argument_parser); argument_parser.addArgument(['--retain', '-r'], { action: 'storeTrue', help: 'Send messages as "retained" messages' }); argument_parser.addArgument(['publish_topic'], { help: 'The MQTT topic to publish to' }); }, setup: function(args) { return utils.connect_to_mqtt_broker(args).then((mqtt_client) => { this.mqtt_client = mqtt_client; }); }, on_input: function(message) { this.mqtt_client.publish(this.args.publish_topic, message, { qos: this.args.qos, retain: this.args.retain }); }, terminate: function() { return utils.disconnect_from_mqtt_broker(this.mqtt_client); } }); //# sourceMappingURL=mqtt_publish.js.map