forked from bitlair/ansible
Add the photos role
This commit is contained in:
parent
24c4d845ae
commit
786bb5cbdc
10 changed files with 169 additions and 0 deletions
13
roles/photos/templates/photo-gallery.service
Normal file
13
roles/photos/templates/photo-gallery.service
Normal file
|
@ -0,0 +1,13 @@
|
|||
# Managed by Ansible
|
||||
|
||||
[Unit]
|
||||
Description=Gallery service
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/bin/node /opt/photo-gallery/server.js
|
||||
Restart=always
|
||||
RestartSec=3
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
55
roles/photos/templates/photos2mqtt.pl
Normal file
55
roles/photos/templates/photos2mqtt.pl
Normal file
|
@ -0,0 +1,55 @@
|
|||
#!/usr/bin/perl -w
|
||||
|
||||
# Managed by Ansible
|
||||
|
||||
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;
|
||||
}
|
16
roles/photos/templates/photos2mqtt.service
Normal file
16
roles/photos/templates/photos2mqtt.service
Normal file
|
@ -0,0 +1,16 @@
|
|||
# Managed by Ansible
|
||||
|
||||
[Unit]
|
||||
Description=Photos to MQTT
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
Restart=on-failure
|
||||
RestartSec=10s
|
||||
ExecStart=/usr/bin/perl /opt/photos2mqtt.pl
|
||||
User=wip
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue