From 7633ae04192afcd71806dbfb9424dc5d96f268c7 Mon Sep 17 00:00:00 2001 From: William Petit Date: Fri, 12 Jan 2024 14:02:53 +0100 Subject: [PATCH] feat(module): arcast integration --- cmd/cli/command/app/run.go | 1 + cmd/cli/command/cast/root.go | 4 +++ cmd/cli/command/cast/scan.go | 3 -- go.mod | 5 +++ go.sum | 12 +++++++ pkg/module/cast/arcast/client.go | 49 +++++++++++++++++++++++++ pkg/module/cast/arcast/device.go | 57 +++++++++++++++++++++++++++++ pkg/module/cast/arcast/service.go | 60 +++++++++++++++++++++++++++++++ pkg/module/cast/arcast/status.go | 22 ++++++++++++ pkg/module/cast/cast_test.go | 51 +++++++++++++++----------- pkg/module/cast/device.go | 28 +++++++++++++++ pkg/module/cast/module.go | 4 +-- pkg/module/cast/registry.go | 24 +++++++------ 13 files changed, 283 insertions(+), 37 deletions(-) create mode 100644 pkg/module/cast/arcast/client.go create mode 100644 pkg/module/cast/arcast/device.go create mode 100644 pkg/module/cast/arcast/service.go create mode 100644 pkg/module/cast/arcast/status.go diff --git a/cmd/cli/command/app/run.go b/cmd/cli/command/app/run.go index 8979fec..6f9fa14 100644 --- a/cmd/cli/command/app/run.go +++ b/cmd/cli/command/app/run.go @@ -53,6 +53,7 @@ import ( _ "forge.cadoles.com/arcad/edge/pkg/storage/driver/sqlite" // Register casting device supported types + _ "forge.cadoles.com/arcad/edge/pkg/module/cast/arcast" _ "forge.cadoles.com/arcad/edge/pkg/module/cast/chromecast" "forge.cadoles.com/arcad/edge/pkg/storage/share" diff --git a/cmd/cli/command/cast/root.go b/cmd/cli/command/cast/root.go index ffad630..a080d6a 100644 --- a/cmd/cli/command/cast/root.go +++ b/cmd/cli/command/cast/root.go @@ -2,6 +2,10 @@ package cast import ( "github.com/urfave/cli/v2" + + // Register casting device supported types + _ "forge.cadoles.com/arcad/edge/pkg/module/cast/arcast" + _ "forge.cadoles.com/arcad/edge/pkg/module/cast/chromecast" ) func Root() *cli.Command { diff --git a/cmd/cli/command/cast/scan.go b/cmd/cli/command/cast/scan.go index eea7fda..1e8edd4 100644 --- a/cmd/cli/command/cast/scan.go +++ b/cmd/cli/command/cast/scan.go @@ -8,9 +8,6 @@ import ( "forge.cadoles.com/arcad/edge/pkg/module/cast" "github.com/pkg/errors" "github.com/urfave/cli/v2" - - // Register casting device supported types - _ "forge.cadoles.com/arcad/edge/pkg/module/cast/chromecast" ) func ScanCommand() *cli.Command { diff --git a/go.mod b/go.mod index e2dd38c..847b4d5 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21.4 toolchain go1.21.5 require ( + forge.cadoles.com/arcad/arcast v0.0.0-20231220090835-5d0311b7315d github.com/getsentry/sentry-go v0.25.0 github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/hashicorp/mdns v1.0.5 @@ -21,12 +22,15 @@ require ( require ( github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/cenkalti/backoff v2.2.1+incompatible // indirect github.com/charmbracelet/lipgloss v0.7.1 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect github.com/goccy/go-json v0.9.11 // indirect github.com/gogo/protobuf v0.0.0-20161014173244-50d1bd39ce4e // indirect + github.com/grandcat/zeroconf v1.0.1-0.20230119201135-e4f60f8407b1 // indirect + github.com/jaevor/go-nanoid v1.3.0 // indirect github.com/leodido/go-urn v1.2.1 // indirect github.com/lestrrat-go/blackmagic v1.0.1 // indirect github.com/lestrrat-go/httpcc v1.0.1 // indirect @@ -39,6 +43,7 @@ require ( github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/termenv v0.15.2 // indirect github.com/rivo/uniseg v0.4.4 // indirect + github.com/wlynxg/anet v0.0.1 // indirect go.opentelemetry.io/otel v1.21.0 // indirect go.opentelemetry.io/otel/trace v1.21.0 // indirect golang.org/x/sync v0.5.0 // indirect diff --git a/go.sum b/go.sum index 072fb96..d5054af 100644 --- a/go.sum +++ b/go.sum @@ -8,10 +8,14 @@ cloud.google.com/go/logging v1.7.0 h1:CJYxlNNNNAMkHp9em/YEXcfJg+rPDg7YfwoRpMU+t5 cloud.google.com/go/logging v1.7.0/go.mod h1:3xjP2CjkM3ZkO73aj4ASA5wRPGGCRrPIAeNqVNkzY8M= cloud.google.com/go/longrunning v0.5.1 h1:Fr7TXftcqTudoyRJa113hyaqlGdiBQkp0Gq7tErFDWI= cloud.google.com/go/longrunning v0.5.1/go.mod h1:spvimkwdz6SPWKEt/XBij79E9fiTkHSQl/fRUUQJYJc= +forge.cadoles.com/arcad/arcast v0.0.0-20231220090835-5d0311b7315d h1:SPDaDDF5StoprDqop8j8zozs8xK32EEWnUHLccWplKM= +forge.cadoles.com/arcad/arcast v0.0.0-20231220090835-5d0311b7315d/go.mod h1:QR8p4kUScWBcTQ0dE/gR+2ndpKOx77mIDSYGqSF1gms= github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= github.com/barnybug/go-cast v0.0.0-20201201064555-a87ccbc26692 h1:JW4WZlqyaNWUUahfr7MigeDW6jmtam5cTzzo1lwsFhE= github.com/barnybug/go-cast v0.0.0-20201201064555-a87ccbc26692/go.mod h1:Au0ipPuCBA7zsOC61SnyrYetm8VT3vo1UJtwHeYke44= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= +github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E= github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= @@ -69,6 +73,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/grandcat/zeroconf v1.0.1-0.20230119201135-e4f60f8407b1 h1:cNb52t5fkWv8ZiicKWnc2eZnhsCCoH7WmRBMIbMp04Q= +github.com/grandcat/zeroconf v1.0.1-0.20230119201135-e4f60f8407b1/go.mod h1:I6CSXU4zCGL08JOk9NbcT0ofAgnIkS/fVXbYzfSoDic= github.com/hashicorp/go.net v0.0.0-20151006203346-104dcad90073/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= @@ -81,6 +87,8 @@ github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf h1:FtEj8sfIcaaB github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf/go.mod h1:yrqSXGoD/4EKfF26AOGzscPOgTTJcyAwM2rpixWT+t4= github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/jaevor/go-nanoid v1.3.0 h1:nD+iepesZS6pr3uOVf20vR9GdGgJW1HPaR46gtrxzkg= +github.com/jaevor/go-nanoid v1.3.0/go.mod h1:SI+jFaPuddYkqkVQoNGHs81navCtH388TcrH0RqFKgY= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/keegancsmith/rpc v1.3.0 h1:wGWOpjcNrZaY8GDYZJfvyxmlLljm3YQWF+p918DXtDk= @@ -168,6 +176,8 @@ github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0o github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli/v2 v2.26.0 h1:3f3AMg3HpThFNT4I++TKOejZO8yU55t3JnnSr4S4QEI= github.com/urfave/cli/v2 v2.26.0/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= +github.com/wlynxg/anet v0.0.1 h1:VbkEEgHxPSrRQSiyRd0pmrbcEQAEU2TTb8fb4DmSYoQ= +github.com/wlynxg/anet v0.0.1/go.mod h1:eay5PRQr7fIVAMbTbchTnO9gG65Hg/uYGdc7mguHxoA= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= @@ -195,6 +205,7 @@ golang.org/x/net v0.0.0-20161013035702-8b4af36cd21a/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8= +golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= @@ -211,6 +222,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210426080607-c94f62235c83/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/pkg/module/cast/arcast/client.go b/pkg/module/cast/arcast/client.go new file mode 100644 index 0000000..fc805fc --- /dev/null +++ b/pkg/module/cast/arcast/client.go @@ -0,0 +1,49 @@ +package arcast + +import ( + "context" + + "forge.cadoles.com/arcad/arcast/pkg/client" + "forge.cadoles.com/arcad/edge/pkg/module/cast" + "github.com/pkg/errors" +) + +type Client struct { + client *client.Client + addr string +} + +// Close implements cast.Client. +func (c *Client) Close() error { + return nil +} + +// Load implements cast.Client. +func (c *Client) Load(ctx context.Context, url string) error { + if _, err := c.client.Cast(ctx, c.addr, url); err != nil { + return errors.WithStack(err) + } + + return nil +} + +// Status implements cast.Client. +func (c *Client) Status(ctx context.Context) (cast.DeviceStatus, error) { + status, err := c.client.Status(ctx, c.addr) + if err != nil { + return nil, errors.WithStack(err) + } + + return &DeviceStatus{status}, nil +} + +// Unload implements cast.Client. +func (c *Client) Unload(ctx context.Context) error { + if _, err := c.client.Reset(ctx, c.addr); err != nil { + return errors.WithStack(err) + } + + return nil +} + +var _ cast.Client = &Client{} diff --git a/pkg/module/cast/arcast/device.go b/pkg/module/cast/arcast/device.go new file mode 100644 index 0000000..00be047 --- /dev/null +++ b/pkg/module/cast/arcast/device.go @@ -0,0 +1,57 @@ +package arcast + +import ( + "context" + "net" + + "forge.cadoles.com/arcad/arcast/pkg/client" + "forge.cadoles.com/arcad/arcast/pkg/server" + "forge.cadoles.com/arcad/edge/pkg/module/cast" + "github.com/pkg/errors" + "gitlab.com/wpetit/goweb/logger" +) + +const DeviceTypeArcast cast.DeviceType = "arcast" + +type Device struct { + player *client.Player +} + +// DeviceHost implements cast.Device. +func (d *Device) DeviceHost() net.IP { + rawPreferredIP, err := server.FindPreferredLocalAddress(d.player.IPs...) + if err != nil { + logger.Error(context.Background(), "could not find preferred local address", logger.CapturedE(errors.WithStack(err))) + return d.player.IPs[0] + } + + preferredIP := net.ParseIP(rawPreferredIP) + if preferredIP == nil { + logger.Error(context.Background(), "could not parse device preferred ip", logger.F("rawPreferredIP", rawPreferredIP)) + return d.player.IPs[0] + } + + return preferredIP +} + +// DeviceID implements cast.Device. +func (d *Device) DeviceID() string { + return d.player.ID +} + +// DeviceName implements cast.Device. +func (d *Device) DeviceName() string { + return d.player.ID +} + +// DevicePort implements cast.Device. +func (d *Device) DevicePort() int { + return d.player.Port +} + +// DeviceType implements cast.Device. +func (d *Device) DeviceType() cast.DeviceType { + return DeviceTypeArcast +} + +var _ cast.Device = &Device{} diff --git a/pkg/module/cast/arcast/service.go b/pkg/module/cast/arcast/service.go new file mode 100644 index 0000000..1139e92 --- /dev/null +++ b/pkg/module/cast/arcast/service.go @@ -0,0 +1,60 @@ +package arcast + +import ( + "context" + "fmt" + + "forge.cadoles.com/arcad/arcast/pkg/client" + "forge.cadoles.com/arcad/edge/pkg/module/cast" + "github.com/pkg/errors" +) + +func init() { + cast.Register(DeviceTypeArcast, &Service{ + client: client.New(), + }) +} + +type Service struct { + client *client.Client +} + +// Find implements cast.Service. +func (s *Service) Find(ctx context.Context, deviceID string) (cast.Device, error) { + players, err := s.client.Scan(ctx, client.WithPlayerIDs(deviceID)) + if err != nil && !errors.Is(err, context.DeadlineExceeded) { + return nil, errors.WithStack(err) + } + + if len(players) == 0 { + return nil, errors.WithStack(cast.ErrDeviceNotFound) + } + + return &Device{players[0]}, nil +} + +// NewClient implements cast.Service. +func (s *Service) NewClient(ctx context.Context, device cast.Device) (cast.Client, error) { + return &Client{ + client: s.client, + addr: fmt.Sprintf("%s:%d", device.DeviceHost(), device.DevicePort()), + }, nil +} + +// Scan implements cast.Service. +func (s *Service) Scan(ctx context.Context) ([]cast.Device, error) { + players, err := s.client.Scan(ctx) + if err != nil && !errors.Is(err, context.DeadlineExceeded) { + return nil, errors.WithStack(err) + } + + devices := make([]cast.Device, len(players)) + + for i, p := range players { + devices[i] = &Device{p} + } + + return devices, nil +} + +var _ cast.Service = &Service{} diff --git a/pkg/module/cast/arcast/status.go b/pkg/module/cast/arcast/status.go new file mode 100644 index 0000000..cee9380 --- /dev/null +++ b/pkg/module/cast/arcast/status.go @@ -0,0 +1,22 @@ +package arcast + +import ( + "forge.cadoles.com/arcad/arcast/pkg/server" + "forge.cadoles.com/arcad/edge/pkg/module/cast" +) + +type DeviceStatus struct { + status *server.StatusResponse +} + +// State implements cast.DeviceStatus. +func (s *DeviceStatus) State() string { + return s.status.Status +} + +// Title implements cast.DeviceStatus. +func (s *DeviceStatus) Title() string { + return s.status.Title +} + +var _ cast.DeviceStatus = &DeviceStatus{} diff --git a/pkg/module/cast/cast_test.go b/pkg/module/cast/cast_test.go index 17e5542..ad4a4d0 100644 --- a/pkg/module/cast/cast_test.go +++ b/pkg/module/cast/cast_test.go @@ -2,6 +2,7 @@ package cast_test import ( "context" + "fmt" "os" "testing" "time" @@ -13,6 +14,7 @@ import ( "gitlab.com/wpetit/goweb/logger" // Register casting device supported types + _ "forge.cadoles.com/arcad/edge/pkg/module/cast/arcast" _ "forge.cadoles.com/arcad/edge/pkg/module/cast/chromecast" ) @@ -39,8 +41,8 @@ func TestCastLoadURL(t *testing.T) { t.Logf("DEVICES: %s", spew.Sdump(devices)) - if e, g := 1, len(devices); e != g { - t.Fatalf("len(devices): expected '%v', got '%v'", e, g) + if e, g := 1, len(devices); e > g { + t.Fatalf("len(devices): expected 'value >= %v', got '%v'", e, g) } devices, err = cast.ListDevices(ctx, false) @@ -50,33 +52,40 @@ func TestCastLoadURL(t *testing.T) { t.Logf("CACHED DEVICES: %s", spew.Sdump(devices)) - if e, g := 1, len(devices); e != g { - t.Fatalf("len(devices): expected '%v', got '%v'", e, g) + if e, g := 1, len(devices); e > g { + t.Fatalf("len(devices): expected 'value >= %v', got '%v'", e, g) } - dev := devices[0] + for _, device := range devices { + testName := fmt.Sprintf("%s(%s)", device.DeviceType(), device.DeviceID()) + func(device cast.Device) { + t.Run(testName, func(t *testing.T) { + t.Parallel() - ctx, cancel2 := context.WithTimeout(context.Background(), 15*time.Second) - defer cancel2() + ctx, cancel2 := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel2() - if err := cast.LoadURL(ctx, dev.DeviceID(), "https://go.dev"); err != nil { - t.Error(errors.WithStack(err)) - } + if err := cast.LoadURL(ctx, device.DeviceID(), "https://go.dev"); err != nil { + t.Error(errors.WithStack(err)) + } - ctx, cancel3 := context.WithTimeout(context.Background(), 15*time.Second) - defer cancel3() + ctx, cancel3 := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel3() - status, err := cast.GetStatus(ctx, dev.DeviceID()) - if err != nil { - t.Error(errors.WithStack(err)) - } + status, err := cast.GetStatus(ctx, device.DeviceID()) + if err != nil { + t.Error(errors.WithStack(err)) + } - t.Logf("DEVICE STATUS: %s", spew.Sdump(status)) + t.Logf("DEVICE STATUS: %s", spew.Sdump(status)) - ctx, cancel4 := context.WithTimeout(context.Background(), 15*time.Second) - defer cancel4() + ctx, cancel4 := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel4() - if err := cast.StopCast(ctx, dev.DeviceID()); err != nil { - t.Error(errors.WithStack(err)) + if err := cast.StopCast(ctx, device.DeviceID()); err != nil { + t.Error(errors.WithStack(err)) + } + }) + }(device) } } diff --git a/pkg/module/cast/device.go b/pkg/module/cast/device.go index ca5dcfc..fc6b0ed 100644 --- a/pkg/module/cast/device.go +++ b/pkg/module/cast/device.go @@ -11,3 +11,31 @@ type Device interface { DeviceName() string DeviceID() string } + +type legacyDevice struct { + Device `json:"-"` + UUID string `goja:"uuid" json:"uuid"` + Host string `goja:"host" json:"host"` + Port int `goja:"port" json:"port"` + Name string `goja:"name" json:"name"` + Type string `goja:"type" json:"type"` +} + +func toLegacyDevice(device Device) *legacyDevice { + return &legacyDevice{ + Device: device, + UUID: device.DeviceID(), + Host: device.DeviceHost().String(), + Port: device.DevicePort(), + Name: device.DeviceName(), + Type: string(device.DeviceType()), + } +} + +func toLegacyDevices(devices ...Device) []*legacyDevice { + legacyDevices := make([]*legacyDevice, len(devices)) + for i, d := range devices { + legacyDevices[i] = toLegacyDevice(d) + } + return legacyDevices +} diff --git a/pkg/module/cast/module.go b/pkg/module/cast/module.go index 82f6959..bd98719 100644 --- a/pkg/module/cast/module.go +++ b/pkg/module/cast/module.go @@ -67,7 +67,7 @@ func (m *Module) refreshDevices(call goja.FunctionCall, rt *goja.Runtime) goja.V return } - promise.Resolve(devices) + promise.Resolve(toLegacyDevices(devices...)) }() return rt.ToValue(promise) @@ -81,7 +81,7 @@ func (m *Module) getDevices(call goja.FunctionCall, rt *goja.Runtime) goja.Value panic(rt.ToValue(errors.WithStack(err))) } - return rt.ToValue(devices) + return rt.ToValue(toLegacyDevices(devices...)) } func (m *Module) loadUrl(call goja.FunctionCall, rt *goja.Runtime) goja.Value { diff --git a/pkg/module/cast/registry.go b/pkg/module/cast/registry.go index ede4dec..e7e6a37 100644 --- a/pkg/module/cast/registry.go +++ b/pkg/module/cast/registry.go @@ -62,20 +62,22 @@ func (r *Registry) Scan(ctx context.Context) ([]Device, error) { wg.Add(len(r.index)) for _, srv := range r.index { - go func() { - defer wg.Done() + func(srv Service) { + go func() { + defer wg.Done() + + devices, err := srv.Scan(ctx) + if err != nil { + lock.Lock() + errs = append(errs, errors.WithStack(err)) + lock.Unlock() + } - devices, err := srv.Scan(ctx) - if err != nil { lock.Lock() - errs = append(errs, errors.WithStack(err)) + results = append(results, devices...) lock.Unlock() - } - - lock.Lock() - results = append(results, devices...) - lock.Unlock() - }() + }() + }(srv) } wg.Wait()