Search in sources :

Example 1 with UserDataResponse

use of net.sf.mbus4j.dataframes.UserDataResponse in project ma-modules-public by infiniteautomation.

the class MBusDataSourceRT method doPoll.

@Override
protected synchronized void doPoll(long time) {
    boolean pointError = false;
    final List<UserDataResponse> udrs = new LinkedList<>();
    if (openConnection()) {
        try {
            for (DataPointRT point : dataPoints) {
                final MBusPointLocatorRT locatorRT = point.getPointLocator();
                final MBusPointLocatorVO locatorVo = locatorRT.getVo();
                UserDataResponse udr = getUdr(udrs, locatorVo);
                if (udr == null) {
                    if (locatorVo.isPrimaryAddressing()) {
                        udr = master.readResponse(locatorVo.getAddress());
                    } else {
                        udr = master.readResponseBySecondary(MBusUtils.int2Bcd(locatorVo.getIdentNumber()), locatorVo.getManufacturer(), locatorVo.getVersion(), locatorVo.getMedium());
                    }
                    if (udr == null) {
                        // insert empty UDR to prevent rurter tries in this polling round
                        locatorRT.needCheckDifAndVif = true;
                        udr = new UserDataResponse();
                        udr.setIdentNumber(locatorVo.getIdentNumber());
                        udr.setMedium(locatorVo.getMedium());
                        udr.setManufacturer(locatorVo.getManufacturer());
                        udr.setVersion(locatorVo.getVersion());
                        udrs.add(udr);
                        raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.exception2", locatorVo.getDeviceName(), "Can't read device"));
                        pointError = true;
                    } else {
                        udrs.add(udr);
                    }
                }
                try {
                    if ((locatorVo.getDbIndex() == -1) || locatorRT.needCheckDifAndVif) {
                        int[] idx = findDataBlocks(udr, locatorVo);
                        switch(idx.length) {
                            case 0:
                                LOG.fatal("DataBlock not found: " + locatorVo.toString());
                                LOG.fatal(udr.toString());
                                raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.exception2", locatorVo.toString() + "Can't find datablock"));
                                pointError = true;
                                return;
                            case 1:
                                if (locatorVo.getDbIndex() == idx[0]) {
                                    locatorRT.needCheckDifAndVif = false;
                                } else if (locatorVo.getDbIndex() == -1) {
                                    LOG.info("Set DB Index: " + locatorVo.toString());
                                    locatorVo.setDbIndex(idx[0]);
                                    locatorRT.needCheckDifAndVif = false;
                                } else {
                                    LOG.fatal("Index changed of datablock: " + locatorVo.toString());
                                    LOG.fatal(udr.toString());
                                    raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.exception2", locatorVo.toString() + "Index changed of datablock Please maually correct the dbIndex"));
                                    return;
                                }
                                break;
                            default:
                                if (locatorVo.getDbIndex() == -1) {
                                    LOG.fatal("too many dataBlocks found: " + locatorVo.toString());
                                    LOG.fatal(udr.toString());
                                    raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.exception2", locatorVo.toString() + "Found more then one datablock! Please specify dbIndex manually!"));
                                    return;
                                } else {
                                    for (int i : idx) {
                                        if (i == locatorVo.getDbIndex()) {
                                            locatorRT.needCheckDifAndVif = false;
                                            break;
                                        }
                                    }
                                    if (locatorRT.needCheckDifAndVif) {
                                        LOG.fatal("Matching dataBlock not found: " + locatorVo.toString());
                                        LOG.fatal(udr.toString());
                                        raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.exception2", locatorVo.toString() + "Can't find matching datablock! Please specify dbIndex manually!"));
                                        pointError = true;
                                        return;
                                    }
                                }
                        }
                    }
                    final DataBlock db = udr.getDataBlock(locatorVo.getDbIndex());
                    pointError = setValue(db, pointError, time, point, locatorRT, locatorVo);
                } catch (IndexOutOfBoundsException ex) {
                    // Handle if datablock is not there...
                    raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.exception2", point.getVO().getExtendedName(), "No Data"));
                    pointError = true;
                }
            }
            if (!pointError) {
                returnToNormal(POINT_READ_EXCEPTION_EVENT, time);
            }
            returnToNormal(DATA_SOURCE_EXCEPTION_EVENT, time);
        } catch (InterruptedException ex) {
            LOG.error("doPoll() interrupted", ex);
            raiseEvent(DATA_SOURCE_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.exception2", ex.getMessage(), "doPoll() Interrupted"));
        } catch (IOException ex) {
            LOG.error("doPoll() IO Ex", ex);
            raiseEvent(DATA_SOURCE_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.exception2", ex.getMessage(), "doPoll() IO Ex"));
        } finally {
            closeConnection();
        }
    }
}
Also used : UserDataResponse(net.sf.mbus4j.dataframes.UserDataResponse) IOException(java.io.IOException) LinkedList(java.util.LinkedList) RealDataBlock(net.sf.mbus4j.dataframes.datablocks.RealDataBlock) StringDataBlock(net.sf.mbus4j.dataframes.datablocks.StringDataBlock) BigDecimalDataBlock(net.sf.mbus4j.dataframes.datablocks.BigDecimalDataBlock) DataBlock(net.sf.mbus4j.dataframes.datablocks.DataBlock) ByteDataBlock(net.sf.mbus4j.dataframes.datablocks.ByteDataBlock) IntegerDataBlock(net.sf.mbus4j.dataframes.datablocks.IntegerDataBlock) LongDataBlock(net.sf.mbus4j.dataframes.datablocks.LongDataBlock) ShortDataBlock(net.sf.mbus4j.dataframes.datablocks.ShortDataBlock) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 2 with UserDataResponse

use of net.sf.mbus4j.dataframes.UserDataResponse in project ma-modules-public by infiniteautomation.

the class MBusEditDwr method addMBusPoint.

@DwrPermission(user = true)
public DataPointVO addMBusPoint(int deviceIndex, int rsIndex, int dbIndex) {
    DataPointVO dp = getPoint(Common.NEW_ID, null);
    MBusPointLocatorVO locator = (MBusPointLocatorVO) dp.getPointLocator();
    MBusDiscovery test = Common.getUser().getTestingUtility(MBusDiscovery.class);
    if (test == null) {
        return null;
    }
    MBusResponseFramesContainer dev = test.getDevice(deviceIndex);
    if (dev.getResponseFrameContainer(rsIndex).getResponseFrame() instanceof UserDataResponse) {
        UserDataResponse udr = (UserDataResponse) dev.getResponseFrameContainer(rsIndex).getResponseFrame();
        DataBlock db = udr.getDataBlock(dbIndex);
        dp.setName(db.getParamDescr());
        locator.setDbIndex(dbIndex);
        locator.setAddressing(MBusAddressing.PRIMARY);
        locator.setAddress(dev.getAddress());
        locator.setMedium(dev.getMedium());
        locator.setManufacturer(dev.getManufacturer());
        locator.setVersion(dev.getVersion());
        locator.setIdentNumber(dev.getIdentNumber());
        locator.setResponseFrame(dev.getResponseFrameContainer(rsIndex).getName());
        locator.setSubUnit(db.getSubUnit());
        locator.setDifCode(db.getDataFieldCode().getLabel());
        locator.setFunctionField(db.getFunctionField().getLabel());
        locator.setStorageNumber(db.getStorageNumber());
        locator.setTariff(db.getTariff());
        locator.setSiPrefix(db.getSiPrefix().getLabel());
        locator.setEffectiveSiPrefix(locator.getSiPrefix());
        locator.setUnitOfMeasurement(db.getUnitOfMeasurement() != null ? db.getUnitOfMeasurement().getLabel() : null);
        locator.setVifType(db.getVif().getVifType().getLabel());
        locator.setVifLabel(db.getVif().getLabel());
        locator.setExponent(db.getExponent());
        if (db.getVifes() != null) {
            final String[] vifeLabels = new String[db.getVifes().length];
            final String[] vifeTypes = new String[db.getVifes().length];
            for (int i = 0; i < vifeLabels.length; i++) {
                vifeTypes[i] = db.getVifes()[i].getVifeType().getLabel();
                vifeLabels[i] = db.getVifes()[i].getLabel();
            }
            locator.setVifeTypes(vifeTypes);
            locator.setVifeLabels(vifeLabels);
        } else {
            locator.setVifeLabels(null);
        }
    }
    return dp;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) UserDataResponse(net.sf.mbus4j.dataframes.UserDataResponse) MBusPointLocatorVO(com.serotonin.m2m2.mbus.MBusPointLocatorVO) MBusResponseFramesContainer(net.sf.mbus4j.dataframes.MBusResponseFramesContainer) DataBlock(net.sf.mbus4j.dataframes.datablocks.DataBlock) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Aggregations

UserDataResponse (net.sf.mbus4j.dataframes.UserDataResponse)2 DataBlock (net.sf.mbus4j.dataframes.datablocks.DataBlock)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 MBusPointLocatorVO (com.serotonin.m2m2.mbus.MBusPointLocatorVO)1 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 MBusResponseFramesContainer (net.sf.mbus4j.dataframes.MBusResponseFramesContainer)1 BigDecimalDataBlock (net.sf.mbus4j.dataframes.datablocks.BigDecimalDataBlock)1 ByteDataBlock (net.sf.mbus4j.dataframes.datablocks.ByteDataBlock)1 IntegerDataBlock (net.sf.mbus4j.dataframes.datablocks.IntegerDataBlock)1 LongDataBlock (net.sf.mbus4j.dataframes.datablocks.LongDataBlock)1 RealDataBlock (net.sf.mbus4j.dataframes.datablocks.RealDataBlock)1 ShortDataBlock (net.sf.mbus4j.dataframes.datablocks.ShortDataBlock)1 StringDataBlock (net.sf.mbus4j.dataframes.datablocks.StringDataBlock)1