diff --git a/integrations/promunifi/unidev/uap.go b/integrations/promunifi/unidev/uap.go index 195ce613..c3e0dc76 100644 --- a/integrations/promunifi/unidev/uap.go +++ b/integrations/promunifi/unidev/uap.go @@ -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, } diff --git a/integrations/promunifi/unidev/uap_type.go b/integrations/promunifi/unidev/uap_type.go index 59688a51..fd928114 100644 --- a/integrations/promunifi/unidev/uap_type.go +++ b/integrations/promunifi/unidev/uap_type.go @@ -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 diff --git a/integrations/promunifi/unidev/unidev.go b/integrations/promunifi/unidev/unidev.go index c7ce4094..a5992836 100644 --- a/integrations/promunifi/unidev/unidev.go +++ b/integrations/promunifi/unidev/unidev.go @@ -3,9 +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" @@ -30,8 +32,28 @@ type AuthedReq struct { baseURL string } -// StringInt is used to unmarshal quoted integers in JSON responses. -type StringInt int +// 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: + j, err := strconv.Atoi(i) + *value = FlexInt(j) + return err + 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. diff --git a/integrations/promunifi/unidev/unidev_test.go b/integrations/promunifi/unidev/unidev_test.go new file mode 100644 index 00000000..5c4986f9 --- /dev/null +++ b/integrations/promunifi/unidev/unidev_test.go @@ -0,0 +1,23 @@ +package unidev + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFlexInt(t *testing.T) { + t.Parallel() + a := assert.New(t) + five := []byte(`{"channel": "5"}`) + seven := []byte(`{"channel": 7}`) + type reply struct { + Channel FlexInt `json:"channel"` + } + var r reply + a.Nil(json.Unmarshal(five, &r)) + a.EqualValues(FlexInt(5), r.Channel) + a.Nil(json.Unmarshal(seven, &r)) + a.EqualValues(FlexInt(7), r.Channel) +}