package APKPM::VOICE;

use base qw(Gearman::Driver::Worker);
use Moose;
use Time::HiRes;
use Data::Dump qw(dump);

with 'APKPM::Config';
with 'APKPM::Gearman::Client';
with 'APKPM::Gearman';

use lib '/opt/managment_tools/lib';
use H1::VOICE;

sub prefix { 'VOICE_' }

sub info : Job : Encode(e_json) : MinProcesses(0) {
	my ( $self, $job, $workload ) = @_;
	warn "# workload = ", dump $workload;

	return { error => "invalid workload", expected => '385xxxxxx', workload => $workload } if $workload !~ m/^385/;

	my $broj = $workload;

	my $voice=H1::VOICE->new($self->config('srukey'),$self->config('saukey'),$self->config('sshid'));

	my $sau = $self->config('sau');

	$voice->connectSRU( $sau->[0] );
       	my %sru = $voice->searchSRU($broj);
	warn "# SRU ",dump( %sru );

	return { broj => $workload, error => "no serverid" } unless defined $sru{serverid};

	$voice->connectSAU( $sau->[ $sru{serverid} ] );
        my %sau = $voice->searchSAU($sru{alias});

	$self->do_background_json( 'Store_insert', {
		_table => 'voice',
		timestamp => $self->datetime_now,
		broj => $broj,
		state => $sau{state},
		serverid => $sru{serverid},
	});

	return { broj => $workload, sru => \%sru, sau => \%sau };
}

1;
