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);
});
}
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);
}
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();
}
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()])));
});
}
Aggregations