ring: skip refetching cameras to increase loading speed and refactor ring url

This commit is contained in:
seydx
2025-05-08 16:09:04 +02:00
parent 7107508286
commit 124556f4db
4 changed files with 20 additions and 27 deletions
+3
View File
@@ -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
+1 -1
View File
@@ -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"`
+12 -22
View File
@@ -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
}
+4 -4
View File
@@ -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
}