-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.js
More file actions
209 lines (170 loc) · 5.89 KB
/
main.js
File metadata and controls
209 lines (170 loc) · 5.89 KB
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
var omx = require('omxdirector')
, child_process = require('child_process')
, logger = require('./src/logger')
, PlayerController = require('./src/player-controller')
, OSCController = require('./src/osc-controller')
, ClusterNode = require('./src/cluster-node')
, Bus = require('./src/bus')
, config = require('./config')
, Web = require('./src/web')
, dns = require('./src/dns')
, Clock = require('./src/clock')
, DEBUG = !!(config.debug || process.env.DEBUG)
;
if ( DEBUG ) { logger.level = 'debug'; }
else { logger.level = 'info'; logger.transports.console.level = 'warn'; }
////////////////////////////////////////////////////////////////////////////////
// Synchronization
var clock = new Clock();
////////////////////////////////////////////////////////////////////////////////
// OSC Initialization
var osc = new OSCController( clock, {
localAddress: '0.0.0.0',
localPort: config.port,
remoteAddress: config.broadcastAddress,
remotePort: config.port,
broadcast: true
} );
osc.on( "ready", function() { logger.info( "OSC sending and receiving on port " + config.port ); } );
osc.open();
////////////////////////////////////////////////////////////////////////////////
// DBus omxplayer control
var bus = new Bus();
bus.on( "non-fatal-error", function() { logger.warn( "Could not connect to dbus, trying again." ); } );
bus.on( "error", function() { logger.fatal( "Failed to connect to dbus." ); } );
var controller = new PlayerController( bus, clock, omx, logger, config );
bus.on( "ready", function( dbus ) {
controller.pollStatus();
} );
////////////////////////////////////////////////////////////////////////////////
// Node
var node = new ClusterNode( { heartbeatTimeout: 2000 } );
node.heartbeat();
////////////////////////////////////////////////////////////////////////////////
// Web Interface
var web = new Web( { port: config.webPort, serviceName: config.serviceName } );
web.listen();
////////////////////////////////////////////////////////////////////////////////
// Node Transport
node.on( "master", function() {
controller.reset();
controller.play();
web.master();
logger.info( "imma master!" );
} );
node.on( "slave", function() {
logger.info( "imma slave!" );
web.slave();
} );
node.on( "elect", function( nid, eid ) {
logger.info( "send elect " + nid );
osc.send( {
address: "/elect",
args: [ { type: 's', value: nid }, { type: 's', value: eid } ]
} );
} );
bus.on( "ready", function() {
osc.on( "/elect", function( args ) {
var otherNid = args[ 0 ];
var eid = args[ 1 ];
var result = node.vote( otherNid, eid );
if ( result === ClusterNode.VOTE_RESULT.MASTER ) {
logger.info( "got elect " + otherNid + ", incrementing votes" );
} else if ( result === ClusterNode.VOTE_RESULT.SLAVE ) {
logger.info( "got elect " + otherNid + ", becoming slave" );
}
} );
} );
controller.on( "sync", function( status ) {
if ( ! node.isMaster ) return;
var elapsed = status.seconds
, time = osc.timeTag( 0, status.time );
osc.send( {
address: "/sync",
args: [ { type: 'f', value: elapsed }, { type: 't', value: time } ]
} );
} );
bus.on( "ready", function() {
osc.on( "/sync", function( args ) {
node.heartbeat();
if ( node.isIndeterminate ) {
logger.info( "got sync from master, becoming a slave without election" );
node.isSlave = true;
}
if ( node.isSlave ) controller.synchronize( args[ 0 ], args[ 1 ].native );
} );
} );
osc.on( "/status", function( args ) {
var nid = args[ 0 ];
try {
var status = JSON.parse( args[ 1 ] );
} catch ( e ) {
logger.error( "invalid status message %s", args[ 1 ] );
}
web.updateStatus( nid, status );
} );
dns.lookupIP( function( ipv4, ipv6, hostname ) {
setInterval( function() {
var status = {
nid: node.nid
, hostname: hostname
, ipv4: ipv4
, ipv6: ipv6
, role: node.role
, sync: controller.masterSync
, delta: controller.delta
};
osc.send( {
address: "/status",
args: [ { type: 's', value: node.nid }, { type: 's', value: JSON.stringify( status ) } ]
} );
}, config.statusIntervalMs );
} );
web.on( "command", function( command ) {
osc.send( {
address: "/command/" + command,
} );
} );
// Listen for explicit messages, so there's no opportunity for injection
osc.on( "/command/refresh", function() {
logger.warn( "restarting process in response to refresh command" );
process.exit(); // should be caught by forever
} );
osc.on( "/command/stop", function() {
logger.warn( "stopping process in response to stop command" );
child_process.exec("sudo /usr/sbin/service video-player stop", function( error, stdout, stderr ) {
logger.info( "stop result", { error: error, stdout: stdout, stderr: stderr } );
} );
} );
osc.on( "/command/restart", function() {
logger.warn( "rebooting system in response to restart command" );
child_process.exec("sudo /sbin/reboot", function( error, stdout, stderr ) {
logger.info( "reboot result", { error: error, stdout: stdout, stderr: stderr } );
} );
} );
osc.on( "/command/halt", function() {
logger.warn( "halting system in response to halt command" );
child_process.exec("sudo /sbin/halt", function( error, stdout, stderr ) {
logger.info( "halt result", { error: error, stdout: stdout, stderr: stderr } );
} );
} );
////////////////////////////////////////////////////////////////////////////////
// OMXDirector setup
omx.enableNativeLoop();
process.on("SIGINT", function() {
logger.info("Quitting");
omx.stop();
});
omx.on('stop', function(){
logger.info("Done.");
process.exit();
});
//omx.on('status', function(status){ localSecs = status.seconds; } );
var args = [];
args.push("--blank");
if ( ! DEBUG ) args.push("--no-osd");
//args = args.concat(["--win", "0,0,960,540"]);
omx.play( config.filename, {loop: true, args: args} );
bus.create();
logger.info( "STARTED" );
logger.info( "config", config );