use of net.sf.mbus4j.dataframes.datablocks.DataBlock 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();
}
}
}
use of net.sf.mbus4j.dataframes.datablocks.DataBlock in project ma-modules-public by infiniteautomation.
the class MBusDataSourceRT method findDataBlocks.
private int[] findDataBlocks(UserDataResponse userDataResponse, MBusPointLocatorVO locatorVo) {
int[] result = new int[0];
for (int i = 0; i < userDataResponse.getDataBlockCount(); i++) {
DataBlock db = userDataResponse.getDataBlock(i);
if (Objects.equals(db.getDataFieldCode(), locatorVo.difCode()) && Objects.equals(db.getUnitOfMeasurement(), locatorVo.unitOfMeasurement()) && Objects.equals(db.getFunctionField(), locatorVo.functionField()) && (db.getStorageNumber() == locatorVo.getStorageNumber()) && (db.getSubUnit() == locatorVo.getSubUnit()) && (db.getTariff() == locatorVo.getTariff())) {
result = Arrays.copyOf(result, result.length + 1);
result[result.length - 1] = i;
}
}
return result;
}
use of net.sf.mbus4j.dataframes.datablocks.DataBlock 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;
}
Aggregations