Skip to content

Commit

Permalink
lint the code
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreMaz committed Oct 19, 2023
1 parent 0795067 commit 7bf3a19
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"es6": true
},
"parserOptions": {
"ecmaVersion": 2017
"ecmaVersion": 2020
},
"extends": "eslint:recommended",
"rules": {
Expand Down
70 changes: 35 additions & 35 deletions examples/tcp_client_abort_signal.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
"use strict";

// create an empty modbus client
//let ModbusRTU = require("modbus-serial");
// let ModbusRTU = require("modbus-serial");
const ModbusRTU = require("../index");
const client = new ModbusRTU();

const abortController = new AbortController();
const { signal } = abortController;
signal.addEventListener("abort", () => {
console.log("Abort signal received by the abort controller");
console.log("Abort signal received by the abort controller");
});

async function connect() {
await client.connectTCP("127.0.0.1", {
port: 8502,
socketOpts: {
signal: signal,
},
});
client.setID(1);
client.setTimeout(2000);
await client.connectTCP("127.0.0.1", {
port: 8502,
socketOpts: {
signal: signal
}
});
client.setID(1);
client.setTimeout(2000);
}

async function readRegisters() {
const data = await client.readHoldingRegisters(5, 4);
console.log("Received:", data.data);
const data = await client.readHoldingRegisters(5, 4);
console.log("Received:", data.data);
}

async function runner() {
await connect();
await connect();

setTimeout(() => {
abortController.abort("Aborting request");
}, 1000);
setTimeout(() => {
abortController.abort("Aborting request");
}, 1000);

await readRegisters();
await readRegisters();
}

runner()
.then(() => {
if (signal.aborted) {
if (signal.reason) {
console.log(`Request aborted with reason: ${signal.reason}`);
} else {
console.log("Request aborted but no reason was given.");
}
} else {
console.log("Request not aborted");
}
})
.catch((error) => {
console.error(error);
})
.finally(async () => {
console.log("Close client");
await client.close();
});
.then(() => {
if (signal.aborted) {
if (signal.reason) {
console.log(`Request aborted with reason: ${signal.reason}`);
} else {
console.log("Request aborted but no reason was given.");
}
} else {
console.log("Request not aborted");
}
})
.catch((error) => {
console.error(error);
})
.finally(async() => {
console.log("Close client");
await client.close();
});
6 changes: 3 additions & 3 deletions ports/tcpport.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class TcpPort extends EventEmitter {
// Default options
...{
host: ip || options.ip,
port: MODBUS_PORT,
port: MODBUS_PORT
},
// User options
...options,
...options
};

if(options.socket) {
Expand All @@ -94,7 +94,7 @@ class TcpPort extends EventEmitter {
this._client = this._externalSocket || new net.Socket(this.socketOpts);

if (options.timeout) this._client.setTimeout(options.timeout);

// register events handlers
this._client.on("data", function(data) {
let buffer;
Expand Down
39 changes: 20 additions & 19 deletions test/ports/tcpport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,54 +28,55 @@ describe("Modbus TCP port methods", function() {
const TcpPort = require("./../../ports/tcpport");
it("with ip as string", function() {
const port = new TcpPort("localhost");

expect(port).to.be.an.instanceOf(TcpPort);
expect(port.connectOptions.host).to.equal("localhost");
expect(port.connectOptions.port).to.equal(502);
});

it("with ip as object", function() {
const port = new TcpPort({ip: "localhost"});
const port = new TcpPort({ ip: "localhost" });

expect(port).to.be.an.instanceOf(TcpPort);
expect(port.connectOptions.host).to.equal("localhost");
expect(port.connectOptions.port).to.equal(502);
});

it("with ip as object and port as number", function() {
const port = new TcpPort({ip: "localhost", port: 9999});
const port = new TcpPort({ ip: "localhost", port: 9999 });

expect(port).to.be.an.instanceOf(TcpPort);
expect(port.connectOptions.host).to.equal("localhost");
expect(port.connectOptions.port).to.equal(9999);
});

it("with ip as string and options as object", function() {
const port = new TcpPort("localhost", { port: 9999 });

expect(port).to.be.an.instanceOf(TcpPort);
expect(port.connectOptions.host).to.equal("localhost");
expect(port.connectOptions.port).to.equal(9999);
});

it("with socket creation options", function() {
const controller = new AbortController();
const port = new TcpPort("localhost", { port: 9999, socketOpts: {
allowHalfOpen: true,
readable: true,
writable: true,
signal: controller.signal,
}});

const port = new TcpPort("localhost", { port: 9999,
socketOpts: {
allowHalfOpen: true,
readable: true,
writable: true,
signal: controller.signal
} });

expect(port).to.be.an.instanceOf(TcpPort);
expect(port.connectOptions.host).to.equal("localhost");
expect(port.connectOptions.port).to.equal(9999);
expect(port.socketOpts).to.deep.equal({
allowHalfOpen: true,
readable: true,
writable: true,
signal: controller.signal,
});
signal: controller.signal
});
});
});

Expand Down

0 comments on commit 7bf3a19

Please sign in to comment.