Skip to content

Commit

Permalink
Merge pull request #150 from yaacov/make-is-open-property
Browse files Browse the repository at this point in the history
Make isOpen property
  • Loading branch information
yaacov authored Oct 1, 2017
2 parents 363b98b + da6c13b commit 0d8d90a
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 104 deletions.
2 changes: 1 addition & 1 deletion examples/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function connect() {
clearTimeout(timeoutConnectRef);

// if client already open, just run
if (client.isOpen()) {
if (client.isOpen) {
run();
}

Expand Down
2 changes: 1 addition & 1 deletion examples/logger_complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function connect() {
clearTimeout(timeoutConnectRef);

// if client already open, just run
if (client.isOpen()) {
if (client.isOpen) {
run();
}

Expand Down
37 changes: 20 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,20 @@ ModbusRTU.prototype.open = function(callback) {
callback(error);
}
});

/**
* Check if port is open
*/
Object.defineProperty(this, "isOpen", {
enumerable: true,
get: function() {
if (this._port) {
return this._port.isOpen;
}

return false;
}
});
};

/**
Expand All @@ -350,17 +364,6 @@ ModbusRTU.prototype.close = function(callback) {
}
};

/**
* Check if port is open
*/
ModbusRTU.prototype.isOpen = function() {
if (this._port) {
return this._port.isOpen();
}

return false;
};

/**
* Write a Modbus "Read Coil Status" (FC=01) to serial port.
*
Expand All @@ -383,7 +386,7 @@ ModbusRTU.prototype.writeFC1 = function(address, dataAddress, length, next) {
*/
ModbusRTU.prototype.writeFC2 = function(address, dataAddress, length, next, code) {
// check port is actually open before attempting write
if (this.isOpen() !== true) {
if (this.isOpen !== true) {
if (next) next(new PortNotOpenError());
return;
}
Expand Down Expand Up @@ -442,7 +445,7 @@ ModbusRTU.prototype.writeFC3 = function(address, dataAddress, length, next) {
*/
ModbusRTU.prototype.writeFC4 = function(address, dataAddress, length, next, code) {
// check port is actually open before attempting write
if (this.isOpen() !== true) {
if (this.isOpen !== true) {
if (next) next(new PortNotOpenError());
return;
}
Expand Down Expand Up @@ -489,7 +492,7 @@ ModbusRTU.prototype.writeFC4 = function(address, dataAddress, length, next, code
*/
ModbusRTU.prototype.writeFC5 = function(address, dataAddress, state, next) {
// check port is actually open before attempting write
if (this.isOpen() !== true) {
if (this.isOpen !== true) {
if (next) next(new PortNotOpenError());
return;
}
Expand Down Expand Up @@ -540,7 +543,7 @@ ModbusRTU.prototype.writeFC5 = function(address, dataAddress, state, next) {
*/
ModbusRTU.prototype.writeFC6 = function(address, dataAddress, value, next) {
// check port is actually open before attempting write
if (this.isOpen() !== true) {
if (this.isOpen !== true) {
if (next) next(new PortNotOpenError());
return;
}
Expand Down Expand Up @@ -587,7 +590,7 @@ ModbusRTU.prototype.writeFC6 = function(address, dataAddress, value, next) {
*/
ModbusRTU.prototype.writeFC15 = function(address, dataAddress, array, next) {
// check port is actually open before attempting write
if (this.isOpen() !== true) {
if (this.isOpen !== true) {
if (next) next(new PortNotOpenError());
return;
}
Expand Down Expand Up @@ -649,7 +652,7 @@ ModbusRTU.prototype.writeFC15 = function(address, dataAddress, array, next) {
*/
ModbusRTU.prototype.writeFC16 = function(address, dataAddress, array, next) {
// check port is actually open before attempting write
if (this.isOpen() !== true) {
if (this.isOpen !== true) {
if (next) next(new PortNotOpenError());
return;
}
Expand Down
21 changes: 12 additions & 9 deletions ports/asciiport.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ var AsciiPort = function(path, options) {
});

EventEmitter.call(this);

/**
* Check if port is open.
*
* @returns {boolean}
*/
Object.defineProperty(this, "isOpen", {
enumerable: true,
get: function() {
return this._client.isOpen;
}
});
};
util.inherits(AsciiPort, EventEmitter);

Expand All @@ -191,15 +203,6 @@ AsciiPort.prototype.close = function(callback) {
this._client.close(callback);
};

/**
* Check if port is open.
*
* @returns {boolean}
*/
AsciiPort.prototype.isOpen = function() {
return this._client.isOpen();
};

/**
* Send data to a modbus slave.
*
Expand Down
21 changes: 12 additions & 9 deletions ports/c701port.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ var UdpPort = function(ip, options) {
});

EventEmitter.call(this);

/**
* Check if port is open.
*
* @returns {boolean}
*/
Object.defineProperty(this, "isOpen", {
enumerable: true,
get: function() {
return this.openFlag;
}
});
};
util.inherits(UdpPort, EventEmitter);

Expand All @@ -129,15 +141,6 @@ UdpPort.prototype.close = function(callback) {
callback(null);
};

/**
* Check if port is open.
*
* @returns {boolean}
*/
UdpPort.prototype.isOpen = function() {
return this.openFlag;
};

/**
* Send data to a modbus-tcp slave.
*
Expand Down
21 changes: 12 additions & 9 deletions ports/rtubufferedport.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ var RTUBufferedPort = function(path, options) {
});

EventEmitter.call(this);

/**
* Check if port is open.
*
* @returns {boolean}
*/
Object.defineProperty(this, "isOpen", {
enumerable: true,
get: function() {
return this._client.isOpen;
}
});
};
util.inherits(RTUBufferedPort, EventEmitter);

Expand Down Expand Up @@ -115,15 +127,6 @@ RTUBufferedPort.prototype.close = function(callback) {
this._client.close(callback);
};

/**
* Check if port is open.
*
* @returns {boolean}
*/
RTUBufferedPort.prototype.isOpen = function() {
return this._client.isOpen();
};

/**
* Send data to a modbus slave.
*
Expand Down
21 changes: 12 additions & 9 deletions ports/tcpport.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ var TcpPort = function(ip, options) {
});

EventEmitter.call(this);

/**
* Check if port is open.
*
* @returns {boolean}
*/
Object.defineProperty(this, "isOpen", {
enumerable: true,
get: function() {
return this.openFlag;
}
});
};
util.inherits(TcpPort, EventEmitter);

Expand All @@ -118,15 +130,6 @@ TcpPort.prototype.close = function(callback) {
this._client.end();
};

/**
* Check if port is open.
*
* @returns {boolean}
*/
TcpPort.prototype.isOpen = function() {
return this.openFlag;
};

/**
* Send data to a modbus-tcp slave.
*
Expand Down
21 changes: 12 additions & 9 deletions ports/tcprtubufferedport.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ var TcpRTUBufferedPort = function(ip, options) {
});

EventEmitter.call(this);

/**
* Check if port is open.
*
* @returns {boolean}
*/
Object.defineProperty(this, "isOpen", {
enumerable: true,
get: function() {
return this.openFlag;
}
});
};
util.inherits(TcpRTUBufferedPort, EventEmitter);

Expand Down Expand Up @@ -166,15 +178,6 @@ TcpRTUBufferedPort.prototype.close = function(callback) {
this._client.end();
};

/**
* Check if port is open.
*
* @returns {boolean}
*/
TcpRTUBufferedPort.prototype.isOpen = function() {
return this.openFlag;
};

/**
* Send data to a modbus slave via telnet server.
*
Expand Down
21 changes: 12 additions & 9 deletions ports/telnetport.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ var TelnetPort = function(ip, options) {
});

EventEmitter.call(this);

/**
* Check if port is open.
*
* @returns {boolean}
*/
Object.defineProperty(this, "isOpen", {
enumerable: true,
get: function() {
return this.openFlag;
}
});
};
util.inherits(TelnetPort, EventEmitter);

Expand Down Expand Up @@ -140,15 +152,6 @@ TelnetPort.prototype.close = function(callback) {
this._client.end();
};

/**
* Check if port is open.
*
* @returns {boolean}
*/
TelnetPort.prototype.isOpen = function() {
return this.openFlag;
};

/**
* Send data to a modbus slave via telnet server.
*
Expand Down
21 changes: 12 additions & 9 deletions ports/testport.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ var TestPort = function() {
this._coils = 0x0000; // TODO 0xa12b, 1010 0001 0010 1011

EventEmitter.call(this);

/**
* Check if port is open.
*
* @returns {boolean}
*/
Object.defineProperty(this, "isOpen", {
enumerable: true,
get: function() {
return true;
}
});
};
util.inherits(TestPort, EventEmitter);

Expand All @@ -55,15 +67,6 @@ TestPort.prototype.close = function(callback) {
callback(null);
};

/**
* Check if port is open.
*
* @returns {boolean}
*/
TestPort.prototype.isOpen = function() {
return true;
};

/**
* Simulate successful/failure port requests and replays.
*
Expand Down
11 changes: 7 additions & 4 deletions test/mocks/SerialPortMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ var SerialPortMock = function(path, options, callback) {
if (callback) {
callback(null);
}

Object.defineProperty(this, "isOpen", {
enumerable: true,
get: function() {
return this._openFlag;
}
});
};
util.inherits(SerialPortMock, EventEmitter);

Expand All @@ -24,10 +31,6 @@ SerialPortMock.prototype.open = function(callback) {
this.emit("open");
};

SerialPortMock.prototype.isOpen = function() {
return this._openFlag;
};

SerialPortMock.prototype.write = function(buffer, callback) {
this._data = buffer;
if (callback) {
Expand Down
Loading

0 comments on commit 0d8d90a

Please sign in to comment.