| 1 | #! perl | ||
| 2 | |||
| 3 | use v5.36; | ||
| 4 | use Object::Pad; | ||
| 5 | use utf8; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Weenect::API - API interaction | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use Weenect::API; | ||
| 14 | my $api = Weenect::API->new; | ||
| 15 | |||
| 16 | # Connect to the server | ||
| 17 | $api->login( "me@example.com", "password" ); | ||
| 18 | |||
| 19 | # Get the trackers. | ||
| 20 | my $trackers = $api->get_trackers; | ||
| 21 | |||
| 22 | # Process tracker data. | ||
| 23 | foreach my $tracker ( $trackers->items->@* ) { | ||
| 24 | printf("Tracker %s [%d%s]\n", $tracker->name, $tracker->id, | ||
| 25 | $tracker->active ? "" : ",inactive" ); | ||
| 26 | } | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | class Weenect::API; | ||
| 31 | |||
| 32 | use Weenect::Connect; | ||
| 33 | |||
| 34 | field $api :accessor; | ||
| 35 | field $acct; | ||
| 36 | field $debug :mutator; | ||
| 37 | |||
| 38 | =head1 METHODS | ||
| 39 | |||
| 40 | The Weenect::API class supports the following methods: | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | =head2 login( $user, $pass ) | ||
| 45 | |||
| 46 | Connects to the Weenect server by loggin in. | ||
| 47 | |||
| 48 | Returns a user object, suitable for further operations. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | method login( $user = "", $pass = "" ) { | ||
| 53 | $api = Weenect::Connect->new; | ||
| 54 | $api->debug = $debug; | ||
| 55 | my $acct = $self->get_acct( $user, $pass ); | ||
| 56 | my $res = $api->request( "user/login", | ||
| 57 | Content => $acct->json ); | ||
| 58 | return unless $res; | ||
| 59 | $api->auth = Weenect::Auth->create($res); | ||
| 60 | } | ||
| 61 | |||
| 62 | method get_acct( $user, $pass ) { | ||
| 63 | return $acct if $acct; | ||
| 64 | if ( $user && $pass ) { | ||
| 65 | return $acct = | ||
| 66 | Weenect::Login->new( username => $user, password => $pass ); | ||
| 67 | } | ||
| 68 | |||
| 69 | require ResInfo; | ||
| 70 | my $passwd = { password => 'ddjc8c*@!eqe6' }; | ||
| 71 | $acct = Weenect::Login->new | ||
| 72 | ( username => ResInfo::resinfo( "weenect.username", $passwd ), | ||
| 73 | password => ResInfo::resinfo( "weenect.password", $passwd ), | ||
| 74 | ); | ||
| 75 | } | ||
| 76 | |||
| 77 | =head2 get_trackers | ||
| 78 | |||
| 79 | Returns a list of trackers associated with this account, in the form | ||
| 80 | of Weenect::Tracker objects | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | method get_trackers { | ||
| 85 | require Weenect::Tracker; | ||
| 86 | |||
| 87 | my $res = $api->request("mytracker"); # same as "mytracker-userspace"? | ||
| 88 | return unless $res; | ||
| 89 | |||
| 90 | return Weenect::Trackers->create_with_api( $res, $api ); | ||
| 91 | } | ||
| 92 | |||
| 93 | =head2 get_preferences | ||
| 94 | |||
| 95 | Returns the current set of preferences in the form of a | ||
| 96 | Weenect::Preferences object. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | method get_preferences { | ||
| 101 | require Weenect::Preferences; | ||
| 102 | |||
| 103 | my $res = $api->request("myuser"); | ||
| 104 | return unless $res; | ||
| 105 | |||
| 106 | return Weenect::Preferences->create($res); | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 set_preferences( %prefs ) | ||
| 110 | |||
| 111 | Sets one or more preferences. | ||
| 112 | |||
| 113 | E.g. | ||
| 114 | |||
| 115 | language => "nl" | ||
| 116 | preferred_metric_system => "km" | ||
| 117 | option => 0 | ||
| 118 | mail_pref => { offers => 0, | ||
| 119 | company_news => 0, | ||
| 120 | new_features => 0, | ||
| 121 | surveys_and_tests => 0 } | ||
| 122 | |||
| 123 | Returns the current set of preferences in the form of a | ||
| 124 | Weenect::Preferences object. | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | method set_preferences( %prefs ) { | ||
| 129 | require Weenect::Preferences; | ||
| 130 | my $res = $api->request( "myuser", Content => \%prefs ); | ||
| 131 | return unless $res; | ||
| 132 | |||
| 133 | return Weenect::Preferences->create($res); | ||
| 134 | } | ||
| 135 | |||
| 136 | =head2 get_animals( $imei ) | ||
| 137 | |||
| 138 | Returns a list of animals associated with a given tracked, identified by its IMEI number (as a string). | ||
| 139 | |||
| 140 | The animals info is in the form of a Weenect::Animal object. | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | method get_animals( $imei ) { | ||
| 145 | require Weenect::Animal; | ||
| 146 | my $res = $api->request( sprintf("animal?imei=%s", $imei) ); | ||
| 147 | return unless $res; | ||
| 148 | |||
| 149 | my $animals = Weenect::Animals->create($res); | ||
| 150 | |||
| 151 | return $animals->items; | ||
| 152 | } | ||
| 153 | |||
| 154 | =head2 logout | ||
| 155 | |||
| 156 | Disconnect. | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | method logout { | ||
| 161 | return $api->request("logout"); | ||
| 162 | } | ||
| 163 | |||
| 164 | =head2 kindex | ||
| 165 | |||
| 166 | Get the planetary magnetic field disturbance. | ||
| 167 | |||
| 168 | Mostly returns nothing. | ||
| 169 | |||
| 170 | =cut | ||
| 171 | |||
| 172 | method kindex { | ||
| 173 | return $api->request("kindex"); | ||
| 174 | } | ||
| 175 | |||
| 176 | =head2 wifi-zone( $zid ) | ||
| 177 | |||
| 178 | Returns a WiFiZone object for the requested wifi zone. | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | method wifi_zone( $zid ) { | ||
| 183 | return $api->request( sprintf("wifi-zone/%d", $zid ) ); | ||
| 184 | } | ||
| 185 | |||
| 186 | =head2 wifi_zone_active( $zid, $active ) | ||
| 187 | |||
| 188 | Enables/disables a WiFi zone. | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | method wifi_zone_active( $zid, $active = 1 ) { | ||
| 193 | |||
| 194 | my $z = $api->wifi_zone($zid); | ||
| 195 | require Weenect::WiFiZone; | ||
| 196 | my $zone = Weenect::WiFiZone->create($z)->hash; | ||
| 197 | $zone->{is_active} = $active; | ||
| 198 | return $api->request( sprintf("wifi-zone/%d", $zid ), | ||
| 199 | Content => $zone, | ||
| 200 | OP => 'PUT' ); | ||
| 201 | } | ||
| 202 | |||
| 203 | =head2 remove_wifi_zone( $zid ) | ||
| 204 | |||
| 205 | Returns a WiFiZone object for the requested wifi zone. | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | method remove_wifi_zone( $zid ) { | ||
| 210 | $zid = $zid->id if $zid isa Weenect::WiFiZone; | ||
| 211 | return $api->request( sprintf("wifi-zone/%d", $zid), OP => 'DELETE' ); | ||
| 212 | } | ||
| 213 | |||
| 214 | =head2 add_wifi_zone( %atts ) | ||
| 215 | |||
| 216 | Adds a new WiFi zone and returns the WiFiZone object for the hew zone. | ||
| 217 | |||
| 218 | %atts may contain _zone => WiFiZone object. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | # name : "Oh Denneboom" | ||
| 223 | # mac_address : "cc:ce:1e:1c:7c:46" | ||
| 224 | # latitude : 52.8849946 | ||
| 225 | # longitude : 6.85922149999999 | ||
| 226 | # radius : 15 | ||
| 227 | # is_active : true | ||
| 228 | # enable_notifications : true | ||
| 229 | |||
| 230 | |||
| 231 | method add_wifi_zone( %atts ) { | ||
| 232 | %atts = ( delete($atts{_zone})->hash->%*, %atts ) if $atts{_zone}; | ||
| 233 | return $api->request( "wifi-zone", Content => \%atts ); | ||
| 234 | } | ||
| 235 | |||
| 236 | ################ Classes ################ | ||
| 237 | |||
| 238 | class Weenect::Login :does(Class::JSON_Object) { | ||
| 239 | field $username :param; | ||
| 240 | field $password :param; | ||
| 241 | } | ||
| 242 | |||
| 243 | class Weenect::Auth :does(Class::JSON_Object) { | ||
| 244 | field $access_token; | ||
| 245 | field $expires_in; | ||
| 246 | field $refresh_token; | ||
| 247 | field $token_type; | ||
| 248 | } | ||
| 249 | |||
| 250 | 1; | ||
| 251 | |||
| 252 | # android | ||
| 253 | # android/token device_uuid site | ||
| 254 | # auth/external-id | ||
| 255 | # mailtoken | ||
| 256 | # myaccountoptions | ||
| 257 | # mysubscription/$subscriptionid | ||
| 258 | # mytracker/$tid/activity | ||
| 259 | # mytracker/register | ||
| 260 | # mytracker/$tid/buttons | ||
| 261 | # mytracker/$tid/imei | ||
| 262 | # mytracker/$tid/wifi-zones/suggest | ||
| 263 | # oauth/token | ||
| 264 | # subscriptionoffer | ||
| 265 | # user/$uid | ||
| 266 | # user/forgotpassword mail | ||
| 267 | # user/register | ||
| 268 |