Search in sources :

Example 1 with MBusPointLocatorVO

use of com.serotonin.m2m2.mbus.MBusPointLocatorVO in project ma-modules-public by infiniteautomation.

the class MBusDataSourceRT method setValue.

protected boolean setValue(DataBlock db, boolean pointError, long time, DataPointRT point, final MBusPointLocatorRT locatorRT, final MBusPointLocatorVO locatorVo) {
    try {
        if ((db instanceof BcdValue) && ((BcdValue) db).isBcdError()) {
            pointError = true;
            LOG.fatal("BCD Error : " + ((BcdValue) db).getBcdError());
            raiseEvent(POINT_READ_EXCEPTION_EVENT, time, true, new TranslatableMessage("event.exception2", point.getVO().getExtendedName(), "BCD error value: " + ((BcdValue) db).getBcdError()));
            pointError = true;
        } else if (db instanceof ByteDataBlock) {
            point.updatePointValue(new PointValueTime(locatorRT.calcCorrectedValue(((ByteDataBlock) db).getValue(), db.getCorrectionExponent(locatorVo.effectiveSiPrefix()), db.getCorrectionConstant()), time));
        } else if (db instanceof ShortDataBlock) {
            point.updatePointValue(new PointValueTime(locatorRT.calcCorrectedValue(((ShortDataBlock) db).getValue(), db.getCorrectionExponent(locatorVo.effectiveSiPrefix()), db.getCorrectionConstant()), time));
        } else if (db instanceof IntegerDataBlock) {
            point.updatePointValue(new PointValueTime(locatorRT.calcCorrectedValue(((IntegerDataBlock) db).getValue(), db.getCorrectionExponent(locatorVo.effectiveSiPrefix()), db.getCorrectionConstant()), time));
        } else if (db instanceof LongDataBlock) {
            point.updatePointValue(new PointValueTime(locatorRT.calcCorrectedValue(((LongDataBlock) db).getValue(), db.getCorrectionExponent(locatorVo.effectiveSiPrefix()), db.getCorrectionConstant()), time));
        } else if (db instanceof RealDataBlock) {
            point.updatePointValue(new PointValueTime(locatorRT.calcCorrectedValue(((RealDataBlock) db).getValue(), db.getCorrectionExponent(locatorVo.effectiveSiPrefix()), db.getCorrectionConstant()), time));
        } else if (db instanceof BigDecimalDataBlock) {
            point.updatePointValue(new PointValueTime(locatorRT.calcCorrectedValue(((BigDecimalDataBlock) db).getValue().doubleValue(), db.getCorrectionExponent(locatorVo.effectiveSiPrefix()), db.getCorrectionConstant()), time));
        } else if (db instanceof StringDataBlock) {
            point.updatePointValue(new PointValueTime(((StringDataBlock) db).getValue(), time));
        } else {
            LOG.fatal("Dont know how to save: " + point.getVO().getExtendedName());
            raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.exception2", point.getVO().getExtendedName(), "Dont know how to save"));
            pointError = true;
        }
    } catch (Exception ex) {
        LOG.fatal("Error during saving: " + vo.getName(), ex);
        raiseEvent(POINT_READ_EXCEPTION_EVENT, System.currentTimeMillis(), true, new TranslatableMessage("event.exception2", point.getVO().getExtendedName(), "Ex: " + ex));
        pointError = true;
    }
    return pointError;
}
Also used : ShortDataBlock(net.sf.mbus4j.dataframes.datablocks.ShortDataBlock) IntegerDataBlock(net.sf.mbus4j.dataframes.datablocks.IntegerDataBlock) BigDecimalDataBlock(net.sf.mbus4j.dataframes.datablocks.BigDecimalDataBlock) StringDataBlock(net.sf.mbus4j.dataframes.datablocks.StringDataBlock) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) BcdValue(net.sf.mbus4j.dataframes.datablocks.BcdValue) RealDataBlock(net.sf.mbus4j.dataframes.datablocks.RealDataBlock) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) LongDataBlock(net.sf.mbus4j.dataframes.datablocks.LongDataBlock) ByteDataBlock(net.sf.mbus4j.dataframes.datablocks.ByteDataBlock) IOException(java.io.IOException)

Example 2 with MBusPointLocatorVO

use of com.serotonin.m2m2.mbus.MBusPointLocatorVO in project ma-modules-public by infiniteautomation.

the class MBusEditDwr method changeMBusAddress.

@DwrPermission(user = true)
public Map<String, Object> changeMBusAddress(int deviceIndex, String newAddress) {
    Map<String, Object> result = new HashMap<>();
    result.put("deviceIndex", deviceIndex);
    MBusDiscovery test = Common.getUser().getTestingUtility(MBusDiscovery.class);
    if (test == null) {
        return null;
    }
    try {
        final byte oldAddress = test.getDevice(deviceIndex).getAddress();
        byte address;
        if (newAddress.startsWith("0x")) {
            address = (byte) Short.parseShort(newAddress.substring(2), 16);
        } else {
            address = (byte) Short.parseShort(newAddress);
        }
        if (test.changeAddress(deviceIndex, address, result)) {
            // if address was changed, then change existing datapoints enabled disabled
            final DataSourceVO<?> ds = Common.getUser().getEditDataSource();
            List<DataPointVO> dpVos = DataPointDao.instance.getDataPoints(ds.getId(), null);
            for (DataPointVO dpVo : dpVos) {
                final MBusPointLocatorVO pl = dpVo.getPointLocator();
                if (pl.getAddress() == oldAddress) {
                    pl.setAddress(address);
                    Common.runtimeManager.saveDataPoint(dpVo);
                }
            }
        }
        result.put("points", getPoints());
    } catch (IOException | InterruptedException e) {
        return null;
    }
    return result;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) HashMap(java.util.HashMap) IOException(java.io.IOException) MBusPointLocatorVO(com.serotonin.m2m2.mbus.MBusPointLocatorVO) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 3 with MBusPointLocatorVO

use of com.serotonin.m2m2.mbus.MBusPointLocatorVO 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 4 with MBusPointLocatorVO

use of com.serotonin.m2m2.mbus.MBusPointLocatorVO 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

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