From f4e1debebde2c3771b5da41ba20ac34b1882a027 Mon Sep 17 00:00:00 2001 From: DN2 Date: Fri, 11 Jan 2019 01:35:56 -0800 Subject: [PATCH 1/2] Save the integer and the string. --- unidev/uap.go | 4 ++-- unidev/unidev.go | 15 +++++++++------ unidev/unidev_test.go | 24 ++++++++++++++++-------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/unidev/uap.go b/unidev/uap.go index c3e0dc76..af84c0ae 100644 --- a/unidev/uap.go +++ b/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(int(p.Channel)), + "channel": p.Channel.String, "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, + "tx_power": p.TxPower.Number, "tx_power_mode": p.TxPowerMode, } diff --git a/unidev/unidev.go b/unidev/unidev.go index 3f8d9e40..32a20b7b 100644 --- a/unidev/unidev.go +++ b/unidev/unidev.go @@ -34,22 +34,25 @@ type AuthedReq struct { // FlexInt provides a container and unmarshalling for fields that may be // numbers or strings in the Unifi API -type FlexInt int +type FlexInt struct { + Number float64 + String string +} // UnmarshalJSON converts a string or number to an integer. -func (value *FlexInt) UnmarshalJSON(b []byte) error { +func (f *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) + f.Number = i + f.String = strconv.FormatFloat(i, 'f', -1, 64) 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) + f.String = i + f.Number, _ = strconv.ParseFloat(i, 64) return nil default: return errors.New("Cannot unmarshal to FlexInt") diff --git a/unidev/unidev_test.go b/unidev/unidev_test.go index ed6cf005..210e4099 100644 --- a/unidev/unidev_test.go +++ b/unidev/unidev_test.go @@ -10,15 +10,23 @@ import ( func TestFlexInt(t *testing.T) { t.Parallel() a := assert.New(t) - type reply struct { - Channel FlexInt `json:"channel"` + type testReply struct { + Five FlexInt `json:"five"` + Seven FlexInt `json:"seven"` + Auto FlexInt `json:"auto"` } - 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) + var r testReply + // test unmarshalling the custom type three times with different values. + a.Nil(json.Unmarshal([]byte(`{"five": "5", "seven": 7, "auto": "auto"}`), &r)) + // test number in string. + a.EqualValues(5, r.Five.Number) + a.EqualValues("5", r.Five.String) + // test number. + a.EqualValues(7, r.Seven.Number) + a.EqualValues("7", r.Seven.String) + // test string. a.Nil(json.Unmarshal([]byte(`{"channel": "auto"}`), &r), "a regular string must not produce an unmarshal error") - a.EqualValues(FlexInt(0), r.Channel) + a.EqualValues(0, r.Auto.Number) + a.EqualValues("auto", r.Auto.String) } From b4e4e2d851fe49f9e69662de0892c304c87f96c3 Mon Sep 17 00:00:00 2001 From: DN2 Date: Fri, 11 Jan 2019 01:38:54 -0800 Subject: [PATCH 2/2] Convert UAP.uptime to FlexInt also. --- unidev/uap.go | 2 +- unidev/uap_type.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/unidev/uap.go b/unidev/uap.go index af84c0ae..d388506c 100644 --- a/unidev/uap.go +++ b/unidev/uap.go @@ -62,7 +62,7 @@ func (u UAP) Points() ([]*influx.Point, error) { "rx_bytes-d": u.RxBytesD, "tx_bytes": u.TxBytes, "tx_bytes-d": u.TxBytesD, - "uptime": u.Uptime, + "uptime": u.Uptime.Number, "considered_lost_at": u.ConsideredLostAt, "next_heartbeat_at": u.NextHeartbeatAt, "scanning": u.Scanning, diff --git a/unidev/uap_type.go b/unidev/uap_type.go index fd928114..053a83e1 100644 --- a/unidev/uap_type.go +++ b/unidev/uap_type.go @@ -306,7 +306,7 @@ type UAP struct { UplinkRemotePort int `json:"uplink_remote_port"` } `json:"uplink"` UplinkTable []interface{} `json:"uplink_table"` - Uptime float64 `json:"uptime"` + Uptime FlexInt `json:"uptime"` UserNumSta int `json:"user-num_sta"` VapTable []struct { ApMac string `json:"ap_mac"`