A stub, add your event loop or just poll.
#!/usr/bin/perl
use Asterisk::AMI;
use v5.14;
# set some variables change to suit your /etc/asterisk/manager*.conf settings
my $host = 'yourhost';
my $amiusername = 'youraccount';
my $amipass = 'yourpassword';
my @extens = ();
# check for fluxbulbs
sub fluxctrl
{
system("flux_led.py @_");
}
my $flux = `flux_led.py -s `;
chomp $flux;
my @bulbs = split /\n/, $flux;
my @count = split /\s+/, $bulbs[0];
if ( not $count[0] ) { print "no bulbs found";exit 1};
# check for connectivity
my $astman = Asterisk::AMI->new(PeerAddr => $host ,Username => $amiusername,Secret => $amipass );
die "Unable to connect to asterisk" unless ($astman);
&fluxctrl("-sS -1 --volatile >/dev/null");
&fluxctrl("-sS -c 'black b --volatile '>/dev/null" );
#check for bulb in database and name un-named ones
foreach my $bulb (1..$#bulbs){
(my $null, my $mac, my $ip) = (split /\s+/, @bulbs[$bulb]) ;
&putbulb($mac, 'ip', $ip);
if ( not &getbulb($mac, 'name')){
&fluxctrl("$ip -p 56 100 --volatile ");
say "Please identify the flashing bulb with a name . . . ";
my $getinput = <>;
chomp $getinput;
&putbulb($mac, 'name', $getinput);
&fluxctrl("$ip -c black --volatile");
}
}
# main work here
# subroutines here
sub showbulbs
{
my $showbulbs = $astman->action({Action => 'Command',Command=> 'database show BULBS'});
return $showbulbs->{'CMD'};
}
sub getbulb
{
my @args = @_;
my $getdb = $astman->action({Action => 'DBGet',Family => "BULBS/$args[0]", Key => "$args[1]"});
return $getdb->{'GOOD'};
}
sub putbulb
{
my @args = @_;
my $putdb = $astman->action({Action => 'DBPut',Family => "BULBS/$args[0]", Key => "$args[1]", Val => "$args[2]"});
}
sub getsip
{
my $getsip = $astman->action({ Action => 'Command',Command => 'SIP show peers' });
return $getsip->{'CMD'};
}
sub getiax2
{
my $getiax2 = $astman->action({ Action => 'Command',Command => 'IAX2 show peers' });
return $getiax2->{'CMD'};
}
# Start script
my
@Sip = @{&getsip};
pop
@Sip;shift
@Sip;
foreach my $ext (0..$#sip){
my $exten = (split /\s+|\// ,
@Sip[$ext])[0];
if ( $exten =~ /^\d+$/){ push @extens, grep(/\d+/, $exten)};
}
my @iax = @{&getiax2};
pop @iax;shift @iax;
foreach my $ext (0..$#iax){
push @extens, (split /\s+/ , @iax[$ext])[0];
}
my @blubs = @{&showbulbs};
pop @blubs;
foreach my $blub (0..$#blubs){
my @data = split /\s+:\s+/, @blubs[$blub];
my ($famkey, $value) = (split /\s+:\s+/, @blubs[$blub])[0, 1];
my @words = split /\//, $famkey;
shift @words;
my $key = pop @words;
my $family = join '/' , @words;;
say "family= $family , key= $key, value= $value";
}
foreach (@extens) {
my $status =$astman->action({Action => 'ExtensionState', Exten => "$_"}, )->{ 'PARSED' }->{ 'Status' };
print "Exten. $_ \tStatus $status\n";
}
# End script
exit 0;