make index page, and per-sensor page
[zc] / zc-mqtt
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 # based on net-mqtt-sub example from Net::MQTT
6 #
7 # 2020-08-15 Dobrica Pavlinusic <dpavlin@rot13.org>
8
9 use strict;
10 use Net::MQTT::Constants;
11 use Net::MQTT::Message;
12 use IO::Select;
13 use IO::Socket::INET;
14 use Time::HiRes;
15 use Getopt::Long;
16 use Pod::Usage;
17 use POSIX qw(strftime);
18 use File::Slurp;
19 use DBD::Pg;
20 use Data::Dump qw(dump);
21 use autodie;
22
23 BEGIN {
24         my $cwd = $0;
25         $cwd =~ s{/[^/]+$}{};
26         chdir $cwd;
27
28         open(my $log, '>', 'log.'.strftime("%Y-%m-%dT%H:%M:%S",localtime()));
29         select($log);
30         $|=1;
31 }
32
33 use lib '.';
34 use Protocol;
35
36 my $queue = "queue";
37
38 my $dbh = DBI->connect("dbi:Pg:dbname=zc", "dpavlin", "", { RaiseError => 1 });
39 my @columns = split(/\n/, <<__COLUMNS__);
40 0x01    PN
41 0x03    X_axis_angle
42 0x04    Y_axis_angle
43 0x0c    Sensor_temperature
44 0x0d    Power_source_voltage
45 0x11    Arming_disarming
46 0x17    Signal_strength
47 0x18    Sensor_operating_mode
48 0x19    Alarm_axis
49 __COLUMNS__
50
51 my $cols = join(',',  map {      (split(/\t/,$_))[1]   } @columns);
52 my @insert_data_ids = map { hex( (split(/\t/,$_))[0] ) } @columns ;
53
54 my $sql_placeholders = ',?' x ($#columns+1);
55 $sql_placeholders =~ s/^,//;
56
57 my $sth = $dbh->prepare(qq{insert into zc ($cols) values ($sql_placeholders)});
58
59 my $help;
60 my $man;
61 my $verbose = 2;
62 my $host = 'localhost';
63 my $port = 1883;
64 my $count;
65 my $client_id;
66 my $keep_alive_timer = 120;
67 GetOptions('help|?' => \$help,
68            'man' => \$man,
69            'verbose+' => \$verbose,
70            'host=s' => \$host,
71            'port=i' => \$port,
72            'count=i' => \$count,
73            'one|1' => sub { $count = 1 },
74            'client_id|client-id|C=s' => \$client_id,
75            'keepalive=i' => \$keep_alive_timer) or pod2usage(2);
76 pod2usage(1) if ($help);
77 pod2usage(-exitstatus => 0, -verbose => 2) if $man;
78 #pod2usage(2) unless (@ARGV); # need a topic
79 push @ARGV, '#' unless (@ARGV);
80
81 open_again:
82 print "Open $host:port keep_alive_timer: $keep_alive_timer\n";
83 my $socket =
84   IO::Socket::INET->new(PeerAddr => $host.':'.$port,
85                         Timeout => $keep_alive_timer,
86                        ) or die "Socket connect failed: $!\n";
87
88 my $buf = '';
89 my $mid = 1;
90 my $next_ping;
91 my $got_ping_response = 1;
92 my @connect = ( message_type => MQTT_CONNECT,
93                 keep_alive_timer => $keep_alive_timer );
94 push @connect, client_id => $client_id if (defined $client_id);
95 send_message($socket, @connect);
96 my $msg = read_message($socket, $buf) or die "No ConnAck\n";
97 print 'Received: ', $msg->string, "\n" if ($verbose >= 2);
98 send_message($socket, message_type => MQTT_SUBSCRIBE,
99              message_id => $mid++,
100              topics => [ map { [ $_ => MQTT_QOS_AT_MOST_ONCE ] } @ARGV ]);
101 $msg = read_message($socket, $buf) or die "No SubAck\n";
102 print 'Received: ', $msg->string, "\n" if ($verbose >= 2);
103
104 while (1) {
105   $msg = read_message($socket, $buf);
106   if ($msg) {
107     my $t = time();
108     print "\n",strftime("%Y-%m-%d %H:%M:%S ", localtime($t)) if $msg->message_type != MQTT_PINGRESP;
109     if ($msg->message_type == MQTT_PUBLISH) {
110       if ($verbose == 0) {
111         print $msg->topic, " ", $msg->message, "\n";
112       } else {
113         print $msg->string, "\n";
114       }
115
116         my $topic = $msg->topic;
117         # Inclinometer/ZCT330Ex_SWP_N_YK/869858031634109/up
118         my $dir = $topic; # leave imei in dir
119         $dir =~ s{\w+/\w+/(\w+)/(\w+)$}{$1};
120         my $up_down = $2;
121         
122         mkdir "$queue" if ( ! -e "$queue" );
123         mkdir "$queue/$dir" if ( ! -e "$queue/$dir" );
124         mkdir "$queue/$dir/.pending" if ( ! -e "$queue/$dir/.pending" );
125
126         my $function_code = unpack('C',substr($msg->message,2,1));
127
128         write_file "$queue/$dir/$t.$up_down.$function_code", $msg->message;
129
130         if ( substr($msg->message,2,1) eq "\x07" ) { # heartbeat
131                 $topic =~ s/up$/down/;
132
133                 my @all_pending = sort glob "$queue/$dir/.pending/*";
134                 if ( my $pending = shift @all_pending ) {
135                         my $raw = read_file $pending;
136                         my $pending_function_code = unpack('C',substr($msg->message,2,1));
137
138                         send_message($socket,
139                                 message_type => MQTT_PUBLISH,
140                                 retain => 0, #$retain,
141                                 topic => $topic,
142                                 message => $raw);
143                         $pending =~ s{$queue/$dir/.pending/}{};
144                         rename "$queue/$dir/.pending/$pending", "$queue/$dir/$pending.sent.$pending_function_code";
145                 }
146
147                 my $hash = protocol_decode( $up_down, $msg->message );
148                 $sth->execute( map { $hash->{data_id}->{$_} } @insert_data_ids );
149
150
151       }
152
153       if (defined $count && --$count == 0) {
154         exit;
155       }
156     } elsif ($msg->message_type == MQTT_PINGRESP) {
157       $got_ping_response = 1;
158       print 'Received: ', $msg->string, "\n" if ($verbose >= 3);
159     } else {
160       print 'Received: ', $msg->string, "\n" if ($verbose >= 2);
161     }
162   }
163   if (Time::HiRes::time > $next_ping) {
164     die "Ping Response timeout.  Exiting\n" unless ($got_ping_response);
165     send_message($socket, message_type => MQTT_PINGREQ);
166   }
167 }
168
169 sub send_message {
170   my $socket = shift;
171   my $msg = Net::MQTT::Message->new(@_);
172   print 'Sending: ', $msg->string, "\n" if ($verbose >= 2 && $_[1] eq 'message_type' && $_[2] != MQTT_PINGREQ);
173   $msg = $msg->bytes;
174   syswrite $socket, $msg, length $msg;
175   print dump_string($msg, 'Sent: '), "\n\n" if ($verbose >= 3);
176   $next_ping = Time::HiRes::time + $keep_alive_timer;
177 }
178
179 sub read_message {
180   my $socket = shift;
181   my $select = IO::Select->new($socket);
182   my $timeout = $next_ping - Time::HiRes::time;
183   do {
184     my $mqtt = Net::MQTT::Message->new_from_bytes($_[0], 1);
185     return $mqtt if (defined $mqtt);
186     $select->can_read($timeout) || return;
187     $timeout = $next_ping - Time::HiRes::time;
188     my $bytes = sysread $socket, $_[0], 2048, length $_[0];
189     unless ($bytes) {
190       warn "Socket closed ", (defined $bytes ? 'gracefully' : 'error'), "\n";
191       # FIXME reopen
192       goto open_again;
193     }
194     print "Receive buffer: ", dump_string($_[0], '   '), "\n\n"
195       if ($verbose >= 3);
196   } while ($timeout > 0);
197   return;
198 }
199
200 __END__
201
202 =pod
203
204 =encoding UTF-8
205
206 =head1 NAME
207
208 net-mqtt-sub - Perl script for subscribing to an MQTT topic
209
210 =head1 VERSION
211
212 version 1.143260
213
214 =head1 SYNOPSIS
215
216   net-mqtt-sub [options] topic1 [topic2] [topic3] ...
217
218 =head1 DESCRIPTION
219
220 This script subscribes to one or more MQTT topics and prints any
221 messages that it receives to stdout.
222
223 =head1 OPTIONS
224
225 =over
226
227 =item B<-help>
228
229 Print a brief help message.
230
231 =item B<-man>
232
233 Print the manual page.
234
235 =item B<-host>
236
237 The host running the MQTT service.  The default is C<127.0.0.1>.
238
239 =item B<-port>
240
241 The port of the running MQTT service.  The default is 1883.
242
243 =item B<-client-id>
244
245 The client id to use in the connect message.  The default is
246 'NetMQTTpm' followed by the process id of the process.
247
248 =item B<-verbose>
249
250 Include more verbose output.  Without this option the script only
251 outputs errors and received messages one per line in the form:
252
253   topic message
254
255 With one B<-verbose> options, publish messages are printed in a form
256 of a summary of the header fields and the payload in hex dump and text
257 form.
258
259 With two B<-verbose> options, summaries are printed for all messages
260 sent and received.
261
262 With three B<-verbose> options, a hex dump of all data transmitted and
263 received is printed.
264
265 =item B<-keepalive NNN>
266
267 The keep alive timer value.  Defaults to 120 seconds.  For simplicity,
268 it is also currently used as the connection/subscription timeout.
269
270 =item B<-count NNN>
271
272 Read the specificed number of MQTT messages and then exit.  Default
273 is 0 - read forever.
274
275 =item B<-one> or B<-1>
276
277 Short for B<-count 1>.  Read one message and exit.
278
279 =back
280
281 =head1 SEE ALSO
282
283 Net::MQTT::Message(3)
284
285 =head1 DISCLAIMER
286
287 This is B<not> official IBM code.  I work for IBM but I'm writing this
288 in my spare time (with permission) for fun.
289
290 =head1 AUTHOR
291
292 Mark Hindess <soft-cpan@temporalanomaly.com>
293
294 =head1 COPYRIGHT AND LICENSE
295
296 This software is copyright (c) 2014 by Mark Hindess.
297
298 This is free software; you can redistribute it and/or modify it under
299 the same terms as the Perl 5 programming language system itself.
300
301 =cut
302