| 1 | use strict; | ||
| 2 | |||
| 3 | package BrandMeister::API; | ||
| 4 | |||
| 5 | use LWP::UserAgent; | ||
| 6 | use HTTP::Request::Common; | ||
| 7 | use JSON; | ||
| 8 | use MIME::Base64; | ||
| 9 | #use LWP::ConsoleLogger::Everywhere (); | ||
| 10 | #use Data::Dumper; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | BM::API - Use the BM API from Perl | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | Implementation of the BM API in Perl. | ||
| 19 | |||
| 20 | =head1 AUTHOR | ||
| 21 | |||
| 22 | Simon (G7RZU) <simon@gb7fr.org.uk> | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | use vars qw($VERSION); | ||
| 27 | #Define version | ||
| 28 | $VERSION = '0.1'; | ||
| 29 | |||
| 30 | =head1 METHODS | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | sub new { | ||
| 35 | my($class) = shift; | ||
| 36 | my($self) = shift; | ||
| 37 | if (!exists($self->{BM_APIKEY}) || !exists($self->{DMRID})) { | ||
| 38 | return(0); | ||
| 39 | } | ||
| 40 | $self->{BM_APIBASEURL} = "https://api.brandmeister.network/v1.0/repeater/"; | ||
| 41 | $self->{BM_APIKEYBASE64} = encode_base64($self->{BM_APIKEY}.':'); | ||
| 42 | bless($self,$class); | ||
| 43 | return($self); | ||
| 44 | }; | ||
| 45 | |||
| 46 | sub _build_request { | ||
| 47 | my($self) = shift; | ||
| 48 | #my($json) = shift; | ||
| 49 | my($requrlpart) = shift; | ||
| 50 | my($formdataref) = shift; | ||
| 51 | my($uri) = $self->{BM_APIBASEURL}.$requrlpart; | ||
| 52 | print("Building HTTP request\n") if($self->{DEBUG}); | ||
| 53 | my($req); | ||
| 54 | if ($formdataref) { | ||
| 55 | $req = HTTP::Request::Common::POST( $uri,$formdataref); | ||
| 56 | } else { | ||
| 57 | $req = HTTP::Request::Common::POST( $uri); | ||
| 58 | }; | ||
| 59 | $req->header( 'Content-Type' => 'application/x-www-form-urlencoded', | ||
| 60 | 'Authorization'=>'Basic ' . $self->{BM_APIKEYBASE64} | ||
| 61 | ); | ||
| 62 | |||
| 63 | #$req->content( $json ); | ||
| 64 | return($req); | ||
| 65 | |||
| 66 | }; | ||
| 67 | |||
| 68 | sub _send_request { | ||
| 69 | my($self) = shift; | ||
| 70 | my($req) = shift; | ||
| 71 | return(1) if (!$req); | ||
| 72 | my($ua) = LWP::UserAgent->new; | ||
| 73 | my($jsonresobj) = JSON->new; | ||
| 74 | my($res) = $ua->request($req); | ||
| 75 | print('Request status line: '.$res->status_line."\n") if($self->{DEBUG}) ; | ||
| 76 | if (!$res->is_success) { | ||
| 77 | return($res->status_line); | ||
| 78 | }; | ||
| 79 | $self->{_JSONRESPONSEREF} = $jsonresobj->decode($res->decoded_content); | ||
| 80 | return(0); | ||
| 81 | }; | ||
| 82 | |||
| 83 | |||
| 84 | =head2 json_response | ||
| 85 | |||
| 86 | Returns a hash ref to the last JSON response or undef if there is none | ||
| 87 | |||
| 88 | $jsonhashref = $jsonobj->json_response; | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub json_response { | ||
| 93 | my($self) = shift; | ||
| 94 | print("Return JSON response\n") if($self->{DEBUG}); | ||
| 95 | return($self->{_JSONRESPONSEREF}); | ||
| 96 | }; | ||
| 97 | |||
| 98 | |||
| 99 | sub _do_action { | ||
| 100 | my($self) = shift; | ||
| 101 | my($requrlpart) = shift; | ||
| 102 | my($formdataref) = shift; | ||
| 103 | my($req) = $self->_build_request($requrlpart,$formdataref); | ||
| 104 | my($res) = $self->_send_request($req); | ||
| 105 | return($res); | ||
| 106 | |||
| 107 | }; | ||
| 108 | |||
| 109 | sub _action { | ||
| 110 | my($self) = shift; | ||
| 111 | my($action) = shift; | ||
| 112 | my($reqaction); | ||
| 113 | my($formdataref); | ||
| 114 | my($ts,$tg) = @_; | ||
| 115 | if ($action eq 'delstatic') { | ||
| 116 | $reqaction = 'talkgroup/?action=DEL&id='.$self->{DMRID}; | ||
| 117 | $formdataref = [ | ||
| 118 | talkgroup => $tg, | ||
| 119 | timeslot => $ts | ||
| 120 | ]; | ||
| 121 | } elsif ($action eq 'addstatic') { | ||
| 122 | $reqaction = 'talkgroup/?action=ADD&id='.$self->{DMRID}; | ||
| 123 | $formdataref = [ | ||
| 124 | talkgroup => $tg, | ||
| 125 | timeslot => $ts | ||
| 126 | ]; | ||
| 127 | } elsif ($action eq 'dropdynamic') { | ||
| 128 | $reqaction = 'setRepeaterTarantool.php?action=dropDynamicGroups&slot='.$ts.'&q='.$self->{DMRID}; | ||
| 129 | } else { | ||
| 130 | return(1); | ||
| 131 | }; | ||
| 132 | print("\n\n$reqaction\n\n"); | ||
| 133 | $self->{LASTACTIONRES} = $self->_do_action($reqaction,$formdataref); | ||
| 134 | return(0); | ||
| 135 | |||
| 136 | }; | ||
| 137 | |||
| 138 | sub result { | ||
| 139 | my($self) = shift; | ||
| 140 | return($self->{LASTACTIONRES}); | ||
| 141 | }; | ||
| 142 | |||
| 143 | =head2 add_static_tg | ||
| 144 | |||
| 145 | Add static TG to repeater config. | ||
| 146 | |||
| 147 | $res = $bmobj->add_static_tg(<timeslot>,<talkgroup>) | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | sub add_static_tg { | ||
| 152 | my($self) = shift; | ||
| 153 | my($ts,$tg) = @_; | ||
| 154 | #return(1) if (($ts < 1 || $ts > 2) || !$tg || $tg == 9 || $tg == 8 || $tg == 6); | ||
| 155 | return($self->_action('addstatic',$ts,$tg)); | ||
| 156 | }; | ||
| 157 | |||
| 158 | =head2 add_static_tg | ||
| 159 | |||
| 160 | Delete static TG from repeater config. | ||
| 161 | |||
| 162 | $res = $bmobj->del_static_tg(<timeslot>,<talkgroup>) | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | sub del_static_tg { | ||
| 167 | my($self) = shift; | ||
| 168 | my($ts,$tg) = @_; | ||
| 169 | #return(1) if (($ts < 1 || $ts > 2) || !$tg || $tg == 9 || $tg == 8 || $tg == 6); | ||
| 170 | return($self->_action('delstatic',$ts,$tg)); | ||
| 171 | }; | ||
| 172 | |||
| 173 | =head2 dropdynamic | ||
| 174 | |||
| 175 | Drop dynamic groups from timeslot | ||
| 176 | |||
| 177 | $bmobj->dropdynamic(<timeslot>); | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | sub dropdynamic { | ||
| 182 | my($self) = shift; | ||
| 183 | my($ts) = shift; | ||
| 184 | return($self->_action('dropdynamic',$ts)); | ||
| 185 | }; | ||
| 186 | |||
| 187 | 1; |