Search in sources :

Example 6 with VariableDataStructure

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

the class DriverConnectionTest method testScanThrowsTimeoutException.

@Test(expected = ConnectionException.class)
public void testScanThrowsTimeoutException() throws Exception {
    MBusConnection con = mock(MBusConnection.class);
    VariableDataStructure vds = new VariableDataStructure(NZR_ANSWER, 6, NZR_ANSWER.length - 6, null, null);
    vds.decode();
    when(con.read(anyInt())).thenThrow(new SerialPortTimeoutException());
    ConnectionInterface serialIntervace = new ConnectionInterface(con, "/dev/ttyS100:5", delay, interfaces);
    serialIntervace.increaseConnectionCounter();
    String[] deviceAddressTokens = { "/dev/ttyS100", "5" };
    DriverConnection driverCon = new DriverConnection(serialIntervace, Integer.parseInt(deviceAddressTokens[1]), null, delay);
    driverCon.scanForChannels(null);
}
Also used : MBusConnection(org.openmuc.jmbus.MBusConnection) SerialPortTimeoutException(org.openmuc.jrxtx.SerialPortTimeoutException) VariableDataStructure(org.openmuc.jmbus.VariableDataStructure) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with VariableDataStructure

use of org.openmuc.jmbus.VariableDataStructure 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 8 with VariableDataStructure

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

the class DriverTest method testInterrupedException.

@Test(expected = ScanInterruptedException.class)
public void testInterrupedException() throws Exception {
    final Driver mdriver = new Driver();
    DriverDeviceScanListener ddsl = mock(DriverDeviceScanListener.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            mdriver.interruptDeviceScan();
            return null;
        }
    }).when(ddsl).deviceFound(any(DeviceScanInfo.class));
    MBusConnection con = mock(MBusConnection.class);
    when(con.read(anyInt())).thenReturn(new VariableDataStructure(null, 0, 0, null, null));
    whenNew(MBusConnection.class).withAnyArguments().thenReturn(con);
    doNothing().when(con).linkReset(ArgumentMatchers.anyInt());
    PowerMockito.when(con.read(ArgumentMatchers.anyInt())).thenReturn(null);
    mdriver.scanForDevices("/dev/ttyS100:2400", ddsl);
}
Also used : MBusConnection(org.openmuc.jmbus.MBusConnection) DeviceScanInfo(org.openmuc.framework.config.DeviceScanInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DriverDeviceScanListener(org.openmuc.framework.driver.spi.DriverDeviceScanListener) VariableDataStructure(org.openmuc.jmbus.VariableDataStructure) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with VariableDataStructure

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

the class Driver method scanPrimaryAddress.

private void scanPrimaryAddress(DriverDeviceScanListener listener, Settings settings, MBusConnection mBusConnection) throws ScanInterruptedException, ScanException {
    VariableDataStructure dataStructure = null;
    for (int i = 0; i <= 250; i++) {
        if (interruptScan) {
            throw new ScanInterruptedException();
        }
        if (i % 5 == 0) {
            listener.scanProgressUpdate(i * 100 / 250);
        }
        logger.debug("scanning for meter with primary address {}", i);
        try {
            dataStructure = mBusConnection.read(i);
            sleep(settings.delay);
        } catch (InterruptedIOException e) {
            logger.debug("No meter found on address {}", i);
            continue;
        } catch (IOException e) {
            throw new ScanException(e);
        } catch (ConnectionException e) {
            throw new ScanException(e);
        }
        String description = "";
        if (dataStructure != null) {
            SecondaryAddress secondaryAddress = dataStructure.getSecondaryAddress();
            description = getScanDescription(secondaryAddress);
        }
        listener.deviceFound(new DeviceScanInfo(settings.scanConnectionAddress + ':' + i, "", description));
        logger.debug("Meter found on address {}", i);
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) DeviceScanInfo(org.openmuc.framework.config.DeviceScanInfo) ScanException(org.openmuc.framework.config.ScanException) SecondaryAddress(org.openmuc.jmbus.SecondaryAddress) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) VariableDataStructure(org.openmuc.jmbus.VariableDataStructure) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ScanInterruptedException(org.openmuc.framework.config.ScanInterruptedException)

Example 10 with VariableDataStructure

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

the class DriverConnection method scanForChannels.

@Override
public List<ChannelScanInfo> scanForChannels(String settings) throws UnsupportedOperationException, ConnectionException {
    int scanDelay = 50 + this.delay;
    synchronized (connectionInterface) {
        List<ChannelScanInfo> channelScanInfo = new ArrayList<>();
        try {
            MBusConnection mBusConnection = connectionInterface.getMBusConnection();
            if (secondaryAddress != null) {
                mBusConnection.selectComponent(secondaryAddress);
            } else {
                mBusConnection.linkReset(mBusAddress);
                sleep(delay);
                mBusConnection.resetReadout(mBusAddress);
            }
            VariableDataStructure variableDataStructure;
            do {
                sleep(scanDelay);
                variableDataStructure = mBusConnection.read(mBusAddress);
                List<DataRecord> dataRecords = variableDataStructure.getDataRecords();
                for (DataRecord dataRecord : dataRecords) {
                    fillDataRecordInChannelScanInfo(channelScanInfo, dataRecord);
                }
            } while (variableDataStructure.moreRecordsFollow());
        } catch (IOException e) {
            throw new ConnectionException(e);
        }
        return channelScanInfo;
    }
}
Also used : ChannelScanInfo(org.openmuc.framework.config.ChannelScanInfo) MBusConnection(org.openmuc.jmbus.MBusConnection) ArrayList(java.util.ArrayList) DataRecord(org.openmuc.jmbus.DataRecord) IOException(java.io.IOException) VariableDataStructure(org.openmuc.jmbus.VariableDataStructure) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException)

Aggregations

VariableDataStructure (org.openmuc.jmbus.VariableDataStructure)15 MBusConnection (org.openmuc.jmbus.MBusConnection)12 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 IOException (java.io.IOException)7 ChannelRecordContainer (org.openmuc.framework.driver.spi.ChannelRecordContainer)5 SerialPortTimeoutException (org.openmuc.jrxtx.SerialPortTimeoutException)5 ConnectionException (org.openmuc.framework.driver.spi.ConnectionException)4 DataRecord (org.openmuc.jmbus.DataRecord)4 ArrayList (java.util.ArrayList)3 DriverDeviceScanListener (org.openmuc.framework.driver.spi.DriverDeviceScanListener)3 InterruptedIOException (java.io.InterruptedIOException)2 ChannelScanInfo (org.openmuc.framework.config.ChannelScanInfo)2 DeviceScanInfo (org.openmuc.framework.config.DeviceScanInfo)2 ScanInterruptedException (org.openmuc.framework.config.ScanInterruptedException)2 Record (org.openmuc.framework.data.Record)2 SecondaryAddress (org.openmuc.jmbus.SecondaryAddress)2 LinkedList (java.util.LinkedList)1 MBusDataRecord (net.solarnetwork.node.io.mbus.MBusDataRecord)1 MBusMessage (net.solarnetwork.node.io.mbus.MBusMessage)1