-
Notifications
You must be signed in to change notification settings - Fork 9
/
plc.js
72 lines (58 loc) · 1.29 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
var nodes7 = require('nodes7');
var fastq = require('fastq');
var connected = false;
var isConnected = function() {
return connected;
}
var setup = function(config, callback) {
// create plc Object
var plc = new nodes7({
silent: !config.debug
});
writeQueue = fastq(plc, function(args, callback) {
queueCallback = callback;
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();
callback();
});
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
}