Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package unifi: Implement Device.String(). #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ before_install:
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover
- go get github.com/golang/lint/golint
- go get golang.org/x/lint/golint
before_script:
- go get -d ./...
script:
Expand Down
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