generated from bitfocus/companion-module-template-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
118 lines (103 loc) · 2.54 KB
/
main.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
const { InstanceBase, Regex, runEntrypoint, InstanceStatus, UDPHelper } = require('@companion-module/base')
const UpgradeScripts = require('./upgrades')
const UpdateActions = require('./actions')
const UpdatePresets = require('./presets.js')
class ModuleInstance extends InstanceBase {
constructor(internal) {
super(internal)
}
//initial module loading
async init(config) {
this.config = config
await this.init_udp()
this.updateStatus(InstanceStatus.Ok)
this.updateActions() // export actions
this.updatePresets()
}
init_udp() {
if (this.udp) {
this.udp.destroy()
delete this.udp
}
this.udp = new UDPHelper(this.config.host, this.config.port)
this.udp.on("error", (err) => {
this.log("error", "Network error: " + err.message);
})
this.udp.on('data', (data) => {
this.updateStatus('ok')
let hexString = data.toString('hex')
if (hexString.length == 80) {
//this is the main settings information
this.updateData(Uint8Array.from(data))
}
//console.log(hexString)
})
}
sendCommand(payload){
var newPayload = payload.replace(/ /g, "")
if(this.config.isviscaoverip){
var viscaIPheader = '010000'
var payloadLength = (newPayload.length / 2).toString(16).padStart(2, "0");
newPayload = viscaIPheader + payloadLength + '00000000' + newPayload
}
var cmdBuff = Buffer.from(newPayload, 'hex')
//console.log(cmdBuff)
this.udp.send(cmdBuff)
//.then(function(response){console.log(response)})
//.catch((error) => console.log('error', `Error with command. ${error}`))
}
// When module gets deleted
async destroy() {
this.log('debug', 'destroy')
if (this.udp) {
this.udp.destroy();
delete this.udp;
}
}
//update to module configuration settings
async configUpdated(config) {
this.config = config
await this.init_udp()
this.updatePresets()
}
// Return config fields for web config
getConfigFields() {
return [
{
type: 'static-text',
id: 'info',
width: 12,
label: 'Information',
value: 'This module controls PTZ cameras with VISCA over IP protocol'
},
{
type: 'textinput',
id: 'host',
label: 'Target IP',
width: 8,
regex: Regex.IP,
},
{
type: 'textinput',
id: 'port',
label: 'Target Port',
width: 4,
default: '52381',
regex: Regex.PORT,
},
{
id: 'isviscaoverip',
type: 'checkbox',
label: 'VISCA over IP',
default: true
},
]
}
updateActions() {
UpdateActions(this)
}
updatePresets(){
UpdatePresets(this)
}
}
runEntrypoint(ModuleInstance, UpgradeScripts)