edge/pkg/module/cast/arcast/service.go

61 lines
1.3 KiB
Go

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{}