ring: skip refetching cameras to increase loading speed and refactor ring url
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/AlexxIT/go2rtc/internal/api"
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
"github.com/AlexxIT/go2rtc/internal/streams"
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
@@ -83,6 +85,7 @@ func apiRing(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
var items []*api.Source
|
var items []*api.Source
|
||||||
for _, camera := range devices.AllCameras {
|
for _, camera := range devices.AllCameras {
|
||||||
|
cleanQuery.Set("camera_id", fmt.Sprint(camera.ID))
|
||||||
cleanQuery.Set("device_id", camera.DeviceID)
|
cleanQuery.Set("device_id", camera.DeviceID)
|
||||||
|
|
||||||
// Stream source
|
// Stream source
|
||||||
|
|||||||
+1
-1
@@ -70,7 +70,7 @@ type CameraKind string
|
|||||||
|
|
||||||
// CameraData contains common fields for all camera types
|
// CameraData contains common fields for all camera types
|
||||||
type CameraData struct {
|
type CameraData struct {
|
||||||
ID float64 `json:"id"`
|
ID int `json:"id"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
DeviceID string `json:"device_id"`
|
DeviceID string `json:"device_id"`
|
||||||
Kind string `json:"kind"`
|
Kind string `json:"kind"`
|
||||||
|
|||||||
+12
-22
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -19,7 +20,7 @@ type Client struct {
|
|||||||
api *RingRestClient
|
api *RingRestClient
|
||||||
ws *websocket.Conn
|
ws *websocket.Conn
|
||||||
prod core.Producer
|
prod core.Producer
|
||||||
camera *CameraData
|
cameraID int
|
||||||
dialogID string
|
dialogID string
|
||||||
sessionID string
|
sessionID string
|
||||||
wsMutex sync.Mutex
|
wsMutex sync.Mutex
|
||||||
@@ -109,6 +110,7 @@ func Dial(rawURL string) (*Client, error) {
|
|||||||
|
|
||||||
query := u.Query()
|
query := u.Query()
|
||||||
encodedToken := query.Get("refresh_token")
|
encodedToken := query.Get("refresh_token")
|
||||||
|
cameraID := query.Get("camera_id")
|
||||||
deviceID := query.Get("device_id")
|
deviceID := query.Get("device_id")
|
||||||
_, isSnapshot := query["snapshot"]
|
_, isSnapshot := query["snapshot"]
|
||||||
|
|
||||||
@@ -116,6 +118,11 @@ func Dial(rawURL string) (*Client, error) {
|
|||||||
return nil, errors.New("ring: wrong query")
|
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
|
// URL-decode the refresh token
|
||||||
refreshToken, err := url.QueryUnescape(encodedToken)
|
refreshToken, err := url.QueryUnescape(encodedToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -128,34 +135,17 @@ func Dial(rawURL string) (*Client, error) {
|
|||||||
return nil, err
|
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
|
// Create base client
|
||||||
client := &Client{
|
client := &Client{
|
||||||
api: ringAPI,
|
api: ringAPI,
|
||||||
camera: camera,
|
cameraID: camID,
|
||||||
dialogID: uuid.NewString(),
|
dialogID: uuid.NewString(),
|
||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if snapshot request
|
// Check if snapshot request
|
||||||
if isSnapshot {
|
if isSnapshot {
|
||||||
client.prod = NewSnapshotProducer(ringAPI, camera)
|
client.prod = NewSnapshotProducer(ringAPI, cameraID)
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -365,7 +355,7 @@ func (c *Client) startMessageLoop(connState *core.Waiter) {
|
|||||||
|
|
||||||
// check if the message is from the correct doorbot
|
// check if the message is from the correct doorbot
|
||||||
doorbotID := res.Body["doorbot_id"].(float64)
|
doorbotID := res.Body["doorbot_id"].(float64)
|
||||||
if doorbotID != float64(c.camera.ID) {
|
if int(doorbotID) != c.cameraID {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -459,7 +449,7 @@ func (c *Client) sendSessionMessage(method string, body map[string]interface{})
|
|||||||
body = make(map[string]interface{})
|
body = make(map[string]interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
body["doorbot_id"] = c.camera.ID
|
body["doorbot_id"] = c.cameraID
|
||||||
if c.sessionID != "" {
|
if c.sessionID != "" {
|
||||||
body["session_id"] = c.sessionID
|
body["session_id"] = c.sessionID
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,10 +11,10 @@ type SnapshotProducer struct {
|
|||||||
core.Connection
|
core.Connection
|
||||||
|
|
||||||
client *RingRestClient
|
client *RingRestClient
|
||||||
camera *CameraData
|
cameraID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSnapshotProducer(client *RingRestClient, camera *CameraData) *SnapshotProducer {
|
func NewSnapshotProducer(client *RingRestClient, cameraID string) *SnapshotProducer {
|
||||||
return &SnapshotProducer{
|
return &SnapshotProducer{
|
||||||
Connection: core.Connection{
|
Connection: core.Connection{
|
||||||
ID: core.NewID(),
|
ID: core.NewID(),
|
||||||
@@ -36,13 +36,13 @@ func NewSnapshotProducer(client *RingRestClient, camera *CameraData) *SnapshotPr
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
client: client,
|
client: client,
|
||||||
camera: camera,
|
cameraID: cameraID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *SnapshotProducer) Start() error {
|
func (p *SnapshotProducer) Start() error {
|
||||||
// Fetch snapshot
|
// 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user