Search in sources :

Example 1 with BinaryOutputStatus

use of com.automatak.dnp3.BinaryOutputStatus 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;
}
Also used : AnalogInput(com.automatak.dnp3.AnalogInput) Counter(com.automatak.dnp3.Counter) FrozenCounter(com.automatak.dnp3.FrozenCounter) DoubleBitBinaryInput(com.automatak.dnp3.DoubleBitBinaryInput) MeasurementType(net.solarnetwork.node.io.dnp3.domain.MeasurementType) List(java.util.List) ArrayList(java.util.ArrayList) BinaryOutputStatus(com.automatak.dnp3.BinaryOutputStatus) MeasurementConfig(net.solarnetwork.node.io.dnp3.domain.MeasurementConfig) BinaryInput(com.automatak.dnp3.BinaryInput) DoubleBitBinaryInput(com.automatak.dnp3.DoubleBitBinaryInput) ControlConfig(net.solarnetwork.node.io.dnp3.domain.ControlConfig) Instant(java.time.Instant) BigDecimal(java.math.BigDecimal) OutstationChangeSet(com.automatak.dnp3.OutstationChangeSet) FrozenCounter(com.automatak.dnp3.FrozenCounter) ControlType(net.solarnetwork.node.io.dnp3.domain.ControlType) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) AnalogOutputStatus(com.automatak.dnp3.AnalogOutputStatus)

Example 2 with BinaryOutputStatus

use of com.automatak.dnp3.BinaryOutputStatus in project solarnetwork-node by SolarNetwork.

the class OutstationService method createEventBufferConfig.

private EventBufferConfig createEventBufferConfig(Map<MeasurementType, List<MeasurementConfig>> configs, Map<ControlType, List<ControlConfig>> controlConfigs) {
    EventBufferConfig config = EventBufferConfig.allTypes(0);
    final int size = getEventBufferSize();
    if (configs != null) {
        for (Map.Entry<MeasurementType, List<MeasurementConfig>> me : configs.entrySet()) {
            MeasurementType type = me.getKey();
            List<MeasurementConfig> list = me.getValue();
            if (type == null || list == null || list.isEmpty()) {
                continue;
            }
            switch(type) {
                case AnalogInput:
                    config.maxAnalogEvents = size;
                    break;
                case AnalogOutputStatus:
                    config.maxAnalogOutputStatusEvents = size;
                    break;
                case BinaryInput:
                    config.maxBinaryEvents = size;
                    break;
                case BinaryOutputStatus:
                    config.maxBinaryOutputStatusEvents = size;
                    break;
                case Counter:
                    config.maxCounterEvents = size;
                    break;
                case DoubleBitBinaryInput:
                    config.maxDoubleBinaryEvents = size;
                    break;
                case FrozenCounter:
                    config.maxFrozenCounterEvents = size;
                    break;
            }
        }
    }
    if (controlConfigs != null) {
        for (Map.Entry<ControlType, List<ControlConfig>> me : controlConfigs.entrySet()) {
            ControlType type = me.getKey();
            List<ControlConfig> list = me.getValue();
            if (type == null || list == null || list.isEmpty()) {
                continue;
            }
            switch(type) {
                case Analog:
                    config.maxAnalogOutputStatusEvents = size;
                    break;
                case Binary:
                    config.maxBinaryOutputStatusEvents = size;
                    break;
            }
        }
    }
    return config;
}
Also used : EventBufferConfig(com.automatak.dnp3.EventBufferConfig) MeasurementConfig(net.solarnetwork.node.io.dnp3.domain.MeasurementConfig) ControlConfig(net.solarnetwork.node.io.dnp3.domain.ControlConfig) MeasurementType(net.solarnetwork.node.io.dnp3.domain.MeasurementType) List(java.util.List) ArrayList(java.util.ArrayList) ControlType(net.solarnetwork.node.io.dnp3.domain.ControlType) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with BinaryOutputStatus

use of com.automatak.dnp3.BinaryOutputStatus in project solarnetwork-node by SolarNetwork.

the class OutstationService method createDatabaseConfig.

private DatabaseConfig createDatabaseConfig(Map<MeasurementType, List<MeasurementConfig>> configs, Map<ControlType, List<ControlConfig>> controlConfigs) {
    int analogCount = 0;
    int aoStatusCount = 0;
    int binaryCount = 0;
    int boStatusCount = 0;
    int counterCount = 0;
    int doubleBinaryCount = 0;
    int frozenCounterCount = 0;
    StringBuilder infoBuf = new StringBuilder();
    if (configs != null) {
        for (Map.Entry<MeasurementType, List<MeasurementConfig>> me : configs.entrySet()) {
            MeasurementType type = me.getKey();
            List<MeasurementConfig> list = me.getValue();
            if (type == null || list == null || list.isEmpty()) {
                continue;
            }
            switch(type) {
                case AnalogInput:
                    analogCount = list.size();
                    appendMeasurementInfos(infoBuf, type, list);
                    break;
                case AnalogOutputStatus:
                    aoStatusCount = list.size();
                    break;
                case BinaryInput:
                    binaryCount = list.size();
                    appendMeasurementInfos(infoBuf, type, list);
                    break;
                case BinaryOutputStatus:
                    boStatusCount = list.size();
                    break;
                case Counter:
                    counterCount = list.size();
                    appendMeasurementInfos(infoBuf, type, list);
                    break;
                case DoubleBitBinaryInput:
                    doubleBinaryCount = list.size();
                    appendMeasurementInfos(infoBuf, type, list);
                    break;
                case FrozenCounter:
                    frozenCounterCount = list.size();
                    appendMeasurementInfos(infoBuf, type, list);
                    break;
            }
        }
    }
    if (controlConfigs != null) {
        for (Map.Entry<ControlType, List<ControlConfig>> me : controlConfigs.entrySet()) {
            ControlType type = me.getKey();
            List<ControlConfig> list = me.getValue();
            if (type == null || list == null || list.isEmpty()) {
                continue;
            }
            switch(type) {
                case Analog:
                    appendControlInfos(infoBuf, type, list, aoStatusCount);
                    aoStatusCount += list.size();
                    break;
                case Binary:
                    appendControlInfos(infoBuf, type, list, boStatusCount);
                    boStatusCount += list.size();
                    break;
            }
        }
    }
    log.info("DNP3 outstation [{}] database configured with following registers:\n{}", getUid(), infoBuf);
    return new DatabaseConfig(binaryCount, doubleBinaryCount, analogCount, counterCount, frozenCounterCount, boStatusCount, aoStatusCount);
}
Also used : MeasurementConfig(net.solarnetwork.node.io.dnp3.domain.MeasurementConfig) ControlConfig(net.solarnetwork.node.io.dnp3.domain.ControlConfig) MeasurementType(net.solarnetwork.node.io.dnp3.domain.MeasurementType) List(java.util.List) ArrayList(java.util.ArrayList) ControlType(net.solarnetwork.node.io.dnp3.domain.ControlType) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) DatabaseConfig(com.automatak.dnp3.DatabaseConfig)

Aggregations

ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 Map (java.util.Map)3 ControlConfig (net.solarnetwork.node.io.dnp3.domain.ControlConfig)3 ControlType (net.solarnetwork.node.io.dnp3.domain.ControlType)3 MeasurementConfig (net.solarnetwork.node.io.dnp3.domain.MeasurementConfig)3 MeasurementType (net.solarnetwork.node.io.dnp3.domain.MeasurementType)3 AnalogInput (com.automatak.dnp3.AnalogInput)1 AnalogOutputStatus (com.automatak.dnp3.AnalogOutputStatus)1 BinaryInput (com.automatak.dnp3.BinaryInput)1 BinaryOutputStatus (com.automatak.dnp3.BinaryOutputStatus)1 Counter (com.automatak.dnp3.Counter)1 DatabaseConfig (com.automatak.dnp3.DatabaseConfig)1 DoubleBitBinaryInput (com.automatak.dnp3.DoubleBitBinaryInput)1 EventBufferConfig (com.automatak.dnp3.EventBufferConfig)1 FrozenCounter (com.automatak.dnp3.FrozenCounter)1 OutstationChangeSet (com.automatak.dnp3.OutstationChangeSet)1 BigDecimal (java.math.BigDecimal)1 Instant (java.time.Instant)1