Search in sources :

Example 11 with ValidationFailure

use of org.openremote.model.ValidationFailure in project openremote by openremote.

the class SimulatorProtocol method doLinkAttribute.

@Override
protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
    // Get element type from the attribute meta
    Optional<String> elementType = getElementType(attribute);
    if (!elementType.isPresent()) {
        LOG.warning("Can't configure simulator, missing " + SIMULATOR_ELEMENT + " meta item on: " + attribute);
        return;
    }
    AttributeRef configRef = protocolConfiguration.getReferenceOrThrow();
    AttributeRef attributeRef = attribute.getReferenceOrThrow();
    SimulatorElement element = createElement(elementType.get(), attribute);
    if (element == null) {
        LOG.warning("Can't simulate element '" + elementType + "': " + attribute);
        return;
    }
    if (attribute.getValue().isPresent()) {
        element.setValue(attribute.getValue().get());
        List<ValidationFailure> failures = element.getValidationFailures();
        if (!failures.isEmpty()) {
            element.clearValue();
            LOG.warning("Failed to initialize simulator element, initial value validation failures " + failures + ": " + attribute);
            return;
        }
    }
    LOG.info("Putting element '" + element + "' for: " + attribute);
    elements.put(attributeRef, element);
    attributeInstanceMap.put(attributeRef, configRef);
}
Also used : NumberSimulatorElement(org.openremote.model.simulator.element.NumberSimulatorElement) SwitchSimulatorElement(org.openremote.model.simulator.element.SwitchSimulatorElement) ColorSimulatorElement(org.openremote.model.simulator.element.ColorSimulatorElement) SimulatorElement(org.openremote.model.simulator.SimulatorElement) ValidationFailure(org.openremote.model.ValidationFailure)

Example 12 with ValidationFailure

use of org.openremote.model.ValidationFailure in project openremote by openremote.

the class SimulatorProtocol method putValue.

/**
 * Call this to simulate a send to actuator.
 */
public boolean putValue(AttributeState attributeState) {
    Boolean result = withLockReturning(getProtocolName() + "::putValue", () -> {
        AttributeRef attributeRef = attributeState.getAttributeRef();
        AttributeRef instanceRef = attributeInstanceMap.get(attributeRef);
        if (instanceRef == null) {
            LOG.warning("Attribute is not referenced by an instance:" + attributeRef);
            return false;
        }
        Instance instance = instances.get(instanceRef);
        if (instance == null) {
            LOG.warning("No instance found by name '" + instanceRef + "'");
            return false;
        }
        if (!instance.isEnabled()) {
            LOG.fine("Simulator protocol configuration is disabled so cannot process request");
            return false;
        }
        LOG.info("Put simulator value: " + attributeState);
        SimulatorElement element = elements.get(attributeRef);
        if (element == null) {
            LOG.warning("No simulated element for: " + attributeRef);
            return false;
        }
        Optional<Value> oldValue = element.getValue();
        element.setValue(attributeState.getValue().orElse(null));
        List<ValidationFailure> failures = element.getValidationFailures();
        if (!failures.isEmpty()) {
            // Reset to old value
            oldValue.ifPresent(element::setValue);
            LOG.warning("Failed to update simulator element, state validation failures " + failures + ": " + attributeRef);
            return false;
        }
        if (instance.getMode() != Mode.MANUAL) {
            updateSensor(attributeRef, instance.getMode() == Mode.WRITE_THROUGH_IMMEDIATE ? 0 : instance.getDelayMilliseconds());
        }
        return true;
    });
    return result != null ? result : false;
}
Also used : NumberSimulatorElement(org.openremote.model.simulator.element.NumberSimulatorElement) SwitchSimulatorElement(org.openremote.model.simulator.element.SwitchSimulatorElement) ColorSimulatorElement(org.openremote.model.simulator.element.ColorSimulatorElement) SimulatorElement(org.openremote.model.simulator.SimulatorElement) Value(org.openremote.model.value.Value) ValidationFailure(org.openremote.model.ValidationFailure)

Example 13 with ValidationFailure

use of org.openremote.model.ValidationFailure in project openremote by openremote.

the class TimerConfiguration method validateTimerConfiguration.

public static boolean validateTimerConfiguration(AssetAttribute attribute, AttributeValidationResult result) {
    boolean failure = false;
    if (!isTimerConfiguration(attribute)) {
        failure = true;
        if (result != null) {
            result.addAttributeFailure(new ValidationFailure(ValueHolder.ValueFailureReason.VALUE_MISMATCH, PROTOCOL_NAME));
        }
    }
    boolean actionFound = false;
    boolean cronFound = false;
    if (attribute.getMeta() != null && !attribute.getMeta().isEmpty()) {
        for (int i = 0; i < attribute.getMeta().size(); i++) {
            MetaItem metaItem = attribute.getMeta().get(i);
            if (isMetaNameEqualTo(metaItem, META_TIMER_ACTION)) {
                actionFound = true;
                if (!getAction(metaItem).isPresent()) {
                    failure = true;
                    if (result == null) {
                        break;
                    }
                    result.addMetaFailure(i, new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_VALUE_MISMATCH, "Timer Action"));
                }
            } else if (isMetaNameEqualTo(metaItem, META_TIMER_CRON_EXPRESSION)) {
                cronFound = true;
                if (!metaItem.getValueAsString().map(TimerProtocol::createCronExpression).isPresent()) {
                    failure = true;
                    if (result == null) {
                        break;
                    }
                    result.addMetaFailure(i, new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_VALUE_MISMATCH, "Timer Cron Expression"));
                }
            }
        }
    }
    if (!cronFound) {
        failure = true;
        if (result != null) {
            result.addMetaFailure(new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_MISSING, META_TIMER_CRON_EXPRESSION));
        }
    }
    if (!actionFound) {
        failure = true;
        if (result != null) {
            result.addMetaFailure(new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_MISSING, META_TIMER_ACTION));
        }
    }
    return !failure;
}
Also used : MetaItem(org.openremote.model.attribute.MetaItem) ValidationFailure(org.openremote.model.ValidationFailure)

Example 14 with ValidationFailure

use of org.openremote.model.ValidationFailure in project openremote by openremote.

the class VelbusConfiguration method validateSerialConfiguration.

public static boolean validateSerialConfiguration(AssetAttribute protocolConfiguration, AttributeValidationResult result) {
    boolean failure = false;
    if (!isSerialConfiguration(protocolConfiguration)) {
        failure = true;
        if (result != null) {
            result.addAttributeFailure(new ValidationFailure(ValueHolder.ValueFailureReason.VALUE_MISMATCH, VelbusSerialProtocol.PROTOCOL_NAME));
        }
    }
    boolean portFound = false;
    if (protocolConfiguration.getMeta() != null && !protocolConfiguration.getMeta().isEmpty()) {
        for (int i = 0; i < protocolConfiguration.getMeta().size(); i++) {
            MetaItem metaItem = protocolConfiguration.getMeta().get(i);
            if (isMetaNameEqualTo(metaItem, VelbusSerialProtocol.META_VELBUS_SERIAL_PORT)) {
                portFound = true;
                if (isNullOrEmpty(metaItem.getValueAsString().orElse(null))) {
                    failure = true;
                    if (result == null) {
                        break;
                    }
                    result.addMetaFailure(i, new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_VALUE_IS_REQUIRED, ValueType.STRING.name()));
                }
            } else if (isMetaNameEqualTo(metaItem, VelbusSerialProtocol.META_VELBUS_SERIAL_BAUDRATE)) {
                int baudrate = metaItem.getValueAsInteger().orElse(0);
                if (baudrate <= 0) {
                    failure = true;
                    if (result == null) {
                        break;
                    }
                    result.addMetaFailure(i, new ValidationFailure(ValueHolder.ValueFailureReason.VALUE_MISMATCH));
                }
            }
        }
    }
    if (!portFound) {
        failure = true;
        if (result != null) {
            result.addMetaFailure(new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_MISSING, VelbusSocketProtocol.META_VELBUS_SOCKET_PORT));
        }
    }
    return !failure;
}
Also used : MetaItem(org.openremote.model.attribute.MetaItem) ValidationFailure(org.openremote.model.ValidationFailure)

Example 15 with ValidationFailure

use of org.openremote.model.ValidationFailure in project openremote by openremote.

the class VelbusConfiguration method validateSocketConfiguration.

public static boolean validateSocketConfiguration(AssetAttribute attribute, AttributeValidationResult result) {
    boolean failure = false;
    if (!isSocketConfiguration(attribute)) {
        failure = true;
        if (result != null) {
            result.addAttributeFailure(new ValidationFailure(ValueHolder.ValueFailureReason.VALUE_MISMATCH, VelbusSocketProtocol.PROTOCOL_NAME));
        }
    }
    boolean hostFound = false;
    boolean portFound = false;
    if (attribute.getMeta() != null && !attribute.getMeta().isEmpty()) {
        for (int i = 0; i < attribute.getMeta().size(); i++) {
            MetaItem metaItem = attribute.getMeta().get(i);
            if (isMetaNameEqualTo(metaItem, VelbusSocketProtocol.META_VELBUS_SOCKET_HOST)) {
                hostFound = true;
                if (isNullOrEmpty(metaItem.getValueAsString().orElse(null))) {
                    failure = true;
                    if (result == null) {
                        break;
                    }
                    result.addMetaFailure(i, new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_VALUE_IS_REQUIRED, ValueType.STRING.name()));
                }
            } else if (isMetaNameEqualTo(metaItem, VelbusSocketProtocol.META_VELBUS_SOCKET_PORT)) {
                portFound = true;
                int port = metaItem.getValueAsInteger().orElse(0);
                if (port <= 0 || port > 65536) {
                    failure = true;
                    if (result == null) {
                        break;
                    }
                    result.addMetaFailure(i, new ValidationFailure(ValueHolder.ValueFailureReason.VALUE_MISMATCH, "1-65536"));
                }
            }
        }
    }
    if (!hostFound) {
        failure = true;
        if (result != null) {
            result.addMetaFailure(new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_MISSING, VelbusSocketProtocol.META_VELBUS_SOCKET_HOST));
        }
    }
    if (!portFound) {
        failure = true;
        if (result != null) {
            result.addMetaFailure(new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_MISSING, VelbusSocketProtocol.META_VELBUS_SOCKET_PORT));
        }
    }
    return !failure;
}
Also used : MetaItem(org.openremote.model.attribute.MetaItem) ValidationFailure(org.openremote.model.ValidationFailure)

Aggregations

ValidationFailure (org.openremote.model.ValidationFailure)15 MetaItem (org.openremote.model.attribute.MetaItem)6 Value (org.openremote.model.value.Value)5 AssetAttribute (org.openremote.model.asset.AssetAttribute)4 IsWidget (com.google.gwt.user.client.ui.IsWidget)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 Environment (org.openremote.app.client.Environment)3 org.openremote.app.client.widget (org.openremote.app.client.widget)3 SimulatorElement (org.openremote.model.simulator.SimulatorElement)3 FlowPanel (com.google.gwt.user.client.ui.FlowPanel)2 IntStream (java.util.stream.IntStream)2 AssetMeta (org.openremote.model.asset.AssetMeta)2 AgentLink (org.openremote.model.asset.agent.AgentLink)2 ProtocolDescriptor (org.openremote.model.asset.agent.ProtocolDescriptor)2 AttributeValidationResult (org.openremote.model.attribute.AttributeValidationResult)2 MetaItemDescriptor (org.openremote.model.attribute.MetaItemDescriptor)2 Supplier (org.openremote.model.interop.Supplier)2 ColorSimulatorElement (org.openremote.model.simulator.element.ColorSimulatorElement)2 NumberSimulatorElement (org.openremote.model.simulator.element.NumberSimulatorElement)2