-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
107 lines (84 loc) · 2.32 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
// Arri Amira and Alexa Tally
import { InstanceBase, InstanceStatus, runEntrypoint, TCPHelper, UDPHelper } from '@companion-module/base'
import { ConfigFields } from './config.js'
import { updateActions } from './actions.js'
import { updatePresets } from './presets.js'
class ArriTally extends InstanceBase {
constructor(internal) {
super(internal)
this.updateActions = updateActions.bind(this)
this.updatePresets = updatePresets.bind(this)
}
async init(config) {
this.config = config
this.updateActions()
this.updatePresets()
await this.configUpdated(config)
}
async configUpdated(config) {
if (this.udp) {
this.udp.destroy()
delete this.udp
}
if (this.socket) {
this.socket.destroy()
delete this.socket
}
this.config = config
this.init_tcp()
this.init_tcp_variables()
}
async destroy() {
if (this.socket) {
this.socket.destroy()
} else if (this.udp) {
this.udp.destroy()
} else {
this.updateStatus(InstanceStatus.Disconnected)
}
}
getConfigFields() {
return ConfigFields
}
init_tcp() {
console.log('initTCP ' + this.config.host + ':' + this.config.port)
if (this.socket) {
this.socket.destroy()
delete this.socket
}
this.updateStatus(InstanceStatus.Connecting)
if (this.config.host) {
this.socket = new TCPHelper(this.config.host, this.config.port)
this.socket.on('status_change', (status, message) => {
this.updateStatus(status, message)
})
this.socket.on('error', (err) => {
this.updateStatus(InstanceStatus.ConnectionFailure, err.message)
this.log('error', 'Network error: ' + err.message)
})
this.socket.on('data', (data) => {
let dataResponse = data
dataResponse = data.toString()
this.log('debug', dataResponse)
this.setVariableValues({ tcp_response: dataResponse.replace(/(\r\n|\n|\r)/gm, " ") })
})
} else {
this.updateStatus(InstanceStatus.BadConfig)
}
}
sendCommand(cmd) {
if (cmd !== undefined) {
if (this.socket !== undefined && this.socket.isConnected) {
this.log('debug', 'sending: ' + cmd)
this.socket.send(cmd)
} else {
this.log('warn', 'Socket not connected')
}
}
}
init_tcp_variables() {
this.setVariableDefinitions([{ name: 'Last Response', variableId: 'tcp_response' }])
this.setVariableValues({ tcp_response: '' })
}
}
runEntrypoint(ArriTally, [])