forked from bitfocus/companion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·198 lines (168 loc) · 5.58 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
/*
* 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'] = '*,-websocket*,-express*,-engine*,-socket.io*,-send*,-db,-NRC*,-follow-redirects,-electron-timer-fix';
}
// Fix timers in electron
require('./electron-timer-fix').fix();
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 util = require('util');
var events = require('events');
var stripAnsi = require('strip-ansi');
var logbuffer = [];
var logwriting = false;
var skeleton_info = {};
var config;
var cfgDir;
system.on('skeleton-info', function(key, val) {
skeleton_info[key] = val;
if (key == 'configDir') {
debug('configuration directory', val);
cfgDir = val + "/companion/";
mkdirp(cfgDir, function(err) {
debug("mkdirp",cfgDir,err);
config = new (require('./bitfocus-libs/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', 'appStatus', 'Starting');
system.emit('skeleton-info', 'bindInterface', config.bind_ip);
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.on('skeleton-ready', function() {
if (system.headless === true) {
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('./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[0]);
}
var line = new Date().toISOString() + " " + stripAnsi(arr.join(" ").trim() );
logbuffer.push(line);
};
}
var server_http= require('./lib/server_http')(system);
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 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 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/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 artnet = require('./lib/artnet')(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 tablet = require('./lib/tablet')(system);
var satellite = require('./lib/satellite_server')(system);
var ws_api = require('./lib/ws_api')(system);
system.emit('modules_loaded');
system.on('exit', function() {
elgatoDM.quit();
});
});
system.on('skeleton-single-instance-only', function (response) {
response(true);
});
exports = module.exports = function(headless) {
if (headless !== undefined && headless === true) {
system.headless = true;
}
return system;
}