use of com.automatak.dnp3.OutstationChangeSet in project solarnetwork-node by SolarNetwork.
the class OutstationService method changeSetForDatumCapturedEvent.
private OutstationChangeSet changeSetForDatumCapturedEvent(final Datum datum, final Event event) {
Map<MeasurementType, List<MeasurementConfig>> map = measurementTypeMap(getMeasurementConfigs());
Map<ControlType, List<ControlConfig>> controlMap = controlTypeMap(getControlConfigs());
if (datum == null || ((map == null || map.isEmpty()) && (controlMap == null || controlMap.isEmpty()))) {
return null;
}
final String sourceId = datum.getSourceId();
final Instant timestamp = datum.getTimestamp();
if (timestamp == null) {
return null;
}
final long ts = timestamp.toEpochMilli();
final Map<String, ?> datumProps = datum.getSampleData();
OutstationChangeSet changes = null;
if (map != null) {
for (Map.Entry<MeasurementType, List<MeasurementConfig>> me : map.entrySet()) {
MeasurementType type = me.getKey();
List<MeasurementConfig> list = me.getValue();
for (ListIterator<MeasurementConfig> itr = list.listIterator(); itr.hasNext(); ) {
MeasurementConfig config = itr.next();
if (sourceId.equals(config.getSourceId())) {
Object propVal = datumProps.get(config.getPropertyName());
if (propVal != null) {
if (propVal instanceof Number) {
if (config.getUnitMultiplier() != null) {
propVal = applyUnitMultiplier((Number) propVal, config.getUnitMultiplier());
}
if (config.getDecimalScale() >= 0) {
propVal = applyDecimalScale((Number) propVal, config.getDecimalScale());
}
}
if (changes == null) {
changes = new OutstationChangeSet();
}
log.debug("Updating DNP3 {}[{}] from [{}].{} -> {}", type, itr.previousIndex(), sourceId, config.getPropertyName(), propVal);
switch(type) {
case AnalogInput:
if (propVal instanceof Number) {
changes.update(new AnalogInput(((Number) propVal).doubleValue(), (byte) AnalogQuality.ONLINE.toType(), ts), itr.previousIndex());
}
break;
case AnalogOutputStatus:
if (propVal instanceof Number) {
changes.update(new AnalogOutputStatus(((Number) propVal).doubleValue(), (byte) AnalogOutputStatusQuality.ONLINE.toType(), ts), itr.previousIndex());
}
break;
case BinaryInput:
changes.update(new BinaryInput(booleanPropertyValue(propVal), (byte) BinaryQuality.ONLINE.toType(), ts), itr.previousIndex());
break;
case BinaryOutputStatus:
changes.update(new BinaryOutputStatus(booleanPropertyValue(propVal), (byte) BinaryOutputStatusQuality.ONLINE.toType(), ts), itr.previousIndex());
break;
case Counter:
if (propVal instanceof Number) {
changes.update(new Counter(((Number) propVal).longValue(), (byte) CounterQuality.ONLINE.toType(), ts), itr.previousIndex());
}
break;
case DoubleBitBinaryInput:
changes.update(new DoubleBitBinaryInput(booleanPropertyValue(propVal) ? DoubleBit.DETERMINED_ON : DoubleBit.DETERMINED_OFF, (byte) DoubleBitBinaryQuality.ONLINE.toType(), ts), itr.previousIndex());
break;
case FrozenCounter:
if (propVal instanceof Number) {
changes.update(new FrozenCounter(((Number) propVal).longValue(), (byte) FrozenCounterQuality.ONLINE.toType(), ts), itr.previousIndex());
}
break;
}
}
}
}
}
if (controlMap != null) {
int analogStatusOffset = typeConfigCount(MeasurementType.AnalogOutputStatus, map);
int binaryStatusOffset = typeConfigCount(MeasurementType.BinaryOutputStatus, map);
for (Map.Entry<ControlType, List<ControlConfig>> me : controlMap.entrySet()) {
ControlType type = me.getKey();
List<ControlConfig> list = me.getValue();
for (ListIterator<ControlConfig> itr = list.listIterator(); itr.hasNext(); ) {
ControlConfig config = itr.next();
if (sourceId.equals(config.getControlId())) {
if (changes == null) {
changes = new OutstationChangeSet();
}
int index = (type == ControlType.Analog ? analogStatusOffset : binaryStatusOffset) + itr.previousIndex();
Object propVal = datumProps.get("value");
log.debug("Updating DNP3 control {}[{}] from [{}].value -> {}", type, index, sourceId, propVal);
switch(type) {
case Analog:
try {
Number n = null;
if (propVal instanceof Number) {
n = (Number) propVal;
} else {
n = new BigDecimal(propVal.toString());
}
changes.update(new AnalogOutputStatus(n.doubleValue(), (byte) AnalogOutputStatusQuality.ONLINE.toType(), ts), index);
} catch (NumberFormatException e) {
log.warn("Cannot convert control [{}] value [{}] to number: {}", sourceId, propVal, e.getMessage());
}
break;
case Binary:
changes.update(new BinaryOutputStatus(booleanPropertyValue(propVal), (byte) BinaryOutputStatusQuality.ONLINE.toType(), ts), index);
break;
}
}
}
}
}
}
return changes;
}
use of com.automatak.dnp3.OutstationChangeSet in project solarnetwork-node by SolarNetwork.
the class OutstationDemo method run.
public static void run(DNP3Manager manager) throws Exception {
// Create a tcp channel class that will connect to the loopback
Channel channel = manager.addTCPServer("client", LogMasks.NORMAL | LogMasks.APP_COMMS, ServerAcceptMode.CloseNew, "127.0.0.1", 20000, new Slf4jChannelListener());
// Create the default outstation configuration
OutstationStackConfig config = new OutstationStackConfig(DatabaseConfig.allValues(5), EventBufferConfig.allTypes(50));
// Create an Outstation instance, pass in a simple a command handler that responds successfully to everything
Outstation outstation = channel.addOutstation("outstation", SuccessCommandHandler.getInstance(), DefaultOutstationApplication.getInstance(), config);
outstation.enable();
// all this stuff just to read a line of text in Java. Oh the humanity.
String line = "";
InputStreamReader converter = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(converter);
int i = 0;
while (true) {
System.out.println("Enter something to update a counter or type <quit> to exit");
line = in.readLine();
if (line.equals("quit"))
break;
else {
OutstationChangeSet set = new OutstationChangeSet();
set.update(new Counter(i, (byte) 0x01, 0), 0);
outstation.apply(set);
++i;
}
}
}
use of com.automatak.dnp3.OutstationChangeSet in project solarnetwork-node by SolarNetwork.
the class OutstationService method applyDatumCapturedUpdates.
private void applyDatumCapturedUpdates(Datum datum, Event event) {
OutstationChangeSet changes = changeSetForDatumCapturedEvent(datum, event);
if (changes == null) {
return;
}
synchronized (this) {
Outstation station = getOutstation();
if (station != null) {
log.info("Applying changes to DNP3 [{}]", getUid());
station.apply(changes);
}
}
}
Aggregations