#!/usr/bin/perl use strict; use warnings; use File::Slurp; use autodie; use lib '.'; use Protocol; my $queue = "queue"; my $path = shift @ARGV; die "Usage: $0 queue/IMEI\n" unless $path; # strip slash to recognize by-name/name/ as symlink if ( -d $path && $path =~ m{/$} ) { my $tmp = $path; $tmp =~ s{/$}{}; if ( -l $tmp ) { $path = $tmp; } } if ( -l $path ) { $path = readlink $path; warn "# using $path\n"; } my $imei; if ( $path =~ m{$queue/(\d+)} ) { $imei = $1; if ( ! -e "$queue/$imei") { die "can't find $queue/$imei\n"; } } else { die "can't find IMEI in $path\n"; } my $raw; if ( $ARGV[0] =~ m/interval/ ) { # heartbeat interval my $interval = $ARGV[1] || 30 * 60; print "set heartbeat interval to $interval s\n"; $raw = write_parameter_frame( "\x21" => "\x04" . pack('l',$interval) ); } elsif ( $ARGV[0] =~ m/zero/ ) { my $rel_zero = $ARGV[1] // 0; print "Set relative zero $rel_zero\n"; $raw = write_parameter_frame( "\x3a" => "\x01" . pack('C',$rel_zero) ); } elsif ( $ARGV[0] =~ m/arming/ ) { my $rel_zero = $ARGV[1] // 1; print "Set alarm arming $rel_zero\n"; $raw = write_parameter_frame( "\x3a" => "\x11" . pack('C',$rel_zero) ); } elsif ( $ARGV[0] =~ m/mqtt/ ) { #my @mqtt = qw(185 201 196 30 1883); my @mqtt = qw(149 5 190 30 1883); print "Set server port ", join(' ', @mqtt), "\n"; $raw = write_parameter_frame( "\x14" => "\x06" . pack('CCCCs',@mqtt) ); } else { print "read_parameter_frame\n"; $raw = read_parameter_frame( "\x00" => "\x04\xe8\x03\x00\x00", "\x0e\x0f\x11\x12", "\x14", "\x1a\x21\x22\x23\x24\x33\x34\x35\x3a\x3b", "\x44", ); } # temporary store to done my $t = time(); write_file "$queue/$imei/.todo.$t", $raw; # atomic rename to ensure that file is complete rename "$queue/$imei/.todo.$t", "$queue/$imei/.pending/$t.$$";