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