User Tools

Site Tools


sib:listeners:filelistener

FileListener

Overview

The FileListener SIB Listener periodically checks for files in a configured path that matches a given RegEx filter. When files matching the filter are found the file will be read and posted on the configured MQTT-topic. The default payload is BASE64 encoded but that can be overridden in the inputFormat method.

If the delete or moveto .custom configurations are set the handled files are either moved to another path or deleted.

Workflow

  • CRONSchedule: [.custom.schedule]
    • on trigger:
      • Check for files (.custom.path)
        • on file found:
          • Check .custom.filter for filename match
            • on match:
              • Publish: .custom.topic

Custom configuration

filelistener/custom.js
var FileListenerCustom = {
	_id: "fileSample:1",
	_name: "SIB FileListener Sample",
	delete: true,
	filter: "\.bat$|\.csv$",
	inputFormat: (topic, msg, cb)=>{
		console.log( "Running inputFormat" );
		msg.contents = new Buffer(msg.contents, "base64").toString( "UTF-8" );
		cb( msg );
	},
	loggerCfg: {
		dest: {
			file: false,
			console: true,
			mqtt: true
		}
	},
	moveto: "/tmp/filelistener1/archive",
        renameOnMove: ( filename )=>{
            return filename;
        },
	path: "/tmp/filelistener1",
	schedule: "*/10 * * * * *",
	mqtt: "mqtt://localhost",
	topic: "SIB/Samples/FileListener"
};
 
module.exports = FileListenerCustom;
  • _id: The unique ID of this service
  • _name: A plaintext description of this service
  • delete: true/false, if true the file is deleted after pickup
  • filter: RegEx filter to test the filename for, if match the file is picked up
  • inputFormat(topic, msg, callback): The method that is run on service activation. The original message is passed as the argument and the return value from this method must be a JSON formatted message conforming to the SIB message standard.
  • loggerCfg: A JSON Object defining what logger actions should be active for this service (see Logger Configuration
  • moveto: If set, moves the file to this path after pickup
  • renameOnMove: Method that is run when files are moved. Returns the name the file should have in the new location.
  • path: The file path to check for files
  • publishInput: If defined overrides the standard publishing to the internal MQTT transfer
  • schedule: The cron-schedule when to run the check
  • mqtt: The MQTT connection string for the internal MQTT transport
  • topic: The MQTT topic to subscribe to on the internal MQTT transport
sib/listeners/filelistener.txt · Last modified: 2019/03/22 13:10 by ozzy