Search in sources :

Example 1 with ControlConfig

use of net.solarnetwork.node.io.dnp3.domain.ControlConfig 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 ControlConfig

use of net.solarnetwork.node.io.dnp3.domain.ControlConfig in project solarnetwork-node by SolarNetwork.

the class OutstationService method getSettingSpecifiers.

/*
	 * =========================================================================
	 * SettingSpecifierProvider implementation
	 * =========================================================================
	 */
@Override
public List<SettingSpecifier> getSettingSpecifiers() {
    List<SettingSpecifier> result = new ArrayList<>(16);
    result.add(new BasicTitleSettingSpecifier("status", getStackStatusMessage(), true));
    result.add(new BasicTextFieldSettingSpecifier("uid", DEFAULT_UID));
    result.add(new BasicTextFieldSettingSpecifier("groupUid", ""));
    result.add(new BasicTextFieldSettingSpecifier("eventBufferSize", String.valueOf(DEFAULT_EVENT_BUFFER_SIZE)));
    result.add(new BasicTextFieldSettingSpecifier("dnp3Channel.propertyFilters['uid']", TcpServerChannelService.DEFAULT_UID));
    result.addAll(linkLayerSettings("linkLayerConfig.", new LinkLayerConfig(false)));
    result.addAll(outstationSettings("outstationConfig.", new OutstationConfig()));
    MeasurementConfig[] measConfs = getMeasurementConfigs();
    List<MeasurementConfig> measConfsList = (measConfs != null ? Arrays.asList(measConfs) : Collections.<MeasurementConfig>emptyList());
    result.add(SettingUtils.dynamicListSettingSpecifier("measurementConfigs", measConfsList, new SettingUtils.KeyedListCallback<MeasurementConfig>() {

        @Override
        public Collection<SettingSpecifier> mapListSettingKey(MeasurementConfig value, int index, String key) {
            BasicGroupSettingSpecifier configGroup = new BasicGroupSettingSpecifier(MeasurementConfig.settings(key + "."));
            return Collections.<SettingSpecifier>singletonList(configGroup);
        }
    }));
    ControlConfig[] cntrlConfs = getControlConfigs();
    List<ControlConfig> cntrlConfsList = (cntrlConfs != null ? Arrays.asList(cntrlConfs) : Collections.<ControlConfig>emptyList());
    result.add(SettingUtils.dynamicListSettingSpecifier("controlConfigs", cntrlConfsList, new SettingUtils.KeyedListCallback<ControlConfig>() {

        @Override
        public Collection<SettingSpecifier> mapListSettingKey(ControlConfig value, int index, String key) {
            BasicGroupSettingSpecifier configGroup = new BasicGroupSettingSpecifier(ControlConfig.settings(key + "."));
            return Collections.<SettingSpecifier>singletonList(configGroup);
        }
    }));
    return result;
}
Also used : MeasurementConfig(net.solarnetwork.node.io.dnp3.domain.MeasurementConfig) ControlConfig(net.solarnetwork.node.io.dnp3.domain.ControlConfig) LinkLayerConfig(net.solarnetwork.node.io.dnp3.domain.LinkLayerConfig) ArrayList(java.util.ArrayList) OutstationConfig(net.solarnetwork.node.io.dnp3.domain.OutstationConfig) BasicGroupSettingSpecifier(net.solarnetwork.settings.support.BasicGroupSettingSpecifier) SettingSpecifier(net.solarnetwork.settings.SettingSpecifier) BasicTitleSettingSpecifier(net.solarnetwork.settings.support.BasicTitleSettingSpecifier) BasicGroupSettingSpecifier(net.solarnetwork.settings.support.BasicGroupSettingSpecifier) BasicTextFieldSettingSpecifier(net.solarnetwork.settings.support.BasicTextFieldSettingSpecifier) BasicTextFieldSettingSpecifier(net.solarnetwork.settings.support.BasicTextFieldSettingSpecifier) BasicTitleSettingSpecifier(net.solarnetwork.settings.support.BasicTitleSettingSpecifier)

Example 3 with ControlConfig

use of net.solarnetwork.node.io.dnp3.domain.ControlConfig in project solarnetwork-node by SolarNetwork.

the class OutstationService method createOutstationStackConfig.

private OutstationStackConfig createOutstationStackConfig() {
    Map<MeasurementType, List<MeasurementConfig>> configs = measurementTypeMap(getMeasurementConfigs());
    Map<ControlType, List<ControlConfig>> controlConfigs = controlTypeMap(getControlConfigs());
    OutstationStackConfig config = new OutstationStackConfig(createDatabaseConfig(configs, controlConfigs), createEventBufferConfig(configs, controlConfigs));
    copySettings(getLinkLayerConfig(), config.linkConfig);
    copySettings(getOutstationConfig(), config.outstationConfig);
    return config;
}
Also used : MeasurementType(net.solarnetwork.node.io.dnp3.domain.MeasurementType) OutstationStackConfig(com.automatak.dnp3.OutstationStackConfig) List(java.util.List) ArrayList(java.util.ArrayList) ControlType(net.solarnetwork.node.io.dnp3.domain.ControlType)

Example 4 with ControlConfig

use of net.solarnetwork.node.io.dnp3.domain.ControlConfig in project solarnetwork-node by SolarNetwork.

the class OutstationServceTests method handleCROBMissingIndex.

@Test
public void handleCROBMissingIndex() {
    // given
    TestOutstationService service = createOutstationService();
    final String controlId = "/foo/switch";
    ControlConfig cConfig = new ControlConfig(null, controlId, ControlType.Binary);
    service.setControlConfigs(new ControlConfig[] { cConfig });
    // when
    replayAll();
    ControlRelayOutputBlock crob = new ControlRelayOutputBlock(ControlCode.LATCH_ON, (short) 1, 0, 0, CommandStatus.SUCCESS);
    CommandStatus status = service.getCommandHandler().operateCROB(crob, 123123123, OperateType.DirectOperate);
    // then
    assertThat("Command rejected because control index out of range", status, equalTo(CommandStatus.NOT_AUTHORIZED));
}
Also used : ControlConfig(net.solarnetwork.node.io.dnp3.domain.ControlConfig) ControlRelayOutputBlock(com.automatak.dnp3.ControlRelayOutputBlock) CommandStatus(com.automatak.dnp3.enums.CommandStatus) Test(org.junit.Test)

Example 5 with ControlConfig

use of net.solarnetwork.node.io.dnp3.domain.ControlConfig in project solarnetwork-node by SolarNetwork.

the class OutstationServceTests method handleCROBLatchOff.

@Test
public void handleCROBLatchOff() {
    // given
    TestOutstationService service = createOutstationService();
    final String controlId = "/foo/switch";
    ControlConfig cConfig = new ControlConfig(null, controlId, ControlType.Binary);
    service.setControlConfigs(new ControlConfig[] { cConfig });
    Capture<Instruction> instrCaptor = Capture.newInstance();
    expect(instructionService.executeInstruction(capture(instrCaptor))).andAnswer(new IAnswer<InstructionStatus>() {

        @Override
        public InstructionStatus answer() throws Throwable {
            return createStatus(instrCaptor.getValue(), Completed);
        }
    });
    // when
    replayAll();
    ControlRelayOutputBlock crob = new ControlRelayOutputBlock(ControlCode.LATCH_OFF, (short) 1, 0, 0, CommandStatus.SUCCESS);
    CommandStatus status = service.getCommandHandler().operateCROB(crob, 0, OperateType.DirectOperate);
    // then
    assertThat("Command OK", status, equalTo(CommandStatus.SUCCESS));
    Instruction instr = instrCaptor.getValue();
    assertThat("Instruction", instr.getTopic(), equalTo(InstructionHandler.TOPIC_SET_CONTROL_PARAMETER));
    assertThat("Control ID param", instr.getParameterValue(controlId), equalTo(Boolean.FALSE.toString()));
}
Also used : ControlConfig(net.solarnetwork.node.io.dnp3.domain.ControlConfig) InstructionStatus(net.solarnetwork.node.reactor.InstructionStatus) ControlRelayOutputBlock(com.automatak.dnp3.ControlRelayOutputBlock) CommandStatus(com.automatak.dnp3.enums.CommandStatus) Instruction(net.solarnetwork.node.reactor.Instruction) Test(org.junit.Test)

Aggregations

ControlConfig (net.solarnetwork.node.io.dnp3.domain.ControlConfig)12 CommandStatus (com.automatak.dnp3.enums.CommandStatus)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 MeasurementType (net.solarnetwork.node.io.dnp3.domain.MeasurementType)5 Instruction (net.solarnetwork.node.reactor.Instruction)5 InstructionStatus (net.solarnetwork.node.reactor.InstructionStatus)5 ControlRelayOutputBlock (com.automatak.dnp3.ControlRelayOutputBlock)4 List (java.util.List)4 ControlType (net.solarnetwork.node.io.dnp3.domain.ControlType)4 MeasurementConfig (net.solarnetwork.node.io.dnp3.domain.MeasurementConfig)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 AnalogOutputInt32 (com.automatak.dnp3.AnalogOutputInt32)2 CapturingExecutorService (net.solarnetwork.node.test.CapturingExecutorService)2 TaskExecutorAdapter (org.springframework.core.task.support.TaskExecutorAdapter)2 AnalogInput (com.automatak.dnp3.AnalogInput)1 AnalogOutputStatus (com.automatak.dnp3.AnalogOutputStatus)1 BinaryInput (com.automatak.dnp3.BinaryInput)1 BinaryOutputStatus (com.automatak.dnp3.BinaryOutputStatus)1