-
Notifications
You must be signed in to change notification settings - Fork 1
/
sockets.js
85 lines (68 loc) · 2.5 KB
/
sockets.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
var fs = require('fs');
export const socketIO_setup = (app) => {
console.log('\nsetting up sockets\n')
var ioServer = app.listen(process.env.PORT || 1234, listen);
var io = require('socket.io')(ioServer);
function listen() {
var host = ioServer.address().address;
var port = ioServer.address().port;
console.log('socket.io listening at http://' + host + ':' + port);
}
io.sockets.on('connection', function (socket) {
console.log("Camera client connected: " + socket.id);
socket.on('disconnect', function() {
console.log("Client has disconnected");
});
socket.on('bloop', function(data) {
console.log('retrieving image ...')
/*
TODO:
timestamp should be originated by blooprint/input application at capture time
*/
var timestamp = generateTimestamp()
var inputSavePath = './whiteSocket/input/'+timestamp+'.bmp'
var outputSavePath = './whiteSocket/output/'+timestamp+'.bmp'
// base64toBMP(data.image, inputSavePath);
base64toBMP(data.image, outputSavePath); // for testing
var flag = true
while(flag) {
if(fs.existsSync(outputSavePath)) {
console.log('ok, got it!')
flag = false
}
}
// whiteSocket(timestamp,'white','write','000000',true)
//
// /*
// TODO:
// server-side rendering (redux)
// */
// flag = true
// while(flag) {
// if(fs.existsSync(outputSavePath)) {
// console.log('whiteSocket output saved')
// flag = false
// }
// }
io.sockets.emit('bloop_out', {
timestamp: timestamp,
// outputSavePath: outputSavePath
})
});
});
}
function base64toBMP(img, file) {
var buff = new Buffer(img, 'base64');
fs.writeFileSync(file, buff);
}
function generateTimestamp() {
var yy = new Date().getFullYear().toString()
var mm = new Date().getMonth().toString()
var dd = new Date().getDate().toString()
var h = new Date().getHours().toString()
var m = new Date().getMinutes().toString()
var s = new Date().getSeconds().toString()
var mmm = new Date().getMilliseconds().toString()
var timestamp = yy.concat(mm,dd,h,m,s,mmm)
return timestamp
}