Search in sources :

Example 1 with Consumer

use of org.openremote.model.interop.Consumer in project openremote by openremote.

the class AssetEditActivity method getLinkableAssetsAndAttributes.

@Override
public void getLinkableAssetsAndAttributes(ValueHolder valueHolder, Consumer<Map<AttributeRefEditor.AssetInfo, List<AttributeRefEditor.AttributeInfo>>> assetAttributeConsumer) {
    AssetQuery query;
    Predicate<AssetAttribute> attributeFilter = null;
    // Is it agent or attribute link?
    if ((valueHolder instanceof MetaItem) && AgentLink.isAgentLink((MetaItem) valueHolder)) {
        query = new AssetQuery().select(new AssetQuery.Select(AssetQuery.Include.ONLY_ID_AND_NAME_AND_ATTRIBUTES)).type(AssetType.AGENT);
        // the query will be automatically restricted to the logged in users realm)
        if (!isNullOrEmpty(asset.getRealmId())) {
            query.tenant(new TenantPredicate(asset.getRealmId()));
        }
        // Agents must have protocol configurations
        query.attributeMeta(new AttributeMetaPredicate(AssetMeta.PROTOCOL_CONFIGURATION, new BooleanPredicate(true)));
        // Only show protocol configurations
        attributeFilter = ProtocolConfiguration::isProtocolConfiguration;
    } else {
        query = new AssetQuery().select(new AssetQuery.Select(AssetQuery.Include.ONLY_ID_AND_NAME_AND_ATTRIBUTE_NAMES));
        // otherwise the query will be automatically restricted to the logged in users realm)
        if (!isNullOrEmpty(asset.getRealmId())) {
            query.tenant(new AssetQuery.TenantPredicate(asset.getRealmId()));
        }
    }
    // Do request
    final Predicate<AssetAttribute> finalAttributeFilter = attributeFilter;
    environment.getApp().getRequests().sendWithAndReturn(assetArrayMapper, assetQueryMapper, requestParams -> assetResource.queryAssets(requestParams, query), 200, assets -> {
        Map<AttributeRefEditor.AssetInfo, List<AttributeRefEditor.AttributeInfo>> assetAttributeMap = Arrays.stream(assets).filter(asset -> !asset.getAttributesList().isEmpty()).collect(Collectors.toMap(asset -> new AttributeRefEditor.AssetInfo(asset.getName(), asset.getId()), asset -> asset.getAttributesStream().filter(attribute -> finalAttributeFilter == null || finalAttributeFilter.test(attribute)).map(attribute -> new AttributeRefEditor.AttributeInfo(attribute.getName().orElse(null), attribute.getLabelOrName().orElse(null))).collect(Collectors.toList())));
        assetAttributeConsumer.accept(assetAttributeMap);
    }, exception -> assetAttributeConsumer.accept(new HashMap<>()));
}
Also used : org.openremote.app.client.assets.attributes(org.openremote.app.client.assets.attributes) java.util(java.util) Environment(org.openremote.app.client.Environment) ProtocolConfiguration(org.openremote.model.asset.agent.ProtocolConfiguration) ATTRIBUTE_NAME_VALIDATOR(org.openremote.model.attribute.Attribute.ATTRIBUTE_NAME_VALIDATOR) ObjectValueMapper(org.openremote.app.client.interop.value.ObjectValueMapper) FormButton(org.openremote.app.client.widget.FormButton) ShowSuccessEvent(org.openremote.app.client.event.ShowSuccessEvent) AgentLink(org.openremote.model.asset.agent.AgentLink) Inject(javax.inject.Inject) AttributeRefEditor(org.openremote.app.client.widget.AttributeRefEditor) IsWidget(com.google.gwt.user.client.ui.IsWidget) AssetTreeNode(org.openremote.app.client.assets.browser.AssetTreeNode) org.openremote.model.asset(org.openremote.model.asset) FileInfoMapper(org.openremote.app.client.interop.jackson.FileInfoMapper) BrowserTreeNode(org.openremote.app.client.assets.browser.BrowserTreeNode) Consumer(org.openremote.model.interop.Consumer) AgentResource(org.openremote.model.asset.agent.AgentResource) MapResource(org.openremote.model.map.MapResource) ValueType(org.openremote.model.value.ValueType) AttributeLinkEditor(org.openremote.app.client.widget.AttributeLinkEditor) Predicate(java.util.function.Predicate) Pair(org.openremote.model.util.Pair) ShowFailureEvent(org.openremote.app.client.event.ShowFailureEvent) ValueEditors(org.openremote.app.client.widget.ValueEditors) EnumUtil(org.openremote.model.util.EnumUtil) AssetAttribute.attributesFromJson(org.openremote.model.asset.AssetAttribute.attributesFromJson) JsonEditor(org.openremote.app.client.app.dialog.JsonEditor) Collectors(java.util.stream.Collectors) org.openremote.app.client.assets(org.openremote.app.client.assets) Value(org.openremote.model.value.Value) Provider(com.google.inject.Provider) AssetBrowser(org.openremote.app.client.assets.browser.AssetBrowser) AssetQuery(org.openremote.model.asset.AssetQuery) org.openremote.model.attribute(org.openremote.model.attribute) ConstraintViolation(org.openremote.model.http.ConstraintViolation) ProtocolDescriptor(org.openremote.model.asset.agent.ProtocolDescriptor) TenantTreeNode(org.openremote.app.client.assets.browser.TenantTreeNode) TextUtil.isNullOrEmpty(org.openremote.model.util.TextUtil.isNullOrEmpty) ValueHolder(org.openremote.model.ValueHolder) Attribute.isAttributeNameEqualTo(org.openremote.model.attribute.Attribute.isAttributeNameEqualTo) MetaItem.isMetaNameEqualTo(org.openremote.model.attribute.MetaItem.isMetaNameEqualTo) AssetQuery(org.openremote.model.asset.AssetQuery) AttributeRefEditor(org.openremote.app.client.widget.AttributeRefEditor) ProtocolConfiguration(org.openremote.model.asset.agent.ProtocolConfiguration)

Example 2 with Consumer

use of org.openremote.model.interop.Consumer 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)

Aggregations

IsWidget (com.google.gwt.user.client.ui.IsWidget)2 java.util (java.util)2 Environment (org.openremote.app.client.Environment)2 ShowSuccessEvent (org.openremote.app.client.event.ShowSuccessEvent)2 Consumer (org.openremote.model.interop.Consumer)2 Provider (com.google.inject.Provider)1 Browser (elemental.client.Browser)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 JsonEditor (org.openremote.app.client.app.dialog.JsonEditor)1 org.openremote.app.client.assets (org.openremote.app.client.assets)1 org.openremote.app.client.assets.attributes (org.openremote.app.client.assets.attributes)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 AssetBrowser (org.openremote.app.client.assets.browser.AssetBrowser)1 AssetTreeNode (org.openremote.app.client.assets.browser.AssetTreeNode)1 BrowserTreeNode (org.openremote.app.client.assets.browser.BrowserTreeNode)1 TenantTreeNode (org.openremote.app.client.assets.browser.TenantTreeNode)1