Commit 00ea29cc authored by Gabriele De Blasi's avatar Gabriele De Blasi
Browse files

wait for the end of read/write operations on the PLC

parent cc0b93d8
Pipeline #3064547 passed with stages
in 12 minutes and 7 seconds
......@@ -30,12 +30,14 @@ class PLC extends Action {
this.cli = new NodeS7({ silent: true });
this.run = cond([
/** @type {[function(PlcCmd): boolean, function(PlcCmd): void]} */
/** @type {[function(PlcCmd): boolean, function(PlcCmd): Promise<string>]} */
([ conforms({ 'address': isString, 'value': negate(isNil) }),
this.write ]),
[ conforms({ 'address': isString }), this.read ],
[ () => true,
(cmd) => this.stderr(`Wrong PLC command [${cmd}].`) ]
(cmd) => {
throw new Error(`Wrong PLC command [${JSON.stringify(cmd)}].`);
} ]
]);
}
......@@ -58,7 +60,7 @@ class PLC extends Action {
this.stdout(`Connecting to PLC... (host: '${this.host}'\n`);
this._retProm = makeDeferred();
this.cli.initiateConnection(opts, (err) => {
this.cli.initiateConnection(opts, async (err) => {
if (err) {
// @ts-ignore: '_retProm' cannot be null here
this._retProm.reject(new Error(
......@@ -68,7 +70,11 @@ class PLC extends Action {
}
this.stdout(`Connected.\n`);
for (const cmd of plcCmds) { this.run(cmd); }
for (const cmd of plcCmds) {
await this.run(cmd)
.then(this.stdout)
.catch(this.stderr);
}
this.cli.dropConnection(() => this.stdout('Disconnected.\n'));
......@@ -81,48 +87,62 @@ class PLC extends Action {
/**
* @param {PlcCmd} cmd
* @return {Promise<string>}
*/
write(cmd) {
if (!this._retProm) { return; }
async write(cmd) {
if (!this._retProm) {
throw new Error(`Cannot execute command '${JSON.stringify(cmd)}'`);
}
const done = makeDeferred();
const code = this.cli.writeItems(cmd.address,
/** @type {NodeS7.S7Types} */(cmd.value), (err) => {
if (err) {
this.stderr(`Failed to perform command '${JSON.stringify(cmd)}': ` +
'invalid command.');
done.reject(
new Error(`Failed to perform command '${JSON.stringify(cmd)}': ` +
'invalid command.'));
}
else {
this.stdout(`Command '${JSON.stringify(cmd)}' done.`);
done.resolve(`Command '${JSON.stringify(cmd)}' done.`);
}
});
if (code !== 0) {
this.stderr(`Failed to perform command '${JSON.stringify(cmd)}': ` +
'another writing is in progress.');
done.reject(
new Error(`Failed to perform command '${JSON.stringify(cmd)}': ` +
'another writing is in progress.'));
}
return done.promise;
}
/**
* @param {PlcCmd} cmd
* @return {Promise<string>}
*/
read(cmd) {
if (!this._retProm) { return; }
async read(cmd) {
if (!this._retProm) {
throw new Error(`Cannot execute command '${JSON.stringify(cmd)}'`);
}
const done = makeDeferred();
this.cli.addItems(cmd.address);
this.cli.readAllItems((err, values) => {
if (err) {
this.stderr(`Failed to perform command '${JSON.stringify(cmd)}': ` +
'invalid command.\n');
done.reject(
new Error(`Failed to perform command '${JSON.stringify(cmd)}': ` +
'invalid command.\n'));
}
else if (values) {
this.stdout('Values read:\n');
forOwn(values, (val, key) => this.stdout(`${key}: ${val}\n`));
let output = 'Values read:\n';
forOwn(values, (val, key) => { output += `- '${key}': ${val}\n`; });
done.resolve(output);
}
else {
this.stderr(
`Command '${JSON.stringify(cmd)}' performed but no value read.\n`);
done.reject(new Error(
`Command '${JSON.stringify(cmd)}' performed but no value read.\n`));
}
});
return done.promise;
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment