Search in sources :

Example 1 with AttributeState

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

the class ControllerProtocol method updateAttributeValue.

/**
 * Update linked attribute with new value. We should take care of attribute type and format
 */
private void updateAttributeValue(AttributeRef attributeRef, String value) {
    LOG.finest("### Updating attribute " + attributeRef + " with value " + value);
    ValueDescriptor<?> valueType = this.linkedAttributes.get(attributeRef).getType();
    Object valueObj = ValueUtil.convert(value, valueType.getType());
    this.updateLinkedAttribute(new AttributeState(attributeRef, valueObj));
}
Also used : AttributeState(org.openremote.model.attribute.AttributeState)

Example 2 with AttributeState

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

the class MQTTProtocol method doLinkAttribute.

@Override
protected void doLinkAttribute(String assetId, Attribute<?> attribute, MQTTAgentLink agentLink) throws RuntimeException {
    agentLink.getSubscriptionTopic().ifPresent(topic -> {
        Consumer<MQTTMessage<String>> messageConsumer = msg -> updateLinkedAttribute(new AttributeState(assetId, attribute.getName(), msg.payload));
        client.addMessageConsumer(topic, messageConsumer);
        protocolMessageConsumers.put(new AttributeRef(assetId, attribute.getName()), messageConsumer);
    });
}
Also used : AttributeState(org.openremote.model.attribute.AttributeState) URIBuilder(org.apache.http.client.utils.URIBuilder) AttributeRef(org.openremote.model.attribute.AttributeRef) HashMap(java.util.HashMap) ValueUtil(org.openremote.model.util.ValueUtil) Logger(java.util.logging.Logger) PROTOCOL(org.openremote.model.syslog.SyslogCategory.PROTOCOL) Consumer(java.util.function.Consumer) UniqueIdentifierGenerator(org.openremote.container.util.UniqueIdentifierGenerator) Attribute(org.openremote.model.attribute.Attribute) AttributeEvent(org.openremote.model.attribute.AttributeEvent) Map(java.util.Map) Optional(java.util.Optional) SyslogCategory(org.openremote.model.syslog.SyslogCategory) URI(java.net.URI) AttributeState(org.openremote.model.attribute.AttributeState) AttributeRef(org.openremote.model.attribute.AttributeRef)

Example 3 with AttributeState

use of org.openremote.model.attribute.AttributeState 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 4 with AttributeState

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

the class SimulatorProtocol method scheduleReplay.

protected ScheduledFuture<?> scheduleReplay(AttributeRef attributeRef, SimulatorReplayDatapoint[] simulatorReplayDatapoints) {
    LOG.finer("Scheduling linked attribute replay update");
    long now = LocalDateTime.now().get(ChronoField.SECOND_OF_DAY);
    SimulatorReplayDatapoint nextDatapoint = Arrays.stream(simulatorReplayDatapoints).filter(replaySimulatorDatapoint -> replaySimulatorDatapoint.timestamp > now).findFirst().orElse(simulatorReplayDatapoints[0]);
    if (nextDatapoint == null) {
        LOG.info("Next datapoint not found so replay cancelled: " + attributeRef);
        return null;
    }
    long nextRun = nextDatapoint.timestamp;
    if (nextRun <= now) {
        // now is after so nextRun is next day
        // day in seconds
        nextRun += 86400;
    }
    long nextRunRelative = nextRun - now;
    LOG.info("Next update for asset " + attributeRef.getId() + " for attribute " + attributeRef.getName() + " in " + nextRunRelative + " second(s)");
    return executorService.schedule(() -> {
        withLock(getProtocolName() + "::firingNextUpdate", () -> {
            LOG.info("Updating asset " + attributeRef.getId() + " for attribute " + attributeRef.getName() + " with value " + nextDatapoint.value.toString());
            try {
                updateLinkedAttribute(new AttributeState(attributeRef, nextDatapoint.value));
            } catch (Exception e) {
                LOG.log(Level.SEVERE, "Exception thrown when updating value: %s", e);
            } finally {
                replayMap.put(attributeRef, scheduleReplay(attributeRef, simulatorReplayDatapoints));
            }
        });
    }, nextRunRelative, TimeUnit.SECONDS);
}
Also used : AttributeState(org.openremote.model.attribute.AttributeState) SimulatorReplayDatapoint(org.openremote.model.simulator.SimulatorReplayDatapoint)

Example 5 with AttributeState

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

the class SimulatorProtocol method updateSensor.

/**
 * Call this to simulate a sensor update using the specified timestamp
 */
public void updateSensor(AttributeRef attributeRef, Object value, long timestamp) {
    Attribute<?> attribute = getLinkedAttributes().get(attributeRef);
    AttributeState state = new AttributeState(attributeRef, value);
    if (attribute == null) {
        LOG.info("Attempt to update unlinked attribute: " + state);
        return;
    }
    updateLinkedAttribute(state, timestamp);
}
Also used : AttributeState(org.openremote.model.attribute.AttributeState)

Aggregations

AttributeState (org.openremote.model.attribute.AttributeState)13 Attribute (org.openremote.model.attribute.Attribute)6 AttributeRef (org.openremote.model.attribute.AttributeRef)6 AttributeEvent (org.openremote.model.attribute.AttributeEvent)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Consumer (java.util.function.Consumer)4 Logger (java.util.logging.Logger)4 SyslogCategory (org.openremote.model.syslog.SyslogCategory)4 AbstractProtocol (org.openremote.agent.protocol.AbstractProtocol)3 Container (org.openremote.model.Container)3 ConnectionStatus (org.openremote.model.asset.agent.ConnectionStatus)3 ValueUtil (org.openremote.model.util.ValueUtil)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 List (java.util.List)2 Optional (java.util.Optional)2 Future (java.util.concurrent.Future)2 Level (java.util.logging.Level)2 AssetTreeNode (org.openremote.model.asset.AssetTreeNode)2 AgentLink.getOrThrowAgentLinkProperty (org.openremote.model.asset.agent.AgentLink.getOrThrowAgentLinkProperty)2