Search in sources :

Example 1 with ModbusSlaveConnection

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;
}
Also used : SerialConnection(net.wimpi.modbus.net.SerialConnection) ModbusSerialTransaction(net.wimpi.modbus.io.ModbusSerialTransaction) ModbusSlaveConnection(net.wimpi.modbus.net.ModbusSlaveConnection)

Example 2 with ModbusSlaveConnection

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);
    }
}
Also used : ModbusSlaveEndpoint(org.openhab.binding.modbus.internal.pooling.ModbusSlaveEndpoint) ModbusResponse(net.wimpi.modbus.msg.ModbusResponse) ModbusSlaveConnection(net.wimpi.modbus.net.ModbusSlaveConnection) ModbusException(net.wimpi.modbus.ModbusException)

Example 3 with ModbusSlaveConnection

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;
}
Also used : ModbusSlaveConnection(net.wimpi.modbus.net.ModbusSlaveConnection) ModbusException(net.wimpi.modbus.ModbusException)

Example 4 with ModbusSlaveConnection

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);
}
Also used : ModbusSlaveEndpoint(org.openhab.binding.modbus.internal.pooling.ModbusSlaveEndpoint) ModbusTCPSlaveEndpoint(org.openhab.binding.modbus.internal.pooling.ModbusTCPSlaveEndpoint) ModbusSlaveConnection(net.wimpi.modbus.net.ModbusSlaveConnection) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) Test(org.junit.Test)

Example 5 with ModbusSlaveConnection

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);
}
Also used : ModbusSlaveConnection(net.wimpi.modbus.net.ModbusSlaveConnection)

Aggregations

ModbusSlaveConnection (net.wimpi.modbus.net.ModbusSlaveConnection)10 ModbusSlaveEndpoint (org.openhab.binding.modbus.internal.pooling.ModbusSlaveEndpoint)4 ModbusException (net.wimpi.modbus.ModbusException)3 ModbusResponse (net.wimpi.modbus.msg.ModbusResponse)2 UnknownHostException (java.net.UnknownHostException)1 NoSuchElementException (java.util.NoSuchElementException)1 ModbusSerialTransaction (net.wimpi.modbus.io.ModbusSerialTransaction)1 ModbusTCPTransaction (net.wimpi.modbus.io.ModbusTCPTransaction)1 ModbusUDPTransaction (net.wimpi.modbus.io.ModbusUDPTransaction)1 SerialConnection (net.wimpi.modbus.net.SerialConnection)1 TCPMasterConnection (net.wimpi.modbus.net.TCPMasterConnection)1 UDPMasterConnection (net.wimpi.modbus.net.UDPMasterConnection)1 SwallowedExceptionListener (org.apache.commons.pool2.SwallowedExceptionListener)1 GenericKeyedObjectPool (org.apache.commons.pool2.impl.GenericKeyedObjectPool)1 Test (org.junit.Test)1 ModbusSlaveConnectionFactoryImpl (org.openhab.binding.modbus.internal.pooling.ModbusSlaveConnectionFactoryImpl)1 ModbusTCPSlaveEndpoint (org.openhab.binding.modbus.internal.pooling.ModbusTCPSlaveEndpoint)1 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1