Search in sources :

Example 6 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)

Example 7 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 8 with AttributeState

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

the class StorageSimulatorProtocol method doLinkedAttributeWrite.

@SuppressWarnings("unchecked")
@Override
protected void doLinkedAttributeWrite(Attribute<?> attribute, StorageSimulatorAgentLink agentLink, AttributeEvent event, Object processedValue) {
    // Power attribute is updated only by this protocol not by clients
    if (attribute.getName().equals(POWER.getName())) {
        return;
    }
    updateLinkedAttribute(new AttributeState(event.getAttributeRef(), processedValue));
    // Push write value into the asset and update
    String assetId = event.getAssetId();
    ((Attribute<Object>) attribute).setValue(processedValue);
    ElectricityStorageAsset asset = assetService.findAsset(assetId, ElectricityStorageAsset.class);
    asset.addOrReplaceAttributes(attribute);
    updateStorageAsset(asset);
}
Also used : AttributeState(org.openremote.model.attribute.AttributeState) ElectricityStorageAsset(org.openremote.model.asset.impl.ElectricityStorageAsset) Attribute(org.openremote.model.attribute.Attribute)

Example 9 with AttributeState

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

the class AbstractProtocol method updateLinkedAttribute.

/**
 * Update the value of a linked attribute. Call this to publish new sensor values. This will call
 * {@link ProtocolUtil#doInboundValueProcessing} before sending on the sensor queue.
 */
protected final void updateLinkedAttribute(final AttributeState state, long timestamp) {
    Attribute<?> attribute = linkedAttributes.get(state.getRef());
    if (attribute == null) {
        LOG.severe("Update linked attribute called for un-linked attribute: " + state);
        return;
    }
    Pair<Boolean, Object> ignoreAndConverted = ProtocolUtil.doInboundValueProcessing(state.getRef().getId(), attribute, agent.getAgentLink(attribute), state.getValue().orElse(null));
    if (ignoreAndConverted.key) {
        LOG.fine("Value conversion returned ignore so attribute will not be updated: " + state.getRef());
        return;
    }
    AttributeEvent attributeEvent = new AttributeEvent(new AttributeState(state.getRef(), ignoreAndConverted.value), timestamp);
    LOG.finer("Sending linked attribute update on sensor queue: " + attributeEvent);
    producerTemplate.sendBodyAndHeader(SENSOR_QUEUE, attributeEvent, Protocol.SENSOR_QUEUE_SOURCE_PROTOCOL, getProtocolName());
}
Also used : AttributeState(org.openremote.model.attribute.AttributeState) AttributeEvent(org.openremote.model.attribute.AttributeEvent)

Example 10 with AttributeState

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

the class AbstractProtocol method processLinkedAttributeWrite.

protected final void processLinkedAttributeWrite(AttributeEvent event) {
    LOG.finest("Processing linked attribute write on protocol '" + this + "': " + event);
    withLock(getProtocolName() + "::processLinkedAttributeWrite", () -> {
        Attribute<?> attribute = linkedAttributes.get(event.getAttributeRef());
        if (attribute == null) {
            LOG.warning("Attribute not linked to protocol '" + this + "':" + event);
        } else {
            AgentLink<?> agentLink = agent.getAgentLink(attribute);
            Pair<Boolean, Object> ignoreAndConverted = ProtocolUtil.doOutboundValueProcessing(event.getAssetId(), attribute, agentLink, event.getValue().orElse(null), dynamicAttributes.contains(event.getAttributeRef()));
            if (ignoreAndConverted.key) {
                LOG.fine("Value conversion returned ignore so attribute will not write to protocol: " + event.getAttributeRef());
                return;
            }
            doLinkedAttributeWrite(attribute, agent.getAgentLink(attribute), event, ignoreAndConverted.value);
            if (agent.isUpdateOnWrite().orElse(false) || agentLink.getUpdateOnWrite().orElse(false)) {
                updateLinkedAttribute(new AttributeState(event.getAttributeRef(), ignoreAndConverted.value));
            }
        }
    });
}
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