-
Notifications
You must be signed in to change notification settings - Fork 8
/
devices.go
159 lines (143 loc) · 6.67 KB
/
devices.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (c) 2018 Jeffrey Walter <[email protected]>
*
* 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.
*/
package arlo
// A Device is the device data, this can be a camera, basestation, arloq, etc.
type Device struct {
arlo *Arlo // Let's hold a reference to the parent arlo object since it holds the http.Client object and references to all devices.
AnalyticsEnabled bool `json:"analyticsEnabled"`
ArloMobilePlan bool `json:"arloMobilePlan"`
ArloMobilePlanId string `json:"arloMobilePlanId"`
ArloMobilePlanName string `json:"arloMobilePlanName"`
ArloMobilePlanThreshold int `json:"arloMobilePlanThreshold"`
Connectivity Connectivity `json:"connectivity"`
CriticalBatteryState bool `json:"criticalBatteryState"`
DateCreated int64 `json:"dateCreated"`
DeviceId string `json:"deviceId"`
DeviceName string `json:"deviceName"`
DeviceType string `json:"deviceType"`
DisplayOrder uint8 `json:"displayOrder"`
FirmwareVersion string `json:"firmwareVersion"`
InterfaceVersion string `json:"interfaceVersion"`
InterfaceSchemaVer string `json:"interfaceSchemaVer"`
LastImageUploaded string `json:"lastImageUploaded"`
LastModified int64 `json:"lastModified"`
MigrateActivityZone bool `json:"migrateActivityZone"`
MobileCarrier string `json:"mobileCarrier"`
MobileTrialUsed bool `json:"mobileTrialUsed"`
PermissionsFilePath string `json:"permissionsFilePath"`
PermissionsSchemaVer string `json:"permissionsSchemaVer"`
PermissionsVerison string `json:"permissionsVerison"` // WTF? Netgear developers think this is OK... *sigh*
PermissionsVersion string `json:"permissionsVersion"`
PresignedFullFrameSnapshotUrl string `json:"presignedFullFrameSnapshotUrl"`
PresignedLastImageUrl string `json:"presignedLastImageUrl"`
PresignedSnapshotUrl string `json:"presignedSnapshotUrl"`
MediaObjectCount uint8 `json:"mediaObjectCount"`
ModelId string `json:"modelId"`
Owner Owner `json:"owner"`
ParentId string `json:"parentId"`
Properties Properties `json:"properties"`
UniqueId string `json:"uniqueId"`
UserId string `json:"userId"`
UserRole string `json:"userRole"`
State string `json:"state"`
XCloudId string `json:"xCloudId"`
}
// Devices is a slice of Device objects.
type Devices []Device
// A DeviceOrder holds a map of device ids and a numeric index. The numeric index is the device order.
// Device order is mainly used by the UI to determine which order to show the devices.
/*
{
"devices":{
"XXXXXXXXXXXXX":1,
"XXXXXXXXXXXXX":2,
"XXXXXXXXXXXXX":3
}
*/
type DeviceOrder struct {
Devices map[string]int `json:"devices"`
}
// Find returns a device with the device id passed in.
func (ds *Devices) Find(deviceId string) *Device {
for _, d := range *ds {
if d.DeviceId == deviceId {
return &d
}
}
return nil
}
func (ds Devices) FindCameras(basestationId string) Cameras {
cs := new(Cameras)
for _, d := range ds {
if d.ParentId == basestationId {
*cs = append(*cs, Camera(d))
}
}
return *cs
}
func (d Device) IsBasestation() bool {
return d.DeviceType == DeviceTypeBasestation || d.DeviceId == d.ParentId
}
func (d Device) IsCamera() bool {
switch(d.DeviceType) {
case
DeviceTypeCamera,
DeviceTypeArloQ:
return true
}
return false
}
func (d Device) IsArloQ() bool {
return d.DeviceType == DeviceTypeArloBridge
}
func (d Device) IsLight() bool {
return d.DeviceType == DeviceTypeLights
}
func (d Device) IsSiren() bool {
return d.DeviceType == DeviceTypeSiren
}
// GetBasestations returns a Basestations object containing all devices that are NOT type "camera".
// I did this because some device types, like arloq, don't have a basestation.
// So, when interacting with them you must treat them like a basestation and a camera.
// Cameras also includes devices of this type, so you can get the same data there or cast.
func (ds Devices) GetBasestations() *Basestations {
basestations := new(Basestations)
for _, d := range ds {
if d.IsBasestation() || !d.IsCamera() {
*basestations = append(*basestations, Basestation{Device: d})
}
}
return basestations
}
// GetCameras returns a Cameras object containing all devices that are of type "camera".
// I did this because some device types, like arloq, don't have a basestation.
// So, when interacting with them you must treat them like a basestation and a camera.
// Basestations also includes devices of this type, so you can get the same data there or cast.
func (ds Devices) GetCameras() *Cameras {
cameras := new(Cameras)
for _, d := range ds {
if d.IsCamera() || !d.IsBasestation() {
*cameras = append(*cameras, Camera(d))
}
}
return cameras
}
// UpdateDeviceName sets the name of the given device to the name argument.
func (d *Device) UpdateDeviceName(name string) error {
body := map[string]string{"deviceId": d.DeviceId, "deviceName": name, "parentId": d.ParentId}
resp, err := d.arlo.put(RenameDeviceUri, d.XCloudId, body, nil)
return checkRequest(resp, err, "failed to update device name")
}