Add two new functions to make the library more useful.
This commit is contained in:
parent
09af1f4183
commit
1c06cea465
|
|
@ -83,14 +83,14 @@ func GetConfig(configFile string) (Config, error) {
|
||||||
func (c *Config) PollUnifiController(infdb influx.Client, unifi *unidev.AuthedReq) {
|
func (c *Config) PollUnifiController(infdb influx.Client, unifi *unidev.AuthedReq) {
|
||||||
ticker := time.NewTicker(c.Interval.value)
|
ticker := time.NewTicker(c.Interval.value)
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
clients, err := unifi.GetUnifiClients()
|
clients, err := unifi.GetUnifiClientAssets()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("unifi.GetUnifiClients():", err)
|
log.Println("unifi.GetUnifiClientsAssets():", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
devices, err := unifi.GetUnifiDevices()
|
devices, err := unifi.GetUnifiDeviceAssets()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("unifi.GetUnifiDevices():", err)
|
log.Println("unifi.GetUnifiDeviceAssets():", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
bp, err := influx.NewBatchPoints(influx.BatchPointsConfig{
|
bp, err := influx.NewBatchPoints(influx.BatchPointsConfig{
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,9 @@ import (
|
||||||
const LoginPath = "/api/login"
|
const LoginPath = "/api/login"
|
||||||
|
|
||||||
// Asset provides a common interface to retreive metrics from a device or client.
|
// Asset provides a common interface to retreive metrics from a device or client.
|
||||||
|
// It currently only supports InfluxDB, but could be amended to support other
|
||||||
|
// libraries that have a similar interface.
|
||||||
|
// This app only uses the .AddPoint/s() methods with the Asset type.
|
||||||
type Asset interface {
|
type Asset interface {
|
||||||
// Point() means this is useful to influxdb..
|
// Point() means this is useful to influxdb..
|
||||||
Points() ([]*influx.Point, error)
|
Points() ([]*influx.Point, error)
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetUnifiClients returns a response full of clients' data from the Unifi Controller.
|
// GetUnifiClients returns a response full of clients' data from the Unifi Controller.
|
||||||
func (c *AuthedReq) GetUnifiClients() ([]Asset, error) {
|
func (c *AuthedReq) GetUnifiClients() ([]UCL, error) {
|
||||||
var response struct {
|
var response struct {
|
||||||
Clients []UCL `json:"data"`
|
Clients []UCL `json:"data"`
|
||||||
Meta struct {
|
Meta struct {
|
||||||
|
|
@ -39,63 +39,98 @@ func (c *AuthedReq) GetUnifiClients() ([]Asset, error) {
|
||||||
} else if err = resp.Body.Close(); err != nil {
|
} else if err = resp.Body.Close(); err != nil {
|
||||||
log.Println("resp.Body.Close():", err) // Not fatal? Just log it.
|
log.Println("resp.Body.Close():", err) // Not fatal? Just log it.
|
||||||
}
|
}
|
||||||
clients := []Asset{}
|
return response.Clients, nil
|
||||||
for _, r := range response.Clients {
|
}
|
||||||
clients = append(clients, r)
|
|
||||||
|
// GetUnifiClientAssets provides an interface to return common asset types.
|
||||||
|
func (c *AuthedReq) GetUnifiClientAssets() ([]Asset, error) {
|
||||||
|
clients, err := c.GetUnifiClients()
|
||||||
|
assets := []Asset{}
|
||||||
|
if err == nil {
|
||||||
|
for _, r := range clients {
|
||||||
|
assets = append(assets, r)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return clients, nil
|
return assets, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUnifiDevices returns a response full of devices' data from the Unifi Controller.
|
// GetUnifiDevices returns a response full of devices' data from the Unifi Controller.
|
||||||
func (c *AuthedReq) GetUnifiDevices() ([]Asset, error) {
|
func (c *AuthedReq) GetUnifiDevices() ([]USG, []USW, []UAP, error) {
|
||||||
var parsed struct {
|
var parsed struct {
|
||||||
Data []json.RawMessage `json:"data"`
|
Data []json.RawMessage `json:"data"`
|
||||||
Meta struct {
|
Meta struct {
|
||||||
Rc string `json:"rc"`
|
Rc string `json:"rc"`
|
||||||
} `json:"meta"`
|
} `json:"meta"`
|
||||||
}
|
}
|
||||||
assets := []Asset{}
|
|
||||||
if req, err := c.UniReq(DevicePath, ""); err != nil {
|
if req, err := c.UniReq(DevicePath, ""); err != nil {
|
||||||
return nil, err
|
return nil, nil, nil, err
|
||||||
} else if resp, err := c.Do(req); err != nil {
|
} else if resp, err := c.Do(req); err != nil {
|
||||||
return nil, err
|
return nil, nil, nil, err
|
||||||
} else if body, err := ioutil.ReadAll(resp.Body); err != nil {
|
} else if body, err := ioutil.ReadAll(resp.Body); err != nil {
|
||||||
return nil, err
|
return nil, nil, nil, err
|
||||||
} else if err = json.Unmarshal(body, &parsed); err != nil {
|
} else if err = json.Unmarshal(body, &parsed); err != nil {
|
||||||
return nil, err
|
return nil, nil, nil, err
|
||||||
} else if err = resp.Body.Close(); err != nil {
|
} else if err = resp.Body.Close(); err != nil {
|
||||||
log.Println("resp.Body.Close():", err) // Not fatal? Just log it.
|
log.Println("resp.Body.Close():", err) // Not fatal? Just log it.
|
||||||
}
|
}
|
||||||
|
var usgs []USG
|
||||||
|
var usws []USW
|
||||||
|
var uaps []UAP
|
||||||
for _, r := range parsed.Data {
|
for _, r := range parsed.Data {
|
||||||
|
var usg USG
|
||||||
|
var usw USW
|
||||||
|
var uap UAP
|
||||||
// Unamrshal into a map and check "type"
|
// Unamrshal into a map and check "type"
|
||||||
var obj map[string]interface{}
|
var obj map[string]interface{}
|
||||||
if err := json.Unmarshal(r, &obj); err != nil {
|
if err := json.Unmarshal(r, &obj); err != nil {
|
||||||
return nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
assetType := ""
|
assetType := "- missing -"
|
||||||
if t, ok := obj["type"].(string); ok {
|
if t, ok := obj["type"].(string); ok {
|
||||||
assetType = t
|
assetType = t
|
||||||
}
|
}
|
||||||
|
if Debug {
|
||||||
|
log.Println("Unmarshalling Device Type:", assetType)
|
||||||
|
}
|
||||||
// Unmarshal again into the correct type..
|
// Unmarshal again into the correct type..
|
||||||
var asset Asset
|
|
||||||
switch assetType {
|
switch assetType {
|
||||||
case "uap":
|
case "uap":
|
||||||
asset = &UAP{}
|
if err := json.Unmarshal(r, uap); err != nil {
|
||||||
case "ugw":
|
return nil, nil, nil, err
|
||||||
asset = &USG{}
|
}
|
||||||
|
uaps = append(uaps, uap)
|
||||||
|
case "ugw", "usg": // in case they ever fix the name in the api.
|
||||||
|
if err := json.Unmarshal(r, usg); err != nil {
|
||||||
|
return nil, nil, nil, err
|
||||||
|
}
|
||||||
|
usgs = append(usgs, usg)
|
||||||
case "usw":
|
case "usw":
|
||||||
asset = &USW{}
|
if err := json.Unmarshal(r, usw); err != nil {
|
||||||
|
return nil, nil, nil, err
|
||||||
|
}
|
||||||
|
usws = append(usws, usw)
|
||||||
default:
|
default:
|
||||||
log.Println("unknown asset type -", assetType, "- skipping")
|
log.Println("unknown asset type -", assetType, "- skipping")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if Debug {
|
|
||||||
log.Println("Unmarshalling", assetType)
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(r, asset); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
assets = append(assets, asset)
|
|
||||||
}
|
}
|
||||||
return assets, nil
|
return usgs, usws, uaps, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUnifiDeviceAssets provides an interface to return common asset types.
|
||||||
|
func (c *AuthedReq) GetUnifiDeviceAssets() ([]Asset, error) {
|
||||||
|
usgs, usws, uaps, err := c.GetUnifiDevices()
|
||||||
|
assets := []Asset{}
|
||||||
|
if err == nil {
|
||||||
|
for _, r := range usgs {
|
||||||
|
assets = append(assets, r)
|
||||||
|
}
|
||||||
|
for _, r := range usws {
|
||||||
|
assets = append(assets, r)
|
||||||
|
}
|
||||||
|
for _, r := range uaps {
|
||||||
|
assets = append(assets, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return assets, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue