$VERSION
=
'0.3'
;
sub
new {
my
(
$class
) =
shift
;
my
(
$self
) =
shift
;
if
(!
exists
(
$self
->{BM_APIKEY}) || !
exists
(
$self
->{DMRID})) {
return
(0);
}
$self
->{BM_APIKEYBASE64} = encode_base64(
$self
->{BM_APIKEY}.
':'
);
bless
(
$self
,
$class
);
return
(
$self
);
};
sub
_build_request {
my
(
$self
) =
shift
;
my
(
$requrlpart
) =
shift
;
my
(
$formdataref
) =
shift
;
my
(
$uri
) =
$self
->{BM_APIBASEURL}.
$requrlpart
;
print
(
"Building HTTP request\n"
)
if
(
$self
->{DEBUG});
my
(
$req
);
if
(
$formdataref
) {
$req
= HTTP::Request::Common::POST(
$uri
,
$formdataref
);
}
else
{
$req
= HTTP::Request::Common::POST(
$uri
);
};
$req
->header(
'Content-Type'
=>
'application/x-www-form-urlencoded'
,
'Authorization'
=>
'Basic '
.
$self
->{BM_APIKEYBASE64}
);
return
(
$req
);
};
sub
_send_request {
my
(
$self
) =
shift
;
my
(
$req
) =
shift
;
return
(1)
if
(!
$req
);
my
(
$ua
) = LWP::UserAgent->new;
my
(
$jsonresobj
) = JSON->new;
my
(
$res
) =
$ua
->request(
$req
);
print
(
'Request status line: '
.
$res
->status_line.
"\n"
)
if
(
$self
->{DEBUG}) ;
if
(!
$res
->is_success) {
return
(
$res
->status_line);
};
$self
->{_JSONRESPONSEREF} =
$jsonresobj
->decode(
$res
->decoded_content);
return
(0);
};
sub
json_response {
my
(
$self
) =
shift
;
print
(
"Return JSON response\n"
)
if
(
$self
->{DEBUG});
return
(
$self
->{_JSONRESPONSEREF});
};
sub
_do_action {
my
(
$self
) =
shift
;
my
(
$requrlpart
) =
shift
;
my
(
$formdataref
) =
shift
;
my
(
$req
) =
$self
->_build_request(
$requrlpart
,
$formdataref
);
my
(
$res
) =
$self
->_send_request(
$req
);
return
(
$res
);
};
sub
_action {
my
(
$self
) =
shift
;
my
(
$action
) =
shift
;
my
(
$reqaction
);
my
(
$formdataref
);
my
(
$ts
,
$tg
) =
@_
;
if
(
$action
eq
'delstatic'
) {
$reqaction
=
'talkgroup/?action=DEL&id='
.
$self
->{DMRID};
$formdataref
= [
talkgroup
=>
$tg
,
timeslot
=>
$ts
];
}
elsif
(
$action
eq
'addstatic'
) {
$reqaction
=
'talkgroup/?action=ADD&id='
.
$self
->{DMRID};
$formdataref
= [
talkgroup
=>
$tg
,
timeslot
=>
$ts
];
}
elsif
(
$action
eq
'dropdynamic'
) {
$reqaction
=
'setRepeaterTarantool.php?action=dropDynamicGroups&slot='
.
$ts
.
'&q='
.
$self
->{DMRID};
}
else
{
return
(1);
};
$self
->{LASTACTIONRES} =
$self
->_do_action(
$reqaction
,
$formdataref
);
return
(0);
};
sub
result {
my
(
$self
) =
shift
;
return
(
$self
->{LASTACTIONRES});
};
sub
add_static_tg {
my
(
$self
) =
shift
;
my
(
$ts
,
$tg
) =
@_
;
return
(
$self
->_action(
'addstatic'
,
$ts
,
$tg
));
};
sub
del_static_tg {
my
(
$self
) =
shift
;
my
(
$ts
,
$tg
) =
@_
;
return
(
$self
->_action(
'delstatic'
,
$ts
,
$tg
));
};
sub
dropdynamic {
my
(
$self
) =
shift
;
my
(
$ts
) =
shift
;
return
(
$self
->_action(
'dropdynamic'
,
$ts
));
};
1;