5 use Data::Dump qw(dump);
13 my $debug = $ENV{DEBUG};
17 my $dbh = DBI->connect("dbi:Pg:dbname=zc", "dpavlin", "", { RaiseError => 1 });
29 my $cols = join(',', @columns);
30 my $sth = $dbh->prepare(qq{insert into zc (time,$cols) values (?} . ',?' x ($#columns+1) . qq{)});
32 warn "## protocol = ",dump( $protocol ) if $debug;
34 open(my $i, '>', '/tmp/influx');
37 my ( $timestamp, $sensor, $imei, $up_down, $hex ) = @_;
39 my ( $yyyy, $mm, $dd, $h, $m, $s ) = split(/[- :]/, $timestamp);
40 my $t = timelocal( $s, $m, $h, $dd, $mm, $yyyy ) * 1_000_000_000;
44 my $raw = join('', map { chr(hex($_)) } split(/\s+/,$hex));
46 warn "raw = ", hex_dump($raw);
48 my $hash = protocol_decode( $up_down, $raw );
50 warn "hash = ",dump($hash);
52 my $function_code = $hash->{function_code} || die "no function_code";
53 my $fc_desc = $function_code_description->{$up_down}->{$function_code} || die "no function_code_description for $up_down $function_code";
54 print " function_code=$function_code ",
56 " ver=", $hash->{ver},
57 " len=", $hash->{len},
62 foreach my $data_id ( @{ $hash->{data_id_order} } ) {
63 my $fmt = $protocol->{$data_id}->{pack_fmt};
64 my $v = $hash->{data_id}->{$data_id};
67 unpack('H*', chr($data_id)),
68 $protocol->{$data_id}->{description},
70 "hex:" . unpack('H*', $hash->{data_range}->{$data_id}),
71 "len:" . $hash->{data_len}->{$data_id},
73 "range:" . $protocol->{$data_id}->{range},
74 "remark:" . $protocol->{$data_id}->{remark}
77 my $name = $protocol->{$data_id}->{description} || die;
80 $json->{$name} = $v if length($v) > 0;
83 if ( $function_code == 0x07 ) {
84 $sth->execute( $timestamp, map { $json->{lc($_)} } @columns );
90 my ( $timestamp, $sensor, $imei, $up_down );
95 if ( m{^(\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d).*Inclinometer/([^/]+)/([^/]+)/(\w+)} ) {
97 $raw =~ s{^\s+}{}; # strip leading spaces
98 print "## $timestamp $sensor $imei $up_down $raw\n";
99 print_hex( $timestamp, $sensor, $imei, $up_down, $raw );
102 ( $timestamp, $sensor, $imei, $up_down ) = ( $1, $2, $3, $4 );
103 $stat->{$sensor}->{$imei}->{$up_down}++;
104 } elsif ( m{^\s+} ) {
106 s/\s\s.+$//g; # remove ascii
116 $raw =~ s{^\s+}{}; # strip leading spaces
117 print "## $timestamp $sensor $imei $up_down $raw\n";
118 print_hex( $timestamp, $sensor, $imei, $up_down, $raw );
121 print "stat = ",dump($stat), "\n";