| 1 | #!/usr/local/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Getopt::Std qw{getopts}; | ||
| 5 | use YAML::XS qw{}; | ||
| 6 | use DateTime; | ||
| 7 | use Net::MQTT::Simple; | ||
| 8 | use Power::Outlet; | ||
| 9 | |||
| 10 | local $| = 1; | ||
| 11 | my $opt = {}; | ||
| 12 | getopts("c:", $opt); | ||
| 13 | my $yamlfile = $opt->{"c"} || '/etc/power-outlet-mqtt-listener.yml'; | ||
| 14 | |||
| 15 | printf "%s: Start\n", DateTime->now; | ||
| 16 | printf "%s: Yaml: %s\n", DateTime->now, $yamlfile; | ||
| 17 | die("Error: Cannot read file $yamlfile") unless -r $yamlfile; | ||
| 18 | |||
| 19 | my $config = YAML::XS::LoadFile($yamlfile) or die("Error: cannot import Yaml file $yamlfile"); | ||
| 20 | die("Error: Yaml configuration must be a dictionary") unless ref($config) eq "HASH"; | ||
| 21 | |||
| 22 | my $version = $config->{'version'} // 1.0; | ||
| 23 | printf "%s: Yaml: Version: $version\n", DateTime->now; | ||
| 24 | |||
| 25 | my $host = $config->{'host'} || '127.0.0.1'; | ||
| 26 | my $LWT = $config->{'last_will'} || 'tele/power-outlet-mqtt-listener/LWT'; | ||
| 27 | |||
| 28 | my $mqtt = Net::MQTT::Simple->new($host); | ||
| 29 | $mqtt->last_will($LWT => 'Offline'); | ||
| 30 | $mqtt->retain($LWT => 'Online'); | ||
| 31 | |||
| 32 | die("Error: Yaml configuration must provide directives entry") unless exists $config->{'directives'}; | ||
| 33 | my $directives = $config->{'directives'}; | ||
| 34 | die("Error: Yaml configuration directives entry must be an array") unless ref($directives) eq 'ARRAY'; | ||
| 35 | printf "%s: Directives: %s\n", DateTime->now, scalar(@$directives); | ||
| 36 | |||
| 37 | my $topics = {}; | ||
| 38 | foreach my $directive (@$directives) { | ||
| 39 | my $topic = $directive->{'topic'}; | ||
| 40 | my $value = $directive->{'value'}; | ||
| 41 | my $dirname = $directive->{'name'} || "$topic => $value"; | ||
| 42 | printf qq{%s: Directive: "%s", Topic: %s, Value: %s\n}, DateTime->now, $dirname, $topic, $value; | ||
| 43 | |||
| 44 | my $actions = $directive->{'actions'} || []; | ||
| 45 | die("Error: Yaml configuration actions must be an array of dictionaries") unless ref($actions) eq 'ARRAY'; | ||
| 46 | |||
| 47 | printf qq{%s: Directive: "%s", Topic: %s, Value: %s, Actions: %s\n}, DateTime->now, $dirname, $topic, $value, scalar(@$actions); | ||
| 48 | |||
| 49 | foreach my $action (@$actions) { | ||
| 50 | die("Error: Yaml configuration action must be a dictionary") unless ref($action) eq 'HASH'; | ||
| 51 | my $driver = $action->{'driver'} or die("Error: Yaml configuration action must have a driver"); | ||
| 52 | |||
| 53 | my $command = $action->{'command'} or die("Error: Yaml configuration action must have a command"); | ||
| 54 | die("Error: Yaml configuration action command must be one of ON|OFF|SWITCH|CYCLE") unless $command =~ m/\A(ON|OFF|SWITCH|TOGGLE|CYCLE)\Z/i; | ||
| 55 | |||
| 56 | my $actname = $action->{'name'} ||= join(' => ', $action->{'driver'}, $action->{'command'}); | ||
| 57 | |||
| 58 | printf qq{%s: Directive: "%s", Topic: %s, Value: %s, Action: "%s", Driver: %s, Command: %s\n}, DateTime->now, $dirname, $topic, $value, $actname, $driver, $command; | ||
| 59 | my $class = "Power::Outlet::$driver"; | ||
| 60 | local $@; | ||
| 61 | eval "use $class"; | ||
| 62 | my $error = $@; | ||
| 63 | die(qq{Error: Power::Outlet class "$class" not loaded}) if $error; | ||
| 64 | } | ||
| 65 | |||
| 66 | $topics->{$topic}->{$value} ||= []; | ||
| 67 | push @{$topics->{$topic}->{$value}}, @$actions; | ||
| 68 | } | ||
| 69 | |||
| 70 | foreach my $topic (sort keys %$topics) { | ||
| 71 | printf qq{%s: Subscription: Topic: %s, Values: [%s]\n}, DateTime->now, $topic, join(", ", map {qq{"$_"}} sort keys %{$topics->{$topic}}); | ||
| 72 | } | ||
| 73 | |||
| 74 | #print Dumper({config=>$config, topics=>$topics}); | ||
| 75 | |||
| 76 | $mqtt->run(map {$_, \&handler} keys %$topics); | ||
| 77 | |||
| 78 | printf "%s: Finish\n", DateTime->now; | ||
| 79 | |||
| 80 | sub handler { | ||
| 81 | my $topic = shift; | ||
| 82 | my $message = shift; | ||
| 83 | printf qq{%s: Topic: "%s", Message: "%s"\n}, DateTime->now, $topic, $message; | ||
| 84 | my $actions = $topics->{$topic}->{$message} || []; | ||
| 85 | if (@$actions) { | ||
| 86 | foreach my $action (@$actions) { | ||
| 87 | my $driver = $action->{'driver'}; | ||
| 88 | my $command = $action->{'command'}; | ||
| 89 | my $actname = $action->{'name'}; | ||
| 90 | my $options = $action->{'options'} || {}; | ||
| 91 | my $outlet = Power::Outlet->new(type=>$driver, %$options); | ||
| 92 | my $return = $command =~ m/\AON/i ? $outlet->on | ||
| 93 | : $command =~ m/\AOFF/i ? $outlet->off | ||
| 94 | : $command =~ m/\A(SWITCH|TOGGLE)\Z/i ? $outlet->switch | ||
| 95 | : $command =~ m/\ACYCLE\Z/i ? $outlet->cycle | ||
| 96 | : die(sprintf(qq{%s: Topic: "%s", Message: "%s", Type: %s, Command: %s not found, Expected, "ON|OFF|SWITCH|CYCLE"\n}, DateTime->now, $topic, $message, $driver, $command)); | ||
| 97 | printf qq{%s: Topic: "%s", Message: "%s", Type: %s, Command: %s, Return: %s\n}, DateTime->now, $topic, $message, $driver, $command, $return; | ||
| 98 | } | ||
| 99 | } else { | ||
| 100 | printf "%s: Topic: %s, Value: %S, No actions registered\n", DateTime->now, $topic, $message; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | __END__ |