| 1 | use strict; | ||
| 2 | |||
| 3 | package DAPNET::API; | ||
| 4 | |||
| 5 | use LWP::UserAgent; | ||
| 6 | use JSON; | ||
| 7 | use MIME::Base64; | ||
| 8 | #use LWP::ConsoleLogger::Everywhere (); | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | DAPNET::API - Use the DAPNET API from Perl | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | my($dapnetobj) = DAPNET::API->new({ | ||
| 17 | DAPNET_USERNAME => '<username>', | ||
| 18 | DAPNET_PW => '<password>', | ||
| 19 | CALLSIGN => '<your callsign>' | ||
| 20 | }); | ||
| 21 | |||
| 22 | $ret = $dapnetobj->send_rubric_content(<text to send>,<rubric name>.<transmitter group name>,<sequence number 1 - 10>); | ||
| 23 | |||
| 24 | $ret = $dapnetobj->send_individual_call(<text>,<destination callsign>,<transmitter group name>,<emergency boolean>)); | ||
| 25 | |||
| 26 | if ($ret) { | ||
| 27 | print('HTTP Error response: '.$ret."\n"); | ||
| 28 | } else { | ||
| 29 | print("Message sent\n"); | ||
| 30 | } | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | Implementation of the DAPNET REST API in Perl. | ||
| 35 | |||
| 36 | =head1 AUTHOR | ||
| 37 | |||
| 38 | Simon (G7RZU) <simon@gb7fr.org.uk> | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | use vars qw($VERSION); | ||
| 43 | #Define version | ||
| 44 | $VERSION = '0.9'; | ||
| 45 | |||
| 46 | =head1 METHODS | ||
| 47 | |||
| 48 | This section describes the methods that are implemented and their use. | ||
| 49 | |||
| 50 | =head2 new | ||
| 51 | |||
| 52 | Instantiates new object. Pass a hash reference with options as follows | ||
| 53 | |||
| 54 | my($dapnetobj) = DAPNET::API->new({ | ||
| 55 | DAPNET_USERNAME => '<username>', | ||
| 56 | DAPNET_PW => '<password>', | ||
| 57 | CALLSIGN => '<your callsign>' | ||
| 58 | DEBUG => [0|1] | ||
| 59 | }); | ||
| 60 | |||
| 61 | returns an object if sucessful or false if not sucessful | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | |||
| 66 | sub new { | ||
| 67 | my($class) = shift; | ||
| 68 | my($self) = shift; | ||
| 69 | if (!exists($self->{DAPNET_USERNAME}) || !exists($self->{DAPNET_PW}) || !exists($self->{CALLSIGN})) { | ||
| 70 | return(0); | ||
| 71 | } | ||
| 72 | bless($self,$class); | ||
| 73 | $self->{_CALL_LEN} = length($self->{CALLSIGN}); | ||
| 74 | return($self); | ||
| 75 | }; | ||
| 76 | |||
| 77 | sub _build_request { | ||
| 78 | my($self) = shift; | ||
| 79 | my($json) = shift; | ||
| 80 | my($type) = shift; | ||
| 81 | my($username) = $self->{DAPNET_USERNAME}; | ||
| 82 | my($pw) = $self->{DAPNET_PW}; | ||
| 83 | my($uri) = 'http://www.hampager.de:8080/'.$type; | ||
| 84 | print("Building HTTP request\n") if($self->{DEBUG}); | ||
| 85 | my($req) = HTTP::Request->new( | ||
| 86 | POST => $uri | ||
| 87 | ); | ||
| 88 | $req->header( 'Content-Type' => 'application/json', | ||
| 89 | 'Authorization'=>'Basic ' . encode_base64($username.':'.$pw) | ||
| 90 | ); | ||
| 91 | |||
| 92 | $req->content( $json ); | ||
| 93 | return($req); | ||
| 94 | |||
| 95 | }; | ||
| 96 | |||
| 97 | sub _json_individual_call { | ||
| 98 | my($self) = shift; | ||
| 99 | my($text,$to,$txgroup,$emergency) = @_; | ||
| 100 | my($jsonobj) = JSON->new; | ||
| 101 | print("Building JSON for individual call\n") if($self->{DEBUG}); | ||
| 102 | my $json = $jsonobj->encode ({ | ||
| 103 | 'text' => $text, | ||
| 104 | 'callSignNames' => [$to], | ||
| 105 | 'transmitterGroupNames' => [$txgroup], | ||
| 106 | 'emergency' => $emergency | ||
| 107 | }); | ||
| 108 | print("JSON:\n\n$json\n") if($self->{DEBUG}); | ||
| 109 | return($json); | ||
| 110 | }; | ||
| 111 | |||
| 112 | sub _json_rubric_content { | ||
| 113 | my($self) = shift; | ||
| 114 | my($text,$rubric,$number) = @_; | ||
| 115 | print("Building JSON for rubric\n") if($self->{DEBUG}); | ||
| 116 | my($jsonobj) = JSON->new; | ||
| 117 | |||
| 118 | my $json = $jsonobj->encode ({ | ||
| 119 | 'text' => $text, | ||
| 120 | 'rubricName'=> $rubric, | ||
| 121 | 'number' => $number | ||
| 122 | }); | ||
| 123 | print("JSON:\n\n$json\n")if($self->{DEBUG}); | ||
| 124 | return($json); | ||
| 125 | }; | ||
| 126 | |||
| 127 | =head2 json_response | ||
| 128 | |||
| 129 | Returns a hash ref to the last JSON response or undef if there is none | ||
| 130 | |||
| 131 | $jsonhashref = $jsonobj->json_response; | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | sub json_response { | ||
| 136 | my($self) = shift; | ||
| 137 | print("Return JSON response\n") if($self->{DEBUG}); | ||
| 138 | return($self->{_JSONRESPONSEREF}); | ||
| 139 | }; | ||
| 140 | |||
| 141 | =head2 send_individual_call | ||
| 142 | |||
| 143 | Send a call to a single callsign | ||
| 144 | |||
| 145 | $ret = $dapnetobj->send_individual_call(<text>,<destination callsign>,<transmitter group name>,<emergency boolean>)); | ||
| 146 | |||
| 147 | Returns 0 on sucess or the HTTP error string on error. | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | sub send_individual_call { | ||
| 152 | |||
| 153 | my $self = shift; | ||
| 154 | my($text,$to,$txgroup,$emergency) = @_; | ||
| 155 | print("Send individual call\n") if($self->{DEBUG}); | ||
| 156 | my($ua) = LWP::UserAgent->new; | ||
| 157 | my($jsonresobj) = JSON->new; | ||
| 158 | my($i) = 1; | ||
| 159 | my($json); | ||
| 160 | print("substr length: ".length($text)."\n") if($self->{DEBUG}); | ||
| 161 | if (length($text) <= (79 - $self->{_CALL_LEN})) { | ||
| 162 | my($json) = $self->_json_individual_call($self->{CALLSIGN}.':'.$text,$to,$txgroup,$emergency); | ||
| 163 | my($req) = $self->_build_request($json,'calls'); | ||
| 164 | my($res) = $ua->request($req); | ||
| 165 | print('Request status line: '.$res->status_line."\n") if($self->{DEBUG}) ; | ||
| 166 | if (!$res->is_success) { | ||
| 167 | return($res->status_line); | ||
| 168 | }; | ||
| 169 | print('JSON Response: '.$res->decoded_content."\n") if($self->{DEBUG}); | ||
| 170 | $self->{_JSONRESPONSEREF} = $jsonresobj->decode($res->decoded_content); | ||
| 171 | } else { | ||
| 172 | while (my $substr = substr($text,0,(76 - $self->{_CALL_LEN}),'')) { | ||
| 173 | if ($i == 1) { | ||
| 174 | print("substr begining: $substr \n") if($self->{DEBUG}); | ||
| 175 | $json = $self->_json_individual_call($self->{CALLSIGN}.':'.$substr.'...',$to,$txgroup,$emergency); | ||
| 176 | } else { | ||
| 177 | print("substr next: $substr \n") if($self->{DEBUG}); | ||
| 178 | $json = $self->_json_individual_call($self->{CALLSIGN}.':'.'...'.$substr,$to,$txgroup,$emergency); | ||
| 179 | }; | ||
| 180 | my($req) = $self->_build_request($json,'calls'); | ||
| 181 | my($res) = $ua->request($req); | ||
| 182 | print('Request status line: '.$res->status_line."\n") if($self->{DEBUG}); | ||
| 183 | if (!$res->is_success) { | ||
| 184 | return($res->status_line); | ||
| 185 | }; | ||
| 186 | print('JSON Response: '.$res->decoded_content."\n") if($self->{DEBUG}); | ||
| 187 | $self->{_JSONRESPONSEREF} = $jsonresobj->decode($res->decoded_content); | ||
| 188 | |||
| 189 | $i++; | ||
| 190 | }; | ||
| 191 | |||
| 192 | }; | ||
| 193 | |||
| 194 | |||
| 195 | return(0); | ||
| 196 | |||
| 197 | } | ||
| 198 | |||
| 199 | =head2 send_rubric_content | ||
| 200 | |||
| 201 | Sends a message to a rubric | ||
| 202 | |||
| 203 | $ret = $dapnetobj->send_rubric_content(<text to send>,<rubric name>.<transmitter group name>,<sequence number 1 - 10>,<send call? [0|1]>); | ||
| 204 | |||
| 205 | Returns 0 on sucess or the HTTP error string on error. | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | |||
| 210 | sub send_rubric_content { | ||
| 211 | my $self = shift; | ||
| 212 | my($text,$rubric,$number,$sendcall) = @_; | ||
| 213 | print("Send rubric\n") if($self->{DEBUG}); | ||
| 214 | my($jsonresobj) = JSON->new; | ||
| 215 | my($ua) = LWP::UserAgent->new; | ||
| 216 | $ua->timeout(20); | ||
| 217 | my($i) = 1; | ||
| 218 | my($json); | ||
| 219 | print("substr length: ".length($text)."\n") if($self->{DEBUG}); | ||
| 220 | if (length($text) <= (79 - $self->{_CALL_LEN})) { | ||
| 221 | if ($sendcall) { | ||
| 222 | $json = $self->_json_rubric_content($self->{CALLSIGN}.':'.$text,$rubric,$number); | ||
| 223 | } else { | ||
| 224 | $json = $self->_json_rubric_content($text,$rubric,$number); | ||
| 225 | }; | ||
| 226 | my($req) = $self->_build_request($json,'news'); | ||
| 227 | my($res) = $ua->request($req); | ||
| 228 | print('Request status line: '.$res->status_line."\n") if($self->{DEBUG}); | ||
| 229 | if (!$res->is_success) { | ||
| 230 | return($res->status_line); | ||
| 231 | }; | ||
| 232 | print('JSON Response: '.$res->decoded_content."\n") if($self->{DEBUG}); | ||
| 233 | $self->{_JSONRESPONSEREF} = $jsonresobj->decode($res->decoded_content); | ||
| 234 | |||
| 235 | } else { | ||
| 236 | while (my $substr = substr($text,0,(76 - $self->{_CALL_LEN}),'')) { | ||
| 237 | if ($i == 1) { | ||
| 238 | print("substr begining: $substr \n") if($self->{DEBUG}); | ||
| 239 | if ($sendcall) { | ||
| 240 | $json = $self->_json_rubric_content($self->{CALLSIGN}.':'.$substr.'...',$rubric,$number); | ||
| 241 | } else { | ||
| 242 | $json = $self->_json_rubric_content($substr.'...',$rubric,$number); | ||
| 243 | }; | ||
| 244 | } else { | ||
| 245 | print("substr next: $substr \n") if($self->{DEBUG}); | ||
| 246 | if ($sendcall) { | ||
| 247 | $json = $self->_json_rubric_content($self->{CALLSIGN}.':'.'...'.$substr,$rubric,$number); | ||
| 248 | } else { | ||
| 249 | $json = $self->_json_rubric_content('...'.$substr,$rubric,$number); | ||
| 250 | }; | ||
| 251 | }; | ||
| 252 | my($req) = $self->_build_request($json,'news'); | ||
| 253 | my($res) = $ua->request($req); | ||
| 254 | print('Request status line: '.$res->status_line."\n") if($self->{DEBUG}); | ||
| 255 | if (!$res->is_success) { | ||
| 256 | return($res->status_line); | ||
| 257 | }; | ||
| 258 | print('JSON Response: '.$res->decoded_content."\n") if($self->{DEBUG}); | ||
| 259 | $self->{_JSONRESPONSEREF} = $jsonresobj->decode($res->decoded_content); | ||
| 260 | |||
| 261 | $i++; | ||
| 262 | }; | ||
| 263 | |||
| 264 | }; | ||
| 265 | |||
| 266 | |||
| 267 | return(0); | ||
| 268 | }; | ||
| 269 | |||
| 270 | 1; |