forked from bitfocus/companion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·206 lines (181 loc) · 6.1 KB
/
app.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
/*
* This file is part of the Companion project
* Copyright (c) 2018 Bitfocus AS
* Authors: William Viker <[email protected]>, Håkon Nessjøen <[email protected]>
*
* This program is free software.
* You should have received a copy of the MIT licence as well as the Bitfocus
* Individual Contributor License Agreement for companion along with
* this program.
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial activities involving the Companion software without
* disclosing the source code of your own applications.
*
*/
if (process.env.DEVELOPER !== undefined && process.env.DEBUG === undefined) {
process.env['DEBUG'] = '*,-websocket*,-express*,-engine*,-socket.io*,-send*,-db,-NRC*,-follow-redirects'
}
global.MAX_BUTTONS = 32
global.MAX_BUTTONS_PER_ROW = 8
var EventEmitter = require('events')
var system = new EventEmitter()
var fs = require('fs')
var path = require('path')
var debug = require('debug')('app')
var mkdirp = require('mkdirp')
var stripAnsi = require('strip-ansi')
var logbuffer = []
var logwriting = false
const pkgInfo = require('./package.json')
let buildNumber
try {
buildNumber = fs
.readFileSync(__dirname + '/BUILD')
.toString()
.trim()
} catch (e) {
console.error('Companion cannot start as the "BUILD" file is missing')
console.error('If you are running from source, you can generate it by running: yarn build:writefile')
process.exit(1)
}
const skeleton_info = {
appName: pkgInfo.description,
appVersion: pkgInfo.version,
appBuild: buildNumber.replace(/-*master/, '').replace(/^-/, ''),
appLaunch: '',
appStatus: 'Starting',
}
var config
var cfgDir
// Supress warnings for too many listeners to io_connect. This can be safely increased if the warning comes back at startup
system.setMaxListeners(20)
system.on('skeleton-info', function (key, val) {
skeleton_info[key] = val
if (key == 'configDir') {
debug('configuration directory', val)
cfgDir = val + '/companion/'
mkdirp.sync(cfgDir)
config = new (require('./lib/config'))(system, cfgDir, {
http_port: 8888,
bind_ip: '127.0.0.1',
start_minimised: false,
})
}
})
system.on('configdir_get', function (cb) {
cb(cfgDir)
})
system.on('skeleton-info-info', function (cb) {
cb(skeleton_info)
})
system.on('config_loaded', function (config) {
system.emit('skeleton-info', 'appURL', 'Waiting for webserver..')
system.emit('skeleton-info', 'startMinimised', config.start_minimised)
})
system.on('exit', function () {
console.log('somewhere, the system wants to exit. kthxbai')
system.emit('instance_getall', function (instances, active) {
try {
for (var key in active) {
if (instances[key].label !== 'internal') {
try {
active[key].destroy()
} catch (e) {
console.log('Could not destroy', instances[key].label)
}
}
}
} catch (e) {
console.log('Could not destroy all instances')
}
})
setImmediate(function () {
process.exit()
})
})
system.on('skeleton-bind-ip', function (ip) {
config.bind_ip = ip
system.emit('config_set', 'bind_ip', ip)
system.emit('ip_rebind')
})
system.on('skeleton-bind-port', function (port) {
var p = parseInt(port)
if (p >= 1024 && p <= 65535) {
config.http_port = p
system.emit('config_set', 'http_port', p)
system.emit('ip_rebind')
}
})
system.on('skeleton-start-minimised', function (minimised) {
config.start_minimised = minimised
system.emit('config_set', 'start_minimised', minimised)
})
system.ready = function (logToFile) {
if (logToFile) {
debug('Going into headless mode. Logs will be written to companion.log')
setInterval(function () {
if (logbuffer.length > 0 && logwriting == false) {
var writestring = logbuffer.join('\n')
logbuffer = []
logwriting = true
fs.appendFile(path.join(cfgDir, 'companion.log'), writestring + '\n', function (err) {
if (err) {
console.log('log write error', err)
}
logwriting = false
})
}
}, 1000)
process.stderr.write = function () {
var arr = []
for (var n in arguments) {
arr.push(arguments[n])
}
var line = new Date().toISOString() + ' ' + stripAnsi(arr.join(' ').trim())
logbuffer.push(line)
}
}
var server_express = require('./lib/server_express')(system)
var server_http = require('./lib/server_http')(system, server_express)
var io = require('./lib/io')(system, server_http)
var log = require('./lib/log')(system, io)
var db = require('./lib/db')(system, cfgDir)
var userconfig = require('./lib/userconfig')(system)
var server_https = require('./lib/server_https')(system, server_express, io)
var update = require('./lib/update')(system, cfgDir)
var page = require('./lib/page')(system)
var appRoot = require('app-root-path')
var variable = require('./lib/variable')(system)
var schedule = require('./lib/schedule')(system)
var feedback = require('./lib/feedback')(system)
var action = require('./lib/action')(system)
var bank = require('./lib/bank')(system)
var elgatoDM = require('./lib/elgato_dm')(system)
var preview = require('./lib/preview')(system)
var instance = require('./lib/instance')(system)
var osc = require('./lib/server_osc')(system)
var server_api = require('./lib/server_api')(system)
var server_tcp = require('./lib/server_tcp')(system)
var server_udp = require('./lib/server_udp')(system)
var server_emberplus = require('./lib/server_emberplus')(system)
var artnet = require('./lib/artnet')(system)
var rosstalk = require('./lib/rosstalk')(system)
var rest = require('./lib/rest')(system)
var rest_poll = require('./lib/rest_poll')(system)
var loadsave = require('./lib/loadsave')(system)
var preset = require('./lib/preset')(system)
var satelliteLegacy = require('./lib/satellite/satellite_server_legacy')(system)
var satellite = require('./lib/satellite/satellite_server')(system)
var elgato_plugin_server = require('./lib/elgato_plugin_server')(system)
var metrics = require('./lib/metrics')(system)
system.emit('modules_loaded')
system.on('exit', function () {
elgatoDM.quit()
})
setTimeout(function () {
system.emit('ip_rebind')
}, 2000)
}
exports = module.exports = system