Add AdoptDevice and ForgetDevice methods (#45)

* Add `AdoptDevice` and `ForgetDevice` methods

* Address comments
This commit is contained in:
Joshua Spence
2021-09-17 11:16:56 +10:00
committed by GitHub
parent 13d5677bf3
commit 0cb7d3e514
3 changed files with 67 additions and 2 deletions

View File

@@ -2,6 +2,24 @@ package unifi
import (
"context"
"fmt"
)
type DeviceState int
const (
DeviceStateUnknown DeviceState = 0
DeviceStateConnected DeviceState = 1
DeviceStatePending DeviceState = 2
DeviceStateFirmwareMismatch DeviceState = 3
DeviceStateUpgrading DeviceState = 4
DeviceStateProvisioning DeviceState = 5
DeviceStateHeartbeatMissed DeviceState = 6
DeviceStateAdopting DeviceState = 7
DeviceStateDeleting DeviceState = 8
DeviceStateInformError DeviceState = 9
DeviceStateAdoptFailed DeviceState = 10
DeviceStateIsolated DeviceState = 11
)
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
return nil, &NotFoundError{}
}
func (c *Client) AdoptDevice(ctx context.Context, site, mac string) error {
reqBody := struct {
Cmd string `json:"cmd"`
MAC string `json:"mac"`
}{
Cmd: "adopt",
MAC: mac,
}
var respBody struct {
Meta meta `json:"meta"`
}
err := c.do(ctx, "POST", fmt.Sprintf("s/%s/cmd/devmgr", site), reqBody, &respBody)
if err != nil {
return err
}
return nil
}
func (c *Client) ForgetDevice(ctx context.Context, site, mac string) error {
reqBody := struct {
Cmd string `json:"cmd"`
MACs []string `json:"macs"`
}{
Cmd: "delete-device",
MACs: []string{mac},
}
var respBody struct {
Meta meta `json:"meta"`
Data []Device `json:"data"`
}
err := c.do(ctx, "POST", fmt.Sprintf("s/%s/cmd/sitemgr", site), reqBody, &respBody)
if err != nil {
return err
}
return nil
}