55 lines
1.2 KiB
Perl
55 lines
1.2 KiB
Perl
#!/usr/bin/perl -w
|
|
|
|
# {{ ansible_managed }}
|
|
|
|
use strict;
|
|
use Net::MQTT::Simple;
|
|
use Linux::Inotify2;
|
|
use POSIX qw(strftime);
|
|
use Time::HiRes qw(sleep time);
|
|
|
|
my $path = "{{ photos_path }}";
|
|
my $mqtt = Net::MQTT::Simple->new('{{ photos_mqtt_host }}');
|
|
|
|
my $inotify = new Linux::Inotify2 or die $!;
|
|
$inotify->blocking(0);
|
|
|
|
sub today {
|
|
return strftime "%Y%m%d", localtime;
|
|
}
|
|
|
|
sub watch_daydir {
|
|
my ($dn) = @_;
|
|
|
|
print "Watching $dn\n";
|
|
|
|
$inotify->watch($dn, IN_CREATE, sub {
|
|
my $event = shift;
|
|
$inotify->watch($event->fullname, IN_CLOSE_WRITE, sub {
|
|
my $event = shift;
|
|
my $fn = $event->fullname;
|
|
unless ($fn =~ m[^\.|/\.]) { # skip hidden files
|
|
print "New file written: $fn\n";
|
|
$mqtt->retain("{{ photos_mqtt_topic }}", $fn =~ s[^$path/][]r);
|
|
}
|
|
$event->w->cancel;
|
|
});
|
|
});
|
|
}
|
|
|
|
print "Watching $path\n";
|
|
$inotify->watch($path, IN_CREATE, sub {
|
|
my $event = shift;
|
|
my $dn = $event->fullname;
|
|
-d $dn or next;
|
|
|
|
watch_daydir($dn);
|
|
});
|
|
|
|
watch_daydir("$path/" . today());
|
|
|
|
while (1) {
|
|
$mqtt->tick(.05);
|
|
$inotify->poll;
|
|
sleep .1;
|
|
}
|