summaryrefslogtreecommitdiff
path: root/bin/mqttsyncdir-subscriber
blob: 0b82ff038288395ca6b40d6f75f7117475b7824c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env perl

use strict;
use warnings;
use 5.020;

our $VERSION = '0.00';

use File::Path qw(make_path);
use File::Slurp qw(read_file write_file);
use Net::MQTT::Simple;
use YAML::XS;

sub load_config {
	my ($config_file) = @_;
	my $content       = read_file($config_file);
	my $yaml          = Load($content);
	return $yaml;
}

my ($config_file) = @ARGV;

if ( not defined $config_file ) {
	die("Usage: $0 <config.yaml>\n");
}

my $user_config = load_config($config_file);
my $outdir      = $user_config->{directory};
my $server      = $user_config->{server};
my %config;

for my $subscription ( @{ $user_config->{subscriptions} } ) {
	$config{ $subscription->{topic} } = {};
	for my $key (qw(freshness norm_factor)) {
		if ( exists $subscription->{$key} ) {
			$config{ $subscription->{topic} }{$key} = $subscription->{$key};
		}
	}
}

if ( not defined $outdir ) {
	die("Error: configuration must specify an output directory\n");
}
if ( not defined $server ) {
	die("Error: configuration must specify a server\n");
}

my %subscription;

for my $name ( keys %config ) {
	$subscription{$name} = sub {
		my ( $topic, $message ) = @_;
		my $basedir = "${outdir}/${topic}";

		$basedir =~ s{ / [^/]+ $ }{}x;

		if ( exists $config{$name}{norm_factor} ) {
			$message *= $config{$name}{norm_factor};
		}

		if ( not -e $basedir ) {
			make_path($basedir);
		}

		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);
$mqtt->subscribe(%subscription);

while (1) {
	$mqtt->tick(30);

	my $now = time();

	for my $topic ( keys %config ) {
		if ( exists $config{$topic}{freshness} ) {
			check_freshness( $topic, $now );
		}
	}
}

__END__

=head1 NAME

mqttsyncdir-subscriber - map MQTT messages to filesystem entries

=head1 SYNOPSIS

B<mqttsyncdir-subscriber> I<config.yaml>

=head1 VERSION

0.00

=head1 DESCRIPTION



=head1 OPTIONS

None yet.

=head1 EXIT STATUS

=head1 CONFIGURATION

=head2 EXAMPLE CONFIGURATION

=begin text

The following configuration will subscribe to three topics, normalize message
bodies of one of them and also remove stale data in two cases.

directory: /srv/mqtt
server: mqtt.example
subscriptions:
  - topic: sensor/+/temperature
    freshness: 600
  - topic: sensor/+/voltage
    freshness: 600
    norm_factor: 0.000001
  - topic: counter/whatever

=end text

This configuration file will subscribe to the topics C<< sensor/+/temperature >>,
C<< sensor/+/voltage >>, and C<< counter/whatever >>. Each time a message is
posted to one of them, the appropriate file in F</srv/mqtt> will be updated.

So, assuming the message C<< 12.5 >> is posted to C<<
sensor/outdoor/temperature >>, the string C<< 12.5 >> will be written to
F</srv/mqtt/sensor/outdoor/temperature>. The message C<< 10700000 >> to
C<< sensor/main_battery/voltage >> will result in C<< 10.7 >> being written
to F</srv/mqtt/sensor/main_battery/voltage>. Any file belonging to the topics
C<< sensor/+/temperature >> or C<< sensor/+/voltage >> which did not receive
an update in the last 10 minutes will be deleted.

=head1 DEPENDENCIES

=over

=item * Net::MQTT::Simple

=back

=head1 BUGS AND LIMITATIONS

=head1 AUTHOR

Copyright (C) 2017-2018 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>

=head1 LICENSE

  0. You just DO WHAT THE FUCK YOU WANT TO.