summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2017-08-12 18:27:18 +0200
committerDaniel Friesel <derf@finalrewind.org>2017-08-12 18:27:18 +0200
commit070fa111d3fe9229b0527860a0f46f990b810b29 (patch)
tree4652964c610095949f1c49f97b141d493773bedf
parent6c836399546d4630cd085f7e1b2058daa3c656bf (diff)
Load subscriber configuration from a user-provided YAML file
-rwxr-xr-xbin/mqttsyncdir-subscriber57
1 files changed, 35 insertions, 22 deletions
diff --git a/bin/mqttsyncdir-subscriber b/bin/mqttsyncdir-subscriber
index df60ffe..09c32c3 100755
--- a/bin/mqttsyncdir-subscriber
+++ b/bin/mqttsyncdir-subscriber
@@ -7,30 +7,43 @@ use 5.020;
our $VERSION = '0.00';
use File::Path qw(make_path);
-use File::Slurp qw(write_file);
+use File::Slurp qw(read_file write_file);
use Net::MQTT::Simple;
+use YAML::XS;
-# proof of concept -- a proper config parser will be implemented later
-my $outdir = '/tmp/mqttsyncdir-subscriber';
-my $server = '172.23.225.193';
-
-my %config = (
- 'sensor/+/am_rh' => {
- freshness => 600,
- norm_factor => 0.1,
- },
- 'sensor/+/am_temp' => {
- freshness => 600,
- norm_factor => 0.1,
- },
- 'sensor/+/temperature' => {
- freshness => 600,
- },
- 'host/+/temperature/+' => {
- freshness => 600,
- norm_factor => 0.001,
- },
-);
+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;