generated from bitfocus/companion-module-template-js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
299 lines (254 loc) · 7.69 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
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
const { InstanceBase, Regex, runEntrypoint, InstanceStatus } = require('@companion-module/base')
const UpgradeScripts = require('./upgrades')
const UpdateActions = require('./actions')
const UpdateFeedbacks = require('./feedbacks')
const UpdatePresets = require('./presets')
const UpdateVariableDefinitions = require('./variables')
let AndroidRemote, RemoteKeyCode, RemoteDirection
const macfromip = require('macfromip')
class ModuleInstance extends InstanceBase {
constructor(internal) {
super(internal)
}
async init(config) {
this.config = config
this.init_tv_connection()
this.updateActions() // export actions
this.updateFeedbacks() // export feedbacks
this.updatePresets() // export feedbacks
this.updateVariableDefinitions() // export variable definitions
}
init_tv_connection() {
this.updateStatus(InstanceStatus.Disconnected)
if (this.config.host || this.config.bonjour_host) {
import('androidtv-remote').then((AndroidTV) => {
this.updateStatus('connecting', 'Connecting')
console.log('--Starting AndroidTV-Remote--')
AndroidRemote = AndroidTV.AndroidRemote
RemoteKeyCode = AndroidTV.RemoteKeyCode
RemoteDirection = AndroidTV.RemoteDirection
this.RemoteKeyCode = RemoteKeyCode
this.RemoteDirection = RemoteDirection
let options = {
pairing_port: 6467,
remote_port: 6466,
name: 'companion-androidtv-remote',
cert: {}
}
if (this.config.certificate !== undefined && this.config.certificate.key !== undefined) {
options.cert = this.config.certificate
}
if (!this.config.certBool) {
options.cert = {}
this.config.certificate = {}
}
let host = this.config.bonjour_host ?? this.config.host
if (host.includes(":")) {
host = host.split(":").shift()
}
console.log('--Creating tv interface--')
this.tv = new AndroidRemote(host, options)
const theTV = this.tv
console.log('--Setting Variables--')
this.tv.on('powered', (powered) => {
console.debug('Powered : ' + powered)
this.setVariableValues({
power_state: powered
})
this.updateStatus(InstanceStatus.Ok)
this.checkFeedbacks('PowerState')
})
this.tv.on('volume', (volume) => {
this.setVariableValues({
volume_level: volume.level,
volume_max: volume.maximum,
volume_muted: volume.muted
})
this.checkFeedbacks()
// console.debug('Volume : ' + volume.level + '/' + volume.maximum + ' | Muted : ' + volume.muted)
})
this.tv.on('current_app', (current_app) => {
this.setVariableValues({
current_app_state: current_app
})
// console.debug('Current App : ' + current_app)
this.checkFeedbacks('CurrentApp')
})
this.tv.on('error', (error) => {
console.error('There was an Error!!!')
this.updateStatus(InstanceStatus.UnknownError, 'Check Log')
this.log('error', 'Network error: ' + error.message)
})
this.tv.on('unpaired', () => {
console.error('Unpaired')
this.updateStatus(InstanceStatus.UnknownWarning, 'Unpaired')
this.log('error', 'Unpaired error')
// this.config.certBool = false
this.saveConfig({
certificate: undefined,
host: host,
bonjour_host: this.config.bonjour_host,
certBool: false,
macAddress: this.config.macAddress,
retryDuration: this.config.retryDuration
})
})
this.tv.on('ready', async () => {
await new Promise((resolve) => setTimeout(resolve, 2000))
if (!this.config.macAddress || this.config.macAddress === '') {
try {
this.config.macAddress = await new Promise(function(resolve, reject) {
macfromip.getMac(host, function(err, data) {
if (err) {
reject(err)
}
resolve(data)
})
})
} catch (e) {
console.error(e)
this.debug('Error Getting Mac Address: You might have to enter it manually.', error)
this.config.macAddress === ''
}
}
// this.config.certificate = this.tv.getCertificate()
// this.config.certBool = true
this.updateStatus(InstanceStatus.Ok)
this.saveConfig({
certificate: this.tv.getCertificate(),
host: host,
bonjour_host: this.config.bonjour_host,
certBool: true,
macAddress: this.config.macAddress,
retryDuration: this.config.retryDuration
})
})
if (this.config.certificate !== undefined && this.config.certificate.key !== undefined) {
console.log('--Starting TV--');
const timeoutDuration = 10000; // Timeout duration in milliseconds
const timeoutPromise = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('Operation timed out'));
}, timeoutDuration);
});
Promise.race([
this.tv.start(),
timeoutPromise
]).then(result => {
if (result === undefined) {
this.updateStatus(InstanceStatus.ConnectionFailure, 'Check IP Address or re-pair(See help)')
this.log('error', `Unable to Connect to ${this.config.host}.`)
} else {
this.updateStatus(InstanceStatus.Ok)
}
this.tv.remoteManager.on('error', (error) => {
console.error('RemoteManager Error')
this.updateStatus(InstanceStatus.UnknownError, 'Check Log')
console.error(JSON.stringify(error));
})
}).catch(error => {
if (error.message === 'Operation timed out') {
this.updateStatus(InstanceStatus.ConnectionFailure, 'Check IP Address. Is device on?')
this.log('error', `Unable to Connect to ${this.config.host}.`)
} else {
console.log('starting error', error)
this.log('error', error.message)
console.error(error);
}
});
} else {
this.updateStatus(InstanceStatus.UnknownWarning, 'Unpaired')
}
})
}
}
// When module gets deleted or disabled
async destroy() {
if (this.tv !== undefined) {
this.tv.remoteManager.client.destroy()
this.tv.stop()
this.tv = undefined
}
this.log('debug', 'destroy')
}
configUpdated(config) {
this.init(config)
}
// Return config fields for web config
getConfigFields() {
return [
{
type: 'static-text',
id: 'info',
label: 'Information',
width: 12,
value: `
This module will connect to most new AndroidTVs. Click on the question mark above and to the right for pairing and helpful instructions.
`
},
{
type: 'bonjour-device',
id: 'bonjour_host',
label: 'Automatic Search',
width: 6,
},
{
type: 'textinput',
id: 'host',
label: 'Target IP',
width: 6,
isVisible: (options) => !options['bonjour_host'],
default: '',
regex: Regex.IP
},
{
type: 'static-text',
id: 'host-filler',
width: 6,
label: '',
isVisible: (options) => !!options['bonjour_host'],
value: '',
},
{
type: 'textinput',
id: 'macAddress',
label: 'Target Mac Address (automatically found during pairing)',
width: 6,
default: '',
regex: '/^([0-9a-f]{2}([:.-]{0,1}|$)){6}$/i'
},
{
type: 'checkbox',
label: 'Paired',
id: 'certBool',
width: 6,
default: false,
tooltip: 'Uncheck and hit save button to clear saved credentials.'
},
{
type: 'number',
label: 'Power On Retry Duration (in seconds)',
id: 'retryDuration',
width: 6,
default: 6,
min: 0,
max: 60,
tooltip: 'Take up to X seconds trying to power the TV on ethernet connections.',
isVisible: () => false, // Hide
}
]
}
updateActions() {
UpdateActions(this)
}
updateFeedbacks() {
UpdateFeedbacks(this)
}
updatePresets() {
UpdatePresets(this)
}
updateVariableDefinitions() {
UpdateVariableDefinitions(this)
}
}
runEntrypoint(ModuleInstance, UpgradeScripts)