diff options
author | Daniel Friesel <derf@finalrewind.org> | 2017-08-07 22:25:21 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2017-08-07 22:25:21 +0200 |
commit | fb3f980823e0ceb77da8bd9a44665ce6fd52e49c (patch) | |
tree | 39fb59cfb94d5076e4df4e1cf55534b9a2c06140 /bin | |
parent | 006a9ab43bcd8b30352842d3cd44eec2e954fb25 (diff) |
add mqttsyncdir-publisher
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/mqttsyncdir-publisher | 98 |
1 files changed, 98 insertions, 0 deletions
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 E<lt>derf@finalrewind.orgE<gt> + +=head1 LICENSE + + 0. You just DO WHAT THE FUCK YOU WANT TO. |