forked from timroemisch/mqtt-s7-connector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplc.js
72 lines (58 loc) · 1.32 KB
/
plc.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
const Nodes7 = require('nodes7');
const fastq = require('fastq');
let connected = false;
const isConnected = function () {
return connected;
};
const setup = function (config, initDevices) {
// create plc Object
const plc = new Nodes7({
silent: !config.debug
});
const writeQueue = fastq(plc, function (args, callback) {
const queueCallback = callback;
const appCallback = args[2];
callback = function (error) {
queueCallback(error, null);
appCallback(error);
}
args[2] = callback;
plc.writeItems.apply(plc, args);
}, 1);
writeQueue.pause();
// connect to plc
plc.initiateConnection({
port: config.port,
host: config.host,
rack: config.rack,
slot: config.slot
}, function (err) {
if (err !== undefined) {
console.log("We have an error. Maybe the PLC is not reachable.");
console.log(err);
process.exit();
}
console.log('PLC Connected');
connected = true;
writeQueue.resume();
initDevices();
});
return {
writeItems: function () {
writeQueue.push(arguments);
},
addItems: function () {
plc.addItems.apply(plc, arguments);
},
setTranslationCB: function () {
plc.setTranslationCB.apply(plc, arguments);
},
readAllItems: function () {
plc.readAllItems.apply(plc, arguments);
},
};
};
module.exports = {
setup: setup,
isConnected: isConnected
}