package APKPM::DSLAM;

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

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

use H1::ZTEDSLAM;
use H1::ZTEMSAN;

sub prefix { 'DSLAM_' }

sub process_name {
	my ( $self, $orig, $job_name ) = @_;
	warn "# process_name $orig $job_name\n";
	return "$orig ($job_name)";
}

sub begin {
	my ( $self, $job, $workload ) = @_;
	warn "# begin ", dump( $workload );
}

sub search : Job : Encode(e_json_encode) {
	my ( $self, $job, $workload ) = @_;
	warn "# search ",dump($workload);

	my ($field,$value);

	if ( $workload =~ m/[:=]/ ) {
		($field,$value) = split(/[:=]/,$workload,2)
	} else {
		return { error => 'invalid workload', expected => 'OPT_82:%' };
	}

	my $crm_results = $self->do('CRM_search' => $workload);

	my $crm = $crm_results->[0];
	warn "ERROR: more than one result from CRM, using first ", dump($crm_results)
		if $#$crm_results > 0;

	warn "# crm ",dump $crm;

	my $hash = $self->do_high( $crm->{TIP_UREDJAJA} . '_' . $crm->{IP_UREDAJA}, $self->e_json( $crm ) );

	warn "# hash ",dump $hash;

	return $hash;
}

1;
