use of net.wimpi.modbus.net.ModbusSlaveConnection in project openhab1-addons by openhab.
the class ModbusSerialSlave method getConnection.
@Override
protected ModbusSlaveConnection getConnection(ModbusSlaveEndpoint endpoint) {
ModbusSlaveConnection connection = super.getConnection(endpoint);
if (connection == null) {
return null;
}
if (!(connection instanceof SerialConnection)) {
throw new IllegalStateException("Should not happen: wrong connection type for slave " + name);
}
SerialConnection serialConnection = (SerialConnection) connection;
((ModbusSerialTransaction) transaction).setSerialConnection(serialConnection);
return connection;
}
use of net.wimpi.modbus.net.ModbusSlaveConnection in project openhab1-addons by openhab.
the class ModbusSlave method executeWriteRequest.
/**
*
* @param request
* @throws ModbusConnectionException when connection cannot be established
* @throws ModbusException ModbusIOException on IO errors, ModbusSlaveException with protocol level exceptions
* @throws ModbusUnexpectedTransactionIdException when response transaction id does not match the request
*/
private void executeWriteRequest(ModbusRequest request) throws ModbusConnectionException, ModbusException, ModbusUnexpectedTransactionIdException {
ModbusSlaveEndpoint endpoint = getEndpoint();
ModbusSlaveConnection connection = null;
try {
connection = getConnection(endpoint);
if (connection == null) {
logger.warn("ModbusSlave ({}): not connected -- aborting request {}", name, request);
throw new ModbusConnectionException(endpoint);
}
transaction.setRequest(request);
try {
transaction.execute();
} catch (Exception e) {
// Note, one could catch ModbusIOException and ModbusSlaveException if more detailed
// exception handling is required. For now, all exceptions are handled the same way with writes.
logger.error("ModbusSlave ({}): error when executing write request ({}): {}", name, request, e.getMessage());
invalidate(endpoint, connection);
// set connection to null such that it is not returned to pool
connection = null;
throw e;
}
ModbusResponse response = transaction.getResponse();
if ((response.getTransactionID() != transaction.getTransactionID()) && !response.isHeadless()) {
logger.warn("ModbusSlave ({}): Transaction id of the response does not match request {}. Endpoint {}. Connection: {}. Ignoring response.", name, request, endpoint, connection);
throw new ModbusUnexpectedTransactionIdException();
}
} finally {
returnConnection(endpoint, connection);
}
}
use of net.wimpi.modbus.net.ModbusSlaveConnection in project openhab1-addons by openhab.
the class ModbusSlave method borrowConnection.
private ModbusSlaveConnection borrowConnection(ModbusSlaveEndpoint endpoint) {
ModbusSlaveConnection connection = null;
long start = System.currentTimeMillis();
try {
connection = connectionPool.borrowObject(endpoint);
} catch (Exception e) {
invalidate(endpoint, connection);
logger.warn("ModbusSlave ({}): Error getting a new connection for endpoint {}. Error was: {}", name, endpoint, e.getMessage());
}
logger.trace("ModbusSlave ({}): borrowing connection (got {}) for endpoint {} took {} ms", name, connection, endpoint, System.currentTimeMillis() - start);
return connection;
}
use of net.wimpi.modbus.net.ModbusSlaveConnection in project openhab1-addons by openhab.
the class SimultaneousReadWriteTestCase method testPoolBlocks.
@Test
public void testPoolBlocks() throws Exception {
final KeyedObjectPool<ModbusSlaveEndpoint, ModbusSlaveConnection> pool = ModbusBinding.getReconstructedConnectionPoolForTesting();
final ModbusTCPSlaveEndpoint endpoint = new ModbusTCPSlaveEndpoint(localAddress().getHostAddress(), this.tcpModbusPort);
ModbusSlaveConnection borrowObject = pool.borrowObject(endpoint);
Thread thread = new Thread() {
@Override
public void run() {
try {
ModbusSlaveConnection borrowObject2 = pool.borrowObject(endpoint);
pool.returnObject(endpoint, borrowObject2);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
};
thread.start();
thread.join(500);
if (!thread.isAlive()) {
throw new AssertionError("Thread should still be alive -- blocking since no objects");
} else {
thread.interrupt();
}
pool.returnObject(endpoint, borrowObject);
// Now that object has been returned, borrowing should work again
ModbusSlaveConnection borrowObject2 = pool.borrowObject(endpoint);
pool.returnObject(endpoint, borrowObject2);
}
use of net.wimpi.modbus.net.ModbusSlaveConnection in project openhab1-addons by openhab.
the class ModbusSlaveConnectionFactoryImpl method passivateObject.
@Override
public void passivateObject(ModbusSlaveEndpoint endpoint, PooledObject<ModbusSlaveConnection> obj) {
ModbusSlaveConnection connection = obj.getObject();
if (connection == null) {
return;
}
logger.trace("Passivating connection {} for endpoint {}...", connection, endpoint);
lastPassivateMillis.put(endpoint, System.currentTimeMillis());
EndpointPoolConfiguration configuration = endpointPoolConfigs.get(endpoint);
long reconnectAfterMillis = configuration == null ? 0 : configuration.getReconnectAfterMillis();
long connectionAgeMillis = System.currentTimeMillis() - ((PooledConnection) obj).getLastConnected();
if (reconnectAfterMillis == 0 || (reconnectAfterMillis > 0 && connectionAgeMillis > reconnectAfterMillis)) {
logger.trace("(passivate) Connection {} (endpoint {}) age {}ms is over the reconnectAfterMillis={}ms limit -> disconnecting.", connection, endpoint, connectionAgeMillis, reconnectAfterMillis);
connection.resetConnection();
} else {
logger.trace("(passivate) Connection {} (endpoint {}) age ({}ms) is below the reconnectAfterMillis ({}ms) limit. Keep the connection open.", connection, endpoint, connectionAgeMillis, reconnectAfterMillis);
}
logger.trace("...Passivated connection {} for endpoint {}", obj.getObject(), endpoint);
}
Aggregations