-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathserver_rpi.js
175 lines (161 loc) · 3.77 KB
/
server_rpi.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
var express = require("express");
var multer = require('multer');
var app = express();
var upload = multer({ dest: './ladder_files/'});
var spawn = require('child_process').spawn;
var openplc = spawn('./OPLC_starter');
var plcRunning = true;
app.use(multer({ dest: './ladder_files/',
rename: function (fieldname, filename)
{
return filename;
},
onFileUploadStart: function (file)
{
console.log(file.originalname + ' is starting ...');
},
onFileUploadComplete: function (file)
{
console.log(file.fieldname + ' uploaded to ' + file.path);
console.log('finishing old program...');
openplc.kill('SIGTERM');
plcRunning = false;
compileLadder(file.originalname);
}
}));
app.get('/',function(req,res)
{
showMainPage(req,res);
});
app.get('/run',function(req,res)
{
if (plcRunning == false)
{
console.log('Starting OpenPLC Software...');
openplc = spawn('./OPLC_starter');
plcRunning = true;
}
showMainPage(req,res);
});
app.get('/stop',function(req,res)
{
if (plcRunning == true)
{
console.log('Stopping OpenPLC Software...');
openplc.kill('SIGTERM');
plcRunning = false;
}
showMainPage(req,res);
});
app.post('/api/upload',function(req,res)
{
upload(req,res,function(err)
{
if(err)
{
return res.end("Error uploading file.");
}
showMainPage(req,res);
});
});
app.listen(8080,function()
{
console.log("Working on port 8080");
});
function showMainPage(req,res)
{
var htmlString = '\
<!DOCTYPE html>\
<html>\
<head>\
<style>\
body {background-color:lightgray}\
h1 {color:Black}\
p {color:black}\
</style>\
</head>\
<body>\
<center><h1>OpenPLC on Raspberry Pi Server</h1></center>';
if (plcRunning == true)
{
htmlString += '<p>Current PLC Status: <font color = "green">Running</font></p>';
}
else
{
htmlString += '<p>Current PLC Status: <font color = "red">Stopped</font></p>';
}
htmlString += '\
<button type="button" onclick="location.href=\'run\';">Run</button>\
<button type="button" onclick="location.href=\'stop\';">Stop</button>\
<br><br><br>\
<p>Change Ladder diagram:</p>\
<form id = "uploadForm"\
enctype = "multipart/form-data"\
action = "/api/upload"\
method = "post">\
<input type="file" name="ladderDiagram" accept=".ld">\
<input type="submit" value="Upload Ladder" name="submit">\
</form>\
</body>\
</html>';
res.send(htmlString);
}
function compileLadder(fileName)
{
console.log('compiling ladder diagram...');
var compiler = spawn('./OPLC_Compiler', ['./ladder_files/' + fileName]);
compiler.stdout.on('data', function(data)
{
console.log('OPLC_Compiler: ' + data);
});
compiler.on('close', function(code)
{
if (code != 0)
{
console.log('Error compiling ladder diagram');
}
else
{
console.log('OPLC_Compiler exit code: ' + code);
moveFiles();
}
});
}
function moveFiles()
{
console.log('moving files...');
var copier = spawn('mv', ['-f', 'ladder.cpp', './core/ladder.cpp']);
copier.on('close', function(code)
{
if (code != 0)
{
console.log('error moving files');
}
else
{
compileOpenPLC();
}
});
}
function compileOpenPLC()
{
console.log('compiling OpenPLC...');
var exec = require('child_process').exec;
exec('g++ -lrt -lwiringPi -lpthread ./core/*.cpp -o ./core/openplc', function(error, stdout, stderr)
{
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null)
{
console.log('exec error: ' + error);
console.log('error compiling OpenPLC. Please check your ladder diagram');
}
else
{
console.log('compiled without errors');
console.log('Starting OpenPLC Software...');
openplc = spawn('./OPLC_starter');
plcRunning = true;
}
});
}