Search in sources :

Example 1 with SecondaryAddress

use of org.openmuc.jmbus.SecondaryAddress in project OpenMUC by isc-konstanz.

the class DriverConnectionTest method newConnection.

private DriverConnection newConnection(String mBusAdresse) throws Exception {
    MBusConnection con = mock(MBusConnection.class);
    VariableDataStructure vds = new VariableDataStructure(NZR_ANSWER, 6, NZR_ANSWER.length - 6, null, null);
    vds.decode();
    PowerMockito.when(con.read(anyInt())).thenReturn(vds);
    ConnectionInterface serialIntervace = new ConnectionInterface(con, mBusAdresse, delay, interfaces);
    serialIntervace.increaseConnectionCounter();
    String[] deviceAddressTokens = mBusAdresse.trim().split(":");
    Integer mBusAddress;
    SecondaryAddress secondaryAddress = null;
    if (deviceAddressTokens[1].length() == 16) {
        mBusAddress = 0xfd;
        byte[] addressData = Helper.hexToBytes(deviceAddressTokens[1]);
        secondaryAddress = SecondaryAddress.newFromLongHeader(addressData, 0);
    } else {
        mBusAddress = Integer.decode(deviceAddressTokens[1]);
    }
    DriverConnection mBusConnection = new DriverConnection(serialIntervace, mBusAddress, secondaryAddress, delay);
    return mBusConnection;
}
Also used : MBusConnection(org.openmuc.jmbus.MBusConnection) SecondaryAddress(org.openmuc.jmbus.SecondaryAddress) VariableDataStructure(org.openmuc.jmbus.VariableDataStructure)

Example 2 with SecondaryAddress

use of org.openmuc.jmbus.SecondaryAddress in project OpenMUC by isc-konstanz.

the class Driver method connect.

@Override
public Connection connect(String deviceAddress, String settingsString) throws ArgumentSyntaxException, ConnectionException {
    String[] deviceAddressTokens = deviceAddress.trim().split(":");
    String serialPortName = "";
    boolean isTCP = false;
    String host = "";
    int port = 0;
    int offset;
    if (deviceAddressTokens[0].equalsIgnoreCase(TCP)) {
        host = deviceAddressTokens[1];
        try {
            port = Integer.parseInt(deviceAddressTokens[2]);
        } catch (NumberFormatException e) {
            throw new ArgumentSyntaxException("Could not parse port.");
        }
        isTCP = true;
        offset = 2;
    } else {
        if (deviceAddressTokens.length != 2) {
            throw new ArgumentSyntaxException("The device address does not consist of two parameters.");
        }
        offset = 0;
        serialPortName = deviceAddressTokens[0 + offset];
    }
    Integer mBusAddress;
    SecondaryAddress secondaryAddress = null;
    try {
        if (deviceAddressTokens[1 + offset].length() == 16) {
            mBusAddress = 0xfd;
            byte[] saData = Helper.hexToBytes(deviceAddressTokens[1 + offset]);
            secondaryAddress = SecondaryAddress.newFromLongHeader(saData, 0);
        } else {
            mBusAddress = Integer.decode(deviceAddressTokens[1 + offset]);
        }
    } catch (Exception e) {
        throw new ArgumentSyntaxException("Settings: mBusAddress (" + deviceAddressTokens[1 + offset] + ") is not a number between 0 and 255 nor a 16 sign long hexadecimal secondary address");
    }
    ConnectionInterface connectionInterface;
    Settings settings = new Settings(settingsString, false);
    synchronized (this) {
        synchronized (interfaces) {
            connectionInterface = setConnectionInterface(deviceAddressTokens, serialPortName, isTCP, host, port, offset, settings);
        }
        synchronized (connectionInterface) {
            if (strictConnectionTest) {
                try {
                    testConnection(mBusAddress, secondaryAddress, connectionInterface, settings);
                } catch (IOException e) {
                    connectionInterface.close();
                    throw new ConnectionException(e);
                }
            }
            connectionInterface.increaseConnectionCounter();
        }
    }
    DriverConnection driverCon = new DriverConnection(connectionInterface, mBusAddress, secondaryAddress, settings.delay);
    driverCon.setResetLink(settings.resetLink);
    driverCon.setResetApplication(settings.resetApplication);
    return driverCon;
}
Also used : SecondaryAddress(org.openmuc.jmbus.SecondaryAddress) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException) ScanException(org.openmuc.framework.config.ScanException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ScanInterruptedException(org.openmuc.framework.config.ScanInterruptedException) SerialPortTimeoutException(org.openmuc.jrxtx.SerialPortTimeoutException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException)

Example 3 with SecondaryAddress

use of org.openmuc.jmbus.SecondaryAddress in project OpenMUC by isc-konstanz.

the class Driver method parseSecondaryAddress.

private SecondaryAddress parseSecondaryAddress(String secondaryAddressAsString) throws ArgumentSyntaxException, DecoderException {
    SecondaryAddress secondaryAddress;
    try {
        byte[] bytes = Hex.decodeHex(secondaryAddressAsString);
        secondaryAddress = SecondaryAddress.newFromWMBusHeader(bytes, 0);
    } catch (NumberFormatException e) {
        throw new ArgumentSyntaxException("The SecondaryAddress: " + secondaryAddressAsString + " could not be converted to a byte array.");
    }
    return secondaryAddress;
}
Also used : SecondaryAddress(org.openmuc.jmbus.SecondaryAddress) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException)

Example 4 with SecondaryAddress

use of org.openmuc.jmbus.SecondaryAddress in project OpenMUC by isc-konstanz.

the class Driver method connect.

@Override
public Connection connect(String deviceAddress, String settings) throws ArgumentSyntaxException, ConnectionException {
    String[] deviceAddressTokens = deviceAddress.trim().split(":");
    if (deviceAddressTokens.length != 2) {
        throw new ArgumentSyntaxException("The device address does not consist of two parameters.");
    }
    String connectionPort = deviceAddressTokens[0];
    String secondaryAddressAsString = "";
    String host = "";
    int port = 0;
    boolean isTCP = false;
    if (connectionPort.equalsIgnoreCase("TCP")) {
        isTCP = true;
        host = deviceAddressTokens[0];
        try {
            port = Integer.decode(deviceAddressTokens[1]);
        } catch (NumberFormatException e) {
            throw new ArgumentSyntaxException("TCP port is not a number.");
        }
        secondaryAddressAsString = deviceAddressTokens[2].toLowerCase();
    } else {
        secondaryAddressAsString = deviceAddressTokens[1].toLowerCase();
    }
    SecondaryAddress secondaryAddress = null;
    try {
        secondaryAddress = parseSecondaryAddress(secondaryAddressAsString);
    } catch (DecoderException e) {
        e.printStackTrace();
    }
    String[] settingsTokens = splitSettingsToken(settings);
    String transceiverString = settingsTokens[0];
    String modeString = settingsTokens[1];
    String keyString = null;
    if (settingsTokens.length == 3) {
        keyString = settingsTokens[2];
    }
    WMBusInterface wmBusInterface;
    if (isTCP) {
        synchronized (this) {
            wmBusInterface = WMBusInterface.getTCPInstance(host, port, transceiverString, modeString);
            try {
                return wmBusInterface.connect(secondaryAddress, keyString);
            } catch (DecoderException e) {
                e.printStackTrace();
            }
        }
    } else {
        synchronized (this) {
            wmBusInterface = WMBusInterface.getSerialInstance(connectionPort, transceiverString, modeString);
            try {
                return wmBusInterface.connect(secondaryAddress, keyString);
            } catch (DecoderException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
Also used : DecoderException(org.apache.commons.codec.DecoderException) SecondaryAddress(org.openmuc.jmbus.SecondaryAddress) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException)

Example 5 with SecondaryAddress

use of org.openmuc.jmbus.SecondaryAddress in project solarnetwork-node by SolarNetwork.

the class WMBusNetworkTests method prepareConnection.

/**
 * Create and prime the connection with test data
 *
 * @throws IOException
 * @throws DecodingException
 */
@BeforeClass
public static void prepareConnection() throws IOException, DecodingException {
    final byte[] bytes = copyToByteArray(WMBusNetworkTests.class.getResourceAsStream("wmbus-message.bin"));
    final byte[] key = { (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB, (byte) 0xAB };
    final SecondaryAddress address = SecondaryAddress.newFromWMBusHeader(bytes, 2);
    keyMap.put(address, key);
    conn = network.createConnection(JMBusConversion.from(address), key);
}
Also used : SecondaryAddress(org.openmuc.jmbus.SecondaryAddress) BeforeClass(org.junit.BeforeClass)

Aggregations

SecondaryAddress (org.openmuc.jmbus.SecondaryAddress)7 ArgumentSyntaxException (org.openmuc.framework.config.ArgumentSyntaxException)3 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 ScanException (org.openmuc.framework.config.ScanException)2 ScanInterruptedException (org.openmuc.framework.config.ScanInterruptedException)2 ConnectionException (org.openmuc.framework.driver.spi.ConnectionException)2 VariableDataStructure (org.openmuc.jmbus.VariableDataStructure)2 MBusMessage (net.solarnetwork.node.io.mbus.MBusMessage)1 MBusMessageHandler (net.solarnetwork.node.io.mbus.MBusMessageHandler)1 MBusSecondaryAddress (net.solarnetwork.node.io.mbus.MBusSecondaryAddress)1 DecoderException (org.apache.commons.codec.DecoderException)1 BeforeClass (org.junit.BeforeClass)1 DeviceScanInfo (org.openmuc.framework.config.DeviceScanInfo)1 MBusConnection (org.openmuc.jmbus.MBusConnection)1 SerialPortTimeoutException (org.openmuc.jrxtx.SerialPortTimeoutException)1