Search in sources :

Example 1 with ArrayValue

use of org.openremote.model.value.ArrayValue in project openremote by openremote.

the class Protocol method getLinkedAttributeMessageFilters.

/**
 * Extract the {@link MessageFilter}s from the specified {@link Attribute}
 */
static Optional<List<MessageFilter>> getLinkedAttributeMessageFilters(Attribute attribute) {
    if (attribute == null) {
        return Optional.empty();
    }
    Optional<ArrayValue> arrayValueOptional = attribute.getMetaItem(META_PROTOCOL_FILTERS).flatMap(AbstractValueHolder::getValueAsArray);
    if (!arrayValueOptional.isPresent()) {
        return Optional.empty();
    }
    try {
        ArrayValue arrayValue = arrayValueOptional.get();
        List<MessageFilter> messageFilters = new ArrayList<>(arrayValue.length());
        for (int i = 0; i < arrayValue.length(); i++) {
            ObjectValue objValue = arrayValue.getObject(i).orElseThrow(() -> new IllegalArgumentException("Attribute protocol filters meta item is invalid"));
            MessageFilter filter = deserialiseMessageFilter(objValue);
            messageFilters.add(filter);
        }
        return messageFilters.isEmpty() ? Optional.empty() : Optional.of(messageFilters);
    } catch (IllegalArgumentException e) {
        LOG.log(Level.WARNING, e.getMessage(), e);
    }
    return Optional.empty();
}
Also used : ObjectValue(org.openremote.model.value.ObjectValue) AbstractValueHolder(org.openremote.model.AbstractValueHolder) ArrayList(java.util.ArrayList) MessageFilter(org.openremote.agent.protocol.filter.MessageFilter) ArrayValue(org.openremote.model.value.ArrayValue)

Example 2 with ArrayValue

use of org.openremote.model.value.ArrayValue in project openremote by openremote.

the class AbstractHttpServerProtocol method createDeployment.

protected ResteasyDeployment createDeployment(Application application, AssetAttribute protocolConfiguration) {
    ResteasyDeployment resteasyDeployment = new ResteasyDeployment();
    resteasyDeployment.setApplication(application);
    List<String> allowedOrigins;
    if (devMode) {
        allowedOrigins = Collections.singletonList("*");
    } else {
        Optional<MetaItem> allowedOriginsMeta = protocolConfiguration.getMetaItem(META_PROTOCOL_ALLOWED_ORIGINS);
        allowedOrigins = allowedOriginsMeta.flatMap(AbstractValueHolder::getValueAsString).map(Collections::singletonList).orElseGet(() -> allowedOriginsMeta.flatMap(AbstractValueHolder::getValueAsArray).flatMap(arrayValue -> Values.getArrayElements(arrayValue, StringValue.class, true, false, StringValue::getString)).orElse(null));
    }
    if (allowedOrigins != null) {
        String allowedMethods = protocolConfiguration.getMetaItem(META_PROTOCOL_ALLOWED_METHODS).flatMap(AbstractValueHolder::getValueAsString).orElse(DEFAULT_ALLOWED_METHODS);
        if (TextUtil.isNullOrEmpty(allowedMethods)) {
            throw new IllegalArgumentException("Allowed methods meta item must be a non empty string: " + META_PROTOCOL_ALLOWED_METHODS);
        }
        CorsFilter corsFilter = new CorsFilter();
        corsFilter.getAllowedOrigins().addAll(allowedOrigins);
        corsFilter.setAllowedMethods(allowedMethods);
        resteasyDeployment.getProviders().add(corsFilter);
    }
    return resteasyDeployment;
}
Also used : java.util(java.util) ServletException(javax.servlet.ServletException) ProtocolConfiguration(org.openremote.model.asset.agent.ProtocolConfiguration) AttributeRef(org.openremote.model.attribute.AttributeRef) ArrayValue(org.openremote.model.value.ArrayValue) Application(javax.ws.rs.core.Application) ServletInfo(io.undertow.servlet.api.ServletInfo) HttpMethod(javax.ws.rs.HttpMethod) Level(java.util.logging.Level) Servlets(io.undertow.servlet.Servlets) Container(org.openremote.container.Container) Lists(com.google.common.collect.Lists) ResteasyDeployment(org.jboss.resteasy.spi.ResteasyDeployment) TextUtil(org.openremote.model.util.TextUtil) PROTOCOL_NAMESPACE(org.openremote.model.Constants.PROTOCOL_NAMESPACE) AbstractValueHolder(org.openremote.model.AbstractValueHolder) StringValue(org.openremote.model.value.StringValue) org.openremote.container.web(org.openremote.container.web) Pair(org.openremote.model.util.Pair) ModelValueMessageBodyConverter(org.openremote.container.json.ModelValueMessageBodyConverter) Logger(java.util.logging.Logger) DeploymentManager(io.undertow.servlet.api.DeploymentManager) HttpServlet30Dispatcher(org.jboss.resteasy.plugins.server.servlet.HttpServlet30Dispatcher) AbstractProtocol(org.openremote.agent.protocol.AbstractProtocol) HttpHandler(io.undertow.server.HttpHandler) MetaItem(org.openremote.model.attribute.MetaItem) IdentityService(org.openremote.container.security.IdentityService) CorsFilter(org.jboss.resteasy.plugins.interceptors.CorsFilter) RoleBasedSecurityFeature(org.jboss.resteasy.plugins.interceptors.RoleBasedSecurityFeature) JacksonConfig(org.openremote.container.json.JacksonConfig) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) Values(org.openremote.model.value.Values) Pattern(java.util.regex.Pattern) ValueHolder(org.openremote.model.ValueHolder) AssetAttribute(org.openremote.model.asset.AssetAttribute) CorsFilter(org.jboss.resteasy.plugins.interceptors.CorsFilter) ResteasyDeployment(org.jboss.resteasy.spi.ResteasyDeployment) MetaItem(org.openremote.model.attribute.MetaItem) AbstractValueHolder(org.openremote.model.AbstractValueHolder) StringValue(org.openremote.model.value.StringValue)

Example 3 with ArrayValue

use of org.openremote.model.value.ArrayValue in project openremote by openremote.

the class ColorRGB method asArrayValue.

public ArrayValue asArrayValue() {
    ArrayValue array = Values.createArray();
    array.set(0, Values.create(getRed()));
    array.set(1, Values.create(getGreen()));
    array.set(2, Values.create(getBlue()));
    return array;
}
Also used : ArrayValue(org.openremote.model.value.ArrayValue)

Example 4 with ArrayValue

use of org.openremote.model.value.ArrayValue in project openremote by openremote.

the class MapWidget method flyTo.

public void flyTo(double[] coordinates) {
    if (!isMapReady())
        throw new IllegalStateException("Map not ready");
    if (coordinates == null || coordinates.length != 2)
        return;
    ObjectValue options = Values.createObject();
    ArrayValue center = Values.createArray();
    center.set(0, coordinates[0]);
    center.set(1, coordinates[1]);
    options.put("center", center);
    mapboxMap.flyTo(options.asAny());
}
Also used : ObjectValue(org.openremote.model.value.ObjectValue) ArrayValue(org.openremote.model.value.ArrayValue)

Example 5 with ArrayValue

use of org.openremote.model.value.ArrayValue in project openremote by openremote.

the class TypeMapper method toDPTXlator.

@SuppressWarnings("ConstantConditions")
public static DPTXlator toDPTXlator(Datapoint datapoint, Value value) throws Exception {
    DPTXlator translator = TranslatorTypes.createTranslator(0, datapoint.getDPT());
    if (translator instanceof DPTXlatorBoolean && value != null && value.getType() == ValueType.BOOLEAN) {
        ((DPTXlatorBoolean) translator).setValue(Values.getBoolean(value).get());
    } else if (translator instanceof DPTXlator8BitUnsigned && value != null && value.getType() == ValueType.NUMBER) {
        ((DPTXlator8BitUnsigned) translator).setValue(Values.getIntegerCoerced(value).orElse(0));
    } else if (translator instanceof DPTXlatorRGB && value != null && value.getType() == ValueType.ARRAY) {
        ArrayValue arrayValue = Values.getArray(value).get();
        ((DPTXlatorRGB) translator).setValue(arrayValue.getNumber(0).get().intValue(), arrayValue.getNumber(1).get().intValue(), arrayValue.getNumber(2).get().intValue());
    } else {
        // TODO depending on the DPT and the value, a more sophisticated translation is needed
        translator.setValue(value.toString());
    }
    return translator;
}
Also used : ArrayValue(org.openremote.model.value.ArrayValue)

Aggregations

ArrayValue (org.openremote.model.value.ArrayValue)8 ObjectValue (org.openremote.model.value.ObjectValue)3 AbstractValueHolder (org.openremote.model.AbstractValueHolder)2 Lists (com.google.common.collect.Lists)1 HttpHandler (io.undertow.server.HttpHandler)1 Servlets (io.undertow.servlet.Servlets)1 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 DeploymentManager (io.undertow.servlet.api.DeploymentManager)1 ServletInfo (io.undertow.servlet.api.ServletInfo)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Pattern (java.util.regex.Pattern)1 ServletException (javax.servlet.ServletException)1 HttpMethod (javax.ws.rs.HttpMethod)1 Application (javax.ws.rs.core.Application)1