cc128_parse.pl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/perl -w
  2. # Read raw cc128 data and republish without xml.
  3. # Probably only works if you have a single channel.
  4. use strict;
  5. use HTTP::Date "str2time";
  6. use FileHandle;
  7. local $| = 1;
  8. my $subclient = "mosquitto_sub -t sensors/cc128/raw -q 1";
  9. my $pubclient = "mosquitto_pub -t sensors/cc128 -q 1 -l";
  10. my $pubclient_ch1 = "mosquitto_pub -t sensors/cc128/ch1 -q 1 -l";
  11. open(SUB, "$subclient|");
  12. open(PUB, "|$pubclient");
  13. open(PUB_CH1, "|$pubclient_ch1");
  14. SUB->autoflush(1);
  15. PUB->autoflush(1);
  16. PUB_CH1->autoflush(1);
  17. while (my $line = <SUB>) {
  18. #<msg><src>CC128-v0.12</src><dsb>00002</dsb><time>00:02:12</time><tmpr>15.7</tmpr><sensor>0</sensor><id>03112</id><type>1</type><ch1><watts>00108</watts></ch1></msg>
  19. if ($line =~ m#<time>(.*)</time><tmpr> *([\-\d.]+)</tmpr><sensor>0</sensor><id>[0-9]*</id><type>1</type><ch1><watts>0*(\d+)</watts></ch1></msg.*#){
  20. my $reading_time = $1;
  21. my $temp = $2;
  22. my $watts = $3;
  23. my $now = time;
  24. my ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst,$r_stamp);
  25. ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = localtime($now);
  26. $year += 1900;
  27. $month += 1;
  28. $r_stamp = str2time("$year-$month-$mday $reading_time");
  29. if($r_stamp > $now){
  30. $r_stamp -= 86400;
  31. }
  32. print PUB "$r_stamp,$temp,$watts\n";
  33. print PUB_CH1 "$r_stamp $watts\n";
  34. }
  35. }