From 6c836399546d4630cd085f7e1b2058daa3c656bf Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 12 Aug 2017 17:41:13 +0200 Subject: Add support for freshness checks for subscription data --- bin/mqttsyncdir-subscriber | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file 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__ -- cgit v1.2.3