| 1 | #! perl | ||
| 2 | |||
| 3 | use v5.36; | ||
| 4 | use Object::Pad; | ||
| 5 | use utf8; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Weenect::Tracker - Tracker data | ||
| 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 | use Class::JSON_Object; | ||
| 31 | |||
| 32 | # Collection class for Weenect::Tracker objects. | ||
| 33 | |||
| 34 | class Weenect::Trackers :does(Class::JSON_Object) { | ||
| 35 | |||
| 36 | field $total; | ||
| 37 | field @items :Class(Weenect::Tracker); | ||
| 38 | |||
| 39 | # Constructor that connects the items to the api. | ||
| 40 | sub create_with_api ( $class, $data, $api ) { | ||
| 41 | my $s = $class->create_sparse($data); | ||
| 42 | for ( @{$s->items} ) { | ||
| 43 | $_->api = $api; | ||
| 44 | } | ||
| 45 | return $s; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | class Weenect::Tracker :does(Class::JSON_Object); | ||
| 50 | |||
| 51 | use Weenect::Position; | ||
| 52 | |||
| 53 | #### Instanciate via Weenect::Trackers only. | ||
| 54 | |||
| 55 | field $api :Optional :mutator; | ||
| 56 | field $id; | ||
| 57 | field $active; | ||
| 58 | field $name; | ||
| 59 | field @position :Class(Weenect::Position); | ||
| 60 | field $geofence_number :Optional; | ||
| 61 | field @features; | ||
| 62 | field $sos_phone; | ||
| 63 | |||
| 64 | =head1 METHODS | ||
| 65 | |||
| 66 | The Weenect::Tracker class supports the following methods: | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | =head2 get_zones | ||
| 71 | |||
| 72 | Returns a Weenect::Zones object, that has a list of zones (geofence | ||
| 73 | areas) in the form of Weenect::Zone objects. | ||
| 74 | |||
| 75 | Use the items method to get at the list. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | method get_zones { | ||
| 80 | require Weenect::Zone; | ||
| 81 | my $res = $api->request( sprintf( "mytracker/%d/zones", $id ) ); | ||
| 82 | return unless $res; | ||
| 83 | return Weenect::Zones->create($res); | ||
| 84 | } | ||
| 85 | |||
| 86 | =head2 get_zone( $zid ) | ||
| 87 | |||
| 88 | Returns a Weenect::Zone object representing a zone (geofence area). | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | method get_zone( $zid ) { | ||
| 93 | require Weenect::Zone; | ||
| 94 | my $res = $api->request( sprintf( "mytracker/%d/zones/%s", $id, $zid ) ); | ||
| 95 | return unless $res; | ||
| 96 | return Weenect::Zone->create($res); | ||
| 97 | } | ||
| 98 | |||
| 99 | =head2 remove_zone( $zid ) | ||
| 100 | |||
| 101 | Removes a zone (geofence areas). | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | method remove_zone( $zid ) { | ||
| 106 | my $res = $api->request( sprintf( "mytracker/%d/zones/%s", $id, $zid ), | ||
| 107 | OP => 'DELETE' ); | ||
| 108 | return $res; | ||
| 109 | } | ||
| 110 | |||
| 111 | # name => "NewHaven", | ||
| 112 | # latitude => 52.85, | ||
| 113 | # longitude => 6.87, | ||
| 114 | # distance => 25, | ||
| 115 | # address => '', | ||
| 116 | # is_outside => 1, # if yes this increases enter/exit detection precision. | ||
| 117 | # mode => 3, | ||
| 118 | |||
| 119 | method add_zone( %args ) { | ||
| 120 | my $res = $api->request( sprintf( "mytracker/%d/zones", $id ), | ||
| 121 | Content => \%args ); | ||
| 122 | return $res; | ||
| 123 | } | ||
| 124 | |||
| 125 | =head2 get_wifizones | ||
| 126 | |||
| 127 | Returns a Weenect::WiFiZones object that has a list of WiFi zones | ||
| 128 | (powersave areas) in the form of Weenect::WiFiZone objects. | ||
| 129 | |||
| 130 | Use its item method to get at the list. | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | method get_wifizones { | ||
| 135 | require Weenect::WiFiZone; | ||
| 136 | my $res = $api->request( sprintf( "mytracker/%d/wifi-zones", $id ) ); | ||
| 137 | return unless $res; | ||
| 138 | return Weenect::WiFiZones->create($res); | ||
| 139 | } | ||
| 140 | |||
| 141 | =head2 get_wifizone( $zid ) | ||
| 142 | |||
| 143 | Returns a Weenect::WiFiZone object representing a WiFi zone (powersave area). | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | method get_wifizone( $zid ) { | ||
| 148 | require Weenect::WiFiZone; | ||
| 149 | my $res = $api->request( sprintf( "mytracker/%d/wifi-zones/%s", $id, $zid ) ); | ||
| 150 | return unless $res; | ||
| 151 | return Weenect::WiFiZone->create($res); | ||
| 152 | } | ||
| 153 | |||
| 154 | #### "mytracker/%d/wifi-zones" only allows HEAD GET OPTIONS. Use "wifi-zone" in API. | ||
| 155 | # | ||
| 156 | =head2 get_history( $start, $end ) | ||
| 157 | |||
| 158 | Returns a list of positions as tracked between $start and $end. | ||
| 159 | |||
| 160 | $start and $end are UTC timestamps in ISO8601 format, e.g. | ||
| 161 | C<2021-07-17T13:57:43.773Z> | ||
| 162 | |||
| 163 | Positions are in the form of Weenect::Position objects. | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | method get_history( $start, $end ) { | ||
| 168 | # https://apiv4.weenect.com/v4/mytracker/135076/activity/v2?metric_system=miles&start=2021-07-17T13:57:43.773Z&end=2021-07-18T13:57:43.773Z | ||
| 169 | my $res = $api->request( sprintf( "mytracker/%d/activity/v2?start=%s&end=%s", | ||
| 170 | $id, $start, $end ) ); | ||
| 171 | return unless $res; | ||
| 172 | return $res; | ||
| 173 | } | ||
| 174 | |||
| 175 | =head2 has_feature( $feat ) | ||
| 176 | |||
| 177 | Checks if the tracker has a certain feature. | ||
| 178 | |||
| 179 | Common features include: | ||
| 180 | |||
| 181 | activity_tracking | ||
| 182 | has_flash | ||
| 183 | has_wifi | ||
| 184 | limited_buttons | ||
| 185 | ringing | ||
| 186 | super_tracking | ||
| 187 | vibrate | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | # My tracker has: | ||
| 192 | # activity_tracking | ||
| 193 | # has_flash | ||
| 194 | # has_flash_stop | ||
| 195 | # has_wifi | ||
| 196 | # limited_buttons | ||
| 197 | # mode_gsensor | ||
| 198 | # mode_selection | ||
| 199 | # ringing | ||
| 200 | # ringing_stop | ||
| 201 | # super_tracking | ||
| 202 | # super_tracking_interval_1 | ||
| 203 | # super_tracking_stop | ||
| 204 | # super_tracking_ttl_cmd_hms | ||
| 205 | # vibrate | ||
| 206 | # vibrate_sequence | ||
| 207 | # vibrate_stop | ||
| 208 | |||
| 209 | method has_feature( $feat ) { | ||
| 210 | require List::Util; | ||
| 211 | return List::Util::any { $_ eq $feat } @features; | ||
| 212 | } | ||
| 213 | |||
| 214 | =head2 flash | ||
| 215 | |||
| 216 | Initiates the flash light of the tracker. | ||
| 217 | |||
| 218 | Default is 100ms on, 100ms off, for 5 minutes. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | method flash( $on = 1 ) { | ||
| 223 | my %content = | ||
| 224 | ( duration_minutes => 1, | ||
| 225 | intermittent_duration_ms_on => $on ? 100 : 0, | ||
| 226 | intermittent_duration_ms_off => $on ? 100 : 0, | ||
| 227 | ); | ||
| 228 | return $api->request( sprintf( "mytracker/%d/flash", $id ), | ||
| 229 | Content => \%content ); | ||
| 230 | } | ||
| 231 | |||
| 232 | =head2 flash_stop | ||
| 233 | |||
| 234 | { | ||
| 235 | data = { | ||
| 236 | intermittent_duration_off = 1, | ||
| 237 | intermittent_duration_on = 1 | ||
| 238 | }, | ||
| 239 | duration = 60, | ||
| 240 | end_at = undef, | ||
| 241 | sent_start_at = '2026-06-01T09:47:26.822271' (dualvar: 2026), | ||
| 242 | sent_stop_at = undef, | ||
| 243 | started_at = undef | ||
| 244 | } | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | method flash_stop { | ||
| 249 | return $api->request( sprintf( "mytracker/%d/flash/stop", $id ) ); | ||
| 250 | } | ||
| 251 | |||
| 252 | =head2 ring | ||
| 253 | |||
| 254 | Initiates the ringer of the tracker. | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | method ring { | ||
| 259 | my $res = $api->request( sprintf( "mytracker/%d/ring", $id ), | ||
| 260 | OP => 'POST' ); | ||
| 261 | # return unless $res; | ||
| 262 | return $res; | ||
| 263 | } | ||
| 264 | |||
| 265 | =head2 ring_stop | ||
| 266 | |||
| 267 | Stops the ringer. | ||
| 268 | |||
| 269 | =cut | ||
| 270 | |||
| 271 | method ring_stop { | ||
| 272 | return $api->request( sprintf( "mytracker/%d/ring/stop", $id ) ); | ||
| 273 | } | ||
| 274 | |||
| 275 | =head2 vibrate | ||
| 276 | |||
| 277 | Initiates the buzzer of the tracker. | ||
| 278 | |||
| 279 | =cut | ||
| 280 | |||
| 281 | method vibrate { | ||
| 282 | my $res = $api->request( sprintf( "mytracker/%d/vibrate", $id ), | ||
| 283 | OP => 'POST' ); | ||
| 284 | # return unless $res; | ||
| 285 | return $res; | ||
| 286 | } | ||
| 287 | |||
| 288 | =head2 vibrate_stop | ||
| 289 | |||
| 290 | Stops the buzzer. | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | method vibrate_stop { | ||
| 295 | return $api->request( sprintf( "mytracker/%d/vibrate/stop", $id ) ); | ||
| 296 | } | ||
| 297 | |||
| 298 | =head2 position_refresh | ||
| 299 | |||
| 300 | Force position update of the tracker. | ||
| 301 | |||
| 302 | =cut | ||
| 303 | |||
| 304 | method position_refresh { | ||
| 305 | my $res = $api->request( sprintf( "mytracker/%d/position/refresh", $id ), | ||
| 306 | OP => 'POST' ); | ||
| 307 | # return unless $res; | ||
| 308 | return $res; | ||
| 309 | } | ||
| 310 | |||
| 311 | =head2 super_live | ||
| 312 | |||
| 313 | Initiates super live tracking (1 second interval reporting) for 5 minutes. | ||
| 314 | |||
| 315 | Additional subscription fees are required for longer periods of super | ||
| 316 | live tracking. | ||
| 317 | |||
| 318 | =cut | ||
| 319 | |||
| 320 | method super_live { | ||
| 321 | my $res = $api->request( sprintf( "mytracker/%d/st-mode", $id ), | ||
| 322 | OP => 'POST' ); | ||
| 323 | # return unless $res; | ||
| 324 | return $res; # {"interval":10} | ||
| 325 | } | ||
| 326 | |||
| 327 | =head2 get_super_live | ||
| 328 | |||
| 329 | Gets the current super_live status. | ||
| 330 | |||
| 331 | { | ||
| 332 | active = 0, | ||
| 333 | duration = undef, | ||
| 334 | freq_mode = undef, | ||
| 335 | interval = undef, | ||
| 336 | ttl = -2 | ||
| 337 | } | ||
| 338 | |||
| 339 | =cut | ||
| 340 | |||
| 341 | method get_super_live { | ||
| 342 | my $res = $api->request( sprintf( "mytracker/%d/superlive", $id ) ); | ||
| 343 | # return unless $res; | ||
| 344 | return $res; # {} | ||
| 345 | } | ||
| 346 | |||
| 347 | =head2 stop_super_live { | ||
| 348 | |||
| 349 | Stops super_live tracking. | ||
| 350 | |||
| 351 | { | ||
| 352 | data = undef, | ||
| 353 | duration = undef, | ||
| 354 | end_at = undef, | ||
| 355 | sent_start_at = undef, | ||
| 356 | sent_stop_at = undef, | ||
| 357 | started_at = undef | ||
| 358 | } | ||
| 359 | |||
| 360 | =cut | ||
| 361 | |||
| 362 | method stop_super_live { | ||
| 363 | my $res = $api->request( sprintf( "mytracker/%d/superlive/stop", $id ) ); | ||
| 364 | # return unless $res; | ||
| 365 | return $res; # {} | ||
| 366 | } | ||
| 367 | |||
| 368 | # "mytracker/%d/sos" | ||
| 369 | # "mytracker/%d/full-sos" | ||
| 370 | # "mytracker/%d/full-sos/ack" | ||
| 371 | |||
| 372 | method sos_call { | ||
| 373 | my $res = $api->request( sprintf( "mytracker/%d/sos", $id ), | ||
| 374 | Content => { phone_number => $sos_phone } ); | ||
| 375 | return $res; # {} | ||
| 376 | } | ||
| 377 | |||
| 378 | =head2 set_mode | ||
| 379 | |||
| 380 | Sets the mode (update interval). | ||
| 381 | |||
| 382 | Valid values are '30S', '1M', '2M', '3M', '5M' and 'OFF'. | ||
| 383 | |||
| 384 | Additional subscription fees are required for '10s' interval. | ||
| 385 | |||
| 386 | =cut | ||
| 387 | |||
| 388 | method set_mode( $mode ) { | ||
| 389 | my $res = $api->request( sprintf( "mytracker/%d/mode", $id ), | ||
| 390 | Content => { mode => uc $mode } ); | ||
| 391 | return $res; # {} | ||
| 392 | } | ||
| 393 | |||
| 394 | =head2 detect_hotspots | ||
| 395 | |||
| 396 | Initiate WiFi hotspot detecting. | ||
| 397 | |||
| 398 | =cut | ||
| 399 | |||
| 400 | method detect_hotspots { | ||
| 401 | my $res = $api->request( sprintf( "mytracker/%d/detect-hotspots", $id ), OP => 'POST' ); | ||
| 402 | return $res; # {} | ||
| 403 | } | ||
| 404 | |||
| 405 | =head2 list_hotspots | ||
| 406 | |||
| 407 | List detected hotspots. | ||
| 408 | |||
| 409 | { | ||
| 410 | completed = 0 (JSON::PP::Boolean), | ||
| 411 | items = [], | ||
| 412 | total = 0 | ||
| 413 | } | ||
| 414 | |||
| 415 | =cut | ||
| 416 | |||
| 417 | method list_hotspots { | ||
| 418 | my $res = $api->request( sprintf( "mytracker/%d/list-hotspots", $id ) ); | ||
| 419 | return $res; # {} | ||
| 420 | } | ||
| 421 | |||
| 422 | =head2 deep-sleep-wifi | ||
| 423 | |||
| 424 | =cut | ||
| 425 | |||
| 426 | method deep_sleep_wifi { | ||
| 427 | my $res = $api->request( sprintf( "mytracker/%d/deep-sleep-wifi", $id ) ); | ||
| 428 | return $res; # {"enable_deep_sleep_wifi":false} | ||
| 429 | } | ||
| 430 | |||
| 431 | =head2 wifizones_suggest | ||
| 432 | |||
| 433 | { | ||
| 434 | size = 0, | ||
| 435 | zone_status = 'new', | ||
| 436 | zone_wifi = [] | ||
| 437 | } | ||
| 438 | |||
| 439 | =cut | ||
| 440 | |||
| 441 | method wifizones_suggest { | ||
| 442 | my $res = $api->request( sprintf( "mytracker/%d/wifi-zones/suggest", $id ) ); | ||
| 443 | return $res; # {} | ||
| 444 | } | ||
| 445 | |||
| 446 | =head2 remove_picture | ||
| 447 | |||
| 448 | Removes the tracker picture, if any. | ||
| 449 | |||
| 450 | =cut | ||
| 451 | |||
| 452 | method remove_picture { | ||
| 453 | my $res = $api->request( sprintf( "mytracker/%d/picture", $id ), OP => 'DELETE' ); | ||
| 454 | return $res; # {} | ||
| 455 | } | ||
| 456 | |||
| 457 | 1; | ||
| 458 | |||
| 459 | # "mytracker/{trackerId}/activity/v2" start end metric_system |