-
Notifications
You must be signed in to change notification settings - Fork 13
/
mock_server.js
103 lines (86 loc) · 2.98 KB
/
mock_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
var express = require('express');
var cors = require('cors');
var bodyParser = require('body-parser');
var app = express();
var port = process.env.PORT || 8080;
var dir = process.env.DIR || 'www';
app.use( express.static(dir) );
app.use( cors() );
app.use( bodyParser.text() );
var heaterT0SelectedTemp = 0;
var heaterT1SelectedTemp = 0;
var bedSelectedTemp = 0;
// routes will go here
app.post('/command', function(req, res) {
var param = req.body.trim();
var output = "";
console.log('Request param: ' + param);
var heaterT0ActualTemp = 30;
var heaterT1ActualTemp = 25;
var bedActualTemp = 22;
if (param == "M105") {
heaterT0ActualTemp = getRandomValue("T0");
heaterT1ActualTemp = getRandomValue("T1");
bedActualTemp = getRandomValue("bed");
output = "ok T:" + heaterT0ActualTemp + " /" + heaterT0SelectedTemp + "@0 ";
output += "T1:" + heaterT1ActualTemp + " /" + heaterT1SelectedTemp + "@0 ";
output += "B:" + bedActualTemp + " /" + bedSelectedTemp + "@0 ";
output += "P:29.7 /0.0 @0" + '\r\n';
} else if (param == "M20") {
rand = Math.floor((Math.random() * 2) * 10 + 1);
output = "Begin file list\n" +
"config.txt\n" +
"web\n" +
"web2\n" +
"test" + (rand + 2) + ".gcode\n" +
"test" + (rand - 2) + ".gcode\n" +
"test" + (rand + 3) + ".gcode\n" +
"End file list\n" +
"ok" + '\r\n';
} else if (param.indexOf("M104") > -1 || param.indexOf("M140") > -1) {
var regex;
var regexResult;
if (param.indexOf("M104") > -1) {
if (param.indexOf("T1")) {
regex = /M104 S(.*) T(.*)/gi;
regexResult = regex.exec(param);
if (regexResult[2] == "0") {
heaterT0SelectedTemp = Number(regexResult[1]).toFixed(1);
} else {
heaterT1SelectedTemp = Number(regexResult[1]).toFixed(1);
}
}
} else {
regex = /M140 S(.*)/gi;
regexResult = regex.exec(param);
bedSelectedTemp = Number(regexResult[1]).toFixed(1);
}
} else if (param == "progress") {
output = "Nothing is printed" + '\r\n';
} else if (param == "abort") {
output = "Abort! Abort!" + '\r\n';
} else {
output = "ok" + '\r\n';
}
console.log('Response: ' + output);
res.send(output);
});
app.options("/*", function(req, res, next){
res.sendStatus(200);
});
function getRandomValue(target) {
var min;
var max;
if (target == "bed") {
min = 30;
max = 32;
} else {
min = 200;
max = 210;
}
var result = Math.random() * (max - min + 1) + min;
return result.toFixed(1);
}
// start the server
app.listen(port);
console.log('Server started! At http://localhost:' + port);