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
|
module.exports = function(express,app,io,fs,exec,os,PythonShell,scriptPath){
var router = express.Router();
var pyEnv = '/usr/bin/python';
router.get('/',function(req,res,next){
res.render('schematic',{});
});
app.use('/',router);
io.on('connection',function(socket){
var socketId = getSocketID(socket);
var analysisFile = '/tmp/' + socketId + '.cir.out';
//Conversion to lower case is required as NgSpice internally converts the filename to lower case.
var dumpv = '/tmp/' + socketId.toLowerCase() + '-dumpv.txt';
var dumpi = '/tmp/' + socketId.toLowerCase() + '-dumpi.txt';
console.log('Client with socket id ' + socketId + ' is now connected.');
socket.on('disconnect', function(){
console.log('Client with socket id ' + socketId + ' has disconnected.');
deleteOnExit(analysisFile);
deleteOnExit(dumpv);
deleteOnExit(dumpi);
});
socket.on('netlist', function(msg){
//Replacing to be created filenames in the generated netlist, so as to identify the client
var update = msg.replace('dumpv.txt', dumpv);
var result = update.replace('dumpi.txt', dumpi);
fs.writeFile(analysisFile, result, function(err){
if(err){
return console.log(err);
}
});
executeNgspiceNetlist(analysisFile);
});
function deleteOnExit(filename){
fs.stat(filename, function(err, stat){
if(err == null){
fs.unlink(filename);
console.log('File: ' + filename + ' deleted.')
}
else
console.log('Unable to delete file : ' + filename + '\n' + err );
});
}
function getSocketID(socket){
socketID = socket.id;
//Removing first two char i.e '/#' from socket id
socketID = socketID.substring(2);
console.log("Return :"+socketID)
return socketID;
}
function executeNgspiceNetlist(fileName)
{
fs.exists(fileName, function(exists) {
if (exists) {
exec('ngspice '+fileName, function(code, stdout, stderr) {
console.log('Exit code:', code);
console.log('Program output:', stdout);
console.log('Program stderr:', stderr);
if(stderr){
switch(stderr){
case (stderr.match(/Error/) || stderr.match(/error/)||{}).input:
console.log("Error in executing ngspice netlist");
socket.emit('serverMessage','Error while executing ngspice netlist: '+stderr);
break;
default:
parsePlotData();
break;
}
}
else
parsePlotData();
});
}
});
}
function parsePlotData()
{
console.log("Ngspice netlist executed successfully ");
socket.emit('serverMessage','Ngspice netlist executed successfully: ');
var analysisInfo = grep('.tran|.dc|.ac', analysisFile);
console.log("Analysis :"+analysisInfo);
var options = {
mode: 'json',
pythonPath: pyEnv,
pythonOptions: ['-u'],
scriptPath: scriptPath,
args: [analysisFile, dumpv, dumpi]
};
PythonShell.run('parser.py', options, function (err, results)
{
if (err) throw err;
// results is an array consisting of messages collected during execution
//console.log('results: %j', results);
var resultString = results[0];
//console.log(resultString);
//Emitting Data Points to client
socket.emit('plotData',resultString);
});
}
});
}
|