Skip to content

Commit

Permalink
Better handling of general push notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
dgreif committed Aug 4, 2024
1 parent c8aab5a commit 1b93b06
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 31 deletions.
6 changes: 6 additions & 0 deletions .changeset/sour-mirrors-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'homebridge-ring': patch
'ring-client-api': patch
---

Better handling of general push notifications. This avoids extraneous logs for events we don't know about or don't care about.
57 changes: 33 additions & 24 deletions packages/ring-client-api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
ProfileResponse,
PushNotification,
PushNotificationAction,
PushNotificationDingV2,
RingDeviceType,
ThirdPartyGarageDoorOpener,
UnknownDevice,
Expand All @@ -31,7 +30,7 @@ import {
switchMap,
throttleTime,
} from 'rxjs/operators'
import { clearTimeouts, enableDebug, logError, logInfo } from './util'
import { clearTimeouts, enableDebug, logDebug, logError, logInfo } from './util'
import { setFfmpegPath } from './ffmpeg'
import { Subscribed } from './subscribed'
import PushReceiver from '@eneris/push-receiver'
Expand Down Expand Up @@ -246,7 +245,7 @@ export class RingApi extends Subscribed {
debug: false,
}),
devicesById: { [id: number]: RingCamera | RingIntercom | undefined } = {},
sendToDevice = (id: number, notification: PushNotificationDingV2) => {
sendToDevice = (id: number, notification: PushNotification) => {
devicesById[id]?.processPushNotification(notification)
},
onPushNotificationToken = new Subject<string>()
Expand Down Expand Up @@ -328,31 +327,41 @@ export class RingApi extends Subscribed {

const notification = messageData as PushNotification

if (!('android_config' in notification)) {
// This is not a v2 ding style notification, so we can't process it
logInfo('Received push notification in unknown format')
logInfo(JSON.stringify(message))
return
}

const deviceId = notification.data?.device?.id
if ('android_config' in notification) {
const deviceId = notification.data?.device?.id

if (deviceId) {
sendToDevice(deviceId, notification)
}
if (deviceId) {
sendToDevice(deviceId, notification)
}

const eventCategory = notification.android_config.category
const eventCategory = notification.android_config.category

if (
eventCategory !== PushNotificationAction.Ding &&
eventCategory !== PushNotificationAction.Motion &&
eventCategory !== PushNotificationAction.IntercomDing
if (
eventCategory !== PushNotificationAction.Ding &&
eventCategory !== PushNotificationAction.Motion &&
eventCategory !== PushNotificationAction.IntercomDing
) {
logDebug(
'Received v2 push notification with unknown category: ' +
eventCategory,
)
logDebug(JSON.stringify(message))
}
} else if (
'data' in notification &&
'gcmData' in notification.data &&
'alarm_meta' in notification.data.gcmData
) {
logInfo(
'Received push notification with unknown category: ' +
eventCategory,
)
logInfo(JSON.stringify(message))
const deviceId = notification.data.gcmData.alarm_meta.device_zid

if (deviceId) {
sendToDevice(deviceId, notification)
}
} else {
// This is not a v1 or v2 style notification, so we can't process it
logDebug('Received push notification in unknown format')
logDebug(JSON.stringify(message))
return
}
} catch (e) {
logError(e)
Expand Down
9 changes: 7 additions & 2 deletions packages/ring-client-api/ring-camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
VideoSearchResponse,
OnvifCameraData,
RingCameraKind,
PushNotification,
} from './ring-types'
import { appApi, clientApi, deviceApi, RingRestClient } from './rest-client'
import { BehaviorSubject, firstValueFrom, ReplaySubject, Subject } from 'rxjs'
Expand Down Expand Up @@ -422,8 +423,12 @@ export class RingCamera extends Subscribed {
this.onActiveNotifications.next(otherDings)
}

processPushNotification(notification: PushNotificationDingV2) {
if (!('ding' in notification.data?.event)) {
processPushNotification(notification: PushNotification) {
if (
!('android_config' in notification) ||
!('event' in notification.data) ||
!('ding' in notification.data?.event)
) {
// only process ding/motion notifications
return
}
Expand Down
7 changes: 2 additions & 5 deletions packages/ring-client-api/ring-intercom.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
IntercomHandsetAudioData,
PushNotification,
PushNotificationAction,
PushNotificationDingV2,
PushNotificationIntercomUnlockV2,
} from './ring-types'
import { clientApi, commandsApi, RingRestClient } from './rest-client'
import { BehaviorSubject, Subject } from 'rxjs'
Expand Down Expand Up @@ -104,9 +103,7 @@ export class RingIntercom {
})
}

processPushNotification(
notification: PushNotificationDingV2 | PushNotificationIntercomUnlockV2,
) {
processPushNotification(notification: PushNotification) {
if (
'android_config' in notification &&
notification.android_config.category ===
Expand Down

0 comments on commit 1b93b06

Please sign in to comment.