Search in sources :

Example 1 with ObjectValue

use of org.openremote.model.value.ObjectValue 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 ObjectValue

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

the class RulesService method processAssetChange.

protected void processAssetChange(ServerAsset asset, PersistenceEvent persistenceEvent) {
    withLock(getClass().getSimpleName() + "::processAssetChange", () -> {
        // We must load the asset from database (only when required), as the
        // persistence event might not contain a completely loaded asset
        BiFunction<Asset, AssetAttribute, AssetState> buildAssetState = (loadedAsset, attribute) -> new AssetState(loadedAsset, attribute.deepCopy(), Source.INTERNAL);
        switch(persistenceEvent.getCause()) {
            case INSERT:
                // New asset has been created so get attributes that have RULE_STATE meta
                List<AssetAttribute> ruleStateAttributes = asset.getAttributesStream().filter(AssetAttribute::isRuleState).collect(Collectors.toList());
                // Build an update with a fully loaded asset
                ruleStateAttributes.forEach(attribute -> {
                    ServerAsset loadedAsset = assetStorageService.find(asset.getId(), true);
                    // If the asset is now gone it was deleted immediately after being inserted, nothing more to do
                    if (loadedAsset == null)
                        return;
                    AssetState assetState = buildAssetState.apply(loadedAsset, attribute);
                    LOG.fine("Asset was persisted (" + persistenceEvent.getCause() + "), inserting fact: " + assetState);
                    updateAssetState(assetState, true, true);
                });
                break;
            case UPDATE:
                int attributesIndex = Arrays.asList(persistenceEvent.getPropertyNames()).indexOf("attributes");
                if (attributesIndex < 0) {
                    return;
                }
                // Fully load the asset
                final Asset loadedAsset = assetStorageService.find(asset.getId(), true);
                // If the asset is now gone it was deleted immediately after being updated, nothing more to do
                if (loadedAsset == null)
                    return;
                // Attributes have possibly changed so need to compare old and new attributes
                // to determine which facts to retract and which to insert
                List<AssetAttribute> oldRuleStateAttributes = attributesFromJson((ObjectValue) persistenceEvent.getPreviousState()[attributesIndex], asset.getId()).filter(AssetAttribute::isRuleState).collect(Collectors.toList());
                List<AssetAttribute> newRuleStateAttributes = attributesFromJson((ObjectValue) persistenceEvent.getCurrentState()[attributesIndex], asset.getId()).filter(AssetAttribute::isRuleState).collect(Collectors.toList());
                // Retract facts for attributes that are obsolete
                getAddedOrModifiedAttributes(newRuleStateAttributes, oldRuleStateAttributes, key -> key.equals(VALUE_TIMESTAMP_FIELD_NAME)).forEach(obsoleteFactAttribute -> {
                    AssetState update = buildAssetState.apply(loadedAsset, obsoleteFactAttribute);
                    LOG.fine("Asset was persisted (" + persistenceEvent.getCause() + "), retracting: " + update);
                    retractAssetState(update);
                });
                // Insert facts for attributes that are new
                getAddedOrModifiedAttributes(oldRuleStateAttributes, newRuleStateAttributes, key -> key.equals(VALUE_TIMESTAMP_FIELD_NAME)).forEach(newFactAttribute -> {
                    AssetState assetState = buildAssetState.apply(loadedAsset, newFactAttribute);
                    LOG.fine("Asset was persisted (" + persistenceEvent.getCause() + "), updating: " + assetState);
                    updateAssetState(assetState, true, true);
                });
                break;
            case DELETE:
                // Retract any facts that were associated with this asset
                asset.getAttributesStream().filter(AssetAttribute::isRuleState).forEach(attribute -> {
                    // We can't load the asset again (it was deleted), so don't use buildAssetState() and
                    // hope that the path of the event asset has been loaded before deletion, although it is
                    // "unlikely" anybody will access it during retraction...
                    AssetState assetState = new AssetState(asset, attribute, Source.INTERNAL);
                    LOG.fine("Asset was persisted (" + persistenceEvent.getCause() + "), retracting fact: " + assetState);
                    retractAssetState(assetState);
                });
                break;
        }
    });
}
Also used : PersistenceEvent(org.openremote.container.persistence.PersistenceEvent) Tenant(org.openremote.model.security.Tenant) java.util(java.util) BiFunction(java.util.function.BiFunction) AssetAttribute.getAddedOrModifiedAttributes(org.openremote.model.asset.AssetAttribute.getAddedOrModifiedAttributes) NotificationService(org.openremote.manager.notification.NotificationService) ObjectValue(org.openremote.model.value.ObjectValue) MapAccess.getString(org.openremote.container.util.MapAccess.getString) GlobalLock.withLockReturning(org.openremote.container.concurrent.GlobalLock.withLockReturning) Container(org.openremote.container.Container) ContainerService(org.openremote.container.ContainerService) PersistenceEvent.isPersistenceEventForEntityType(org.openremote.container.persistence.PersistenceEvent.isPersistenceEventForEntityType) PersistenceService(org.openremote.container.persistence.PersistenceService) ManagerExecutorService(org.openremote.manager.concurrent.ManagerExecutorService) PERSISTENCE_TOPIC(org.openremote.container.persistence.PersistenceEvent.PERSISTENCE_TOPIC) org.openremote.model.rules(org.openremote.model.rules) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) Asset(org.openremote.model.asset.Asset) Pair(org.openremote.model.util.Pair) VALUE_TIMESTAMP_FIELD_NAME(org.openremote.model.AbstractValueTimestampHolder.VALUE_TIMESTAMP_FIELD_NAME) AssetAttribute.attributesFromJson(org.openremote.model.asset.AssetAttribute.attributesFromJson) EntityManager(javax.persistence.EntityManager) Logger(java.util.logging.Logger) MessageBrokerSetupService(org.openremote.container.message.MessageBrokerSetupService) Collectors(java.util.stream.Collectors) AssetMeta(org.openremote.model.asset.AssetMeta) org.openremote.manager.asset(org.openremote.manager.asset) AssetQuery(org.openremote.model.asset.AssetQuery) Stream(java.util.stream.Stream) RouteBuilder(org.apache.camel.builder.RouteBuilder) TimerService(org.openremote.container.timer.TimerService) Source(org.openremote.model.attribute.AttributeEvent.Source) AssetAttribute(org.openremote.model.asset.AssetAttribute) FINEST(java.util.logging.Level.FINEST) GlobalLock.withLock(org.openremote.container.concurrent.GlobalLock.withLock) AssetAttribute(org.openremote.model.asset.AssetAttribute) Asset(org.openremote.model.asset.Asset)

Example 3 with ObjectValue

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

the class AttributeValueConstraint method toModelValue.

public ObjectValue toModelValue() {
    ObjectValue objectValue = Values.createObject();
    objectValue.put("valueComparator", valueComparator.name());
    objectValue.put("value", value);
    return objectValue;
}
Also used : ObjectValue(org.openremote.model.value.ObjectValue)

Example 4 with ObjectValue

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

the class CalendarEvent method fromValue.

public static Optional<CalendarEvent> fromValue(Value value) {
    if (value == null || value.getType() != ValueType.OBJECT) {
        return Optional.empty();
    }
    ObjectValue objectValue = (ObjectValue) value;
    Optional<Long> start = objectValue.get("start").flatMap(Values::getLongCoerced);
    Optional<Long> end = objectValue.get("end").flatMap(Values::getLongCoerced);
    Optional<RecurrenceRule> recurrence = RecurrenceRule.fromValue(objectValue.getObject("recurrence").orElse(null));
    if (!start.isPresent() || !end.isPresent()) {
        return Optional.empty();
    }
    return Optional.of(new CalendarEvent(new Date(1000L * start.get()), new Date(1000L * end.get()), recurrence.orElse(null)));
}
Also used : ObjectValue(org.openremote.model.value.ObjectValue) Values(org.openremote.model.value.Values) Date(java.util.Date)

Example 5 with ObjectValue

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

the class RecurrenceRule method toValue.

public Value toValue() {
    ObjectValue objectValue = Values.createObject();
    objectValue.put("frequency", Values.create(frequency.name()));
    if (interval != null) {
        objectValue.put("interval", Values.create(interval));
    }
    if (count != null) {
        objectValue.put("count", Values.create(count));
    }
    if (until != null) {
        objectValue.put("until", Values.create(until.getTime() / 1000));
    }
    return objectValue;
}
Also used : ObjectValue(org.openremote.model.value.ObjectValue)

Aggregations

ObjectValue (org.openremote.model.value.ObjectValue)21 java.util (java.util)3 Logger (java.util.logging.Logger)3 Collectors (java.util.stream.Collectors)3 Stream (java.util.stream.Stream)3 EntityManager (javax.persistence.EntityManager)3 RouteBuilder (org.apache.camel.builder.RouteBuilder)3 Container (org.openremote.container.Container)3 ContainerService (org.openremote.container.ContainerService)3 GlobalLock.withLock (org.openremote.container.concurrent.GlobalLock.withLock)3 GlobalLock.withLockReturning (org.openremote.container.concurrent.GlobalLock.withLockReturning)3 MessageBrokerSetupService (org.openremote.container.message.MessageBrokerSetupService)3 PersistenceEvent (org.openremote.container.persistence.PersistenceEvent)3 TimerService (org.openremote.container.timer.TimerService)3 org.openremote.manager.asset (org.openremote.manager.asset)3 ManagerIdentityService (org.openremote.manager.security.ManagerIdentityService)3 VALUE_TIMESTAMP_FIELD_NAME (org.openremote.model.AbstractValueTimestampHolder.VALUE_TIMESTAMP_FIELD_NAME)3 AssetAttribute.attributesFromJson (org.openremote.model.asset.AssetAttribute.attributesFromJson)3 AssetAttribute.getAddedOrModifiedAttributes (org.openremote.model.asset.AssetAttribute.getAddedOrModifiedAttributes)3 Source (org.openremote.model.attribute.AttributeEvent.Source)3