nixfilter-mqtt
Version:
Filters for publishing and subscribing to MQTT topics, similar to "mosquitto_pub" and "mosquitto_sub"
39 lines (28 loc) • 890 B
text/coffeescript
`#!/usr/bin/env node
'use strict'`
# 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: (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'
return
setup: (args) ->
utils.connect_to_mqtt_broker(args)
.then (@mqtt_client) =>
on_input: (message) ->
@mqtt_client.publish @args.publish_topic, message,
qos: @args.qos
retain: @args.retain
return
terminate: ->
utils.disconnect_from_mqtt_broker(@mqtt_client)