Merge branch 'master' of mjesec.ffzg.hr:/git/zc
[zc] / log2pgsql
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Data::Dump qw(dump);
6 use DBD::Pg;
7 use JSON;
8 use Time::Local;
9
10 use lib '.';
11 use Protocol;
12
13 my $debug = $ENV{DEBUG};
14
15 $| = 1;
16
17 my $dbh = DBI->connect("dbi:Pg:dbname=zc", "dpavlin", "", { RaiseError => 1 });
18 my @columns = qw(
19 PN
20 Alarm_axis
21 X_axis_angle
22 Y_axis_angle
23 Signal_strength
24 Arming_disarming
25 Sensor_temperature
26 Power_source_voltage
27 Sensor_operating_mode
28 );
29 my $cols = join(',', @columns);
30 my $sth = $dbh->prepare(qq{insert into zc (time,$cols) values (?} . ',?' x ($#columns+1) . qq{)});
31
32 warn "## protocol = ",dump( $protocol ) if $debug;
33
34 open(my $i, '>', '/tmp/influx');
35
36 sub print_hex {
37         my ( $timestamp, $sensor, $imei, $up_down, $hex ) = @_;
38
39         my ( $yyyy, $mm, $dd, $h, $m, $s ) = split(/[- :]/, $timestamp);
40         my $t = timelocal( $s, $m, $h, $dd, $mm, $yyyy ) * 1_000_000_000;
41
42         warn "hex = $hex\n";
43
44         my $raw = join('', map { chr(hex($_)) } split(/\s+/,$hex));
45
46         warn "raw = ", hex_dump($raw);
47
48         my $hash = protocol_decode( $up_down, $raw );
49
50         warn "hash = ",dump($hash);
51
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 ",
55                 $fc_desc,
56                 " ver=", $hash->{ver},
57                 " len=", $hash->{len},
58                 "\n";
59
60         my $json;
61
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};
65                 print join(" | ", 
66                         $hash->{up_down},
67                         unpack('H*', chr($data_id)),
68                         $protocol->{$data_id}->{description},
69                         $v,
70                         "hex:" . unpack('H*', $hash->{data_range}->{$data_id}),
71                         "len:" . $hash->{data_len}->{$data_id},
72                         "fmt:" . $fmt,
73                         "range:" . $protocol->{$data_id}->{range}, 
74                         "remark:" . $protocol->{$data_id}->{remark}
75                 ),"\n";
76
77                 my $name = $protocol->{$data_id}->{description} || die;
78                 $name =~ s/\W+/_/g;
79                 $name = lc($name);
80                 $json->{$name} = $v if length($v) > 0;
81         }
82
83         if ( $function_code == 0x07 ) {
84                 $sth->execute( $timestamp, map { $json->{lc($_)} } @columns );
85         }
86
87 }
88
89 my $raw;
90 my ( $timestamp, $sensor, $imei, $up_down );
91 my $stat;
92 while(<>) {
93         chomp;
94
95         if ( m{^(\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d).*Inclinometer/([^/]+)/([^/]+)/(\w+)} ) {
96                 if ( $raw ) {
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 );
100                         $raw = '';
101                 }
102                 ( $timestamp, $sensor, $imei, $up_down ) = ( $1, $2, $3, $4 );
103                 $stat->{$sensor}->{$imei}->{$up_down}++;
104         } elsif ( m{^\s+} ) {
105                 s/^\s+/ /;
106                 s/\s\s.+$//g; # remove ascii
107                 $raw .= $_;
108                 warn "++ $_\n";
109         } else {
110                 warn "IGNORE: $_\n";
111         }
112
113 }
114
115 if ( $raw ) {
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 );
119 }
120
121 print "stat = ",dump($stat), "\n";
122