forked from tsightler/ring-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.js
1385 lines (1271 loc) · 59 KB
/
camera.js
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import RingPolledDevice from './base-polled-device.js'
import utils from '../lib/utils.js'
import pathToFfmpeg from 'ffmpeg-for-homebridge'
import { Worker } from 'worker_threads'
import { spawn } from 'child_process'
import { parseISO, addSeconds } from 'date-fns';
import chalk from 'chalk'
export default class Camera extends RingPolledDevice {
constructor(deviceInfo, events) {
super(deviceInfo, 'camera')
const savedState = this.getSavedState()
this.hasBattery1 = Boolean(this.device.data.hasOwnProperty('battery_voltage'))
this.hasBattery2 = Boolean(this.device.data.hasOwnProperty('battery_voltage_2'))
this.hevcEnabled = this.device.data?.settings?.video_settings?.hevc_enabled
? this.device.data.settings.video_settings.hevc_enabled
: false
this.data = {
motion: {
active_ding: false,
duration: savedState?.motion?.duration ? savedState.motion.duration : 180,
publishedDuration: false,
last_ding: 0,
last_ding_expires: 0,
last_ding_time: 'none',
is_person: false,
detection_enabled: null,
warning_enabled: null,
events: events.filter(event => event.event_type === 'motion'),
latestEventId: ''
},
...this.device.isDoorbot ? {
ding: {
active_ding: false,
duration: savedState?.ding?.duration ? savedState.ding.duration : 180,
publishedDurations: false,
last_ding: 0,
last_ding_expires: 0,
last_ding_time: 'none',
events: events.filter(event => event.event_type === 'ding'),
latestEventId: ''
}
} : {},
snapshot: {
mode: savedState?.snapshot?.mode
? savedState.snapshot.mode.replace(/(^\w{1})|(\s+\w{1})/g, letter => letter.toUpperCase())
: 'Auto',
ding: false,
motion: false,
interval: false,
autoInterval: savedState?.snapshot?.autoInterval
? savedState.snapshot.autoInterval
: true,
intervalDuration: savedState?.snapshot?.intervalDuration
? savedState.snapshot.intervalDuration
: (this.device.operatingOnBattery) ? 600 : 30,
intervalTimerId: null,
cache: null,
cacheType: null,
timestamp: null,
onDemandTimestamp: 0
},
stream: {
live: {
state: 'OFF',
status: 'inactive',
session: false,
publishedStatus: '',
worker: new Worker('./devices/camera-livestream.js', {
workerData: {
doorbotId: this.device.id,
deviceName: this.deviceData.name
}
})
},
event: {
state: 'OFF',
status: 'inactive',
session: false,
publishedStatus: ''
},
keepalive:{
active: false,
session: false,
expires: 0
}
},
event_select: {
state: savedState?.event_select?.state
? savedState.event_select.state
: 'Motion 1',
publishedState: null,
pollCycle: 0,
recordingUrl: null,
recordingUrlExpire: null,
transcoded: false,
eventId: '0'
},
...this.device.hasLight ? {
light: {
state: null,
setTime: Math.floor(Date.now()/1000)
}
} : {},
...this.device.hasSiren ? {
siren: {
state: null
}
} : {}
}
this.entity = {
...this.entity,
motion: {
component: 'binary_sensor',
device_class: 'motion',
attributes: true
},
stream: {
component: 'switch',
attributes: true,
name: 'Live Stream',
icon: 'mdi:cctv',
// Use internal MQTT server for inter-process communications
ipc: true
},
event_stream: {
component: 'switch',
attributes: true,
icon: 'mdi:vhs',
// Use internal MQTT server for inter-process communications
ipc: true
},
event_select: {
component: 'select',
options: [
...this.device.isDoorbot
? [ 'Ding 1', 'Ding 2', 'Ding 3', 'Ding 4', 'Ding 5',
'Ding 1 (Transcoded)', 'Ding 2 (Transcoded)', 'Ding 3 (Transcoded)',
'Ding 4 (Transcoded)', 'Ding 5 (Transcoded)'
]
: [],
'Motion 1', 'Motion 2', 'Motion 3', 'Motion 4', 'Motion 5',
'Motion 1 (Transcoded)', 'Motion 2 (Transcoded)', 'Motion 3 (Transcoded)',
'Motion 4 (Transcoded)', 'Motion 5 (Transcoded)',
'Person 1', 'Person 2', 'Person 3', 'Person 4', 'Person 5',
'Person 1 (Transcoded)', 'Person 2 (Transcoded)', 'Person 3 (Transcoded)',
'Person 4 (Transcoded)', 'Person 5 (Transcoded)',
'On-demand 1', 'On-demand 2', 'On-demand 3', 'On-demand 4', 'On-demand 5',
'On-demand 1 (Transcoded)', 'On-demand 2 (Transcoded)', 'On-demand 3 (Transcoded)',
'On-demand 4 (Transcoded)', 'On-demand 5 (Transcoded)',
],
attributes: true
},
...this.device.isDoorbot ? {
ding: {
component: 'binary_sensor',
device_class: 'occupancy',
attributes: true,
icon: 'mdi:doorbell-video'
}
} : {},
...this.device.hasLight ? {
light: {
component: 'light'
}
} : {},
...this.device.hasSiren ? {
siren: {
component: 'switch',
icon: 'mdi:alarm-light'
}
} : {},
snapshot: {
component: 'camera',
attributes: true
},
snapshot_mode: {
component: 'select',
options: [
...this.device.isDoorbot
? [
'All', 'Auto', 'Ding', 'Interval', 'Interval + Ding',
'Interval + Motion', 'Motion', 'Motion + Ding', 'Disabled'
]
: [ 'All', 'Auto', 'Interval', 'Motion', 'Disabled' ]
]
},
snapshot_interval: {
component: 'number',
min: 10,
max: 604800,
mode: 'box',
icon: 'hass:timer'
},
take_snapshot: {
component: 'button',
icon: 'mdi:camera'
},
motion_detection: {
component: 'switch'
},
...this.device.data.features?.motion_message_enabled ? {
motion_warning: {
component: 'switch'
}
} : {},
motion_duration: {
component: 'number',
min: 10,
max: 180,
mode: 'box',
icon: 'hass:timer'
},
...this.device.isDoorbot ? {
ding_duration: {
component: 'number',
min: 10,
max: 180,
icon: 'hass:timer'
}
} : {},
info: {
component: 'sensor',
device_class: 'timestamp',
value_template: '{{ value_json["lastUpdate"] | default("") }}'
}
}
this.data.stream.live.worker.on('message', (message) => {
if (message.type === 'state') {
switch (message.data) {
case 'active':
this.data.stream.live.status = 'active'
this.data.stream.live.session = true
break;
case 'inactive':
this.data.stream.live.status = 'inactive'
this.data.stream.live.session = false
break;
case 'failed':
this.data.stream.live.status = 'failed'
this.data.stream.live.session = false
break;
}
this.publishStreamState()
} else {
switch (message.type) {
case 'log_info':
this.debug(message.data, 'wrtc')
break;
case 'log_error':
this.debug(chalk.redBright(message.data), 'wrtc')
break;
}
}
})
this.device.onNewNotification.subscribe(notification => {
this.processNotification(notification)
})
this.updateSnapshotMode()
this.scheduleSnapshotRefresh()
this.updateDeviceState()
}
updateDeviceState() {
const stateData = {
snapshot: {
mode: this.data.snapshot.mode,
autoInterval: this.data.snapshot.autoInterval,
interval: this.data.snapshot.intervalDuration
},
event_select: {
state: this.data.event_select.state
},
motion: {
duration: this.data.motion.duration
},
...this.device.isDoorbot ? {
ding: {
duration: this.data.ding.duration
}
} : {}
}
this.setSavedState(stateData)
}
// Build standard and optional entities for device
async initAttributeEntities() {
// If device is wireless publish signal strength entity
const deviceHealth = await this.device.getHealth()
if (deviceHealth && !(deviceHealth?.network_connection && deviceHealth.network_connection === 'ethernet')) {
this.entity.wireless = {
component: 'sensor',
device_class: 'signal_strength',
unit_of_measurement: 'dBm',
parent_state_topic: 'info/state',
attributes: 'wireless',
value_template: '{{ value_json["wirelessSignal"] | default("") }}'
}
}
// If device is battery powered publish battery entity
if (this.device.batteryLevel || this.hasBattery1 || this.hasBattery2) {
this.entity.battery = {
component: 'sensor',
device_class: 'battery',
unit_of_measurement: '%',
state_class: 'measurement',
parent_state_topic: 'info/state',
attributes: 'battery',
value_template: '{{ value_json["batteryLevel"] | default("") }}'
}
}
// If no motion events in device event cache, request recent motion events
if (this.data.motion.events.length === 0) {
const response = await this.getDeviceHistory({limit: 5, event_types: 'motion'})
if (Array.isArray(response?.items) && response.items.length > 0) {
this.data.motion.events = response.items
}
}
if (this.data.motion.events.length > 0) {
const lastMotionEvent = this.data.motion.events[0]
const lastMotionDate = lastMotionEvent?.start_time ? new Date(lastMotionEvent.start_time) : false
this.data.motion.last_ding = lastMotionDate ? Math.floor(lastMotionDate/1000) : 0
this.data.motion.last_ding_time = lastMotionDate ? utils.getISOTime(lastMotionDate) : ''
this.data.motion.is_person = Boolean(lastMotionEvent?.cv?.person_detected)
this.data.motion.latestEventId = lastMotionEvent.event_id
// Try to get URL for most recent motion event, if it fails, assume there's no subscription
let recordingUrl = false
const recordingEvent = this.data.motion.events.find(e => e.recording_status === 'ready')
if (recordingEvent && Array.isArray(recordingEvent.visualizations?.cloud_media_visualization?.media)) {
recordingUrl = (recordingEvent.visualizations.cloud_media_visualization.media.find(e => e.file_type === 'VIDEO'))?.url
}
if (!recordingUrl) {
this.debug('Could not retrieve recording URL for any motion event, assuming no Ring Protect subscription')
delete this.entity.event_stream
delete this.entity.event_select
}
} else {
this.debug('Unable to retrieve most recent motion event for this camera')
}
// Get most recent ding event data
if (this.device.isDoorbot) {
// If no ding events in device event cache, request recent ding events
if (this.data.ding.events.length === 0) {
const response = await this.getDeviceHistory({limit: 5, event_types: 'ding'})
if (Array.isArray(response?.items) && response.items.length > 0) {
this.data.ding.events = response.items
}
}
if (this.data.ding.events.length > 0) {
const lastDingEvent = this.data.ding.events[0]
const lastDingDate = lastDingEvent?.start_time ? new Date(lastDingEvent.start_time) : false
this.data.ding.last_ding = lastDingDate ? Math.floor(lastDingDate/1000) : 0
this.data.ding.last_ding_time = lastDingDate ? utils.getISOTime(lastDingDate) : ''
this.data.ding.latestEventId = lastDingEvent.event_id
} else {
this.debug('Unable to retrieve most recent ding event for this doorbell')
}
}
let stillImageUrlBase = 'localhost'
let streamSourceUrlBase
if (process.env.RUNMODE === 'addon') {
// For the addon we get some values populated from the startup script
// that queries the HA API via bashio
stillImageUrlBase = process.env.HAHOSTNAME
streamSourceUrlBase = process.env.ADDONHOSTNAME
} else if (process.env.RUNMODE === 'docker') {
// For docker we don't have any API to query so we just use the IP of the docker container
// since it probably doesn't have a DNS entry
streamSourceUrlBase = await utils.getHostIp()
} else {
// For the stadalone install we try to get the host FQDN
streamSourceUrlBase = await utils.getHostFqdn()
}
// Set some helper attributes for streaming
this.data.stream.live.stillImageURL = `https://${stillImageUrlBase}:8123{{ states.camera.${this.device.name.toLowerCase().replace(" ","_")}_snapshot.attributes.entity_picture }}`,
this.data.stream.live.streamSource = (utils.config().livestream_user && utils.config().livestream_pass)
? `rtsp://${utils.config().livestream_user}:${utils.config().livestream_pass}@${streamSourceUrlBase}:8554/${this.deviceId}_live`
: `rtsp://${streamSourceUrlBase}:8554/${this.deviceId}_live`
}
updateSnapshotMode() {
this.data.snapshot.ding = Boolean(this.device.isDoorbot && this.data.snapshot.mode.match(/(ding|^all|auto$)/i))
this.data.snapshot.motion = Boolean(this.data.snapshot.mode.match(/(motion|^all|auto$)/i))
this.data.snapshot.interval = this.data.snapshot.mode === 'Auto'
? Boolean(!this.device.operatingOnBattery)
: Boolean(this.data.snapshot.mode.match(/(interval|^all$)/i))
if (this.data.snapshot.interval && this.data.snapshot.autoInterval) {
// If interval snapshots are enabled but interval is not manually set, try to detect a reasonable defaults
if (this.device.operatingOnBattery) {
if (this.device.data.settings.lite_24x7?.enabled) {
this.data.snapshot.intervalDuration = this.device.data.settings.lite_24x7.frequency_secs
} else {
this.data.snapshot.intervalDuration = 600
}
} else {
// For wired cameras default to 30 seconds
this.data.snapshot.intervalDuration = 30
}
}
}
// Publish camera capabilities and state and subscribe to events
async publishState(data) {
const isPublish = Boolean(data === undefined)
this.publishPolledState(isPublish)
// Checks for new events or expired recording URL every 3 polling cycles (~1 minute)
if (this.entity.hasOwnProperty('event_select')) {
this.data.event_select.pollCycle--
if (this.data.event_select.pollCycle <= 0) {
this.data.event_select.pollCycle = 3
if (await this.updateEventStreamUrl() && !isPublish) {
this.publishEventSelectState()
}
}
}
if (isPublish) {
// Publish stream state
this.publishStreamState(isPublish)
if (this.entity.event_select) {
this.publishEventSelectState(isPublish)
}
this.publishDingStates()
this.publishDingDurationState(isPublish)
this.publishSnapshotMode()
if (this.data.snapshot.motion || this.data.snapshot.ding || this.data.snapshot.interval) {
this.data.snapshot.cache ? this.publishSnapshot() : this.refreshSnapshot('interval')
this.publishSnapshotInterval(isPublish)
}
this.publishAttributes()
}
// Check for subscription to ding and motion events and attempt to resubscribe
if (this.device.isDoorbot && !this.device.data.subscribed === true) {
this.debug('Camera lost subscription to ding events, attempting to resubscribe...')
this.device.subscribeToDingEvents().catch(e => {
this.debug('Failed to resubscribe camera to ding events. Will retry in 60 seconds.')
this.debug(e)
})
}
if (!this.device.data.subscribed_motions === true) {
this.debug('Camera lost subscription to motion events, attempting to resubscribe...')
this.device.subscribeToMotionEvents().catch(e => {
this.debug('Failed to resubscribe camera to motion events. Will retry in 60 seconds.')
this.debug(e)
})
}
}
// Process a ding event
async processNotification(pushData) {
let dingKind
// Is it a motion or doorbell ding? (for others we do nothing)
switch (pushData.action) {
case 'com.ring.push.HANDLE_NEW_DING':
dingKind = 'ding'
break
case 'com.ring.push.HANDLE_NEW_motion':
dingKind = 'motion'
break
default:
this.debug(`Received push notification of unknown type ${pushData.action}`)
return
}
const ding = pushData.ding
ding.created_at = Math.floor(Date.now()/1000)
this.debug(`Received ${dingKind} push notification, expires in ${this.data[dingKind].duration} seconds`)
// Is this a new Ding or refresh of active ding?
const newDing = Boolean(!this.data[dingKind].active_ding)
this.data[dingKind].active_ding = true
// Update last_ding and expire time
this.data[dingKind].last_ding = ding.created_at
this.data[dingKind].last_ding_time = utils.getISOTime(ding.created_at*1000)
this.data[dingKind].last_ding_expires = this.data[dingKind].last_ding+this.data[dingKind].duration
// If motion ding and snapshots on motion are enabled, publish a new snapshot
if (dingKind === 'motion') {
this.data[dingKind].is_person = Boolean(ding.detection_type === 'human')
if (this.data.snapshot.motion) {
this.refreshSnapshot('motion', ding.image_uuid)
}
} else if (this.data.snapshot.ding) {
// If doorbell press and snapshots on ding are enabled, publish a new snapshot
this.refreshSnapshot('ding', ding.image_uuid)
}
// Publish MQTT active sensor state
// Will republish to MQTT for new dings even if ding is already active
this.publishDingState(dingKind)
// If new ding, begin expiration loop (only needed for first ding as others just extend time)
if (newDing) {
// Loop until current time is > last_ding expires time. Sleeps until
// estimated expire time, but may loop if new dings increase last_ding_expires
while (Math.floor(Date.now()/1000) < this.data[dingKind].last_ding_expires) {
const sleeptime = (this.data[dingKind].last_ding_expires - Math.floor(Date.now()/1000)) + 1
await utils.sleep(sleeptime)
}
// All dings have expired, set ding state back to false/off and publish
this.debug(`All ${dingKind} dings for camera have expired`)
this.data[dingKind].active_ding = false
this.publishDingState(dingKind)
}
}
// Publishes all current ding states for this camera
publishDingStates() {
this.publishDingState('motion')
if (this.device.isDoorbot) {
this.publishDingState('ding')
}
}
// Publish ding state and attributes
publishDingState(dingKind) {
const dingState = this.data[dingKind].active_ding ? 'ON' : 'OFF'
this.mqttPublish(this.entity[dingKind].state_topic, dingState)
if (dingKind === 'motion') {
this.publishMotionAttributes()
} else {
this.publishDingAttributes()
}
}
publishMotionAttributes() {
const attributes = {
lastMotion: this.data.motion.last_ding,
lastMotionTime: this.data.motion.last_ding_time,
personDetected: this.data.motion.is_person
}
if (this.device.data.settings && typeof this.device.data.settings.motion_detection_enabled !== 'undefined') {
this.data.motion.detection_enabled = this.device.data.settings.motion_detection_enabled
attributes.motionDetectionEnabled = this.data.motion.detection_enabled
}
this.mqttPublish(this.entity.motion.json_attributes_topic, JSON.stringify(attributes), 'attr')
}
publishDingAttributes() {
const attributes = {
lastDing: this.data.ding.last_ding,
lastDingTime: this.data.ding.last_ding_time
}
this.mqttPublish(this.entity.ding.json_attributes_topic, JSON.stringify(attributes), 'attr')
}
// Publish camera state for polled attributes (light/siren state, etc)
// Writes state to custom property to keep from publishing state except
// when values change from previous polling interval
publishPolledState(isPublish) {
if (this.device.hasLight) {
const lightState = this.device.data.led_status === 'on' ? 'ON' : 'OFF'
if ((lightState !== this.data.light.state && Date.now()/1000 - this.data.light.setTime > 30) || isPublish) {
this.data.light.state = lightState
this.mqttPublish(this.entity.light.state_topic, this.data.light.state)
}
}
if (this.device.hasSiren) {
const sirenState = this.device.data.siren_status.seconds_remaining > 0 ? 'ON' : 'OFF'
if (sirenState !== this.data.siren.state || isPublish) {
this.data.siren.state = sirenState
this.mqttPublish(this.entity.siren.state_topic, this.data.siren.state)
}
}
// Publish motion switch settings and attributes
if (this.device.data.settings.motion_detection_enabled !== this.data.motion.detection_enabled || isPublish) {
this.publishMotionAttributes()
this.mqttPublish(this.entity.motion_detection.state_topic, this.device.data?.settings?.motion_detection_enabled ? 'ON' : 'OFF')
}
if (this.entity.hasOwnProperty('motion_warning') && (this.device.data.settings.motion_announcement !== this.data.motion.warning_enabled || isPublish)) {
this.mqttPublish(this.entity.motion_warning.state_topic, this.device.data.settings.motion_announcement ? 'ON' : 'OFF')
this.data.motion.warning_enabled = this.device.data.settings.motion_announcement
}
}
// Publish device data to info topic
async publishAttributes() {
const attributes = {
stream_Source: this.data.stream.live.streamSource,
still_Image_URL: this.data.stream.live.stillImageURL
}
const deviceHealth = await this.device.getHealth()
if (this.device.batteryLevel || this.hasBattery1 || this.hasBattery2) {
if (deviceHealth && deviceHealth.hasOwnProperty('active_battery')) {
attributes.activeBattery = deviceHealth.active_battery
}
// Reports the level of the currently active battery, might be null if removed so report 0% in that case
attributes.batteryLevel = this.device.batteryLevel && utils.isNumeric(this.device.batteryLevel)
? this.device.batteryLevel
: 0
// Must have at least one battery, but it might not be inserted, so report 0% in that case
attributes.batteryLife = this.device.data.hasOwnProperty('battery_life') && utils.isNumeric(this.device.data.battery_life)
? Number.parseFloat(this.device.data.battery_life)
: 0
if (this.hasBattery2) {
attributes.batteryLife2 = this.device.data.hasOwnProperty('battery_life_2') && utils.isNumeric(this.device.data.battery_life_2)
? Number.parseFloat(this.device.data.battery_life_2)
: 0
}
}
if (deviceHealth) {
attributes.firmwareStatus = deviceHealth.firmware
attributes.lastUpdate = deviceHealth.updated_at.slice(0,-6)+"Z"
if (deviceHealth.hasOwnProperty('network_connection') && deviceHealth.network_connection === 'ethernet') {
attributes.wiredNetwork = this.device.data.alerts.connection
} else {
attributes.wirelessNetwork = deviceHealth.wifi_name
attributes.wirelessSignal = deviceHealth.latest_signal_strength
}
}
if (Object.keys(attributes).length > 0) {
this.mqttPublish(this.entity.info.state_topic, JSON.stringify(attributes), 'attr')
this.publishAttributeEntities(attributes)
}
}
publishSnapshotInterval(isPublish) {
if (isPublish) {
this.mqttPublish(this.entity.snapshot_interval.state_topic, this.data.snapshot.intervalDuration.toString())
} else {
// Update snapshot frequency in case it's changed
if (this.data.snapshot.autoInterval && this.data.snapshot.intervalDuration !== this.device.data.settings.lite_24x7.frequency_secs) {
this.data.snapshot.intervalDuration = this.device.data.settings.lite_24x7.frequency_secs
clearInterval(this.data.snapshot.intervalTimerId)
this.scheduleSnapshotRefresh()
}
this.mqttPublish(this.entity.snapshot_interval.state_topic, this.data.snapshot.intervalDuration.toString())
}
}
publishSnapshotMode() {
this.mqttPublish(this.entity.snapshot_mode.state_topic, this.data.snapshot.mode)
}
publishStreamState(isPublish) {
['live', 'event'].forEach(type => {
const entityProp = (type === 'live') ? 'stream' : `${type}_stream`
if (this.entity.hasOwnProperty(entityProp)) {
const streamState = (this.data.stream[type].status === 'active' || this.data.stream[type].status === 'activating') ? 'ON' : 'OFF'
if (streamState !== this.data.stream[type].state || isPublish) {
this.data.stream[type].state = streamState
this.mqttPublish(this.entity[entityProp].state_topic, this.data.stream[type].state)
// Publish state to IPC broker as well
utils.event.emit('mqtt_ipc_publish', this.entity[entityProp].state_topic, this.data.stream[type].state)
}
if (this.data.stream[type].publishedStatus !== this.data.stream[type].status || isPublish) {
this.data.stream[type].publishedStatus = this.data.stream[type].status
const attributes = { status: this.data.stream[type].status }
this.mqttPublish(this.entity[entityProp].json_attributes_topic, JSON.stringify(attributes), 'attr')
// Publish attribute state to IPC broker as well
utils.event.emit('mqtt_ipc_publish', this.entity[entityProp].json_attributes_topic, JSON.stringify(attributes))
}
}
})
}
publishEventSelectState(isPublish) {
if (this.data.event_select.state !== this.data.event_select.publishedState || isPublish) {
this.data.event_select.publishedState = this.data.event_select.state
this.mqttPublish(this.entity.event_select.state_topic, this.data.event_select.state)
}
const attributes = {
recordingUrl: this.data.event_select.recordingUrl,
eventId: this.data.event_select.eventId
}
this.mqttPublish(this.entity.event_select.json_attributes_topic, JSON.stringify(attributes), 'attr', '<recording_url_masked>')
}
publishDingDurationState(isPublish) {
const dingTypes = this.device.isDoorbot ? [ 'ding', 'motion' ] : [ 'motion' ]
dingTypes.forEach(dingType => {
if (this.data[dingType].duration !== this.data[dingType].publishedDuration || isPublish) {
this.mqttPublish(this.entity[`${dingType}_duration`].state_topic, this.data[dingType].duration)
this.data[dingType].publishedDuration = this.data[dingType].duration
}
})
}
// Publish snapshot image/metadata
publishSnapshot() {
this.mqttPublish(this.entity.snapshot.topic, this.data.snapshot.cache, 'mqtt', '<binary_image_data>')
const attributes = {
timestamp: this.data.snapshot.timestamp,
type: this.data.snapshot.cacheType
}
this.mqttPublish(this.entity.snapshot.json_attributes_topic, JSON.stringify(attributes), 'attr')
}
// Refresh snapshot on scheduled interval
scheduleSnapshotRefresh() {
this.data.snapshot.intervalTimerId = setInterval(() => {
if (this.isOnline() && this.data.snapshot.interval && !(this.data.snapshot.motion && this.data.motion.active_ding)) {
this.refreshSnapshot('interval')
}
}, this.data.snapshot.intervalDuration * 1000)
}
async refreshSnapshot(type, image_uuid) {
let newSnapshot = false
let loop = 3
if (this.device.snapshotsAreBlocked) {
this.debug('Snapshots are unavailable, check if motion capture is disabled manually or via modes settings')
return
}
while (!newSnapshot && loop > 0) {
try {
switch (type) {
case 'interval':
case 'on-demand':
this.debug(`Requesting an updated ${type} snapshot`)
newSnapshot = await this.device.getNextSnapshot({ force: true })
break;
case 'motion':
case 'ding':
if (image_uuid) {
this.debug(`Requesting ${type} snapshot using notification image UUID: ${image_uuid}`)
newSnapshot = await this.device.getNextSnapshot({ uuid: image_uuid })
} else if (!this.device.operatingOnBattery) {
this.debug(`Requesting an updated ${type} snapshot`)
newSnapshot = await this.device.getNextSnapshot({ force: true })
} else {
this.debug(`The ${type} notification did not contain image UUID and battery cameras are unable to snapshot while recording`)
loop = 0 // Don't retry in this case
}
break;
}
} catch (err) {
this.debug(err)
if (loop > 1) {
this.debug(`Failed to retrieve updated ${type} snapshot, retrying in one second...`)
await utils.sleep(1)
} else {
this.debug(`Failed to retrieve updated ${type} snapshot after three attempts, aborting`)
}
}
loop--
}
if (newSnapshot) {
this.debug(`Successfully retrieved updated ${type} snapshot`)
this.data.snapshot.cache = newSnapshot
this.data.snapshot.cacheType = type
this.data.snapshot.timestamp = Math.round(Date.now()/1000)
this.publishSnapshot()
}
}
async startLiveStream(rtspPublishUrl) {
this.data.stream.live.session = true
const streamData = {
rtspPublishUrl,
ticket: null
}
try {
this.debug('Acquiring a live stream WebRTC signaling session ticket')
const response = await this.device.restClient.request({
method: 'POST',
url: 'https://app.ring.com/api/v1/clap/ticket/request/signalsocket'
})
streamData.ticket = response.ticket
} catch(error) {
if (error?.response?.statusCode === 403) {
this.debug(`Camera returned 403 when starting a live stream. This usually indicates that live streaming is blocked by Modes settings. Check your Ring app and verify that you are able to stream from this camera with the current Modes settings.`)
} else {
this.debug(error)
}
}
if (streamData.ticket) {
this.debug('Live stream WebRTC signaling session ticket acquired, starting live stream worker')
this.data.stream.live.worker.postMessage({ command: 'start', streamData })
} else {
this.debug('Live stream failed to initialize WebRTC signaling session')
this.data.stream.live.status = 'failed'
this.data.stream.live.session = false
this.publishStreamState()
}
}
async startEventStream(rtspPublishUrl) {
const eventSelect = this.data.event_select.state.split(' ')
const eventType = eventSelect[0].toLowerCase().replace('-', '_')
const eventNumber = eventSelect[1]
if (this.data.event_select.recordingUrl.match(/Recording Not Found|Transcoding in Progress/)) {
this.debug(`No recording available for the ${(eventNumber==1?"":eventNumber==2?"2nd ":eventNumber==3?"3rd ":eventNumber+"th ")}most recent ${eventType} event!`)
this.data.stream.event.status = 'failed'
this.data.stream.event.session = false
this.publishStreamState()
return
}
this.debug(`Streaming the ${(eventNumber==1?"":eventNumber==2?"2nd ":eventNumber==3?"3rd ":eventNumber+"th ")}most recently recorded ${eventType} event`)
try {
if (this.data.event_select.transcoded || this.hevcEnabled) {
// If camera is in HEVC mode, recordings are also in HEVC so transcode the video back to H.264/AVC on the fly
// Ring videos transcoded for download are not optimized for RTSP streaming (limited keyframes) so they must
// also be re-transcoded on-the-fly to allow streamers to join early
this.data.stream.event.session = spawn(pathToFfmpeg, [
'-re',
'-i', this.data.event_select.recordingUrl,
'-map', '0:v',
'-map', '0:a',
'-map', '0:a',
'-c:v', 'libx264',
'-g', '20',
'-keyint_min', '10',
'-crf', '23',
'-preset', 'ultrafast',
'-c:a:0', 'copy',
'-c:a:1', 'libopus',
'-flags', '+global_header',
'-rtsp_transport', 'tcp',
'-f', 'rtsp',
rtspPublishUrl
])
} else {
this.data.stream.event.session = spawn(pathToFfmpeg, [
'-re',
'-i', this.data.event_select.recordingUrl,
'-map', '0:v',
'-map', '0:a',
'-map', '0:a',
'-c:v', 'copy',
'-c:a:0', 'copy',
'-c:a:1', 'libopus',
'-flags', '+global_header',
'-rtsp_transport', 'tcp',
'-f', 'rtsp',
rtspPublishUrl
])
}
this.data.stream.event.session.on('spawn', async () => {
this.debug(`The recorded ${eventType} event stream has started`)
this.data.stream.event.status = 'active'
this.publishStreamState()
})
this.data.stream.event.session.on('close', async () => {
this.debug(`The recorded ${eventType} event stream has ended`)
this.data.stream.event.status = 'inactive'
this.data.stream.event.session = false
this.publishStreamState()
})
} catch(e) {
this.debug(e)
this.data.stream.event.status = 'failed'
this.data.stream.event.session = false
this.publishStreamState()
}
}
async startKeepaliveStream() {
const duration = 86400
if (this.data.stream.keepalive.active) { return }
this.data.stream.keepalive.active = true
const rtspPublishUrl = (utils.config().livestream_user && utils.config().livestream_pass)
? `rtsp://${utils.config().livestream_user}:${utils.config().livestream_pass}@localhost:8554/${this.deviceId}_live`
: `rtsp://localhost:8554/${this.deviceId}_live`
this.debug(`Starting a keepalive stream for camera`)
// Keepalive stream is used only when the live stream is started
// manually. It copies only the audio stream to null output just to
// trigger rtsp server to start the on-demand stream and keep it running
// when there are no other RTSP readers.
this.data.stream.keepalive.session = spawn(pathToFfmpeg, [
'-i', rtspPublishUrl,
'-map', '0:a:0',
'-c:a', 'copy',
'-f', 'null',
'/dev/null'
])
this.data.stream.keepalive.session.on('spawn', async () => {
this.debug(`The keepalive stream has started`)
})
this.data.stream.keepalive.session.on('close', async () => {
this.data.stream.keepalive.active = false
this.data.stream.keepalive.session = false
this.debug(`The keepalive stream has stopped`)
})
// The keepalive stream will time out after 24 hours
this.data.stream.keepalive.expires = Math.floor(Date.now()/1000) + duration
while (this.data.stream.keepalive.active && Math.floor(Date.now()/1000) < this.data.stream.keepalive.expires) {
await utils.sleep(60)
}
this.data.stream.keepalive.session.kill()
this.data.stream.keepalive.active = false
this.data.stream.keepalive.session = false
}
async updateEventStreamUrl() {
const eventSelect = this.data.event_select.state.split(' ')
const eventType = eventSelect[0].toLowerCase().replace('-', '_')
const eventNumber = eventSelect[1]
const transcoded = Boolean(eventSelect[2] === '(Transcoded)')
const urlExpired = this.data.event_select.recordingUrlExpire < Date.now()
let selectedEvent
let recordingUrl = false
try {
const events = await(this.getRecordedEvents(eventType, eventNumber))
if (events.length >= eventNumber) {
selectedEvent = events[eventNumber-1]
if (selectedEvent.event_id !== this.data.event_select.eventId || this.data.event_select.transcoded !== transcoded) {
if (this.data.event_select.recordingUrl) {
this.debug(`New ${this.data.event_select.state} event detected, updating the recording URL`)
}
recordingUrl = await this.getRecordingUrl(selectedEvent, transcoded)
} else if (urlExpired) {
this.debug(`Previous ${this.data.event_select.state} URL has expired, updating the recording URL`)
recordingUrl = await this.getRecordingUrl(selectedEvent, transcoded)
}
} else {
this.debug(`No event recording corresponding to ${this.data.event_select.state} was found in device event history`)
}
} catch(error) {
this.debug(error)
this.debug(`Failed to retrieve recording URL for ${this.data.event_select.state} event`)
}
if (recordingUrl) {
this.data.event_select.recordingUrl = recordingUrl
this.data.event_select.transcoded = transcoded
this.data.event_select.eventId = selectedEvent.event_id
try {
const urlSearch = new URLSearchParams(recordingUrl)
const amzExpires = Number(urlSearch.get('X-Amz-Expires'))
const amzDate = parseISO(urlSearch.get('X-Amz-Date'))
this.data.event_select.recordingUrlExpire = Date.parse(addSeconds(amzDate, amzExpires/3*2))
} catch {
this.data.event_select.recordingUrlExpire = Date.now() + 600000
}
} else if (urlExpired || !selectedEvent) {
this.data.event_select.recordingUrl = '<Recording Not Found>'
this.data.event_select.transcoded = transcoded
this.data.event_select.eventId = '0'
}
return recordingUrl
}
async getRecordedEvents(eventType, eventNumber) {
let events = []
let paginationKey = false
let loop = eventType === 'person' ? 4 : 1
try {
while (loop > 0) {
const history = await this.getDeviceHistory({
...paginationKey ? { pagination_key: paginationKey }: {},
event_types: eventType === 'person' ? 'motion' : eventType,
limit: eventType === 'person' ? 50 : eventNumber
})
if (Array.isArray(history.items) && history.items.length > 0) {