-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
797 lines (712 loc) · 22.7 KB
/
index.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
import { InstanceBase, runEntrypoint, UDPHelper } from '@companion-module/base'
import { getActions } from './actions.js'
import { getPresets } from './presets.js'
import { updateVariableDefinitions, updateVariables } from './variables.js'
import { getFeedbacks } from './feedbacks.js'
import { upgradeScripts } from './upgrades.js'
import { addStringToBinary, strToPQRS, getModelQueries } from './utils.js'
import { VISCA } from './constants.js'
import CHOICES from './choices.js'
import { MODEL_QUERIES, MODEL_SPECS } from './models.js'
import fetch from 'node-fetch'
import WebSocket from 'ws'
class BirdDogPTZInstance extends InstanceBase {
constructor(internal) {
super(internal)
}
async init(config) {
this.updateStatus('connecting')
this.config = config
this.port = 52381 // Visca port
this.addStringToBinary = addStringToBinary
this.strToPQRS = strToPQRS
this.updateVariables = updateVariables
// Keep track of setInterval
this.timers = {
pollCameraConfig: null, // ID of setInterval for Camera Config polling
pollCameraStatus: null, // ID of setInterval for Camera Status polling
}
// Get Initial Camera Info
this.getCameraModel()
}
async destroy() {
// Clear open connections
if (this.udp !== undefined) {
this.udp.destroy()
}
if (this.ws !== undefined) {
this.ws.close(1000)
delete this.ws
}
// Clear polling timers
if (this.timers.pollCameraStatus !== undefined) {
clearInterval(this.timers.pollCameraStatus)
}
if (this.timers.pollCameraConfig) {
clearInterval(this.timers.pollCameraConfig)
this.timers.pollCameraConfig = null
}
if (this.timers.ws_reconnect) {
clearInterval(this.timers.ws_reconnect)
}
}
getConfigFields() {
return [
{
type: 'static-text',
id: 'info',
width: 12,
label: 'Information',
value: 'This module controls BirdDog PTZ Cameras.',
},
{
type: 'textinput',
id: 'host',
label: 'Device IP',
width: 6,
regex: this.REGEX_IP,
},
{
type: 'dropdown',
id: 'model',
label: 'BirdDog Model',
default: 'Auto',
choices: CHOICES.CAMERAS,
},
]
}
async configUpdated(config) {
this.config = config
this.updateStatus('connecting')
if (this.config.host !== undefined) {
this.log('debug', '----Config Model Choice:- ' + this.config.model)
this.getCameraModel()
} else {
this.updateStatus(
'bad_config',
'Unable to connect, please enter an IP address for your camera in the module settings'
)
}
}
initVariables() {
updateVariableDefinitions.bind(this)()
}
initFeedbacks() {
const feedbacks = getFeedbacks.bind(this)()
this.setFeedbackDefinitions(feedbacks)
}
initPresets() {
const presets = getPresets.bind(this)()
this.setPresetDefinitions(presets)
}
initActions() {
const actions = getActions.bind(this)()
this.setActionDefinitions(actions)
}
sendCommand(cmd, type, params) {
let url = `http://${this.config.host}:8080/${cmd}`
let options = {
method: type,
headers: { 'Content-Type': 'application/json' },
}
if (type == 'PUT' || type == 'POST') {
options.body = params != undefined ? JSON.stringify(params) : null
}
fetch(url, options)
.then((res) => {
if (res.status == 200) {
this.updateStatus('ok')
return res.json()
}
})
.then((json) => {
let data = json
if (data && type == 'GET') {
this.processData(decodeURI(url), data)
} else if ((data && type == 'PUT') || type == 'POST') {
} else {
if (!url.match('birddogptzsetup')) {
//Suppress this log: this failure is due to a BirdDog firmware bug in v5.5.114
this.log('debug', `Command failed ${url}`)
}
}
})
.catch((err) => {
this.log('debug', `Command Error: ${err}`)
let errorText = String(err)
if (
errorText.match('ECONNREFUSED') ||
errorText.match('ENOTFOUND') ||
errorText.match('EHOSTDOWN') ||
errorText.match('ETIMEDOUT')
) {
this.updateStatus('connection_failure')
//this.log('error', `Connection lost to ${this.camera?.HostName ? this.camera.HostName : 'BirdDog PTZ camera'}`)
}
})
}
processData(cmd, data) {
let changed
switch (cmd.slice(cmd.lastIndexOf('/') + 1)) {
case 'about':
changed = this.storeState(data, 'about')
//this.camera.about = data
break
case 'analogaudiosetup':
changed = this.storeState(data, 'analogaudiosetup')
//this.camera.audio = data
break
case 'devicesettings':
changed = this.storeState(data, 'devicesettings')
//this.camera.devicesettings = data
break
case 'videooutputinterface':
changed = this.storeState(data, 'videooutputinterface')
//this.camera.video = data
break
case 'encodesetup':
changed = this.storeState(data, 'encodesetup')
let match = data.VideoFormat.match(/\d+\D(\S*)/) // match the framerate
if (this.camera?.shutter_table) {
switch (match[1]) {
// If the current stored framerate doesn't match the camera framerate, change the stored framerate and repopulate actions
case '23.98':
case '24':
if (!(this.camera.shutter_table === '24')) {
this.camera.shutter_table = '24'
this.initActions()
}
break
case '25':
case '50':
if (!(this.camera.shutter_table === '50')) {
this.camera.shutter_table = '50'
this.initActions()
}
break
default:
if (!(this.camera.shutter_table === '60')) {
this.camera.shutter_table = '60'
this.initActions()
}
break
}
}
if (this.camera?.framerate) {
this.camera.framerate = match[1]
}
//this.camera.encode = data
break
case 'encodetransport':
changed = this.storeState(data, 'encodetransport')
//this.camera.transport = data
break
case 'NDIDisServer':
changed = this.storeState(data, 'NDIDisServer')
//this.camera.ndiserver = data
break
case 'birddogptzsetup':
let originalSpeedState = this.camera?.speedControl
changed = this.storeState(data, 'birddogptzsetup')
if (!originalSpeedState || originalSpeedState !== data.SpeedControl) {
this.initActions()
this.initFeedbacks()
}
//this.camera.ptz = data
break
case 'birddogexpsetup':
changed = this.storeState(data, 'birddogexpsetup')
if (changed.includes('gain_limit')) {
// rebuild actions as GainLimit has changed
this.log('debug', '-----Gain Limit changed')
this.initActions()
this.initFeedbacks()
}
if (changed.includes('shutter_max_speed')) {
// rebuild actions as Shutter Max speed has changed
this.log('debug', '-----ShutterMaxSpeed changed')
this.initActions()
this.initFeedbacks()
}
if (changed.includes('shutter_min_speed')) {
// rebuild actions as Shutter Min speed has changed
this.log('debug', '-----ShutterMinSpeed changed')
this.initActions()
this.initFeedbacks()
}
//this.camera.expsetup = data
break
case 'birddogwbsetup':
changed = this.storeState(data, 'birddogwbsetup')
//this.camera.wbsetup = data
break
case 'birddogpicsetup':
changed = this.storeState(data, 'birddogpicsetup')
//this.camera.picsetup = data
break
case 'birddogcmsetup':
changed = this.storeState(data, 'birddogcmsetup')
//this.camera.cmsetup = data
break
case 'birddogadvancesetup':
changed = this.storeState(data, 'birddogadvancesetup')
//this.camera.advancesetup = data
break
case 'birddogexternalsetup':
changed = this.storeState(data, 'birddogexternalsetup')
//this.camera.externalsetup = data
break
case 'birddogdetsetup':
changed = this.storeState(data, 'birddogdetsetup')
//this.camera.detsetup = data
break
case 'birddoggammasetup':
changed = this.storeState(data, 'birddoggammasetup')
//this.camera.gammasetup = data
break
case 'birddogscope':
changed = this.storeState(data, 'birddogscope')
//this.camera.birddogscope = data
break
}
if (changed.length > 0) {
this.updateVariables()
this.checkFeedbacks()
}
}
sendVISCACommand(payload, counter) {
let buf = Buffer.alloc(32)
// 0x01 0x00 = VISCA Command
buf[0] = 0x01
buf[1] = 0x10
this.packet_counter = (this.packet_counter + 1) % 0xffffffff
buf.writeUInt16BE(payload.length, 2)
buf.writeUInt32BE(this.packet_counter, 4)
if (typeof payload == 'string') {
buf.write(payload, 8, 'binary')
} else if (typeof payload == 'object' && payload instanceof Buffer) {
payload.copy(buf, 8)
}
if (typeof counter == 'string') {
buf.write(counter, 7, 'binary')
} else if (typeof counter == 'object' && counter instanceof Buffer) {
counter.copy(buf, 7)
}
let newbuf = buf.subarray(0, 8 + payload.length)
//this.log('debug', '-----Sending VISCA message: ' + Buffer.from(newbuf, 'binary').toString('hex'))
this.udp.send(newbuf)
}
incomingData(data) {
let changed = null
//this.log('debug', '-----Incoming VISCA message: ' + Buffer.from(data, 'binary').toString('hex'))
if (data[8] == 0x90 && data[9] == 0x50 && data[10] == 0x17 && data[27] == 0xff && data.length == 28) {
let newPanVal = data[11].toString(16) + data[12].toString(16) + data[13].toString(16) + data[14].toString(16)
let newTiltVal = data[15].toString(16) + data[16].toString(16) + data[17].toString(16) + data[18].toString(16)
let newZoomVal = data[19].toString(16) + data[20].toString(16) + data[21].toString(16) + data[22].toString(16)
if (this.camera.pan_position !== newPanVal) {
changed = true
this.camera.pan_position = newPanVal
}
if (this.camera.tilt_position !== newTiltVal) {
changed = true
this.camera.tilt_position = newTiltVal
}
if (this.camera.zoom_position !== newZoomVal) {
changed = true
this.camera.zoom_position = newZoomVal
}
}
if (data[8] == 0x90 && data[9] == 0x50 && data[10] == 0x15 && data[23] == 0xff && data.length == 24) {
let newFocusVal
if (data[20] == 0x02) {
newFocusVal = 'Auto'
} else {
newFocusVal = 'Manual'
}
if (this.camera.focusM !== newFocusVal) {
changed = true
this.camera.focusM = newFocusVal
}
let newStandbyVal
if (data[21] == 0x02) {
newStandbyVal = 'on'
} else {
newStandbyVal = 'standby'
}
if (this.camera.standby !== newStandbyVal) {
changed = true
this.camera.standby = newStandbyVal
}
let newFreezeVal
if (data[22] == 0x02) {
newFreezeVal = 'On'
} else {
newFreezeVal = 'Off'
}
if (this.camera.freeze !== newFreezeVal) {
changed = true
this.camera.freeze = newFreezeVal
}
}
if (changed) {
this.updateVariables()
this.checkFeedbacks()
}
}
sendControlCommand(payload) {
let buf = Buffer.alloc(32)
// 0x01 0x00 = VISCA Command
buf[0] = 0x02
buf[1] = 0x00
this.packet_counter = (this.packet_counter + 1) % 0xffffffff
buf.writeUInt16BE(payload.length, 2)
buf.writeUInt32BE(this.packet_counter, 4)
if (typeof payload == 'string') {
buf.write(payload, 8, 'binary')
} else if (typeof payload == 'object' && payload instanceof Buffer) {
payload.copy(buf, 8)
}
let newbuf = buf.subarray(0, 8 + payload.length)
this.udp.send(newbuf)
}
init_udp() {
this.log('debug', '----init udp')
if (this.udp !== undefined) {
this.udp.destroy()
delete this.udp
}
if (this.timers.pollCameraStatus !== undefined) {
clearInterval(this.timers.pollCameraStatus)
}
if (this.config.host !== undefined) {
this.udp = new UDPHelper(this.config.host, this.port)
// Reset sequence number
this.sendControlCommand('\x01')
this.packet_counter = 0
this.startPolling()
this.udp.on('status_change', (status, message) => {
//this.updateStatus('unknown_error', message)
})
this.udp.on('data', (data) => {
this.incomingData(data)
})
this.udp.on('error', (error) => {
this.log('debug', '----UDP Error: ' + error)
})
this.log('debug', this.udp.host, ':', this.port)
}
}
init_ws_listener() {
this.log('debug', '----init websocket')
clearInterval(this.timers.ws_reconnect)
if (this.ws !== undefined) {
this.ws.close(1000)
delete this.ws
}
this.ws = new WebSocket(`ws://${this.config.host}:6790/`)
this.ws.on('open', () => {
this.log('debug', `WebSocket connection opened to ${this.camera.hostname}`)
})
this.ws.on('close', (code) => {
this.log('debug', `---- WebSocket Connection closed with code ${code}`)
if (code !== 1000) {
this.timers.ws_reconnect = setInterval(this.init_ws_listener.bind(this), 500)
}
})
this.ws.on('message', (message) => {
let data
try {
data = JSON.parse(message.toString())
this.storeState(data, 'WebSocket')
} catch (e) {
this.log('debug', 'JSON Error:' + e)
}
//this.log('debug', `---- WebSocket received: ${JSON.stringify(data)}`)
})
this.ws.on('error', (data) => {
this.log('debug', `WebSocket error: ${data}`)
})
}
// Poll for BirdDog camera configuration/status
startPolling() {
this.stopPolling()
//Immediately do the poll
this.pollCameraConfig()
this.pollCameraStatus()
// Repeat the poll at set intervals
this.timers.pollCameraConfig = setInterval(this.pollCameraConfig.bind(this), 10000) // No need to poll frequently
this.timers.pollCameraStatus = setInterval(this.pollCameraStatus.bind(this), 1000) // This will be used to get status of the camera
}
stopPolling() {
if (this.timers.pollCameraConfig) {
clearInterval(this.timers.pollCameraConfig)
this.timers.pollCameraConfig = null
}
if (this.timers.pollCameraStatus) {
clearInterval(this.timers.pollCameraStatus)
this.timers.pollCameraStatus = null
}
}
// Get Camera configuration
pollCameraConfig() {
let MODEL_QRY = getModelQueries(MODEL_QUERIES, this.camera.model, this.camera.firmware.major)
if (MODEL_QRY?.about) {
this.sendCommand('about', 'GET')
}
if (MODEL_QRY?.encodesetup) {
this.sendCommand('encodesetup', 'GET')
}
if (MODEL_QRY?.analogaudiosetup) {
this.sendCommand('analogaudiosetup', 'GET')
}
if (MODEL_QRY?.devicesettings) {
this.sendCommand('NDIDisServer', 'GET')
}
if (MODEL_QRY?.videooutputinterface) {
this.sendCommand('videooutputinterface', 'GET')
}
if (MODEL_QRY?.encodetransport) {
this.sendCommand('encodetransport', 'GET')
}
if (MODEL_QRY?.NDIDisServer) {
this.sendCommand('NDIDisServer', 'GET')
}
}
// Get Camera Status
pollCameraStatus() {
let MODEL_QRY = getModelQueries(MODEL_QUERIES, this.camera.model, this.camera.firmware.major)
if (MODEL_QRY?.birddogptzsetup) {
this.sendCommand('birddogptzsetup', 'GET')
}
if (MODEL_QRY?.birddogexpsetup) {
this.sendCommand('birddogexpsetup', 'GET')
}
if (MODEL_QRY?.birddogwbsetup) {
this.sendCommand('birddogwbsetup', 'GET')
}
if (MODEL_QRY?.birddogpicsetup) {
this.sendCommand('birddogpicsetup', 'GET')
}
if (this.udp) {
//this.sendVISCACommand(VISCA.MSG_QRY + VISCA.CAM_POWER + VISCA.END_MSG, '\x4a') // Query Standby status
this.sendVISCACommand(VISCA.MSG_BLOCK_QRY + '\x15' + VISCA.END_MSG) // Query Camera Details
if (this.camera?.standby === 'on') {
//Sending this while camera is in standby makes it unable to wake-up
//this.sendVISCACommand(VISCA.MSG_QRY + VISCA.CAM_FOCUS_AUTO + VISCA.END_MSG, '\x5a') // Query Auto Focus Mode
//this.sendVISCACommand(VISCA.MSG_QRY + VISCA.CAM_FREEZE + VISCA.END_MSG, '\x5b') // Query Freeze
//this.sendVISCACommand(VISCA.MSG_QRY + VISCA.CAM_ZOOM_DIRECT + VISCA.END_MSG, '\x5c') // Query Zoom Position
//this.sendVISCACommand(VISCA.MSG_BLOCK_QRY + '\x15' + VISCA.END_MSG)
this.sendVISCACommand(VISCA.MSG_BLOCK_QRY + '\x17' + VISCA.END_MSG) // Query PTZ Position
}
if (MODEL_QRY?.pt_pos) {
//this.sendVISCACommand(VISCA.MSG_QRY_OPERATION + VISCA.OP_PAN_POS + VISCA.END_MSG, '\x5d') // Query Pan/Tilt Position
}
}
if (MODEL_QRY?.birddogcmsetup) {
this.sendCommand('birddogcmsetup', 'GET')
}
if (MODEL_QRY?.birddogadvancesetup) {
this.sendCommand('birddogadvancesetup', 'GET')
}
if (MODEL_QRY?.birddogexternalsetup) {
this.sendCommand('birddogexternalsetup', 'GET')
}
if (MODEL_QRY?.birddogdetsetup) {
this.sendCommand('birddogdetsetup', 'GET')
}
if (MODEL_QRY?.birddoggammasetup) {
this.sendCommand('birddoggammasetup', 'GET')
}
if (MODEL_QRY?.birddogscope) {
this.sendCommand('birddogscope', 'GET')
}
//this.log('debug', `----Camera Setup for - ${this.camera.model}`)
//this.log('debug', this.camera)
}
getCameraModel() {
if (this.config.model === 'Auto') {
let url = `http://${this.config.host}:8080/version`
let options = {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
}
fetch(url, options)
.then((res) => {
if (res.status == 200) {
//this.log('debug',res)
return res.text()
}
})
.then((data) => {
let model = data
if (model) {
model = model.replace(/BirdDog| |_/g, '')
this.getCameraFW(this.checkCameraModel(model))
} else if (!model) {
this.log('error', 'Please upgrade your BirdDog camera to the latest LTS firmware to use this module')
this.updateStatus('connection_failure')
if (this.timers.pollCameraStatus !== undefined) {
clearInterval(this.timers.pollCameraStatus)
}
}
})
.catch((err) => {
this.log('debug', `Get Cam Model Error ${err}`)
let errorText = String(err)
if (
errorText.match('ECONNREFUSED') ||
errorText.match('ENOTFOUND') ||
errorText.match('EHOSTDOWN') ||
errorText.match('ETIMEDOUT')
) {
this.updateStatus('connection_failure')
this.log('error', `Unable to connect to BirdDog PTZ Camera (Error: ${errorText?.split('reason:')[1]})`)
}
})
} else {
//this.camera.model = this.config.model
this.getCameraFW(this.config.model)
}
}
getCameraFW(model) {
let url = `http://${this.config.host}:8080/about`
let options = {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
}
fetch(url, options)
.then((res) => {
if (res.status == 200) {
//this.log('debug',res)
return res.json()
}
})
.then((data) => {
if (data.FirmwareVersion) {
let FW_major = data.FirmwareVersion.substring(data.FirmwareVersion.lastIndexOf(' ') + 1).substring(0, 1)
let FW_minor = data.FirmwareVersion.substring(data.FirmwareVersion.lastIndexOf(' ') + 2).substring(1)
// Set Initial State for Camera
this.intializeState(model, data.HostName, FW_major, FW_minor)
// InitializeCamera
this.initializeCamera()
} else if (data.Version === '1.0') {
this.log('error', 'Please upgrade your BirdDog camera to the latest LTS firmware to use this module')
this.updateStatus('connection_failure')
if (this.timers.pollCameraStatus !== undefined) {
clearInterval(this.timers.pollCameraStatus)
}
}
})
.catch((err) => {
this.log('debug', `Camera Firmware Error: ${err}`)
let errorText = String(err)
if (
errorText.match('ECONNREFUSED') ||
errorText.match('ENOTFOUND') ||
errorText.match('EHOSTDOWN') ||
errorText.match('ETIMEDOUT')
) {
this.updateStatus('connection_failure')
this.log('error', `Unable to connect to BirdDog PTZ Camera (Error: ${errorText?.split('reason:')[1]})`)
}
})
}
initializeCamera() {
// this.log('debug','---- in initializeCamera')
if (this.camera.firmware.major && this.camera.model) {
this.updateStatus('ok')
this.log('info', `Connected to ${this.camera.hostname}`)
this.log('debug', `---- Connected to ${this.camera.hostname}`)
this.initActions()
this.initPresets()
this.initVariables()
this.initFeedbacks()
this.startPolling()
this.init_udp()
if (this.camera.firmware.major === '5') {
this.init_ws_listener()
}
} else {
this.updateStatus('connection_failure')
this.log('error', `Unable to connect to ${this.camera.hostname}`)
}
}
checkCameraModel(detectedModel) {
//this.log('debug','---- In checkCameraModel with detectedModel as', detectedModel)
let model = CHOICES.CAMERAS.find((element) => {
// this.log('debug','---- Checking element ', element)
if (element.id === detectedModel) {
return detectedModel
} else if (element?.other) {
let tempArray = Object.entries(element)
return tempArray[2][1].includes(detectedModel)
} else {
// this.log('debug','---- Returning False for ', element)
return false
}
})
if (model) {
this.log('info', `Detected camera model: ${model.id}`)
this.log('debug', '---- Detected camera model: ' + model.id)
return model.id
} else {
this.log('error', `Unrecognized camera model: ${detectedModel}. Using "Default" camera profile`)
this.log('debug', `Unrecognized camera model: ${detectedModel}. Using "Default" camera profile`)
return 'Default'
}
}
intializeState(model, hostname, FW_major, FW_minor) {
// Take all level 1 elements from MODEL_SPECS filtered by;
// - All cameras or model matches
// - FW matches
// - 'store_state' is true
// and add them to this.camera object
this.camera = {}
let filteredArray = Object.entries(MODEL_SPECS).filter(
(array) =>
// filter array based on: All cameras or Model matches, and FW matches & has 'store_state' object
(array[1].camera.includes(model) || array[1].camera.includes('All')) &&
array[1].firmware.includes(FW_major) &&
array[1].store_state === true
)
Object.keys(Object.fromEntries(filteredArray))
.sort()
.map((element) => (this.camera[element] = {}))
// Set some defaults
this.camera.model = model
this.camera.hostname = hostname
this.camera.firmware = {}
this.camera.firmware.major = FW_major
this.camera.firmware.minor = FW_minor
this.camera.shutter_table = 60 // Camera defaults to 59.94 on startup
this.camera.unknown = [] // Array to store unknown API variables
//this.log('debug', '---- Initial State for camera', this.camera)
}
storeState(data, endpoint) {
// Returns an array of this.camera keys that have been changed
let changed = []
Object.entries(data).forEach((element) => {
let stored = Object.entries(MODEL_SPECS).find(
(array) =>
// find location in this.camera to store API variable
// based on: All cameras or Model matches, FW matches, api_endpoint matches & api_variable matches API element
(array[1].camera.includes(this.camera.model) || array[1].camera.includes('All')) &&
array[1].firmware.includes(this.camera.firmware.major) &&
array[1]?.api_endpoint?.includes(endpoint) &&
array[1]?.api_variable?.includes(element[0])
)
if (!stored) {
if (!this.camera.unknown.includes(element[0])) {
//Only warn about unknown API variables once
this.log('debug', `Unknown API variable returned from ${endpoint}: ${element[0]}`)
this.camera.unknown.push(element[0])
}
} else if (this.camera[stored[0]] !== element[1]) {
changed.push(stored[0])
this.camera[stored[0]] = element[1]
}
})
return changed
}
}
runEntrypoint(BirdDogPTZInstance, upgradeScripts)