Skip to content

Commit 0cb7d3e

Browse files
authored
Add AdoptDevice and ForgetDevice methods (#45)
* Add `AdoptDevice` and `ForgetDevice` methods * Address comments
1 parent 13d5677 commit 0cb7d3e

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

fields/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ func NewResource(version string, structName string, resourcePath string) *Resour
148148
baseType.Fields[" Key"] = NewFieldInfo("Key", "key", "string", "", false, false, "")
149149
case resource.StructName == "Device":
150150
baseType.Fields[" MAC"] = NewFieldInfo("MAC", "mac", "string", "", true, false, "")
151+
baseType.Fields["Adopted"] = NewFieldInfo("Adopted", "adopted", "bool", "", false, false, "")
152+
baseType.Fields["State"] = NewFieldInfo("State", "state", "DeviceState", "", false, false, "")
151153
case resource.StructName == "User":
152154
baseType.Fields[" IP"] = NewFieldInfo("IP", "ip", "string", "non-generated field", true, false, "")
153155
baseType.Fields[" DevIdOverride"] = NewFieldInfo("DevIdOverride", "dev_id_override", "int", "non-generated field", true, false, "")

unifi/device.generated.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unifi/device.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@ package unifi
22

33
import (
44
"context"
5+
"fmt"
6+
)
7+
8+
type DeviceState int
9+
10+
const (
11+
DeviceStateUnknown DeviceState = 0
12+
DeviceStateConnected DeviceState = 1
13+
DeviceStatePending DeviceState = 2
14+
DeviceStateFirmwareMismatch DeviceState = 3
15+
DeviceStateUpgrading DeviceState = 4
16+
DeviceStateProvisioning DeviceState = 5
17+
DeviceStateHeartbeatMissed DeviceState = 6
18+
DeviceStateAdopting DeviceState = 7
19+
DeviceStateDeleting DeviceState = 8
20+
DeviceStateInformError DeviceState = 9
21+
DeviceStateAdoptFailed DeviceState = 10
22+
DeviceStateIsolated DeviceState = 11
523
)
624

725
func (c *Client) ListDevice(ctx context.Context, site string) ([]Device, error) {
@@ -39,3 +57,46 @@ func (c *Client) GetDevice(ctx context.Context, site, id string) (*Device, error
3957

4058
return nil, &NotFoundError{}
4159
}
60+
61+
func (c *Client) AdoptDevice(ctx context.Context, site, mac string) error {
62+
reqBody := struct {
63+
Cmd string `json:"cmd"`
64+
MAC string `json:"mac"`
65+
}{
66+
Cmd: "adopt",
67+
MAC: mac,
68+
}
69+
70+
var respBody struct {
71+
Meta meta `json:"meta"`
72+
}
73+
74+
err := c.do(ctx, "POST", fmt.Sprintf("s/%s/cmd/devmgr", site), reqBody, &respBody)
75+
if err != nil {
76+
return err
77+
}
78+
79+
return nil
80+
}
81+
82+
func (c *Client) ForgetDevice(ctx context.Context, site, mac string) error {
83+
reqBody := struct {
84+
Cmd string `json:"cmd"`
85+
MACs []string `json:"macs"`
86+
}{
87+
Cmd: "delete-device",
88+
MACs: []string{mac},
89+
}
90+
91+
var respBody struct {
92+
Meta meta `json:"meta"`
93+
Data []Device `json:"data"`
94+
}
95+
96+
err := c.do(ctx, "POST", fmt.Sprintf("s/%s/cmd/sitemgr", site), reqBody, &respBody)
97+
if err != nil {
98+
return err
99+
}
100+
101+
return nil
102+
}

0 commit comments

Comments
 (0)