$VERSION
=
'0.2'
;
sub
new {
my
(
$class
) =
shift
;
my
(
$self
) =
shift
;
if
(!
exists
(
$self
->{DAPNET_USERNAME}) || !
exists
(
$self
->{DAPNET_PW}) || !
exists
(
$self
->{CALLSIGN})) {
return
(0);
}
bless
(
$self
,
$class
);
$self
->{_CALL_LEN} =
length
(
$self
->{CALLSIGN});
return
(
$self
);
};
sub
_build_request {
my
(
$self
) =
shift
;
my
(
$json
) =
shift
;
my
(
$type
) =
shift
;
my
(
$username
) =
$self
->{DAPNET_USERNAME};
my
(
$pw
) =
$self
->{DAPNET_PW};
my
(
$req
) = HTTP::Request->new(
POST
=>
$uri
);
$req
->header(
'Content-Type'
=>
'application/json'
,
'Authorization'
=>
'Basic '
. encode_base64(
$username
.
':'
.
$pw
)
);
$req
->content(
$json
);
return
(
$req
);
};
sub
_json_individual_call {
my
(
$self
) =
shift
;
my
(
$text
,
$to
,
$txgroup
,
$emergency
) =
@_
;
my
(
$jsonobj
) = JSON->new;
my
$json
=
$jsonobj
->encode ({
'text'
=>
$text
,
'callSignNames'
=> [
$to
],
'transmitterGroupNames'
=> [
$txgroup
],
'emergency'
=>
$emergency
});
return
(
$json
);
};
sub
_json_rubric_content {
my
(
$self
) =
shift
;
my
(
$text
,
$rubric
,
$number
) =
@_
;
my
(
$jsonobj
) = JSON->new;
my
$json
=
$jsonobj
->encode ({
'text'
=>
$text
,
'rubricName'
=>
$rubric
,
'number'
=>
$number
});
return
(
$json
);
};
sub
send_individual_call {
my
$self
=
shift
;
my
(
$text
,
$to
,
$txgroup
,
$emergency
) =
@_
;
my
(
$ua
) = LWP::UserAgent->new;
my
(
$jsonobj
);
my
(
$i
) = 1;
my
(
$json
);
if
(
length
(
$text
) <= (80 -
$self
->{_CALL_LEN})) {
my
(
$json
) =
$self
->_json_individual_call(
$self
->{CALLSIGN}.
':'
.
$text
,
$to
,
$txgroup
,
$emergency
);
my
(
$req
) =
$self
->_build_request(
$json
,
'calls'
);
my
(
$res
) =
$ua
->request(
$req
);
warn
(
$res
->status_line);
if
(!
$res
->is_success) {
return
(
$res
->status_line);
};
}
else
{
while
(
my
$substr
=
substr
(
$text
,0,(80 -
$self
->{_CALL_LEN}),
''
)) {
if
(
$i
== 1) {
$json
=
$self
->_json_individual_call(
$self
->{CALLSIGN}.
':'
.
$substr
.
'...'
,
$to
,
$txgroup
,
$emergency
);
}
else
{
$json
=
$self
->_json_individual_call(
$self
->{CALLSIGN}.
':'
.
'...'
.
$substr
,
$to
,
$txgroup
,
$emergency
);
};
my
(
$req
) =
$self
->_build_request(
$json
);
my
(
$res
) =
$ua
->request(
$req
);
warn
(
$res
->status_line);
if
(!
$res
->is_success) {
return
(
$res
->status_line);
};
$i
++;
};
};
return
(0);
}
sub
send_rubric_content {
my
$self
=
shift
;
my
(
$text
,
$rubric
,
$number
) =
@_
;
my
(
$ua
) = LWP::UserAgent->new;
my
(
$jsonobj
);
my
(
$i
) = 1;
my
(
$json
);
if
(
length
(
$text
) <= (80 -
$self
->{_CALL_LEN})) {
my
(
$json
) =
$self
->_json_rubric_content(
$self
->{CALLSIGN}.
':'
.
$text
,
$rubric
,
$number
);
my
(
$req
) =
$self
->_build_request(
$json
,
'news'
);
my
(
$res
) =
$ua
->request(
$req
);
warn
(
$res
->status_line);
if
(!
$res
->is_success) {
return
(
$res
->status_line);
};
}
else
{
while
(
my
$substr
=
substr
(
$text
,0,(80 -
$self
->{_CALL_LEN}),
''
)) {
if
(
$i
== 1) {
$json
=
$self
->_json_rubrik_content(
$self
->{CALLSIGN}.
':'
.
$substr
.
'...'
,
$rubric
,
$number
);
}
else
{
$json
=
$self
->_json_rubrik_content(
$self
->{CALLSIGN}.
':'
.
'...'
.
$substr
,
$rubric
,
$number
);
};
my
(
$req
) =
$self
->_build_request(
$json
);
my
(
$res
) =
$ua
->request(
$req
);
warn
(
$res
->status_line);
if
(!
$res
->is_success) {
return
(
$res
->status_line);
};
$i
++;
};
};
return
(0);
};
1;