From fb3f980823e0ceb77da8bd9a44665ce6fd52e49c Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 7 Aug 2017 22:25:21 +0200 Subject: add mqttsyncdir-publisher --- bin/mqttsyncdir-publisher | 98 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100755 bin/mqttsyncdir-publisher diff --git a/bin/mqttsyncdir-publisher b/bin/mqttsyncdir-publisher new file mode 100755 index 0000000..53c36dd --- /dev/null +++ b/bin/mqttsyncdir-publisher @@ -0,0 +1,98 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use 5.020; + +our $VERSION = '0.00'; + +use File::Slurp qw(read_dir read_file); +use Linux::Inotify2; +use Net::MQTT::Simple; + +# proof of concept -- a proper config parser will be implemented later +my %config = ( + directory => '/tmp/mqttsyncdir-publisher', + server => '172.23.225.193', +); + +my $mqtt = Net::MQTT::Simple->new( $config{server} ); + +my $inotify = Linux::Inotify2->new(); +my $mask = IN_MODIFY | IN_CREATE | IN_DELETE | IN_DELETE_SELF; +my $cb; + +$cb = sub { + my ($event) = @_; + my $path = $event->fullname; + + if ( $event->IN_MODIFY and -f $path ) { + my $content = read_file($path); + chomp $content; + $path =~ s{^$config{directory}/}{}; + + $mqtt->retain( $path, $content ); + } + elsif ( $event->IN_DELETE ) { + $path =~ s{^$config{directory}/}{}; + $mqtt->retain( $path, undef ); + } + elsif ( $event->IN_CREATE and -d $path ) { + $inotify->watch( $path, $mask, $cb ); + } + elsif ( $event->IN_DELETE_SELF ) { + $event->w->cancel; + } + +}; + +$inotify->watch( $config{directory}, $mask, $cb ); + +my @queue = grep { -d } read_dir( $config{directory}, prefix => 1 ); + +while ( my $dir = shift @queue ) { + $inotify->watch( $dir, $mask, $cb ); + push( @queue, grep { -d } read_dir( $dir, prefix => 1 ) ); +} + +while (1) { + $inotify->poll; +} + +__END__ + +=head1 NAME + +=head1 SYNOPSIS + +=head1 VERSION + +=head1 DESCRIPTION + +=head1 OPTIONS + +=over + +=back + +=head1 EXIT STATUS + +=head1 CONFIGURATION + +None. + +=head1 DEPENDENCIES + +=over + +=back + +=head1 BUGS AND LIMITATIONS + +=head1 AUTHOR + +Copyright (C) 2017 by Daniel Friesel Ederf@finalrewind.orgE + +=head1 LICENSE + + 0. You just DO WHAT THE FUCK YOU WANT TO. -- cgit v1.2.3