-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
346 lines (282 loc) · 9.05 KB
/
server.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import express from 'express';
import { SyncServer } from '@ircam/sync'
import { Server as HttpServer } from 'http';
import { Server as IoServer } from "socket.io";
import Conf from 'conf';
import fs from 'fs';
import 'dotenv/config'
if (!'PORT' in process.env) process.env.PORT = 5000;
if (!'VIDEO_PATH' in process.env) process.env.VIDEO_PATH = './www/video';
console.log("\n === Flanerie Mozaic Server === \n")
const __dirname = new URL('.', import.meta.url).pathname;
// const options={
// key:fs.readFileSync(path.join(__dirname,'./cert/key.pem')),
// cert:fs.readFileSync(path.join(__dirname,'./cert/cert.pem'))
// }
const config = new Conf({projectName: 'panoptic'});
// config.clear();
console.log("Loading config from: ", config.path)
var app = express();
var server = HttpServer(app);
var io = new IoServer(server);
// STATE
//
var defaultState = {
zoom: 1.0,
offsetTime: 0,
media: '',
paused: false,
ctrls: false
}
var state = config.get('state', {});
if (!state['default'])
state['default'] = Object.assign({}, defaultState );
// PLAYLIST from list of files in /video
//
var playlist = fs.readdirSync(process.env.VIDEO_PATH).filter((f) => f.endsWith('.mp4'));
// watch for changes in /video
fs.watch(process.env.VIDEO_PATH, (eventType, filename) => {
playlist = fs.readdirSync(process.env.VIDEO_PATH).filter((f) => f.endsWith('.mp4'));
io.emit('playlist', playlist);
//console.log('playlist', playlist);
})
// SYNC Server
//
const startTime = process.hrtime();
const getTimeFunction = () => {
const now = process.hrtime(startTime);
return now[0] + now[1] * 1e-9;
}
const syncServer = new SyncServer(getTimeFunction);
// Devices
//
var devices = config.get('devices', { 'default': {} });
for (let room in devices)
for (let uuid in devices[room]) {
devices[room][uuid].alive = false;
if (devices[room][uuid].mode.startsWith('guest'))
devices[room][uuid].resolution = {x: 400, y: 800};
if (!devices[room][uuid].zoomdevice) devices[room][uuid].zoomdevice = 1.0;
}
// console.log('devices', devices);
// Create a new device entry if it doesn't exist
function bootstrapDevice(uuid, room, reso) {
if (uuid === undefined) return;
if (room === undefined) room = 'default';
if (!devices[room]) devices[room] = {}
if (!state[room]) state[room] = Object.assign({}, defaultState );
devices[room][uuid] = devices[room][uuid] || {
room: 'default',
position: {x: 0, y: 0},
resolution: {x: 400, y: 800},
zoomdevice: 1.0,
alive: false,
selected: false,
mode: 'new' // new, fixed, guest
};
if (reso) devices[room][uuid].resolution = reso;
if (room) devices[room][uuid].room = room;
if (reso) devices[room][uuid].alive = true;
}
// Save devices to config and emit to clients
function updateDevices(room) {
config.set('devices', devices);
io.to(room).emit('devices', devices);
}
// Socket.io Server
//
io.on('connection', (socket) =>
{
//console.log('a user connected');
socket.on('disconnect', () => {
//console.log('user disconnected');
if (socket.uuid && devices[socket.room]) {
devices[socket.room][socket.uuid].alive = false;
if (socket.uuid.startsWith('guest')) devices[socket.room][socket.uuid].resolution = {x: 400, y: 800};
updateDevices(socket.room);
}
});
// Client is ready to receive initial data
socket.on('hi', (uuid, room, reso) =>
{
if (room === undefined) room = 'default';
socket.uuid = uuid;
socket.room = room;
socket.join(room);
bootstrapDevice(uuid, room, reso);
updateDevices(room);
socket.emit('state', state[room])
socket.emit('playlist', playlist)
// console.log('hi', uuid, room, reso, playlist);
// if new, try to move to a dead guest
if (devices[room][uuid].mode === 'new') {
for (let guestid in devices[room]) {
if (devices[room][guestid].mode === 'guest' && !devices[room][guestid].alive) {
io.to(room).emit('rename', uuid, guestid);
break;
}
}
}
})
// Global zoom
socket.on('zoom', (room, z) => {
if (room === undefined) room = 'default';
state[room].zoom = z;
state[room].zoom = Math.max(0.1, state[room].zoom);
config.set('state', state);
io.to(room).emit('state', state[room]);
})
// Local zoom
socket.on('zoomdevice', (room, uuid, z) => {
if (room === undefined) room = 'default';
if (uuid === undefined) return;
bootstrapDevice(uuid, room);
devices[room][uuid].zoomdevice = z;
updateDevices(room);
})
// Rename device
socket.on('rename', (room, uuid, newuuid) => {
if (room === undefined) room = 'default';
if (uuid === undefined) return;
if (newuuid === undefined) return;
devices[room][newuuid] = devices[room][uuid];
io.to(room).emit('rename', uuid, newuuid);
})
// Remove device
socket.on('remove', (room, uuid) => {
if (room === undefined) room = 'default';
if (uuid === undefined) return;
if (devices[room][uuid]) {
if (uuid.startsWith('guest')) io.to(room).emit('reload', uuid);
delete devices[room][uuid];
updateDevices(room);
}
})
// Mode
socket.on('mode', (room, uuid, mode) => {
if (room === undefined) room = 'default';
if (uuid === undefined) return;
if (mode === undefined) return;
bootstrapDevice(uuid, room);
devices[room][uuid].mode = mode;
config.set('state', state);
updateDevices(room);
})
// select device
socket.on('select', (room, uuid, selected) => {
io.to(room).emit('select', uuid, selected);
})
// Add guest
socket.on('guestAdd', (room) => {
if (room === undefined) room = 'default';
var guestCount = 0;
for (let uuid in devices[room])
if (devices[room][uuid].mode === 'guest') guestCount++;
while (devices[room]['guest'+guestCount]) guestCount++;
var uuid = "guest"+guestCount
bootstrapDevice(uuid, room, {x: 400, y: 800});
devices[room][uuid].alive = false;
devices[room][uuid].mode = 'guest';
updateDevices(room);
})
// Move device
socket.on('move', (uuid, room, delta) =>
{
if (uuid === undefined) return;
if (room === undefined) room = 'default';
bootstrapDevice(uuid, room);
devices[room][uuid].position.x += delta.x;
devices[room][uuid].position.y += delta.y;
updateDevices(room);
// console.log('move', uuid, room, delta, devices[room][uuid].position);
})
// Move all devices
socket.on('moveAll', (room, delta) =>
{
if (room === undefined) room = 'default';
if (!devices[room]) return;
for (let uuid in devices[room]) {
devices[room][uuid].position.x += delta.x;
devices[room][uuid].position.y += delta.y;
}
updateDevices(room);
})
// Set device position
socket.on('setPosition', (uuid, room, pos) =>
{
if (uuid === undefined) return;
if (room === undefined) room = 'default';
bootstrapDevice(uuid, room);
devices[room][uuid].position = pos;
updateDevices(room);
})
// Clear devices
socket.on('clearDevices', (room) =>
{
if (room === undefined) room = 'default';
if (!devices[room]) return;
for (let uuid in devices[room]) {
if (devices[room][uuid].alive) devices[room][uuid].alive = false;
else if (!uuid.startsWith('guest')) delete devices[room][uuid];
}
updateDevices(room);
})
// Toggle controls
socket.on('toggleCtrls', (room) =>
{
if (room === undefined) room = 'default';
state[room].ctrls = !state[room].ctrls;
io.to(room).emit('ctrls', state[room].ctrls);
})
// Load and play media
socket.on('play', (room, media) => {
if (room === undefined) room = 'default';
state[room].offsetTime = getTimeFunction();
state[room].media = media;
state[room].paused = false;
config.set('state', state);
io.to(room).emit('state', state[room]);
})
// Stop media
socket.on('stop', (room) => {
if (room === undefined) room = 'default';
state[room].media = '';
state[room].paused = false;
config.set('state', state);
io.to(room).emit('stop');
})
// Pause
socket.on('pause', (room) => {
if (room === undefined) room = 'default';
state[room].paused = !state[room].paused;
io.to(room).emit( state[room].paused ? 'pause' : 'play' );
})
// reloadAll
socket.on('reloadAll', (room) => {
io.to(room).emit('reload', 'all');
})
// SYNC Server - client init
syncServer.start(
(...args) => { socket.emit('pong', ...args) }, // send
(callback) => { socket.on('ping', (...args) => { callback(...args) }) }, // receive
);
// Send initial HELLO trigger
socket.emit('hello');
});
// Express Server
//
server.listen(process.env.PORT, function() {
console.log('listening on *:' + process.env.PORT);
});
app.get(['/qr', '/qrcode'], function(req, res) {
res.sendFile(__dirname + '/www/qr.html');
});
app.get('/control/:room?', function(req, res) {
res.sendFile(__dirname + '/www/mapping.html');
});
app.get('/:room?', function(req, res) {
res.sendFile(__dirname + '/www/index.html');
});
// Serve static files /static
app.use('/static', express.static('www'));
app.use('/media', express.static(process.env.VIDEO_PATH));