feat: 自动建立实时监看媒体路径 (#51)

This commit is contained in:
ila
2026-08-13 12:42:08 +08:00
parent 82672baf02
commit 8a40e85866
+40 -4
View File
@@ -10,15 +10,34 @@ import (
"sync"
"time"
"yovision.local/sense/app/sense/admission"
"yovision.local/sense/app/sense/device"
"yovision.local/sense/app/sense/media"
)
type Route struct {
ID string `json:"id"`
DeviceID string `json:"device_id"`
DeviceName string `json:"device_name"`
DeviceLocation string `json:"device_location"`
ProfileToken string `json:"profile_token"`
ProfileName string `json:"profile_name"`
ProfileKind string `json:"profile_kind"`
Desired string `json:"desired"`
Actual string `json:"actual"`
Detail string `json:"detail"`
Readers int `json:"readers"`
}
type Session struct {
ID string `json:"id"`
OwnerID string `json:"-"`
RouteID string `json:"route_id"`
DeviceID string `json:"device_id"`
DeviceName string `json:"device_name"`
ProfileToken string `json:"profile_token"`
ProfileName string `json:"profile_name"`
ProfileKind string `json:"profile_kind"`
PlayerURL string `json:"player_url"`
Status string `json:"status"`
Detail string `json:"detail"`
@@ -47,15 +66,24 @@ func NewService(rawBase string, ttl time.Duration) (*Service, error) {
}
return &Service{base: parsed, ttl: ttl, route: media.PlaybackRoute, routes: media.PlaybackRoutes, sessions: map[string]Session{}, now: time.Now}, nil
}
func (s *Service) Routes(ctx context.Context) ([]media.Route, error) {
func (s *Service) Routes(ctx context.Context) ([]Route, error) {
items, err := s.routes(ctx)
if err != nil {
return nil, err
}
result := items[:0]
result := make([]Route, 0, len(items))
for _, item := range items {
if item.Desired == "running" {
result = append(result, item)
view := Route{ID: item.ID, DeviceID: item.DeviceID, DeviceName: item.DeviceID, ProfileToken: item.ProfileToken, ProfileName: item.ProfileToken, Desired: item.Desired, Actual: item.Actual, Detail: item.Detail, Readers: item.Readers}
if info, describeErr := device.Describe(ctx, item.DeviceID); describeErr == nil {
view.DeviceName = info.Name
view.DeviceLocation = info.Location
}
if profile, profileErr := admission.VerifiedProfile(item.DeviceID, item.ProfileToken); profileErr == nil {
view.ProfileName = profile.Name
view.ProfileKind = profile.Kind
}
result = append(result, view)
}
}
return result, nil
@@ -69,7 +97,14 @@ func (s *Service) Create(ctx context.Context, owner, routeID string) (Session, e
return Session{}, fmt.Errorf("media route is stopped")
}
id := newID()
session := Session{ID: id, OwnerID: owner, RouteID: route.ID, DeviceID: route.DeviceID, ProfileToken: route.ProfileToken, PlayerURL: "/api/v1/liveview/sessions/" + id + "/player", Status: route.Actual, Detail: route.Detail, ExpiresAt: s.now().UTC().Add(s.ttl)}
session := Session{ID: id, OwnerID: owner, RouteID: route.ID, DeviceID: route.DeviceID, DeviceName: route.DeviceID, ProfileToken: route.ProfileToken, ProfileName: route.ProfileToken, PlayerURL: "/api/v1/liveview/sessions/" + id + "/player", Status: route.Actual, Detail: route.Detail, ExpiresAt: s.now().UTC().Add(s.ttl)}
if info, describeErr := device.Describe(ctx, route.DeviceID); describeErr == nil {
session.DeviceName = info.Name
}
if profile, profileErr := admission.VerifiedProfile(route.DeviceID, route.ProfileToken); profileErr == nil {
session.ProfileName = profile.Name
session.ProfileKind = profile.Kind
}
s.mu.Lock()
s.sessions[id] = session
s.mu.Unlock()
@@ -110,3 +145,4 @@ func newID() string {
}
return "view_" + hex.EncodeToString(b)
}