| 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.1'; | ||
| 45 | |||
| 46 | =head1 new | ||
| 47 | |||
| 48 | Instantiates new object. Pass a hash reference with options as follows | ||
| 49 | |||
| 50 | my($dapnetobj) = DAPNET::API->new({ | ||
| 51 | DAPNET_USERNAME => '<username>', | ||
| 52 | DAPNET_PW => '<password>', | ||
| 53 | CALLSIGN => '<your callsign>' | ||
| 54 | }); | ||
| 55 | |||
| 56 | returns an object if sucessful or false if not sucessful | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | |||
| 61 | sub new { | ||
| 62 | my($class) = shift; | ||
| 63 | my($self) = shift; | ||
| 64 | if (!exists($self->{DAPNET_USERNAME}) || !exists($self->{DAPNET_PW}) || !exists($self->{CALLSIGN})) { | ||
| 65 | return(0); | ||
| 66 | } | ||
| 67 | bless($self,$class); | ||
| 68 | $self->{_CALL_LEN} = length($self->{CALLSIGN}); | ||
| 69 | return($self); | ||
| 70 | }; | ||
| 71 | |||
| 72 | sub _build_request { | ||
| 73 | my($self) = shift; | ||
| 74 | my($json) = shift; | ||
| 75 | my($type) = shift; | ||
| 76 | my($username) = $self->{DAPNET_USERNAME}; | ||
| 77 | my($pw) = $self->{DAPNET_PW}; | ||
| 78 | my($uri) = 'http://www.hampager.de:8080/'.$type; | ||
| 79 | my($req) = HTTP::Request->new( | ||
| 80 | POST => $uri | ||
| 81 | ); | ||
| 82 | $req->header( 'Content-Type' => 'application/json', | ||
| 83 | 'Authorization'=>'Basic ' . encode_base64($username.':'.$pw) | ||
| 84 | ); | ||
| 85 | |||
| 86 | $req->content( $json ); | ||
| 87 | return($req); | ||
| 88 | |||
| 89 | }; | ||
| 90 | |||
| 91 | sub _json_individual_call { | ||
| 92 | my($self) = shift; | ||
| 93 | my($text,$to,$txgroup,$emergency) = @_; | ||
| 94 | my($jsonobj) = JSON->new; | ||
| 95 | |||
| 96 | my $json = $jsonobj->encode ({ | ||
| 97 | 'text' => $text, | ||
| 98 | 'callSignNames' => [$to], | ||
| 99 | 'transmitterGroupNames' => [$txgroup], | ||
| 100 | 'emergency' => $emergency | ||
| 101 | }); | ||
| 102 | return($json); | ||
| 103 | }; | ||
| 104 | |||
| 105 | sub _json_rubric_content { | ||
| 106 | my($self) = shift; | ||
| 107 | my($text,$rubric,$number) = @_; | ||
| 108 | my($jsonobj) = JSON->new; | ||
| 109 | |||
| 110 | my $json = $jsonobj->encode ({ | ||
| 111 | 'text' => $text, | ||
| 112 | 'rubricName'=> $rubric, | ||
| 113 | 'number' => $number | ||
| 114 | }); | ||
| 115 | return($json); | ||
| 116 | }; | ||
| 117 | |||
| 118 | =head send_individual_call | ||
| 119 | |||
| 120 | Send a call to a single callsign | ||
| 121 | |||
| 122 | $ret = $dapnetobj->send_individual_call(<text>,<destination callsign>,<transmitter group name>,<emergency boolean>)); | ||
| 123 | |||
| 124 | Returns 0 on sucess or the HTTP error string on error. | ||
| 125 | |||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub send_individual_call { | ||
| 130 | |||
| 131 | my $self = shift; | ||
| 132 | my($text,$to,$txgroup,$emergency) = @_; | ||
| 133 | |||
| 134 | my($ua) = LWP::UserAgent->new; | ||
| 135 | my($jsonobj); | ||
| 136 | my($i) = 1; | ||
| 137 | my($json); | ||
| 138 | |||
| 139 | if (length($text) <= (80 - $self->{_CALL_LEN})) { | ||
| 140 | my($json) = $self->_json_individual_call($self->{CALLSIGN}.':'.$text,$to,$txgroup,$emergency); | ||
| 141 | my($req) = $self->_build_request($json,'calls'); | ||
| 142 | my($res) = $ua->request($req); | ||
| 143 | warn($res->status_line); | ||
| 144 | if (!$res->is_success) { | ||
| 145 | return($res->status_line); | ||
| 146 | }; | ||
| 147 | } else { | ||
| 148 | while (my $substr = substr($text,0,(80 - $self->{_CALL_LEN}),'')) { | ||
| 149 | if ($i == 1) { | ||
| 150 | $json = $self->_json_individual_call($self->{CALLSIGN}.':'.$substr.'...',$to,$txgroup,$emergency); | ||
| 151 | } else { | ||
| 152 | $json = $self->_json_individual_call($self->{CALLSIGN}.':'.'...'.$substr,$to,$txgroup,$emergency); | ||
| 153 | }; | ||
| 154 | my($req) = $self->_build_request($json); | ||
| 155 | my($res) = $ua->request($req); | ||
| 156 | warn($res->status_line); | ||
| 157 | if (!$res->is_success) { | ||
| 158 | return($res->status_line); | ||
| 159 | }; | ||
| 160 | $i++; | ||
| 161 | }; | ||
| 162 | |||
| 163 | }; | ||
| 164 | |||
| 165 | |||
| 166 | return(0); | ||
| 167 | |||
| 168 | } | ||
| 169 | |||
| 170 | =head send_rubric_content | ||
| 171 | |||
| 172 | Sends a message to a rubric | ||
| 173 | |||
| 174 | $ret = $dapnetobj->send_rubric_content(<text to send>,<rubric name>.<transmitter group name>,<sequence number 1 - 10>); | ||
| 175 | |||
| 176 | Returns 0 on sucess or the HTTP error string on error. | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | |||
| 181 | sub send_rubric_content { | ||
| 182 | my $self = shift; | ||
| 183 | my($text,$rubric,$number) = @_; | ||
| 184 | |||
| 185 | my($ua) = LWP::UserAgent->new; | ||
| 186 | my($jsonobj); | ||
| 187 | my($i) = 1; | ||
| 188 | my($json); | ||
| 189 | |||
| 190 | if (length($text) <= (80 - $self->{_CALL_LEN})) { | ||
| 191 | my($json) = $self->_json_rubric_content($self->{CALLSIGN}.':'.$text,$rubric,$number); | ||
| 192 | my($req) = $self->_build_request($json,'news'); | ||
| 193 | my($res) = $ua->request($req); | ||
| 194 | warn($res->status_line); | ||
| 195 | if (!$res->is_success) { | ||
| 196 | return($res->status_line); | ||
| 197 | }; | ||
| 198 | } else { | ||
| 199 | while (my $substr = substr($text,0,(80 - $self->{_CALL_LEN}),'')) { | ||
| 200 | if ($i == 1) { | ||
| 201 | $json = $self->_json_rubrik_content($self->{CALLSIGN}.':'.$substr.'...',$rubric,$number); | ||
| 202 | } else { | ||
| 203 | $json = $self->_json_rubrik_content($self->{CALLSIGN}.':'.'...'.$substr,$rubric,$number); | ||
| 204 | }; | ||
| 205 | my($req) = $self->_build_request($json); | ||
| 206 | my($res) = $ua->request($req); | ||
| 207 | warn($res->status_line); | ||
| 208 | if (!$res->is_success) { | ||
| 209 | return($res->status_line); | ||
| 210 | }; | ||
| 211 | $i++; | ||
| 212 | }; | ||
| 213 | |||
| 214 | }; | ||
| 215 | |||
| 216 | |||
| 217 | return(0); | ||
| 218 | }; | ||
| 219 | |||
| 220 | 1; |