unpack data from file
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 18 Sep 2020 09:00:48 +0000 (11:00 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 18 Sep 2020 09:00:48 +0000 (11:00 +0200)
unpack.pl

index 51a3786..00f840f 100755 (executable)
--- a/unpack.pl
+++ b/unpack.pl
@@ -4,6 +4,8 @@ use strict;
 
 use Data::Dump qw(dump);
 
+my $debug = $ENV{DEBUG};
+
 # Table of CRC values for high order byte
 use constant CRC_HI => [
     0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
@@ -87,42 +89,11 @@ while(<DATA>) {
        };
 }
 
-warn "## protocol = ",dump( $protocol );
-
+warn "## protocol = ",dump( $protocol ) if $debug;
 
-my @raw;
 
-#Publish/at-most-once,retain Inclinometer/ZCT330Ex_SWP_N_YK/869858035167585/up
-push @raw, qq{
-  5a 0c 08 29 00 01 04 55 79 fb 71 02 01 10 03 04  Z..)...Uy.q.....
-  00 00 df c1 04 04 75 93 6c c1 0c 02 49 0a 0d 02  ......u.l...I...
-  61 01 11 01 01 17 01 12 18 01 01 19 01 03 48 5d  a.............H]
-};
-
-#Publish/at-most-once,retain Inclinometer/ZCT330Ex_SWP_N_YK/869858031634109/up
-push @raw, qq{
-  5a 0c 07 29 00 01 04 00 00 00 00 02 01 10 03 04  Z..)............
-  df 4f ad 3f 04 04 d1 22 4b c0 0c 02 c2 14 0d 02  .O.?..."K.......
-  41 01 11 01 01 17 01 16 18 01 01 19 01 00 a8 b9  A...............
-};
-#Publish/at-most-once,retain Inclinometer/ZCT330Ex_SWP_N_YK/864823041482779/up
-push @raw, qq|
-  5a 0c 08 29 00 01 04 00 00 00 00 02 01 10 03 04  Z..)............
-  db f9 3e 3f 04 04 b2 9d 6f be 0c 02 b7 10 0d 02  ..>?....o.......
-  48 01 11 01 01 17 01 1a 18 01 01 19 01 03 7d 2c  H.............},
-|;
-#Publish/at-most-once,retain Inclinometer/ZCT330Ex_SWP_N_YK/864823041482498/up
-push @raw, qq{
-  5a 0c 08 29 00 01 04 e3 b1 a0 77 02 01 10 03 04  Z..)......w.....
-  02 2b 87 be 04 04 27 31 e8 c1 0c 02 bd 0b 0d 02  .+....'1........
-  45 01 11 01 01 17 01 16 18 01 01 19 01 02 2a 69  E.............*i
-};
-
-my $raw_nr = shift @ARGV || 0;
-my $raw = $raw[$raw_nr];
-$raw =~ s/  \S{16}\n//gs;
-$raw =~ s/^\s+//;
-my $up_down = 'up';
+sub protocol_decode {
+       my ( $up_down, $raw ) = @_;
 
 warn "up/down: $up_down raw = ",dump($raw);
 
@@ -224,9 +195,40 @@ while ( $data ) {
        }
 
        my $description = $protocol->{$data_id}->{description} || die "can't find description for data_id $data_id";
-       print "$up_down | $data_id | $description | hex:", unpack('H*', $data_range), " fmt:$pack_fmt [v=$v] range:",$protocol->{$data_id}->{range}, " remark:", $protocol->{$data_id}->{remark},"\n";
+       print "$up_down | $data_id | $description | v=$v | hex:", unpack('H*', $data_range), " fmt:$pack_fmt | range:",$protocol->{$data_id}->{range}, " | remark:", $protocol->{$data_id}->{remark},"\n";
 }
 
+
+} # protocol_decode
+
+my $raw;
+my ( $timestamp, $sensor, $imei, $up_down );
+my $stat;
+while(<>) {
+       chomp;
+
+       if ( m{^(\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d).*Inclinometer/([^/]+)/([^/]+)/(\w+)} ) {
+               if ( $raw ) {
+                       $raw =~ s{^\s+}{}; # strip leading spaces
+                       print "## $timestamp $sensor $imei $up_down $raw\n";
+                       protocol_decode( $up_down, $raw );
+                       $raw = '';
+               }
+               ( $timestamp, $sensor, $imei, $up_down ) = ( $1, $2, $3, $4 );
+               $stat->{$sensor}->{$imei}->{$up_down}++;
+       } elsif ( m{^\s+} ) {
+               s/\s\s\S\S\S+//g; # remove ascii
+               s/^\s+/ /;
+               $raw .= $_;
+               warn "++ $_\n";
+       } else {
+               warn "IGNORE: $_\n";
+       }
+
+}
+
+print "stat = ",dump($stat), "\n";
+
 # 6. Protocol Format 
 # Valid data ID and parameter range supported by the product 
 # DATA ID      ID Description  Data Type( Data Length)       R/W     Range   Default         Remark