-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp-s7-mediaselect.js
51 lines (39 loc) · 1.39 KB
/
app-s7-mediaselect.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
var nodes7 = require('nodes7');
var config = require('./config');
var s7conn = new nodes7;
const express = require('express');
const app = express();
app.use(express.json());
////////////////////////////////////////////////////////////////////////
// S7 handling
var globalValues;
var variables = config.plc.vars;
s7conn.initiateConnection({port: 102, host: config.plc.host, rack: config.plc.rack, slot: config.plc.slot}, connected);
function cyclicReadFromPlc()
{
s7conn.readAllItems(valuesReady);
}
function connected(err) {
if (typeof(err) !== "undefined") {
console.log("ERROR: CANNOT CONNECT TO S7 PLC");
console.log(err);
}
s7conn.setTranslationCB(function(tag) {return variables[tag];});
s7conn.addItems(Object.keys(variables));
s7conn.readAllItems(valuesReady);
setInterval(cyclicReadFromPlc, config.plc.pollinterval);
}
function valuesReady(err, values) {
if (err) { console.log("ERROR: READING VALUES FROM S7 PLC FAILED!"); }
globalValues = values;
}
////////////////////////////////////////////////////////////////////////
// HTTP Server handling
app.use(express.static('public'));
app.listen(config.webserver.port, function(){
console.log("Webserver is listening on port", config.webserver.port);
});
app.get('/getvalues', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(globalValues));
});