-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.js
More file actions
66 lines (64 loc) · 1.77 KB
/
socket.js
File metadata and controls
66 lines (64 loc) · 1.77 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
var socket_io = require("socket.io");
var io = socket_io();
var socketApi = {};
var {spawn} = require("child_process");
var rimraf = require("rimraf");
var async = require("async");
socketApi.io = io;
io.on("connect",function(socket){
console.log("New client connected "+socket.id);
socket.on("disconnect",function(){
console.log("User",socket.id,"disconnected.");
rimraf("plots/"+socket.id,function(){
console.log("Plots deleted");
});
});
socket.on("calculate",function(data){
console.log("Recived:",data);
console.log("Starting calculation...")
data.id = socket.id;
async.parallel({
bode: function(callback){
let py = spawn("python3",['generate_bode.py']);
let dataString = '';
py.stdout.on("data",function(data){
dataString += data.toString();
});
py.stdout.on("end",function(){
if (dataString.endsWith(".png")){
callback(null,dataString);
}else{
callback(dataString);
}
});
py.stdin.write(JSON.stringify(data));
py.stdin.end();
},
nyquist: function(callback){
let py2 = spawn("python3",['generate_nyquist.py']);
let dataString = '';
py2.stdout.on("data",function(data){
dataString += data.toString();
});
py2.stdout.on("end",function(){
if(dataString.endsWith(".png")){
callback(null,dataString);
}else{
callback("Some error ocurred creating the nyquist plot");
}
});
py2.stdin.write(JSON.stringify(data));
py2.stdin.end();
}},function(err,res){
console.log("Results recived");
console.log("Err:",err,"\nRes:",res);
if(err){
socket.emit("Error");
}else{
let result = '<img id="plot"src="'+socket.id+'/'+res.bode+'"/><br><img id="plot"src="'+socket.id+'/'+res.nyquist+'"/>';
socket.emit("response",result);
}
});
});
});
module.exports = socketApi;