Search in sources :

Example 1 with NameValuePredicate

use of org.openremote.model.query.filter.NameValuePredicate in project openremote by openremote.

the class AgentService method startAgent.

protected void startAgent(Agent<?, ?, ?> agent) {
    withLock(getClass().getSimpleName() + "::startAgent", () -> {
        Protocol<?> protocol = null;
        try {
            protocol = agent.getProtocolInstance();
            protocolInstanceMap.put(agent.getId(), protocol);
            LOG.fine("Starting protocol instance: " + protocol);
            protocol.start(container);
            LOG.fine("Started protocol instance:" + protocol);
            LOG.finer("Linking attributes to protocol instance: " + protocol);
            // Get all assets that have attributes with agent link meta for this agent
            List<Asset<?>> assets = assetStorageService.findAll(new AssetQuery().attributes(new AttributePredicate().meta(new NameValuePredicate(AGENT_LINK, new StringPredicate(agent.getId()), false, new NameValuePredicate.Path("id")))));
            LOG.finer("Found '" + assets.size() + "' asset(s) with attributes linked to this protocol instance: " + protocol);
            assets.forEach(asset -> getGroupedAgentLinkAttributes(asset.getAttributes().stream(), assetAttribute -> assetAttribute.getMetaValue(AGENT_LINK).map(agentLink -> agentLink.getId().equals(agent.getId())).orElse(false)).forEach((agnt, attributes) -> linkAttributes(agnt, asset.getId(), attributes)));
        } catch (Exception e) {
            if (protocol != null) {
                try {
                    protocol.stop(container);
                } catch (Exception ignored) {
                }
            }
            protocolInstanceMap.remove(agent.getId());
            LOG.log(Level.SEVERE, "Failed to start protocol instance for agent: " + agent, e);
            sendAttributeEvent(new AttributeEvent(agent.getId(), Agent.STATUS.getName(), ConnectionStatus.ERROR));
        }
    });
}
Also used : Protocol(org.openremote.model.asset.agent.Protocol) AssetProcessingException(org.openremote.manager.asset.AssetProcessingException) AssetProcessingService(org.openremote.manager.asset.AssetProcessingService) ASSET_QUEUE(org.openremote.manager.asset.AssetProcessingService.ASSET_QUEUE) ProtocolInstanceDiscovery(org.openremote.model.protocol.ProtocolInstanceDiscovery) ACTUATOR_TOPIC(org.openremote.model.asset.agent.Protocol.ACTUATOR_TOPIC) AgentLink(org.openremote.model.asset.agent.AgentLink) Future(java.util.concurrent.Future) TextUtil(org.openremote.model.util.TextUtil) Attribute.getAddedOrModifiedAttributes(org.openremote.model.attribute.Attribute.getAddedOrModifiedAttributes) AssetTreeNode(org.openremote.model.asset.AssetTreeNode) Predicate(java.util.function.Predicate) ProtocolAssetDiscovery(org.openremote.model.protocol.ProtocolAssetDiscovery) ContainerService(org.openremote.model.ContainerService) AttributePredicate(org.openremote.model.query.filter.AttributePredicate) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Container(org.openremote.model.Container) Stream(java.util.stream.Stream) StringPredicate(org.openremote.model.query.filter.StringPredicate) RouteBuilder(org.apache.camel.builder.RouteBuilder) PERSISTENCE_TOPIC(org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC) org.openremote.model.attribute(org.openremote.model.attribute) Agent(org.openremote.model.asset.agent.Agent) PathPredicate(org.openremote.model.query.filter.PathPredicate) GatewayService(org.openremote.manager.gateway.GatewayService) GlobalLock.withLock(org.openremote.container.concurrent.GlobalLock.withLock) AssetStorageService(org.openremote.manager.asset.AssetStorageService) GatewayService.isNotForGateway(org.openremote.manager.gateway.GatewayService.isNotForGateway) java.util(java.util) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus) Level(java.util.logging.Level) GlobalLock.withLockReturning(org.openremote.container.concurrent.GlobalLock.withLockReturning) AGENT_LINK(org.openremote.model.value.MetaItemType.AGENT_LINK) Collectors.mapping(java.util.stream.Collectors.mapping) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NameValuePredicate(org.openremote.model.query.filter.NameValuePredicate) ManagerWebService(org.openremote.manager.web.ManagerWebService) PersistenceEvent(org.openremote.model.PersistenceEvent) MessageBrokerService(org.openremote.container.message.MessageBrokerService) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) Asset(org.openremote.model.asset.Asset) AssetQuery(org.openremote.model.query.AssetQuery) Pair(org.openremote.model.util.Pair) ProtocolAssetService(org.openremote.agent.protocol.ProtocolAssetService) EntityManager(javax.persistence.EntityManager) HEADER_SOURCE(org.openremote.model.attribute.AttributeEvent.HEADER_SOURCE) Consumer(java.util.function.Consumer) SENSOR_QUEUE(org.openremote.model.asset.agent.Protocol.SENSOR_QUEUE) ClientEventService(org.openremote.manager.event.ClientEventService) ProtocolAssetImport(org.openremote.model.protocol.ProtocolAssetImport) Collectors.toList(java.util.stream.Collectors.toList) TimerService(org.openremote.container.timer.TimerService) AssetUpdateProcessor(org.openremote.manager.asset.AssetUpdateProcessor) PersistenceService.isPersistenceEventForEntityType(org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType) Source(org.openremote.model.attribute.AttributeEvent.Source) StringPredicate(org.openremote.model.query.filter.StringPredicate) AssetQuery(org.openremote.model.query.AssetQuery) Asset(org.openremote.model.asset.Asset) NameValuePredicate(org.openremote.model.query.filter.NameValuePredicate) AttributePredicate(org.openremote.model.query.filter.AttributePredicate) AssetProcessingException(org.openremote.manager.asset.AssetProcessingException)

Example 2 with NameValuePredicate

use of org.openremote.model.query.filter.NameValuePredicate in project openremote by openremote.

the class AssetDatapointService method purgeDataPoints.

protected void purgeDataPoints() {
    LOG.info("Starting data points purge daily task");
    try {
        // Get list of attributes that have custom durations
        List<Asset<?>> assets = assetStorageService.findAll(new AssetQuery().attributes(new AttributePredicate().meta(new NameValuePredicate(MetaItemType.DATA_POINTS_MAX_AGE_DAYS, null))));
        List<Pair<String, Attribute<?>>> attributes = assets.stream().map(asset -> asset.getAttributes().stream().filter(assetAttribute -> assetAttribute.hasMeta(MetaItemType.DATA_POINTS_MAX_AGE_DAYS)).map(assetAttribute -> new Pair<String, Attribute<?>>(asset.getId(), assetAttribute)).collect(toList())).flatMap(List::stream).collect(toList());
        // Purge data points not in the above list using default duration
        LOG.fine("Purging data points of attributes that use default max age days of " + maxDatapointAgeDays);
        persistenceService.doTransaction(em -> em.createQuery("delete from AssetDatapoint dp " + "where dp.timestamp < :dt" + buildWhereClause(attributes, true)).setParameter("dt", Date.from(timerService.getNow().truncatedTo(DAYS).minus(maxDatapointAgeDays, DAYS))).executeUpdate());
        if (!attributes.isEmpty()) {
            // Purge data points that have specific age constraints
            Map<Integer, List<Pair<String, Attribute<?>>>> ageAttributeRefMap = attributes.stream().collect(groupingBy(attributeRef -> attributeRef.value.getMetaValue(MetaItemType.DATA_POINTS_MAX_AGE_DAYS).orElse(maxDatapointAgeDays)));
            ageAttributeRefMap.forEach((age, attrs) -> {
                LOG.fine("Purging data points of " + attrs.size() + " attributes that use a max age of " + age);
                try {
                    persistenceService.doTransaction(em -> em.createQuery("delete from AssetDatapoint dp " + "where dp.timestamp < :dt" + buildWhereClause(attrs, false)).setParameter("dt", Date.from(timerService.getNow().truncatedTo(DAYS).minus(age, DAYS))).executeUpdate());
                } catch (Exception e) {
                    LOG.log(Level.SEVERE, "An error occurred whilst deleting data points, this should not happen", e);
                }
            });
        }
    } catch (Exception e) {
        LOG.log(Level.WARNING, "Failed to run data points purge", e);
    }
    LOG.info("Finished data points purge daily task");
}
Also used : AssetStorageService(org.openremote.manager.asset.AssetStorageService) Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) AssetProcessingException(org.openremote.manager.asset.AssetProcessingException) AttributeRef(org.openremote.model.attribute.AttributeRef) LocalDateTime(java.time.LocalDateTime) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) AttributeWriteFailure(org.openremote.model.attribute.AttributeWriteFailure) STORE_DATA_POINTS(org.openremote.model.value.MetaItemType.STORE_DATA_POINTS) MapAccess.getInteger(org.openremote.container.util.MapAccess.getInteger) MapAccess.getString(org.openremote.container.util.MapAccess.getString) Level(java.util.logging.Level) UniqueIdentifierGenerator(org.openremote.container.util.UniqueIdentifierGenerator) Attribute(org.openremote.model.attribute.Attribute) Duration(java.time.Duration) Map(java.util.Map) NameValuePredicate(org.openremote.model.query.filter.NameValuePredicate) ManagerWebService(org.openremote.manager.web.ManagerWebService) Path(java.nio.file.Path) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) Asset(org.openremote.model.asset.Asset) MetaItemType(org.openremote.model.value.MetaItemType) AssetQuery(org.openremote.model.query.AssetQuery) Pair(org.openremote.model.util.Pair) AssetDatapoint(org.openremote.model.datapoint.AssetDatapoint) AttributePredicate(org.openremote.model.query.filter.AttributePredicate) EntityManager(javax.persistence.EntityManager) Instant(java.time.Instant) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) File(java.io.File) ZoneId(java.time.ZoneId) Container(org.openremote.model.Container) Date(java.sql.Date) TimeUnit(java.util.concurrent.TimeUnit) DAYS(java.time.temporal.ChronoUnit.DAYS) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Paths(java.nio.file.Paths) TimerService(org.openremote.container.timer.TimerService) AssetUpdateProcessor(org.openremote.manager.asset.AssetUpdateProcessor) Source(org.openremote.model.attribute.AttributeEvent.Source) Attribute(org.openremote.model.attribute.Attribute) AssetQuery(org.openremote.model.query.AssetQuery) MapAccess.getString(org.openremote.container.util.MapAccess.getString) AttributePredicate(org.openremote.model.query.filter.AttributePredicate) AssetProcessingException(org.openremote.manager.asset.AssetProcessingException) MapAccess.getInteger(org.openremote.container.util.MapAccess.getInteger) Asset(org.openremote.model.asset.Asset) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) NameValuePredicate(org.openremote.model.query.filter.NameValuePredicate) Pair(org.openremote.model.util.Pair)

Aggregations

Level (java.util.logging.Level)2 Logger (java.util.logging.Logger)2 Collectors (java.util.stream.Collectors)2 Collectors.toList (java.util.stream.Collectors.toList)2 EntityManager (javax.persistence.EntityManager)2 TimerService (org.openremote.container.timer.TimerService)2 AssetProcessingException (org.openremote.manager.asset.AssetProcessingException)2 AssetStorageService (org.openremote.manager.asset.AssetStorageService)2 AssetUpdateProcessor (org.openremote.manager.asset.AssetUpdateProcessor)2 ManagerIdentityService (org.openremote.manager.security.ManagerIdentityService)2 ManagerWebService (org.openremote.manager.web.ManagerWebService)2 Container (org.openremote.model.Container)2 Asset (org.openremote.model.asset.Asset)2 Source (org.openremote.model.attribute.AttributeEvent.Source)2 AssetQuery (org.openremote.model.query.AssetQuery)2 AttributePredicate (org.openremote.model.query.filter.AttributePredicate)2 NameValuePredicate (org.openremote.model.query.filter.NameValuePredicate)2 Pair (org.openremote.model.util.Pair)2 File (java.io.File)1 Path (java.nio.file.Path)1