Search in sources :

Example 16 with AttributeRef

use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.

the class SimulatorProtocol method doLinkAttribute.

@Override
protected void doLinkAttribute(String assetId, Attribute<?> attribute, SimulatorAgentLink agentLink) {
    // Look for replay data
    agentLink.getReplayData().ifPresent(simulatorReplayDatapoints -> {
        LOG.info("Simulator replay data found for linked attribute: " + attribute);
        AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());
        ScheduledFuture<?> updateValueFuture = scheduleReplay(attributeRef, simulatorReplayDatapoints);
        if (updateValueFuture != null) {
            replayMap.put(attributeRef, updateValueFuture);
        } else {
            LOG.warning("Failed to schedule replay update value for simulator replay attribute: " + attribute);
            replayMap.put(attributeRef, null);
        }
    });
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef)

Example 17 with AttributeRef

use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.

the class SNMPProtocol method doStart.

@Override
protected void doStart(Container container) throws Exception {
    String snmpBindHost = agent.getBindHost().orElseThrow(() -> {
        String msg = "No SNMP bind host provided for protocol: " + this;
        LOG.info(msg);
        return new IllegalArgumentException(msg);
    });
    Integer snmpBindPort = agent.getBindPort().orElse(162);
    SNMPAgent.SNMPVersion snmpVersion = agent.getSNMPVersion().orElse(SNMPAgent.SNMPVersion.V2c);
    String snmpUri = String.format("snmp:%s:%d?protocol=udp&type=TRAP&snmpVersion=%d", snmpBindHost, snmpBindPort, snmpVersion.getVersion());
    messageBrokerContext.addRoutes(new RouteBuilder() {

        @Override
        public void configure() {
            from(snmpUri).routeId(getProtocolName() + getAgent().getId()).process(exchange -> {
                SnmpMessage msg = exchange.getIn(SnmpMessage.class);
                LOG.fine(String.format("Message received: %s", msg));
                PDU pdu = msg.getSnmpMessage();
                AttributeRef wildCardAttributeRef;
                if ((wildCardAttributeRef = oidMap.get("*")) != null) {
                    ObjectNode wildCardValue = ValueUtil.createJsonObject();
                    pdu.getVariableBindings().forEach(variableBinding -> {
                        wildCardValue.put(variableBinding.getOid().format(), variableBinding.toValueString());
                    });
                    updateLinkedAttribute(new AttributeState(wildCardAttributeRef, wildCardValue));
                }
                pdu.getVariableBindings().forEach(variableBinding -> {
                    AttributeRef attributeRef = oidMap.get(variableBinding.getOid().format());
                    if (attributeRef != null) {
                        updateLinkedAttribute(new AttributeState(attributeRef, variableBinding.toValueString()));
                    }
                });
            });
        }
    });
    setConnectionStatus(ConnectionStatus.CONNECTED);
}
Also used : AttributeState(org.openremote.model.attribute.AttributeState) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus) AttributeRef(org.openremote.model.attribute.AttributeRef) HashMap(java.util.HashMap) ValueUtil(org.openremote.model.util.ValueUtil) Logger(java.util.logging.Logger) PDU(org.snmp4j.PDU) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AbstractProtocol(org.openremote.agent.protocol.AbstractProtocol) Container(org.openremote.model.Container) PROTOCOL(org.openremote.model.syslog.SyslogCategory.PROTOCOL) SnmpMessage(org.apache.camel.component.snmp.SnmpMessage) RouteBuilder(org.apache.camel.builder.RouteBuilder) Attribute(org.openremote.model.attribute.Attribute) AttributeEvent(org.openremote.model.attribute.AttributeEvent) Map(java.util.Map) SyslogCategory(org.openremote.model.syslog.SyslogCategory) PDU(org.snmp4j.PDU) AttributeState(org.openremote.model.attribute.AttributeState) RouteBuilder(org.apache.camel.builder.RouteBuilder) AttributeRef(org.openremote.model.attribute.AttributeRef) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SnmpMessage(org.apache.camel.component.snmp.SnmpMessage)

Example 18 with AttributeRef

use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.

the class SimulatorService method getSimulatorState.

/**
 * Get info about all attributes linked to this instance (for frontend usage)
 */
protected SimulatorState getSimulatorState(SimulatorProtocol protocolInstance) {
    return withLockReturning(protocolInstance.getProtocolInstanceUri() + "::getSimulatorInfo", () -> {
        LOG.info("Getting simulator info for protocol instance: " + protocolInstance);
        // We need asset names instead of identifiers for user-friendly display
        List<String> linkedAssetIds = protocolInstance.getLinkedAttributes().keySet().stream().map(AttributeRef::getId).distinct().collect(Collectors.toList());
        List<String> assetNames = assetStorageService.findNames(linkedAssetIds.toArray(new String[0]));
        if (assetNames.size() != linkedAssetIds.size()) {
            LOG.warning("Retrieved asset names don't match requested asset IDs");
            return null;
        }
        SimulatorAttributeInfo[] attributeInfos = protocolInstance.getLinkedAttributes().entrySet().stream().map(refAttributeEntry -> {
            String assetName = assetNames.get(linkedAssetIds.indexOf(refAttributeEntry.getKey().getId()));
            return new SimulatorAttributeInfo(assetName, refAttributeEntry.getKey().getId(), refAttributeEntry.getValue(), protocolInstance.getReplayMap().containsKey(refAttributeEntry.getKey()));
        }).toArray(SimulatorAttributeInfo[]::new);
        return new SimulatorState(protocolInstance.getAgent().getId(), attributeInfos);
    });
}
Also used : AssetStorageService(org.openremote.manager.asset.AssetStorageService) MessageBrokerService(org.openremote.container.message.MessageBrokerService) SimulatorState(org.openremote.model.simulator.SimulatorState) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) AuthContext(org.openremote.container.security.AuthContext) Protocol(org.openremote.model.asset.agent.Protocol) CLIENT_EVENT_TOPIC(org.openremote.manager.event.ClientEventService.CLIENT_EVENT_TOPIC) AttributeRef(org.openremote.model.attribute.AttributeRef) SimulatorProtocol(org.openremote.agent.protocol.simulator.SimulatorProtocol) ContainerService(org.openremote.model.ContainerService) RequestSimulatorState(org.openremote.model.simulator.RequestSimulatorState) Constants(org.openremote.model.Constants) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Container(org.openremote.model.Container) GlobalLock.withLockReturning(org.openremote.container.concurrent.GlobalLock.withLockReturning) ClientEventService(org.openremote.manager.event.ClientEventService) List(java.util.List) RouteBuilder(org.apache.camel.builder.RouteBuilder) ClientEventService.getSessionKey(org.openremote.manager.event.ClientEventService.getSessionKey) AgentService(org.openremote.manager.agent.AgentService) SimulatorAttributeInfo(org.openremote.model.simulator.SimulatorAttributeInfo) AttributeRef(org.openremote.model.attribute.AttributeRef) SimulatorAttributeInfo(org.openremote.model.simulator.SimulatorAttributeInfo) SimulatorState(org.openremote.model.simulator.SimulatorState) RequestSimulatorState(org.openremote.model.simulator.RequestSimulatorState)

Example 19 with AttributeRef

use of org.openremote.model.attribute.AttributeRef 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)

Example 20 with AttributeRef

use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.

the class EnergyOptimisationService method get24HAttributeValues.

protected double[] get24HAttributeValues(String assetId, Attribute<Double> attribute, double intervalSize, int intervalCount, Instant optimisationTime) {
    double[] values = new double[intervalCount];
    if (attribute == null) {
        return values;
    }
    AttributeRef ref = new AttributeRef(assetId, attribute.getName());
    if (attribute.hasMeta(MetaItemType.HAS_PREDICTED_DATA_POINTS)) {
        LocalDateTime timestamp = LocalDateTime.ofInstant(optimisationTime, ZoneId.systemDefault());
        ValueDatapoint<?>[] predictedData = assetPredictedDatapointService.getValueDatapoints(ref, DatapointInterval.MINUTE, (int) (intervalSize * 60), timestamp, timestamp.plus(24, HOURS).minus((long) (intervalSize * 60), ChronoUnit.MINUTES));
        if (predictedData.length != values.length) {
            LOG.warning("Returned predicted data point count does not match interval count: Ref=" + ref + ", expected=" + values.length + ", actual=" + predictedData.length);
        } else {
            IntStream.range(0, predictedData.length).forEach(i -> {
                if (predictedData[i].getValue() != null) {
                    values[i] = (double) (Object) predictedData[i].getValue();
                } else {
                    // Average previous and next values to fill in gaps (goes up to 5 back and forward) - this fixes
                    // issues with resolution differences between stored predicted data and optimisation interval
                    Double previous = null;
                    Double next = null;
                    int j = i - 1;
                    while (previous == null && j >= 0) {
                        previous = (Double) predictedData[j].getValue();
                        j--;
                    }
                    j = i + 1;
                    while (next == null && j < predictedData.length) {
                        next = (Double) predictedData[j].getValue();
                        j++;
                    }
                    if (next == null) {
                        next = previous;
                    }
                    if (previous == null) {
                        previous = next;
                    }
                    if (next != null) {
                        values[i] = (previous + next) / 2;
                    }
                }
            });
        }
    }
    values[0] = attribute.getValue().orElse(0d);
    return values;
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef) ValueDatapoint(org.openremote.model.datapoint.ValueDatapoint) ValueDatapoint(org.openremote.model.datapoint.ValueDatapoint)

Aggregations

AttributeRef (org.openremote.model.attribute.AttributeRef)42 Logger (java.util.logging.Logger)9 AttributeEvent (org.openremote.model.attribute.AttributeEvent)8 AttributeState (org.openremote.model.attribute.AttributeState)8 Attribute (org.openremote.model.attribute.Attribute)7 List (java.util.List)6 Level (java.util.logging.Level)6 Container (org.openremote.model.Container)6 SyslogCategory (org.openremote.model.syslog.SyslogCategory)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 ScheduledFuture (java.util.concurrent.ScheduledFuture)5 Consumer (java.util.function.Consumer)5 Asset (org.openremote.model.asset.Asset)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 HashMap (java.util.HashMap)4 TimeUnit (java.util.concurrent.TimeUnit)4 AbstractProtocol (org.openremote.agent.protocol.AbstractProtocol)4 AssetStorageService (org.openremote.manager.asset.AssetStorageService)4 ConnectionStatus (org.openremote.model.asset.agent.ConnectionStatus)4