Skip to content

Commit

Permalink
add --timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
asymmetricia committed Aug 19, 2023
1 parent 2d50515 commit dcc8ba6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
7 changes: 5 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd

import (
"github.com/rs/zerolog"
"golang.org/x/crypto/ssh/terminal"
"os"
"time"

"github.com/rs/zerolog"
"golang.org/x/crypto/ssh/terminal"

"github.com/spf13/cobra"
)

Expand All @@ -23,6 +24,8 @@ func init() {
Root.Flags().String("host", "localhost", "hostname of the UniFi "+
"controller")
Root.Flags().Int("port", 8443, "UniFi controller port")
Root.Flags().Duration("timeout", 5*time.Second, "unifi server connection "+
"timeout")
Root.Flags().Bool("verify-tls", false, "if true, verify the TLS "+
"certificate of the UniFi controller")
Root.Flags().String("username", "", "login user on UniFi controller "+
Expand Down
6 changes: 4 additions & 2 deletions cmd/unifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package cmd

import (
"fmt"
"github.com/rs/zerolog"
"os"

"github.com/rs/zerolog"

"github.com/pdbogen/unifi2mqtt/types"
"github.com/spf13/cobra"
)
Expand All @@ -26,8 +27,9 @@ func startUnifi(cmd *cobra.Command, log zerolog.Logger) (<-chan types.Client, er
port, _ := cmd.Flags().GetInt("port")
verifyTls, _ := cmd.Flags().GetBool("verify-tls")
deviceTimeout, _ := cmd.Flags().GetDuration("unifi-timeout")
connectTimeout, _ := cmd.Flags().GetDuration("timeout")

u, err := types.NewUnifi(username, password, host, port, verifyTls, deviceTimeout, matchers, log)
u, err := types.NewUnifi(username, password, host, port, verifyTls, deviceTimeout, connectTimeout, matchers, log)
if err != nil {
return nil, fmt.Errorf("starting unifi client: %w", err)
}
Expand Down
46 changes: 25 additions & 21 deletions types/unifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ type Client struct {
}

type Unifi struct {
Username string
Password string
Host string
Port int
VerifyTls bool
DeviceTimeout time.Duration
Matchers []*Matcher
seenClients map[string]string
unifiClient *unifi.Unifi
ch chan Client
loginBackoff time.Duration
Username string
Password string
Host string
Port int
VerifyTls bool
DeviceTimeout time.Duration
ConnectTimeout time.Duration
Matchers []*Matcher
seenClients map[string]string
unifiClient *unifi.Unifi
ch chan Client
loginBackoff time.Duration
}

func (u *Unifi) Login(log zerolog.Logger) error {
Expand All @@ -43,6 +44,7 @@ func (u *Unifi) Login(log zerolog.Logger) error {
DebugLog: func(msg string, fmt ...interface{}) {
log.Debug().Msgf(msg, fmt...)
},
Timeout: u.ConnectTimeout,
})

if err == nil {
Expand Down Expand Up @@ -93,18 +95,20 @@ func (u *Unifi) Start(log zerolog.Logger) error {
}

func NewUnifi(username, password, host string, port int, verifyTls bool,
deviceTimeout time.Duration, matchers []*Matcher, log zerolog.Logger,
deviceTimeout time.Duration, connectTimeout time.Duration,
matchers []*Matcher, log zerolog.Logger,
) (*Unifi, error) {
ret := &Unifi{
Username: username,
Password: password,
Host: host,
Port: port,
VerifyTls: verifyTls,
DeviceTimeout: deviceTimeout,
Matchers: matchers,
seenClients: map[string]string{},
ch: make(chan Client),
Username: username,
Password: password,
Host: host,
Port: port,
VerifyTls: verifyTls,
DeviceTimeout: deviceTimeout,
ConnectTimeout: connectTimeout,
Matchers: matchers,
seenClients: map[string]string{},
ch: make(chan Client),
}

if err := ret.Start(log); err != nil {
Expand Down

0 comments on commit dcc8ba6

Please sign in to comment.