Search in sources :

Example 6 with DataRecord

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

the class DriverConnection method read.

@Override
public Object read(List<ChannelRecordContainer> containers, Object containerListHandle, String samplingGroup) throws ConnectionException {
    synchronized (connectionInterface) {
        List<DataRecord> dataRecords = new ArrayList<>();
        if (!connectionInterface.isOpen()) {
            throw new ConnectionException("Connection " + connectionInterface.getInterfaceAddress() + " is closed.");
        }
        MBusConnection mBusConnection = connectionInterface.getMBusConnection();
        if (secondaryAddress != null) {
            try {
                mBusConnection.selectComponent(secondaryAddress);
                sleep(delay);
            } catch (IOException e) {
                for (ChannelRecordContainer container : containers) {
                    container.setRecord(new Record(Flag.DRIVER_ERROR_UNSPECIFIED));
                }
                connectionInterface.close();
                logger.error(e.getMessage());
                throw new ConnectionException(e);
            }
        }
        try {
            if (secondaryAddress == null) {
                if (resetLink) {
                    mBusConnection.linkReset(mBusAddress);
                    sleep(delay);
                }
                if (resetApplication) {
                    mBusConnection.resetReadout(mBusAddress);
                    sleep(delay);
                }
            }
            VariableDataStructure variableDataStructure = null;
            do {
                variableDataStructure = mBusConnection.read(mBusAddress);
                sleep(delay);
                dataRecords.addAll(variableDataStructure.getDataRecords());
            } while (variableDataStructure.moreRecordsFollow());
        } catch (IOException e) {
            for (ChannelRecordContainer container : containers) {
                container.setRecord(new Record(Flag.DRIVER_ERROR_UNSPECIFIED));
            }
            connectionInterface.close();
            logger.error(e.getMessage());
            throw new ConnectionException(e);
        }
        long timestamp = System.currentTimeMillis();
        String[] dibvibs = new String[dataRecords.size()];
        setDibVibs(dataRecords, dibvibs);
        boolean selectForReadoutSet = setRecords(containers, mBusConnection, timestamp, dataRecords, dibvibs);
        if (selectForReadoutSet) {
            try {
                mBusConnection.resetReadout(mBusAddress);
                sleep(delay);
            } catch (IOException e) {
                try {
                    mBusConnection.linkReset(mBusAddress);
                    sleep(delay);
                } catch (IOException e1) {
                    for (ChannelRecordContainer container : containers) {
                        container.setRecord(new Record(Flag.CONNECTION_EXCEPTION));
                    }
                    connectionInterface.close();
                    logger.error("{}\n{}", e.getMessage(), e1.getMessage());
                    throw new ConnectionException(e);
                }
            }
        }
        return null;
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) MBusConnection(org.openmuc.jmbus.MBusConnection) ChannelRecordContainer(org.openmuc.framework.driver.spi.ChannelRecordContainer) DataRecord(org.openmuc.jmbus.DataRecord) Record(org.openmuc.framework.data.Record) DataRecord(org.openmuc.jmbus.DataRecord) VariableDataStructure(org.openmuc.jmbus.VariableDataStructure) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException)

Aggregations

DataRecord (org.openmuc.jmbus.DataRecord)6 VariableDataStructure (org.openmuc.jmbus.VariableDataStructure)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ConnectionException (org.openmuc.framework.driver.spi.ConnectionException)3 MBusDataRecord (net.solarnetwork.node.io.mbus.MBusDataRecord)2 Record (org.openmuc.framework.data.Record)2 ChannelRecordContainer (org.openmuc.framework.driver.spi.ChannelRecordContainer)2 DecodingException (org.openmuc.jmbus.DecodingException)2 MBusConnection (org.openmuc.jmbus.MBusConnection)2 MBusData (net.solarnetwork.node.io.mbus.MBusData)1 MBusMessage (net.solarnetwork.node.io.mbus.MBusMessage)1 ChannelScanInfo (org.openmuc.framework.config.ChannelScanInfo)1 WMBusMessage (org.openmuc.jmbus.wireless.WMBusMessage)1 SerialPortTimeoutException (org.openmuc.jrxtx.SerialPortTimeoutException)1