Skip to content

Commit

Permalink
Package unifi: Implement Device.String().
Browse files Browse the repository at this point in the history
Behaves like the Unifi UI, i.e. returns the device's name if set and the
serial otherwise. If the serial has exactly 12 characters, it is
formatted like a MAC address, again like the UI does.
  • Loading branch information
octo committed Oct 15, 2018
1 parent d84cee4 commit 9d3de47
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"net/url"
"strings"
"time"
)

Expand Down Expand Up @@ -224,6 +225,20 @@ func (d *Device) UnmarshalJSON(b []byte) error {
return nil
}

func (d Device) String() string {
if d.Name != "" {
return d.Name
}
if d.Serial != "" {
if len(d.Serial) != 12 {
return d.Serial
}
s := strings.ToLower(d.Serial)
return strings.Join([]string{s[0:2], s[2:4], s[4:6], s[6:8], s[8:10], s[10:12]}, ":")
}
return d.ID
}

// A device is the raw structure of a Device returned from the UniFi Controller
// API.
type device struct {
Expand Down

0 comments on commit 9d3de47

Please sign in to comment.