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);
}
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);
}
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);
}
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());
}
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));
}
}
});
}
Aggregations