From 124556f4db2f13e53a6c759cd1ab0761dce94997 Mon Sep 17 00:00:00 2001 From: seydx Date: Thu, 8 May 2025 16:09:04 +0200 Subject: [PATCH] ring: skip refetching cameras to increase loading speed and refactor ring url --- internal/ring/ring.go | 3 +++ pkg/ring/api.go | 2 +- pkg/ring/client.go | 34 ++++++++++++---------------------- pkg/ring/snapshot.go | 8 ++++---- 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/internal/ring/ring.go b/internal/ring/ring.go index 673ea480..e1615151 100644 --- a/internal/ring/ring.go +++ b/internal/ring/ring.go @@ -5,6 +5,8 @@ import ( "net/http" "net/url" + "fmt" + "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" @@ -83,6 +85,7 @@ func apiRing(w http.ResponseWriter, r *http.Request) { var items []*api.Source for _, camera := range devices.AllCameras { + cleanQuery.Set("camera_id", fmt.Sprint(camera.ID)) cleanQuery.Set("device_id", camera.DeviceID) // Stream source diff --git a/pkg/ring/api.go b/pkg/ring/api.go index ed69465f..3b67173d 100644 --- a/pkg/ring/api.go +++ b/pkg/ring/api.go @@ -70,7 +70,7 @@ type CameraKind string // CameraData contains common fields for all camera types type CameraData struct { - ID float64 `json:"id"` + ID int `json:"id"` Description string `json:"description"` DeviceID string `json:"device_id"` Kind string `json:"kind"` diff --git a/pkg/ring/client.go b/pkg/ring/client.go index 18244a39..ccd17743 100644 --- a/pkg/ring/client.go +++ b/pkg/ring/client.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "net/url" + "strconv" "sync" "time" @@ -19,7 +20,7 @@ type Client struct { api *RingRestClient ws *websocket.Conn prod core.Producer - camera *CameraData + cameraID int dialogID string sessionID string wsMutex sync.Mutex @@ -109,6 +110,7 @@ func Dial(rawURL string) (*Client, error) { query := u.Query() encodedToken := query.Get("refresh_token") + cameraID := query.Get("camera_id") deviceID := query.Get("device_id") _, isSnapshot := query["snapshot"] @@ -116,6 +118,11 @@ func Dial(rawURL string) (*Client, error) { return nil, errors.New("ring: wrong query") } + camID, err := strconv.Atoi(cameraID) + if err != nil { + return nil, fmt.Errorf("ring: invalid camera_id: %w", err) + } + // URL-decode the refresh token refreshToken, err := url.QueryUnescape(encodedToken) if err != nil { @@ -128,34 +135,17 @@ func Dial(rawURL string) (*Client, error) { return nil, err } - // Get camera details - devices, err := ringAPI.FetchRingDevices() - if err != nil { - return nil, err - } - - var camera *CameraData - for _, cam := range devices.AllCameras { - if fmt.Sprint(cam.DeviceID) == deviceID { - camera = &cam - break - } - } - if camera == nil { - return nil, errors.New("ring: camera not found") - } - // Create base client client := &Client{ api: ringAPI, - camera: camera, + cameraID: camID, dialogID: uuid.NewString(), done: make(chan struct{}), } // Check if snapshot request if isSnapshot { - client.prod = NewSnapshotProducer(ringAPI, camera) + client.prod = NewSnapshotProducer(ringAPI, cameraID) return client, nil } @@ -365,7 +355,7 @@ func (c *Client) startMessageLoop(connState *core.Waiter) { // check if the message is from the correct doorbot doorbotID := res.Body["doorbot_id"].(float64) - if doorbotID != float64(c.camera.ID) { + if int(doorbotID) != c.cameraID { continue } @@ -459,7 +449,7 @@ func (c *Client) sendSessionMessage(method string, body map[string]interface{}) body = make(map[string]interface{}) } - body["doorbot_id"] = c.camera.ID + body["doorbot_id"] = c.cameraID if c.sessionID != "" { body["session_id"] = c.sessionID } diff --git a/pkg/ring/snapshot.go b/pkg/ring/snapshot.go index f64e4f79..6d1a97bf 100644 --- a/pkg/ring/snapshot.go +++ b/pkg/ring/snapshot.go @@ -11,10 +11,10 @@ type SnapshotProducer struct { core.Connection client *RingRestClient - camera *CameraData + cameraID string } -func NewSnapshotProducer(client *RingRestClient, camera *CameraData) *SnapshotProducer { +func NewSnapshotProducer(client *RingRestClient, cameraID string) *SnapshotProducer { return &SnapshotProducer{ Connection: core.Connection{ ID: core.NewID(), @@ -36,13 +36,13 @@ func NewSnapshotProducer(client *RingRestClient, camera *CameraData) *SnapshotPr }, }, client: client, - camera: camera, + cameraID: cameraID, } } func (p *SnapshotProducer) Start() error { // Fetch snapshot - response, err := p.client.Request("GET", fmt.Sprintf("https://app-snaps.ring.com/snapshots/next/%d", int(p.camera.ID)), nil) + response, err := p.client.Request("GET", fmt.Sprintf("https://app-snaps.ring.com/snapshots/next/%s", p.cameraID), nil) if err != nil { return err }