-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (61 loc) · 1.82 KB
/
index.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
var Modbus = require('jsmodbus');
var Device = require('losant-mqtt').Device;
// IP Address of PLC
var host = 'PLC_IP_ADDRESS';
// Modbus TCP Port (Normally 502)
var port = 502;
// Construct a Losant device.
// LOSANT_DEVICE_ID, LOSANT_ACCESS_KEY, & LOSANT_ACCESS_SECRET are obtained from Losant
var device = new Device({
id: 'LOSANT_DEVICE_ID',
key: 'LOSANT_ACCESS_KEY',
secret: 'LOSANT_ACCESS_SECRET'
});
var client = Modbus.client.tcp.complete({
'host': host,
'port': port,
'autoReconnect': true,
'reconnectTimeout': 1000,
'timeout': 5000,
'unitId': 0
});
// Connect to PLC.
client.connect();
// Connect to Losant.
device.connect(function (error) {
if (error) {
// Handle error
throw error;
}
// Successfully connected
console.log('Connected to Losant');
});
// Handle PLC Connection
client.on('connect', function () {
console.log('Connected to PLC');
// Read Discrete Inputs 0-15 every second
setInterval(() => {
client.readDiscreteInputs(0, 15).then(function (resp) {
var state = resp.coils.reduce((object, coil, index) => {
object[`input-${index}`] = coil;
return object
}, {})
// Report all Discrete Inputs to Losant.
device.sendState(state);
}, console.error);
}, 1000);
// Listen for Losant commands.
device.on('command', function (command) {
// The command 'output' will write to a single coil.
if (command.name = 'output') {
// command.payload.coil and command.payload.value are apart of the device command from Losant.
client.writeSingleCoil(command.payload.coil, command.payload.value).then(function (resp) {
console.log(`Wrote the vale "${command.payload.value}" to coil "${command.payload.coil}" `);
}, console.error);
}
});
});
// Log errors
client.on('error', function (err) {
console.log(err);
})