-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
112 lines (90 loc) · 2.77 KB
/
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
104
105
106
107
108
109
110
111
112
const express = require('express');
const bodyParser = require('body-parser');
const spawn = require('child_process');
const config = require('config');
const port = config.has('port') ? config.get('port') : 80;
const {
HeatingDevice,
Eq3CommandFactory
} = require('./lib/heating');
const baseCommand = config.has('executeCommand') ? config.get('executeCommand') : undefined;
const scriptBase = './eq3.exp';
app = express();
app.use(bodyParser.json());
app.post('/:mac/:type/:command', (req, res) => {
let devices = config.has('devices') ? config.get('devices') : [];
let success = false;
if (!devices.length) {
return res.json({
success,
message: 'Es ist keine Konfiguration vorhanden'
});
}
devices = devices.filter((device) => device.mac.replace(/\:/g, '') === req.params.mac);
if (!req.params || !req.params.mac || !devices.length) {
return res.status(400).json({
success: false,
message: 'Es konnte kein Gerät gefunden werden.'
});
}
const type = req.params.type || undefined;
const cmd = (req.params.command || undefined).replace('_', ' ');
const parameters = req.body.parameters || [];
if (!type || !cmd) {
return res.status(400).json({
success: false,
message: 'Es wurde kein korrekter Befehl aufgerufen.'
});
}
let attempts = 0;
success = true;
const [ deviceConfig ] = devices;
const device = new HeatingDevice(deviceConfig.mac);
establishConnection(device, (success, data) => {
data.message = data.message.replace(/\n|\r|\t/g, '')
try {
data.message = JSON.parse(data.message);
}
catch (e) {}
return res.json({
success,
request: {
executable: data.executable
},
response: {
attempts,
data: data.message
}
});
});
function establishConnection(device, callback) {
const command = Eq3CommandFactory.createCommand(type, cmd, parameters);
const executable = Eq3CommandFactory.getExecutable(scriptBase, command, device);
const ssh = spawn.exec(baseCommand.replace('[eq3-command]', executable));
let message = '';
ssh.stdout.on('data', (data) => {
error = false;
message += data;
});
ssh.stderr.on('data', (err) => {
error = true;
attempts += 1;
});
ssh.on('close', (code) => {
if (attempts > 5) {
return callback(false, {message: 'too much attempts', executable});
}
if (!connectionFailed(message)) {
return callback(true, {message, executable});
}
attempts++;
return establishConnection(device, callback);
})
}
function connectionFailed(message) {
return message.indexOf('Connection failed') !== -1;
}
});
app.listen(port, () => {
console.log('listening on port ' + port);
});