Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh support #1225

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thick-bananas-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ring-client-api': patch
---

Adds the `refresh()` method to the RingApi class, which allows locations and devices to be reloaded cleanly. Additionally adds `onNewNotification` subject to the `RingApi` allowing a client to set up a single subscription for all push notifications without needing to resubscribe after a refresh.
49 changes: 37 additions & 12 deletions packages/ring-client-api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export interface RingApiOptions extends SessionOptions {
export class RingApi extends Subscribed {
public readonly restClient
public readonly onRefreshTokenUpdated
onNewNotification = new Subject<{
device: RingCamera | RingIntercom
notification: PushNotification
}>()

constructor(public readonly options: RingApiOptions & RefreshTokenAuth) {
super()
Expand Down Expand Up @@ -217,30 +221,44 @@ export class RingApi extends Subscribed {
}
}

pushReceiver: PushReceiver | undefined
devicesById: { [id: number]: RingCamera | RingIntercom | undefined } = {}

private async registerPushReceiver(
cameras: RingCamera[],
intercoms: RingIntercom[]
) {
const credentials =
this.restClient._internalOnly_pushNotificationCredentials,
pushReceiver = new PushReceiver({
credentials,
logLevel: 'NONE',
senderId: '876313859327', // for Ring android app. 703521446232 for ring-site
}),
devicesById: { [id: number]: RingCamera | RingIntercom | undefined } = {},
sendToDevice = (id: number, notification: PushNotification) => {
devicesById[id]?.processPushNotification(notification)
const sendToDevice = (id: number, notification: PushNotification) => {
const device = this.devicesById[id]
if (device) {
this.onNewNotification.next({ device, notification })
device.processPushNotification(notification)
}
},
onPushNotificationToken = new Subject<string>()

this.devicesById = {}
for (const camera of cameras) {
devicesById[camera.id] = camera
this.devicesById[camera.id] = camera
}
for (const intercom of intercoms) {
devicesById[intercom.id] = intercom
this.devicesById[intercom.id] = intercom
}

if (this.pushReceiver) {
return
}

const credentials =
this.restClient._internalOnly_pushNotificationCredentials,
pushReceiver = new PushReceiver({
credentials,
logLevel: 'NONE',
senderId: '876313859327', // for Ring android app. 703521446232 for ring-site
})

this.pushReceiver = pushReceiver

pushReceiver.onCredentialsChanged(({ newCredentials }) => {
// Store the new credentials in the rest client so that it can be used for subsequent restarts
this.restClient._internalOnly_pushNotificationCredentials = newCredentials
Expand Down Expand Up @@ -446,4 +464,11 @@ export class RingApi extends Subscribed {
this.restClient.clearTimeouts()
clearTimeouts()
}

refresh() {
this.disconnect()
if (this.locationsPromise) {
this.locationsPromise = this.fetchAndBuildLocations()
}
}
}