edge/pkg/module/cast/cast.go

196 lines
3.6 KiB
Go
Raw Normal View History

2023-02-17 10:38:45 +01:00
package cast
import (
"context"
"sync"
2023-02-17 10:38:45 +01:00
"time"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
)
type CachedDevice struct {
Device
UpdatedAt time.Time
}
func (d CachedDevice) Expired() bool {
return d.UpdatedAt.Add(30 * time.Minute).Before(time.Now())
}
var cache sync.Map
func getCachedDevice(uuid string) (Device, bool) {
value, exists := cache.Load(uuid)
if !exists {
return nil, false
}
cachedDevice, ok := value.(CachedDevice)
if !ok {
return nil, false
}
if cachedDevice.Expired() {
return nil, false
}
return cachedDevice.Device, true
}
func cacheDevice(dev Device) {
cache.Store(dev.DeviceID(), CachedDevice{
Device: dev,
UpdatedAt: time.Now(),
})
}
func getDeviceClientByID(ctx context.Context, deviceID string) (Client, error) {
device, err := findCachedDeviceByID(ctx, deviceID)
2023-02-17 10:38:45 +01:00
if err != nil {
return nil, errors.WithStack(err)
}
client, err := NewClient(ctx, device)
if err != nil {
return nil, errors.WithStack(err)
}
2023-02-17 10:38:45 +01:00
return client, nil
}
func findCachedDeviceByID(ctx context.Context, deviceID string) (Device, error) {
device, exists := getCachedDevice(deviceID)
if exists {
return device, nil
}
2023-02-17 10:38:45 +01:00
ctx, cancel := context.WithCancel(ctx)
defer cancel()
device, err := Find(ctx, deviceID)
if err != nil {
return nil, errors.WithStack(err)
}
cacheDevice(device)
2023-02-17 10:38:45 +01:00
return device, nil
}
func ListDevices(ctx context.Context, refresh bool) ([]Device, error) {
devices := make([]Device, 0)
if !refresh {
cache.Range(func(key, value any) bool {
cached, ok := value.(CachedDevice)
if !ok || cached.Expired() {
return true
2023-02-17 10:38:45 +01:00
}
devices = append(devices, cached.Device)
return true
})
return devices, nil
2023-02-17 10:38:45 +01:00
}
devices, err := SearchDevices(ctx)
if err != nil {
2023-02-17 10:38:45 +01:00
return nil, errors.WithStack(err)
}
return devices, nil
2023-02-17 10:38:45 +01:00
}
var searchDevicesMutex sync.Mutex
func SearchDevices(ctx context.Context) ([]Device, error) {
searchDevicesMutex.Lock()
defer searchDevicesMutex.Unlock()
devices, err := Scan(ctx)
if err != nil {
return nil, errors.WithStack(err)
}
for _, device := range devices {
cacheDevice(device)
}
2023-02-17 10:38:45 +01:00
return devices, nil
}
var loadURLMutex sync.Mutex
func LoadURL(ctx context.Context, deviceID string, url string) error {
loadURLMutex.Lock()
defer loadURLMutex.Unlock()
client, err := getDeviceClientByID(ctx, deviceID)
2023-02-17 10:38:45 +01:00
if err != nil {
return errors.WithStack(err)
}
defer func() {
if err := client.Close(); err != nil {
logger.Error(ctx, "could not close client", logger.CapturedE(errors.WithStack(err)))
}
}()
2023-02-17 10:38:45 +01:00
if err := client.Load(ctx, url); err != nil {
2023-02-17 10:38:45 +01:00
return errors.WithStack(err)
}
return nil
}
var stopCastMutex sync.Mutex
func StopCast(ctx context.Context, deviceID string) error {
stopCastMutex.Lock()
defer stopCastMutex.Unlock()
client, err := getDeviceClientByID(ctx, deviceID)
2023-02-17 10:38:45 +01:00
if err != nil {
return errors.WithStack(err)
}
defer func() {
if err := client.Close(); err != nil {
logger.Error(ctx, "could not close client", logger.CapturedE(errors.WithStack(err)))
}
}()
2023-02-17 10:38:45 +01:00
if err := client.Unload(ctx); err != nil {
2023-02-17 10:38:45 +01:00
return errors.WithStack(err)
}
return nil
}
var getStatusMutex sync.Mutex
func GetStatus(ctx context.Context, deviceID string) (DeviceStatus, error) {
getStatusMutex.Lock()
defer getStatusMutex.Unlock()
client, err := getDeviceClientByID(ctx, deviceID)
2023-02-17 10:38:45 +01:00
if err != nil {
return nil, errors.WithStack(err)
}
defer func() {
if err := client.Close(); err != nil {
logger.Error(ctx, "could not close client", logger.CapturedE(errors.WithStack(err)))
}
}()
2023-02-17 10:38:45 +01:00
status, err := client.Status(ctx)
2023-02-17 10:38:45 +01:00
if err != nil {
return nil, errors.WithStack(err)
}
return status, nil
}