level 177
[zc] / zc-send
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use File::Slurp;
6 use autodie;
7
8 use lib '.';
9 use Protocol;
10
11 my $queue = "queue";
12
13 my $path = shift @ARGV;
14 die "Usage: $0 queue/IMEI\n" unless $path;
15
16 # strip slash to recognize by-name/name/ as symlink
17 if ( -d $path && $path =~ m{/$} ) {
18         my $tmp = $path;
19         $tmp =~ s{/$}{};
20         if ( -l $tmp ) {
21                 $path = $tmp;
22         }
23 }
24
25 if ( -l $path ) {
26         $path = readlink $path;
27         warn "# using $path\n";
28 }
29
30 my $imei;
31 if ( $path =~ m{$queue/(\d+)} ) {
32         $imei = $1;
33         if ( ! -e "$queue/$imei") {
34                 die "can't find $queue/$imei\n";
35         }
36 } else {
37         die "can't find IMEI in $path\n";
38 }
39
40 my $raw;
41
42 if ( $ARGV[0] =~ m/interval/ ) {
43         # heartbeat interval
44         my $interval = $ARGV[1] || 30 * 60;
45         print "set heartbeat interval to $interval s\n";
46         $raw = write_parameter_frame( "\x21" => "\x04" . pack('l',$interval) );
47 } elsif ( $ARGV[0] =~ m/zero/ ) {
48         my $rel_zero = $ARGV[1] // 0;
49         print "Set relative zero $rel_zero\n";
50         $raw = write_parameter_frame( "\x3a" => "\x01" . pack('C',$rel_zero) );
51
52 } elsif ( $ARGV[0] =~ m/arming/ ) {
53         my $rel_zero = $ARGV[1] // 1;
54         print "Set alarm arming $rel_zero\n";
55         $raw = write_parameter_frame( "\x3a" => "\x11" . pack('C',$rel_zero) );
56
57 } elsif ( $ARGV[0] =~ m/mqtt/ ) {
58         my @mqtt = qw(185 201 196 30 1883);
59         print "Set server port ", join(' ', @mqtt), "\n";
60         $raw = write_parameter_frame( "\x14" => "\x06" . pack('CCCCs',@mqtt) );
61
62 } else {
63         print "read_parameter_frame\n";
64         $raw = read_parameter_frame( "\x00" => "\x04\xe8\x03\x00\x00",
65         "\x0e\x0f\x11\x12",
66         "\x14",
67         "\x1a\x21\x22\x23\x24\x33\x34\x35\x3a\x3b",
68         "\x44",
69         );
70 }
71
72 # temporary store to done
73 my $t = time();
74 write_file "$queue/$imei/.todo.$t", $raw;
75 # atomic rename to ensure that file is complete
76
77 rename "$queue/$imei/.todo.$t", "$queue/$imei/.pending/$t";
78