Search in sources :

Example 1 with SimulatorState

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

the class SimulatorService method configure.

@Override
public void configure() throws Exception {
    from(CLIENT_EVENT_TOPIC).routeId("FromClientSimulatorRequests").filter(body().isInstanceOf(RequestSimulatorState.class)).process(exchange -> {
        RequestSimulatorState event = exchange.getIn().getBody(RequestSimulatorState.class);
        LOG.fine("Handling from client: " + event);
        String sessionKey = getSessionKey(exchange);
        AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
        // Superuser can get all
        if (!authContext.isSuperUser())
            return;
        for (AttributeRef protocolConfiguration : event.getConfigurations()) {
            publishSimulatorState(sessionKey, protocolConfiguration);
        }
    });
    from(CLIENT_EVENT_TOPIC).routeId("FromClientSimulatorState").filter(body().isInstanceOf(SimulatorState.class)).process(exchange -> {
        SimulatorState event = exchange.getIn().getBody(SimulatorState.class);
        LOG.fine("Handling from client: " + event);
        AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
        // Superuser can get all
        if (!authContext.isSuperUser())
            return;
        // TODO Should realm admins be able to work with simulators in their tenant?
        simulatorProtocol.updateSimulatorState(event);
    });
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef) SimulatorState(org.openremote.model.simulator.SimulatorState) RequestSimulatorState(org.openremote.model.simulator.RequestSimulatorState) AuthContext(org.openremote.container.security.AuthContext) RequestSimulatorState(org.openremote.model.simulator.RequestSimulatorState)

Example 2 with SimulatorState

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

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

the class Simulator method createSimulator.

protected void createSimulator() {
    eventRegistration = environment.getEventBus().register(SimulatorState.class, simulatorState -> {
        if (!simulatorState.getProtocolConfigurationRef().equals(protocolConfiguration))
            return;
        this.simulatorState = simulatorState;
        writeView();
    });
    environment.getEventService().dispatch(new RequestSimulatorState(protocolConfiguration));
    onCreate.run();
}
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) SimulatorState(org.openremote.model.simulator.SimulatorState) RequestSimulatorState(org.openremote.model.simulator.RequestSimulatorState) RequestSimulatorState(org.openremote.model.simulator.RequestSimulatorState)

Example 4 with SimulatorState

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

Aggregations

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