forked from HQarroum/modbus-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modbus-browser-write-single-coil.js
70 lines (62 loc) · 2.38 KB
/
modbus-browser-write-single-coil.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
const _ = require('lodash');
const program = require('commander');
const signale = require('signale');
const Chain = require('middleware-chain-js');
// Instanciating the middleware chain.
const chain = new Chain();
/**
* Command-line interface.
*/
program
.name('modbus-browser write-single-coil')
.description('Writes the given value of a coil at the given address.')
.option('-s, --server <hostname>', 'The hostname or IP address of the Modbus server to initiate a connection to.')
.option('-p, --port <port>', 'The port of the Modbus server to initiate a connection to (set to `502` by default).')
.option('-u, --unit-id <unitId>', 'The unit identifier to perform the action on (set to `1` by default).')
.option('-a, --address <address>', 'The address of the coil to be written.')
.option('-v, --value <value>', 'The value of the coil to be written.')
.parse(process.argv);
/**
* Injecting the initialization routines into the `chain`.
*/
chain.use(require('./lib/middlewares/initialization-routines'));
/**
* Verifying that the given arguments are valid.
*/
chain.use((input, output, next) => {
if (!_.isString(program.server)) {
return (output.fail(`The hostname or IP address of the Modbus server to connect to is expected.`));
}
if (!_.isString(program.address)) {
return (output.fail(`The address at which the value must be written should be a valid number.`));
}
if (_.isString(program.unitId) && isNaN(parseInt(program.unitId, 10))) {
return (output.fail(`The unit identifier must be a valid number.`));
}
if (!_.isString(program.value)) {
return (output.fail(`A value to be written at the given address is expected.`));
}
if (program.value !== '0' && program.value !== '1') {
return (output.fail(`A boolean value is expected for coils (e.g '0', '1').`));
}
next();
});
/**
* Injecting the connection routines into the `chain`.
*/
chain.use(require('./lib/middlewares/connection-routines'));
/**
* Writing the coil.
*/
chain.use((input, output) => {
const addr = parseInt(program.address, 10);
const value = parseInt(program.value, 10);
// Writing the value.
input.client.writeSingleCoil(addr, value).then(() => {
signale.success(`Successfully wrote value '${value}' at address '${addr}'.`);
// Closing the socket.
input.socket.destroy();
}).catch(output.fail);
});
// Triggering the `chain`.
chain.handle({}, {});