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