remove only device info
[air-quality] / iio2mqtt.pl
index 1879ce1..14c9430 100755 (executable)
@@ -8,6 +8,8 @@ use Time::HiRes;
 
 my $influx_url = shift @ARGV || 'http://10.13.37.92:8086/write?consistency=any&db=rot13';
 
+my $delay = $ENV{DELAY} || 1;
+
 my $hostname = `hostname -s`;
 chomp($hostname);
 
@@ -21,10 +23,19 @@ while(1) {
        my $device;
        my $name;
 
-       my @influx;
+       my $influx = '';
+
        foreach ( split(/\n/, $iio) ) {
                if ( m/iio:device\d+:\s+(\S+)/ ) {
                        $device = $1;
+
+                       if ( $influx =~ m/,$/ ) {
+                               $influx =~ s/,$/ $t_influx\n/;
+                       } elsif ( $influx =~ m/ $/ ) { # only device
+                               $influx =~ s/\S+\s$//;
+                       }
+                       $influx .= "iio,dc=trnjanska,host=$hostname,device=$device ";
+
                } elsif ( m/(\S+):\s+\(input\)/ ) {
                        $name = $1;
                } elsif ( m/attr\s+0:\s+input\svalue: (\d+[\.\d]+)/ ) {
@@ -36,13 +47,15 @@ while(1) {
                        #print "$topic $val\n";
                        system "mosquitto_pub -h rpi2 -t $topic -m $val";
 
-                       push @influx, "${device}_${name}=$val";
+                       $influx .= "$name=$val,";
                } else {
                        #warn "# $_\n";
                }
        }
-       my $influx = "iio,dc=trnjanska,host=$hostname " . join(",", @influx) . " $t_influx";
+
+       $influx =~ s/,$/ $t_influx/;
        system "curl --silent -XPOST '$influx_url' --data-binary '$influx'";
+       warn "$influx\n";
 
-       sleep Time::HiRes::time + 1 - $t;
+       sleep Time::HiRes::time + $delay - $t;
 }