Search in sources :

Example 1 with SimulatorElement

use of org.openremote.model.simulator.SimulatorElement in project openremote by openremote.

the class Simulator method writeView.

protected void writeView() {
    clear();
    addLabel(environment.getMessages().simulator());
    formGroups.clear();
    List<SimulatorElement> sortedElements = Arrays.asList(simulatorState.getElements());
    sortedElements.sort(Comparator.comparing(o -> simulatorState.getElementName(o)));
    for (SimulatorElement element : sortedElements) {
        FormGroup formGroup = new FormGroup();
        String elementName = simulatorState.getElementName(element);
        FormLabel formLabel = new FormLabel(elementName);
        formLabel.addStyleName("largest");
        formGroup.setFormLabel(formLabel);
        FormField formField = new FormField();
        formGroup.setFormField(formField);
        // Don't push simulator value validation up to the presenter as it is a special case that should
        // just be evaluated in-situ and shouldn't invalidate the parent attribute
        Consumer<Value> onModified = value -> {
            element.setValue(value);
            List<ValidationFailure> failures = element.getValidationFailures();
            formGroup.setError(failures != null && !failures.isEmpty());
        };
        ValueType valueType = element.getExpectedType().getValueType();
        IsWidget editor = valueEditorSupplier.createValueEditor(element, valueType, style, parentView, onModified);
        formField.add(editor);
        formGroups.put(element.getAttributeRef(), formGroup);
        add(formGroup);
    }
    if (sortedElements.size() > 0) {
        FormGroup submitGroup = new FormGroup();
        submitGroup.getElement().getStyle().setWidth(80, com.google.gwt.dom.client.Style.Unit.PCT);
        FormField submitField = new FormField();
        submitGroup.setFormField(submitField);
        FormButton writeButton = new FormButton(environment.getMessages().writeSimulatorState());
        writeButton.setPrimary(true);
        writeButton.addClickHandler(event -> {
            if (isValid()) {
                environment.getEventService().dispatch(simulatorState);
                environment.getEventBus().dispatch(new ShowSuccessEvent(environment.getMessages().simulatorStateSubmitted()));
            }
        });
        submitField.add(writeButton);
        add(submitGroup);
    } else {
        add(new FormInlineLabel(environment.getMessages().noAttributesLinkedToSimulator()));
    }
    // "Blink" the editor so users know there might be a new value
    for (FormGroup formGroup : formGroups.values()) {
        formGroup.addStyleName(environment.getWidgetStyle().HighlightBackground());
    }
    Browser.getWindow().setTimeout(() -> {
        for (FormGroup formGroup : formGroups.values()) formGroup.removeStyleName(environment.getWidgetStyle().HighlightBackground());
    }, 250);
}
Also used : Browser(elemental.client.Browser) EventRegistration(org.openremote.model.event.bus.EventRegistration) Consumer(org.openremote.model.interop.Consumer) SimulatorState(org.openremote.model.simulator.SimulatorState) java.util(java.util) ValueType(org.openremote.model.value.ValueType) Environment(org.openremote.app.client.Environment) AttributeRef(org.openremote.model.attribute.AttributeRef) ValidationFailure(org.openremote.model.ValidationFailure) org.openremote.app.client.widget(org.openremote.app.client.widget) AttributeViewImpl(org.openremote.app.client.assets.attributes.AttributeViewImpl) RequestSimulatorState(org.openremote.model.simulator.RequestSimulatorState) ShowSuccessEvent(org.openremote.app.client.event.ShowSuccessEvent) AttributeValidationResult(org.openremote.model.attribute.AttributeValidationResult) Value(org.openremote.model.value.Value) SimulatorElement(org.openremote.model.simulator.SimulatorElement) AbstractAttributeViewExtension(org.openremote.app.client.assets.attributes.AbstractAttributeViewExtension) IsWidget(com.google.gwt.user.client.ui.IsWidget) AssetAttribute(org.openremote.model.asset.AssetAttribute) AttributeView(org.openremote.app.client.assets.attributes.AttributeView) IsWidget(com.google.gwt.user.client.ui.IsWidget) ValueType(org.openremote.model.value.ValueType) SimulatorElement(org.openremote.model.simulator.SimulatorElement) ShowSuccessEvent(org.openremote.app.client.event.ShowSuccessEvent) Value(org.openremote.model.value.Value)

Example 2 with SimulatorElement

use of org.openremote.model.simulator.SimulatorElement 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 3 with SimulatorElement

use of org.openremote.model.simulator.SimulatorElement in project openremote by openremote.

the class SimulatorProtocol method getSimulatorState.

/**
 * Read a state snapshot.
 */
public Optional<SimulatorState> getSimulatorState(AttributeRef protocolConfigurationRef) {
    return withLockReturning(getProtocolName() + "::getSimulatorState", () -> {
        LOG.info("Getting simulator state for protocol configuration: " + protocolConfigurationRef);
        if (!instances.containsKey(protocolConfigurationRef))
            return Optional.empty();
        List<SimulatorElement> linkedElements = getLinkedElements(protocolConfigurationRef);
        return Optional.of(new SimulatorState(timerService.getCurrentTimeMillis(), protocolConfigurationRef, linkedElements.toArray(new SimulatorElement[linkedElements.size()])));
    });
}
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) SimulatorState(org.openremote.model.simulator.SimulatorState)

Example 4 with SimulatorElement

use of org.openremote.model.simulator.SimulatorElement 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)

Aggregations

SimulatorElement (org.openremote.model.simulator.SimulatorElement)4 ValidationFailure (org.openremote.model.ValidationFailure)3 ColorSimulatorElement (org.openremote.model.simulator.element.ColorSimulatorElement)3 NumberSimulatorElement (org.openremote.model.simulator.element.NumberSimulatorElement)3 SwitchSimulatorElement (org.openremote.model.simulator.element.SwitchSimulatorElement)3 SimulatorState (org.openremote.model.simulator.SimulatorState)2 Value (org.openremote.model.value.Value)2 IsWidget (com.google.gwt.user.client.ui.IsWidget)1 Browser (elemental.client.Browser)1 java.util (java.util)1 Environment (org.openremote.app.client.Environment)1 AbstractAttributeViewExtension (org.openremote.app.client.assets.attributes.AbstractAttributeViewExtension)1 AttributeView (org.openremote.app.client.assets.attributes.AttributeView)1 AttributeViewImpl (org.openremote.app.client.assets.attributes.AttributeViewImpl)1 ShowSuccessEvent (org.openremote.app.client.event.ShowSuccessEvent)1 org.openremote.app.client.widget (org.openremote.app.client.widget)1 AssetAttribute (org.openremote.model.asset.AssetAttribute)1 AttributeRef (org.openremote.model.attribute.AttributeRef)1 AttributeValidationResult (org.openremote.model.attribute.AttributeValidationResult)1 EventRegistration (org.openremote.model.event.bus.EventRegistration)1