Skip to content

Commit

Permalink
Code refactoring for streams HandleFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jun 14, 2024
1 parent 1ac9d54 commit ecfe802
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 93 deletions.
12 changes: 3 additions & 9 deletions internal/bubble/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
8 changes: 0 additions & 8 deletions internal/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 1 addition & 10 deletions internal/dvrip/dvrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 3 additions & 5 deletions internal/gopro/gopro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 2 additions & 7 deletions internal/hass/hass.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 3 additions & 12 deletions internal/isapi/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
10 changes: 2 additions & 8 deletions internal/ivideon/ivideon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
12 changes: 3 additions & 9 deletions internal/nest/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
15 changes: 3 additions & 12 deletions internal/roborock/roborock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
8 changes: 4 additions & 4 deletions internal/tapo/tapo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
8 changes: 6 additions & 2 deletions pkg/bubble/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
8 changes: 6 additions & 2 deletions pkg/isapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions pkg/ivideon/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/nest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions pkg/roborock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ecfe802

Please sign in to comment.