forked from dgreif/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.ts
243 lines (219 loc) · 7.81 KB
/
camera.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { hap } from './hap'
import { RingPlatformConfig } from './config'
import { RingCamera } from '../api'
import { BaseDataAccessory } from './base-data-accessory'
import { map, mapTo, switchMap } from 'rxjs/operators'
import { CameraSource } from './camera-source'
import { Logging, PlatformAccessory } from 'homebridge'
import { merge } from 'rxjs'
export class Camera extends BaseDataAccessory<RingCamera> {
private inHomeDoorbellStatus: boolean | undefined
private cameraSource = new CameraSource(this.device)
constructor(
public readonly device: RingCamera,
public readonly accessory: PlatformAccessory,
public readonly logger: Logging,
public readonly config: RingPlatformConfig
) {
super()
if (!hap.CameraController) {
const error =
'HAP CameraController not found. Please make sure you are on homebridge version 1.0.0 or newer'
logger.error(error)
throw new Error(error)
}
const { Characteristic, Service } = hap,
{ ChargingState, StatusLowBattery } = Characteristic
accessory.configureController(this.cameraSource.controller)
this.registerCharacteristic({
characteristicType: Characteristic.Mute,
serviceType: Service.Microphone,
getValue: () => false,
})
this.registerCharacteristic({
characteristicType: Characteristic.Mute,
serviceType: Service.Speaker,
getValue: () => false,
})
if (!config.hideCameraMotionSensor) {
this.registerObservableCharacteristic({
characteristicType: Characteristic.MotionDetected,
serviceType: Service.MotionSensor,
onValue: device.onMotionDetected.pipe(
switchMap((motion) => {
if (!motion) {
return Promise.resolve(false)
}
return this.loadSnapshotForEvent('Detected Motion', true)
})
),
})
}
if (device.isDoorbot) {
const onDoorbellPressed = device.onDoorbellPressed.pipe(
mapTo('Doorbell Pressed')
),
onEventDescription = config.sendDoorbellMotionNotificationsToTv
? merge(
onDoorbellPressed,
device.onMotionStarted.pipe(
mapTo('Motion Detected - Simulating Doorbell Press')
)
)
: onDoorbellPressed
this.registerObservableCharacteristic({
characteristicType: Characteristic.ProgrammableSwitchEvent,
serviceType: Service.Doorbell,
onValue: onEventDescription.pipe(
switchMap((eventDescription) => {
return this.loadSnapshotForEvent(
eventDescription,
Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS
)
})
),
})
if (!config.hideDoorbellSwitch) {
this.registerObservableCharacteristic({
characteristicType: Characteristic.ProgrammableSwitchEvent,
serviceType: Service.StatelessProgrammableSwitch,
onValue: device.onDoorbellPressed.pipe(
mapTo(Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS)
),
})
// Hide long and double press events by setting max value
this.getService(Service.StatelessProgrammableSwitch)
.getCharacteristic(Characteristic.ProgrammableSwitchEvent)
.setProps({
maxValue: Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS,
})
}
} else if (config.sendCameraMotionNotificationsToTv) {
// allow standalone cameras to act as a doorbell press when motion is detected
// this allows tvOS 14 notifications to show camera motion alerts
this.registerObservableCharacteristic({
characteristicType: Characteristic.ProgrammableSwitchEvent,
serviceType: Service.Doorbell,
onValue: device.onMotionStarted.pipe(
switchMap(() => {
return this.loadSnapshotForEvent(
'Motion Detected - Simulating Doorbell Press',
Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS
)
})
),
})
}
if (device.hasLight) {
this.registerCharacteristic({
characteristicType: Characteristic.On,
serviceType: Service.Lightbulb,
getValue: (data) => data.led_status === 'on',
setValue: (value) => device.setLight(value),
requestUpdate: () => device.requestUpdate(),
})
}
if (device.hasSiren && !config.hideCameraSirenSwitch) {
this.registerCharacteristic({
characteristicType: Characteristic.On,
serviceType: Service.Switch,
serviceSubType: 'Siren',
name: device.name + ' Siren',
getValue: (data) => {
return Boolean(
data.siren_status && data.siren_status.seconds_remaining
)
},
setValue: (value) => device.setSiren(value),
requestUpdate: () => device.requestUpdate(),
})
}
if (device.hasInHomeDoorbell && !config.hideInHomeDoorbellSwitch) {
this.device.onInHomeDoorbellStatus.subscribe(
(data: boolean | undefined) => {
this.inHomeDoorbellStatus = data
}
)
this.registerObservableCharacteristic({
characteristicType: Characteristic.On,
serviceType: Service.Switch,
serviceSubType: 'In-Home Doorbell',
name: device.name + ' In-Home Doorbell',
onValue: device.onInHomeDoorbellStatus,
setValue: (value) => device.setInHomeDoorbell(value),
requestUpdate: () => device.requestUpdate(),
})
}
this.registerCharacteristic({
characteristicType: Characteristic.Manufacturer,
serviceType: Service.AccessoryInformation,
getValue: () => 'Ring',
})
this.registerCharacteristic({
characteristicType: Characteristic.Model,
serviceType: Service.AccessoryInformation,
getValue: (data) => `${device.model} (${data.kind})`,
})
this.registerCharacteristic({
characteristicType: Characteristic.SerialNumber,
serviceType: Service.AccessoryInformation,
getValue: (data) => data.device_id,
})
if (
device.hasBattery ||
// detected as having battery in the past
accessory.getService(Service.BatteryService)
) {
this.registerCharacteristic({
characteristicType: Characteristic.StatusLowBattery,
serviceType: Service.BatteryService,
getValue: () => {
return device.hasLowBattery
? StatusLowBattery.BATTERY_LEVEL_LOW
: StatusLowBattery.BATTERY_LEVEL_NORMAL
},
})
this.registerCharacteristic({
characteristicType: Characteristic.ChargingState,
serviceType: Service.BatteryService,
getValue: (data) => {
return data.external_connection
? ChargingState.CHARGING
: ChargingState.NOT_CHARGING
},
})
this.registerObservableCharacteristic({
characteristicType: Characteristic.BatteryLevel,
serviceType: Service.BatteryService,
onValue: device.onBatteryLevel.pipe(
map((batteryLevel) => {
return batteryLevel === null ? 100 : batteryLevel
})
),
})
}
}
private async loadSnapshotForEvent<T>(
eventDescription: string,
characteristicValue: T
) {
if (this.device.operatingOnBattery) {
// battery cameras cannot fetch a new snapshot while recording is in progress
this.logger.info(this.device.name + ' ' + eventDescription)
return characteristicValue
}
this.logger.info(
this.device.name +
` ${eventDescription}. Loading snapshot before sending event to HomeKit`
)
try {
await this.cameraSource.loadSnapshot()
} catch (e) {
this.logger.info(
this.device.name +
' Failed to load snapshot. Sending event to HomeKit without new snapshot'
)
}
return characteristicValue
}
}