commit
						7f0981c915
					
				|  | @ -181,7 +181,7 @@ func (u UAP) Points() ([]*influx.Point, error) { | |||
| 			"device_mac":   u.Mac, | ||||
| 			"name":         p.Name, | ||||
| 			"wlangroup_id": p.WlangroupID, | ||||
| 			"channel":      strconv.Itoa(p.Channel.Value), | ||||
| 			"channel":      strconv.Itoa(int(p.Channel)), | ||||
| 			"radio":        p.Radio, | ||||
| 		} | ||||
| 		fields := map[string]interface{}{ | ||||
|  | @ -196,7 +196,7 @@ func (u UAP) Points() ([]*influx.Point, error) { | |||
| 			"min_txpower":          p.MinTxpower, | ||||
| 			"nss":                  p.Nss, | ||||
| 			"radio_caps":           p.RadioCaps, | ||||
| 			"tx_power":             p.TxPower.Value, | ||||
| 			"tx_power":             p.TxPower, | ||||
| 			"tx_power_mode":        p.TxPowerMode, | ||||
| 		} | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,36 +1,5 @@ | |||
| package unidev | ||||
| 
 | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"errors" | ||||
| 	"strconv" | ||||
| ) | ||||
| 
 | ||||
| // FlexInt provides a container and unmarshalling for fields that may be
 | ||||
| // numbers or strings in the Unifi API
 | ||||
| type FlexInt struct { | ||||
| 	Value int | ||||
| } | ||||
| 
 | ||||
| func (this FlexInt) UnmarshalJSON(b []byte) error { | ||||
| 	var unk interface{} | ||||
| 	err := json.Unmarshal(b, &unk) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	switch i := unk.(type) { | ||||
| 	case float64: | ||||
| 		this.Value = int(i) | ||||
| 		return nil | ||||
| 	case string: | ||||
| 		this.Value, err = strconv.Atoi(i) | ||||
| 		return err | ||||
| 	default: | ||||
| 		return errors.New("Cannot unmarshal to FlexInt") | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| // UAP is a Unifi Access Point.
 | ||||
| type UAP struct { | ||||
| 	/* This was auto generated and then slowly edited by hand | ||||
|  |  | |||
|  | @ -3,8 +3,11 @@ package unidev | |||
| import ( | ||||
| 	"bytes" | ||||
| 	"crypto/tls" | ||||
| 	"encoding/json" | ||||
| 	"log" | ||||
| 	"net/http" | ||||
| 	"net/http/cookiejar" | ||||
| 	"strconv" | ||||
| 
 | ||||
| 	influx "github.com/influxdata/influxdb/client/v2" | ||||
| 	"github.com/pkg/errors" | ||||
|  | @ -29,6 +32,30 @@ type AuthedReq struct { | |||
| 	baseURL string | ||||
| } | ||||
| 
 | ||||
| // FlexInt provides a container and unmarshalling for fields that may be
 | ||||
| // numbers or strings in the Unifi API
 | ||||
| type FlexInt int | ||||
| 
 | ||||
| // UnmarshalJSON converts a string or number to an integer.
 | ||||
| func (value *FlexInt) UnmarshalJSON(b []byte) error { | ||||
| 	var unk interface{} | ||||
| 	if err := json.Unmarshal(b, &unk); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	switch i := unk.(type) { | ||||
| 	case float64: | ||||
| 		*value = FlexInt(i) | ||||
| 		return nil | ||||
| 	case string: | ||||
| 		// If it's a string like the word "auto" just set the integer to 0 and proceed.
 | ||||
| 		j, _ := strconv.Atoi(i) | ||||
| 		*value = FlexInt(j) | ||||
| 		return nil | ||||
| 	default: | ||||
| 		return errors.New("Cannot unmarshal to FlexInt") | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| // AuthController creates a http.Client with authenticated cookies.
 | ||||
| // Used to make additional, authenticated requests to the APIs.
 | ||||
| func AuthController(user, pass, url string) (*AuthedReq, error) { | ||||
|  | @ -41,11 +68,20 @@ func AuthController(user, pass, url string) (*AuthedReq, error) { | |||
| 		Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}, | ||||
| 		Jar:       jar, | ||||
| 	}, url} | ||||
| 	if req, err := authReq.UniReq(LoginPath, json); err != nil { | ||||
| 	req, err := authReq.UniReq(LoginPath, json) | ||||
| 	if err != nil { | ||||
| 		return nil, errors.Wrap(err, "UniReq(LoginPath, json)") | ||||
| 	} else if resp, err := authReq.Do(req); err != nil { | ||||
| 	} | ||||
| 	resp, err := authReq.Do(req) | ||||
| 	if err != nil { | ||||
| 		return nil, errors.Wrap(err, "authReq.Do(req)") | ||||
| 	} else if resp.StatusCode != http.StatusOK { | ||||
| 	} | ||||
| 	defer func() { | ||||
| 		if err := resp.Body.Close(); err != nil { | ||||
| 			log.Println("resp.Body.Close():", err) // Not fatal. Just log it.
 | ||||
| 		} | ||||
| 	}() | ||||
| 	if resp.StatusCode != http.StatusOK { | ||||
| 		return nil, errors.Errorf("authentication failed (%v): %v (status: %v/%v)", | ||||
| 			user, url+LoginPath, resp.StatusCode, resp.Status) | ||||
| 	} | ||||
|  |  | |||
|  | @ -0,0 +1,24 @@ | |||
| package unidev | ||||
| 
 | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"testing" | ||||
| 
 | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
| 
 | ||||
| func TestFlexInt(t *testing.T) { | ||||
| 	t.Parallel() | ||||
| 	a := assert.New(t) | ||||
| 	type reply struct { | ||||
| 		Channel FlexInt `json:"channel"` | ||||
| 	} | ||||
| 	var r reply | ||||
| 	a.Nil(json.Unmarshal([]byte(`{"channel": "5"}`), &r)) | ||||
| 	a.EqualValues(FlexInt(5), r.Channel) | ||||
| 	a.Nil(json.Unmarshal([]byte(`{"channel": 7}`), &r)) | ||||
| 	a.EqualValues(FlexInt(7), r.Channel) | ||||
| 	a.Nil(json.Unmarshal([]byte(`{"channel": "auto"}`), &r), | ||||
| 		"a regular string must not produce an unmarshal error") | ||||
| 	a.EqualValues(FlexInt(0), r.Channel) | ||||
| } | ||||
		Loading…
	
		Reference in New Issue