From ecfe802065fdcdef770a0ed49aea24339a807212 Mon Sep 17 00:00:00 2001 From: Alex X Date: Fri, 14 Jun 2024 12:48:29 +0300 Subject: [PATCH] Code refactoring for streams HandleFunc --- internal/bubble/bubble.go | 12 +++--------- internal/debug/debug.go | 8 -------- internal/dvrip/dvrip.go | 11 +---------- internal/gopro/gopro.go | 8 +++----- internal/hass/hass.go | 9 ++------- internal/isapi/init.go | 15 +++------------ internal/ivideon/ivideon.go | 10 ++-------- internal/nest/init.go | 12 +++--------- internal/roborock/roborock.go | 15 +++------------ internal/tapo/tapo.go | 8 ++++---- pkg/bubble/client.go | 8 ++++++-- pkg/isapi/client.go | 8 ++++++-- pkg/ivideon/client.go | 9 +++++++-- pkg/nest/client.go | 2 +- pkg/roborock/client.go | 11 +++++++++-- 15 files changed, 53 insertions(+), 93 deletions(-) diff --git a/internal/bubble/bubble.go b/internal/bubble/bubble.go index 65d0237e..6c526fc5 100644 --- a/internal/bubble/bubble.go +++ b/internal/bubble/bubble.go @@ -7,13 +7,7 @@ import ( ) func Init() { - streams.HandleFunc("bubble", handle) -} - -func handle(url string) (core.Producer, error) { - conn := bubble.NewClient(url) - if err := conn.Dial(); err != nil { - return nil, err - } - return conn, nil + streams.HandleFunc("bubble", func(source string) (core.Producer, error) { + return bubble.Dial(source) + }) } diff --git a/internal/debug/debug.go b/internal/debug/debug.go index 3d40d1f1..fc7d2453 100644 --- a/internal/debug/debug.go +++ b/internal/debug/debug.go @@ -2,16 +2,8 @@ package debug import ( "github.com/AlexxIT/go2rtc/internal/api" - "github.com/AlexxIT/go2rtc/internal/streams" - "github.com/AlexxIT/go2rtc/pkg/core" ) func Init() { api.HandleFunc("api/stack", stackHandler) - - streams.HandleFunc("null", nullHandler) -} - -func nullHandler(string) (core.Producer, error) { - return nil, nil } diff --git a/internal/dvrip/dvrip.go b/internal/dvrip/dvrip.go index 095372d2..db1c60db 100644 --- a/internal/dvrip/dvrip.go +++ b/internal/dvrip/dvrip.go @@ -10,25 +10,16 @@ import ( "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" - "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/dvrip" ) func Init() { - streams.HandleFunc("dvrip", handle) + streams.HandleFunc("dvrip", dvrip.Dial) // DVRIP client autodiscovery api.HandleFunc("api/dvrip", apiDvrip) } -func handle(url string) (core.Producer, error) { - client, err := dvrip.Dial(url) - if err != nil { - return nil, err - } - return client, nil -} - const Port = 34569 // UDP port number for dvrip discovery func apiDvrip(w http.ResponseWriter, r *http.Request) { diff --git a/internal/gopro/gopro.go b/internal/gopro/gopro.go index 55d2641b..ee578049 100644 --- a/internal/gopro/gopro.go +++ b/internal/gopro/gopro.go @@ -10,15 +10,13 @@ import ( ) func Init() { - streams.HandleFunc("gopro", handleGoPro) + streams.HandleFunc("gopro", func(source string) (core.Producer, error) { + return gopro.Dial(source) + }) api.HandleFunc("api/gopro", apiGoPro) } -func handleGoPro(rawURL string) (core.Producer, error) { - return gopro.Dial(rawURL) -} - func apiGoPro(w http.ResponseWriter, r *http.Request) { var items []*api.Source diff --git a/internal/hass/hass.go b/internal/hass/hass.go index cd95ffe1..ea172b02 100644 --- a/internal/hass/hass.go +++ b/internal/hass/hass.go @@ -45,14 +45,9 @@ func Init() { return "", nil }) - streams.HandleFunc("hass", func(url string) (core.Producer, error) { + streams.HandleFunc("hass", func(source string) (core.Producer, error) { // support hass://supervisor?entity_id=camera.driveway_doorbell - client, err := hass.NewClient(url) - if err != nil { - return nil, err - } - - return client, nil + return hass.NewClient(source) }) // load static entries from Hass config diff --git a/internal/isapi/init.go b/internal/isapi/init.go index a37afa23..887a6748 100644 --- a/internal/isapi/init.go +++ b/internal/isapi/init.go @@ -7,16 +7,7 @@ import ( ) func Init() { - streams.HandleFunc("isapi", handle) -} - -func handle(url string) (core.Producer, error) { - conn, err := isapi.NewClient(url) - if err != nil { - return nil, err - } - if err = conn.Dial(); err != nil { - return nil, err - } - return conn, nil + streams.HandleFunc("isapi", func(source string) (core.Producer, error) { + return isapi.Dial(source) + }) } diff --git a/internal/ivideon/ivideon.go b/internal/ivideon/ivideon.go index 0ae5dc9f..03feb742 100644 --- a/internal/ivideon/ivideon.go +++ b/internal/ivideon/ivideon.go @@ -4,16 +4,10 @@ import ( "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/ivideon" - "strings" ) func Init() { - streams.HandleFunc("ivideon", func(url string) (core.Producer, error) { - id := strings.Replace(url[8:], "/", ":", 1) - prod := ivideon.NewClient(id) - if err := prod.Dial(); err != nil { - return nil, err - } - return prod, nil + streams.HandleFunc("ivideon", func(source string) (core.Producer, error) { + return ivideon.Dial(source) }) } diff --git a/internal/nest/init.go b/internal/nest/init.go index 1281ccdc..01682414 100644 --- a/internal/nest/init.go +++ b/internal/nest/init.go @@ -10,19 +10,13 @@ import ( ) func Init() { - streams.HandleFunc("nest", streamNest) + streams.HandleFunc("nest", func(source string) (core.Producer, error) { + return nest.Dial(source) + }) api.HandleFunc("api/nest", apiNest) } -func streamNest(url string) (core.Producer, error) { - client, err := nest.NewClient(url) - if err != nil { - return nil, err - } - return client, nil -} - func apiNest(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() cliendID := query.Get("client_id") diff --git a/internal/roborock/roborock.go b/internal/roborock/roborock.go index 27e29bb5..32a436d8 100644 --- a/internal/roborock/roborock.go +++ b/internal/roborock/roborock.go @@ -11,22 +11,13 @@ import ( ) func Init() { - streams.HandleFunc("roborock", handle) + streams.HandleFunc("roborock", func(source string) (core.Producer, error) { + return roborock.Dial(source) + }) api.HandleFunc("api/roborock", apiHandle) } -func handle(url string) (core.Producer, error) { - conn := roborock.NewClient(url) - if err := conn.Dial(); err != nil { - return nil, err - } - if err := conn.Connect(); err != nil { - return nil, err - } - return conn, nil -} - var Auth struct { UserData *roborock.UserInfo `json:"user_data"` BaseURL string `json:"base_url"` diff --git a/internal/tapo/tapo.go b/internal/tapo/tapo.go index a54c8c5e..724c9e86 100644 --- a/internal/tapo/tapo.go +++ b/internal/tapo/tapo.go @@ -8,11 +8,11 @@ import ( ) func Init() { - streams.HandleFunc("kasa", func(url string) (core.Producer, error) { - return kasa.Dial(url) + streams.HandleFunc("kasa", func(source string) (core.Producer, error) { + return kasa.Dial(source) }) - streams.HandleFunc("tapo", func(url string) (core.Producer, error) { - return tapo.Dial(url) + streams.HandleFunc("tapo", func(source string) (core.Producer, error) { + return tapo.Dial(source) }) } diff --git a/pkg/bubble/client.go b/pkg/bubble/client.go index b8b77ae9..c0a79701 100644 --- a/pkg/bubble/client.go +++ b/pkg/bubble/client.go @@ -43,8 +43,12 @@ type Client struct { recv int } -func NewClient(url string) *Client { - return &Client{url: url} +func Dial(rawURL string) (*Client, error) { + client := &Client{url: rawURL} + if err := client.Dial(); err != nil { + return nil, err + } + return client, nil } const ( diff --git a/pkg/isapi/client.go b/pkg/isapi/client.go index e5dfafd4..83dd9026 100644 --- a/pkg/isapi/client.go +++ b/pkg/isapi/client.go @@ -23,7 +23,7 @@ type Client struct { send int } -func NewClient(rawURL string) (*Client, error) { +func Dial(rawURL string) (*Client, error) { // check if url is valid url u, err := url.Parse(rawURL) if err != nil { @@ -33,7 +33,11 @@ func NewClient(rawURL string) (*Client, error) { u.Scheme = "http" u.Path = "" - return &Client{url: u.String()}, nil + client := &Client{url: u.String()} + if err = client.Dial(); err != nil { + return nil, err + } + return client, err } func (c *Client) Dial() (err error) { diff --git a/pkg/ivideon/client.go b/pkg/ivideon/client.go index c1b055b8..7cbf0b38 100644 --- a/pkg/ivideon/client.go +++ b/pkg/ivideon/client.go @@ -46,8 +46,13 @@ type Client struct { recv int } -func NewClient(id string) *Client { - return &Client{ID: id} +func Dial(source string) (*Client, error) { + id := strings.Replace(source[8:], "/", ":", 1) + client := &Client{ID: id} + if err := client.Dial(); err != nil { + return nil, err + } + return client, nil } func (c *Client) Dial() (err error) { diff --git a/pkg/nest/client.go b/pkg/nest/client.go index cb73cc98..2169773b 100644 --- a/pkg/nest/client.go +++ b/pkg/nest/client.go @@ -14,7 +14,7 @@ type Client struct { api *API } -func NewClient(rawURL string) (*Client, error) { +func Dial(rawURL string) (*Client, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err diff --git a/pkg/roborock/client.go b/pkg/roborock/client.go index 6a3bf0e0..522b0e13 100644 --- a/pkg/roborock/client.go +++ b/pkg/roborock/client.go @@ -34,8 +34,15 @@ type Client struct { backchannel bool } -func NewClient(url string) *Client { - return &Client{url: url} +func Dial(rawURL string) (*Client, error) { + client := &Client{url: rawURL} + if err := client.Dial(); err != nil { + return nil, err + } + if err := client.Connect(); err != nil { + return nil, err + } + return client, nil } func (c *Client) Dial() error {