talk to tasmota over mqtt and emulate LG TV remote
[mqtt-homehub] / mqtt-trend.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 my $trend = {
6         hist_spec => {
7                 'fade/LDR' => '300x1',
8                 '__default' => '100x1',
9         }
10 };
11
12 sub DESTROY {
13         warn "DESTROY";
14         system "killall mosquitto_sub trend";
15 }
16 system "killall mosquitto_sub trend";
17 system "i3-msg layout splitv";
18 #system "i3-msg border none";
19
20 my $border = 2;
21
22 my $topic = 'fade/#';
23 $topic = $ARGV[0] if $ARGV[0];
24
25 open(my $in, '-|', "mosquitto_sub -h rpi2 -t '$topic' -v");
26 while(<$in>) {
27         s/\s+$//sg;
28
29         my ($t,$v) = split(/\s/,$_,2);
30         print STDERR "$t=$v ";
31
32         if ( ! exists $trend->{$t} && $v =~ m/^\d+/ ) {
33                 my $hist = $trend->{hist_spec}->{$t} || $trend->{hist_spec}->{__default};
34                 my $label = $t;
35                 $label =~ s/^.+\///;
36                 open($trend->{$t}, '|-', "trend -L $label -v - $hist");
37                 $border = 2;
38         }
39
40         if ( $trend->{$t} ) {
41                 select $trend->{$t};
42                 $| = 1;
43                 print "$v\n";
44         } else {
45                 warn "CLOSED $!\n";
46         }
47
48         system "i3-msg border 1pixel" if $border-- > 0;
49 }
50