2018-09-22 19:22:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Jeffrey Walter <jeffreydwalter@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
|
|
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
|
|
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-09-19 07:07:32 +00:00
|
|
|
package arlo
|
2017-12-10 19:17:09 +00:00
|
|
|
|
|
|
|
import (
|
2018-12-11 23:48:51 +00:00
|
|
|
"fmt"
|
2018-09-22 19:22:42 +00:00
|
|
|
"net/http"
|
2018-09-22 14:15:22 +00:00
|
|
|
"sync"
|
2018-12-11 23:48:51 +00:00
|
|
|
"time"
|
2018-09-22 14:15:22 +00:00
|
|
|
|
2018-12-11 23:48:51 +00:00
|
|
|
"github.com/jeffreydwalter/arlo-go/internal/request"
|
2018-09-19 19:12:06 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2017-12-10 19:17:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Arlo struct {
|
|
|
|
user string
|
|
|
|
pass string
|
|
|
|
client *request.Client
|
2018-09-17 04:44:41 +00:00
|
|
|
Account Account
|
|
|
|
Basestations Basestations
|
|
|
|
Cameras Cameras
|
2018-09-22 14:15:22 +00:00
|
|
|
rwmutex sync.RWMutex
|
2017-12-10 19:17:09 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 22:38:01 +00:00
|
|
|
func newArlo(user string, pass string) (arlo *Arlo) {
|
|
|
|
|
2018-09-19 19:12:06 +00:00
|
|
|
// Add important headers.
|
2018-09-22 19:22:42 +00:00
|
|
|
baseHeaders := make(http.Header)
|
|
|
|
baseHeaders.Add("DNT", "1")
|
|
|
|
baseHeaders.Add("schemaVersion", "1")
|
2019-10-10 21:10:15 +00:00
|
|
|
baseHeaders.Add("Host", "my.arlo.com")
|
|
|
|
baseHeaders.Add("Referer", "https://my.arlo.com/")
|
2018-09-22 19:22:42 +00:00
|
|
|
|
|
|
|
c, _ := request.NewClient(BaseUrl, baseHeaders)
|
2018-09-19 19:12:06 +00:00
|
|
|
|
|
|
|
return &Arlo{
|
2017-12-10 19:17:09 +00:00
|
|
|
user: user,
|
|
|
|
pass: pass,
|
|
|
|
client: c,
|
|
|
|
}
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 22:38:01 +00:00
|
|
|
func Login(user string, pass string) (arlo *Arlo, err error) {
|
|
|
|
arlo = newArlo(user, pass)
|
2018-09-19 19:12:06 +00:00
|
|
|
|
2018-09-20 22:38:01 +00:00
|
|
|
body := map[string]string{"email": arlo.user, "password": arlo.pass}
|
2018-12-11 23:48:51 +00:00
|
|
|
resp, err := arlo.post(LoginV2Uri, "", body, nil)
|
2018-09-22 19:22:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed to login")
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
2018-09-20 22:38:01 +00:00
|
|
|
defer resp.Body.Close()
|
2018-09-19 19:12:06 +00:00
|
|
|
|
|
|
|
var loginResponse LoginResponse
|
|
|
|
if err := resp.Decode(&loginResponse); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if loginResponse.Success {
|
|
|
|
// Cache the auth token.
|
2018-09-22 19:22:42 +00:00
|
|
|
arlo.client.AddHeader("Authorization", loginResponse.Data.Token)
|
2018-09-19 19:12:06 +00:00
|
|
|
|
|
|
|
// Save the account info with the arlo struct.
|
2018-09-20 22:38:01 +00:00
|
|
|
arlo.Account = loginResponse.Data
|
2018-09-19 19:12:06 +00:00
|
|
|
|
|
|
|
// Get the devices, which also caches them on the arlo object.
|
2018-09-20 22:38:01 +00:00
|
|
|
if _, err := arlo.GetDevices(); err != nil {
|
2018-09-19 19:12:06 +00:00
|
|
|
return nil, errors.WithMessage(err, "failed to login")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("failed to login")
|
|
|
|
}
|
|
|
|
|
2018-09-20 22:38:01 +00:00
|
|
|
return arlo, nil
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Arlo) Logout() error {
|
|
|
|
resp, err := a.put(LogoutUri, "", nil, nil)
|
2018-09-20 22:38:01 +00:00
|
|
|
return checkRequest(resp, err, "failed to logout")
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 23:48:51 +00:00
|
|
|
func (a *Arlo) CheckSession() (session *Session, err error) {
|
|
|
|
msg := "failed to get session"
|
|
|
|
resp, err := a.get(SessionUri, "", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, msg)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var response SessionResponse
|
|
|
|
if err := resp.Decode(&response); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Success == false {
|
|
|
|
return nil, errors.WithMessage(errors.New(response.Reason), msg)
|
|
|
|
}
|
|
|
|
return &response.Data, nil
|
|
|
|
}
|
|
|
|
|
2018-09-19 19:12:06 +00:00
|
|
|
// GetDevices returns an array of all devices.
|
|
|
|
// When you call Login, this method is called and all devices are cached in the arlo object.
|
2018-09-22 19:22:42 +00:00
|
|
|
func (a *Arlo) GetDevices() (devices *Devices, err error) {
|
2018-12-11 23:48:51 +00:00
|
|
|
resp, err := a.get(fmt.Sprintf(DevicesUri, time.Now().Format("20060102")), "", nil)
|
2018-09-22 19:22:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed to get devices")
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-12-11 23:48:51 +00:00
|
|
|
var response DeviceResponse
|
|
|
|
if err := resp.Decode(&response); err != nil {
|
2018-09-19 19:12:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-11 23:48:51 +00:00
|
|
|
if !response.Success {
|
2018-09-19 19:12:06 +00:00
|
|
|
return nil, errors.New("failed to get devices")
|
|
|
|
}
|
|
|
|
|
2018-12-11 23:48:51 +00:00
|
|
|
if len(response.Data) == 0 {
|
2018-09-20 22:38:01 +00:00
|
|
|
return nil, errors.New("no devices found")
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:22:42 +00:00
|
|
|
// Cache a pointer to the arlo object with each device.
|
2018-12-11 23:48:51 +00:00
|
|
|
for i := range response.Data {
|
|
|
|
response.Data[i].arlo = a
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 19:22:42 +00:00
|
|
|
// Disconnect all of the basestations from the EventStream.
|
2018-09-19 19:12:06 +00:00
|
|
|
for i := range a.Basestations {
|
2018-09-22 14:15:22 +00:00
|
|
|
if err := a.Basestations[i].Disconnect(); err != nil {
|
2018-09-19 19:12:06 +00:00
|
|
|
return nil, errors.WithMessage(err, "failed to get devices")
|
|
|
|
}
|
2018-09-19 21:35:05 +00:00
|
|
|
}
|
2018-09-19 19:12:06 +00:00
|
|
|
|
2018-09-22 14:15:22 +00:00
|
|
|
a.rwmutex.Lock()
|
2018-09-19 21:35:05 +00:00
|
|
|
// Cache the devices as their respective types.
|
2018-12-11 23:48:51 +00:00
|
|
|
a.Cameras = *response.Data.GetCameras()
|
|
|
|
a.Basestations = *response.Data.GetBasestations()
|
2018-09-22 14:15:22 +00:00
|
|
|
a.rwmutex.Unlock()
|
2018-09-19 21:35:05 +00:00
|
|
|
|
2018-09-22 14:15:22 +00:00
|
|
|
// subscribe each basestation to the EventStream.
|
2018-09-19 21:35:05 +00:00
|
|
|
for i := range a.Basestations {
|
2018-09-19 19:12:06 +00:00
|
|
|
if err := a.Basestations[i].Subscribe(); err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed to get devices")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 23:48:51 +00:00
|
|
|
return &response.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProfile returns the user profile for the currently logged in user.
|
|
|
|
func (a *Arlo) GetProfile() (profile *UserProfile, err error) {
|
|
|
|
resp, err := a.get(ProfileUri, "", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed to get user profile")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var response UserProfileResponse
|
|
|
|
if err := resp.Decode(&response); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !response.Success {
|
|
|
|
return nil, errors.New("failed to get user profile")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &response.Data, nil
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDisplayOrder sets the display order according to the order defined in the DeviceOrder given.
|
|
|
|
func (a *Arlo) UpdateDisplayOrder(d DeviceOrder) error {
|
2018-12-11 23:48:51 +00:00
|
|
|
resp, err := a.post(CameraOrderUri, "", d, nil)
|
2018-09-20 22:38:01 +00:00
|
|
|
return checkRequest(resp, err, "failed to display order")
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateProfile takes a first and last name, and updates the user profile with that information.
|
|
|
|
func (a *Arlo) UpdateProfile(firstName, lastName string) error {
|
|
|
|
body := map[string]string{"firstName": firstName, "lastName": lastName}
|
2018-12-11 23:48:51 +00:00
|
|
|
resp, err := a.put(ProfileUri, "", body, nil)
|
2018-09-20 22:38:01 +00:00
|
|
|
return checkRequest(resp, err, "failed to update profile")
|
2018-09-19 19:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Arlo) UpdatePassword(pass string) error {
|
|
|
|
body := map[string]string{"currentPassword": a.pass, "newPassword": pass}
|
2018-12-11 23:48:51 +00:00
|
|
|
resp, err := a.post(UpdatePasswordUri, "", body, nil)
|
2018-09-20 22:38:01 +00:00
|
|
|
if err := checkRequest(resp, err, "failed to update password"); err != nil {
|
2018-09-19 19:12:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.pass = pass
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-10 19:17:09 +00:00
|
|
|
|
2018-09-19 19:12:06 +00:00
|
|
|
func (a *Arlo) UpdateFriends(f Friend) error {
|
2018-12-11 23:48:51 +00:00
|
|
|
resp, err := a.put(FriendsUri, "", f, nil)
|
2018-09-20 22:38:01 +00:00
|
|
|
return checkRequest(resp, err, "failed to update friends")
|
2017-12-10 19:17:09 +00:00
|
|
|
}
|