diff options
author | Daniel Friesel <derf@finalrewind.org> | 2017-08-12 17:41:13 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2017-08-12 17:41:13 +0200 |
commit | 6c836399546d4630cd085f7e1b2058daa3c656bf (patch) | |
tree | 7f82b3229c41ef706c3eaf5d998efbb9cb9f012b | |
parent | fb3f980823e0ceb77da8bd9a44665ce6fd52e49c (diff) |
Add support for freshness checks for subscription data
-rwxr-xr-x | bin/mqttsyncdir-subscriber | 43 |
1 files changed, 40 insertions, 3 deletions
diff --git a/bin/mqttsyncdir-subscriber b/bin/mqttsyncdir-subscriber index 6efe5aa..df60ffe 100755 --- a/bin/mqttsyncdir-subscriber +++ b/bin/mqttsyncdir-subscriber @@ -51,6 +51,39 @@ for my $name ( keys %config ) { write_file( "${outdir}/${topic}", $message ); }; + + if ( exists $config{$name}{freshness} and $name =~ m{ [*] }x ) { + say STDERR + "Warning: $name: freshness checks on subscriptions containing " + . "an asterisk ('*') are not supported."; + say STDERR " I will not check this topic's freshness"; + delete $config{$name}{freshness}; + } + if ( exists $config{$name}{freshness} and $name =~ m{ [#] }x ) { + say STDERR + "Warning: $name: freshness checks on subscriptions containing " + . "multi-level wildcards ('#') are not yet supported."; + say STDERR " I will not check this topic's freshness"; + delete $config{$name}{freshness}; + } + +} + +sub check_freshness { + my ( $topic, $now ) = @_; + + my $glob_expr = $topic; + $glob_expr =~ s{ [+] }{*}gx; + + for my $file ( glob("${outdir}/${glob_expr}") ) { + my $mtime = ( stat($file) )[9]; + + if ( -f $file and $now - $mtime > $config{$topic}{freshness} ) { + unlink($file) + or say STDERR + "Unable to delete ${file} during freshness check: $!"; + } + } } my $mqtt = Net::MQTT::Simple->new($server); @@ -59,10 +92,14 @@ $mqtt->subscribe(%subscription); while (1) { $mqtt->tick(30); - # TODO handle freshness -} + my $now = time(); -$mqtt->run(%subscription); + for my $topic ( keys %config ) { + if ( exists $config{$topic}{freshness} ) { + check_freshness( $topic, $now ); + } + } +} __END__ |