use of org.openremote.model.attribute.Attribute in project openremote by openremote.
the class ControllerProtocol method onPollingResponse.
/**
* Polling request should return three different responses :
* <ul>
* <li>OK (200) : new values are available for at least one of the sensor provided in queryParam</li>
* <li>TIMEOUT (408) : during the last 60 seconds following the start of the request, none of the sensors have new values</li>
* <li>Others : error</li>
* </ul>
* <p>
* In every case, we should start a new polling request directly. Only the 200 response induce an update of every linked attribute having a sensor
* status updated.
*/
private void onPollingResponse(String deviceName, List<String> sensorNameList, Response response) {
if (response != null) {
if (response.getStatusInfo() == Response.Status.OK) {
String responseBodyAsString = response.readEntity(String.class);
LOG.info("### New sensors status received");
LOG.finer("### Polling request body response : " + responseBodyAsString);
ArrayNode statusArray = ValueUtil.convert(responseBodyAsString, ArrayNode.class);
if (statusArray == null) {
LOG.warning("### Polling response is not a JSON array or empty: " + responseBodyAsString);
} else {
statusArray.forEach(status -> {
String name = Optional.ofNullable(status.get("name")).flatMap(ValueUtil::getString).orElse(null);
String value = Optional.ofNullable(status.get("value")).flatMap(ValueUtil::getString).orElse(null);
/**
* For every sensors in the request body, find the linked attributeref and update value by calling {@link #updateAttributeValue}
*/
controller.getSensorsListForDevice(deviceName).stream().filter(entry -> entry.getValue().getSensorName().equals(name)).forEach(e -> this.updateAttributeValue(e.getKey(), value));
});
}
} else if (response.getStatusInfo() == Response.Status.REQUEST_TIMEOUT) {
LOG.info("### Timeout from polling no changes on Controller side given sensors [device=" + deviceName + ", sensors=" + this.formatSensors(sensorNameList) + "]");
} else {
LOG.severe("### Status code received error : " + response.getStatus() + " --> " + response.getStatusInfo().getReasonPhrase());
}
} else {
LOG.severe("### Received null response from polling (due to previous exception)");
}
// No matter status code, we're continuing to poll
this.schedulePollingTask(deviceName);
}
use of org.openremote.model.attribute.Attribute 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.Attribute 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.Attribute in project openremote by openremote.
the class ValueUtil method initialiseAssetAttributes.
public static void initialiseAssetAttributes(Asset<?> asset) throws IllegalStateException {
AssetTypeInfo assetInfo = getAssetInfo(asset.getType()).orElseThrow(() -> new IllegalStateException("Cannot get asset model info for requested asset type: " + asset.getType()));
asset.getAttributes().addOrReplace(Arrays.stream(assetInfo.getAttributeDescriptors()).filter(attributeDescriptor -> !attributeDescriptor.isOptional()).map(Attribute::new).collect(Collectors.toList()));
}
use of org.openremote.model.attribute.Attribute in project openremote by openremote.
the class BluetoothMeshProtocol method doLinkAttribute.
@Override
protected synchronized void doLinkAttribute(String assetId, Attribute<?> attribute, BluetoothMeshAgentLink agentLink) throws RuntimeException {
if (meshNetwork == null) {
return;
}
final AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());
Integer appKeyIndex = getOrThrowAgentLinkProperty(agentLink.getAppKeyIndex(), "Bluetooth Mesh Application Key Index");
String modelName = getOrThrowAgentLinkProperty(agentLink.getModelName(), "Bluetooth Mesh Model Name");
Integer modelId = toModelId(modelName, attributeRef);
if (modelId == null) {
return;
}
String addressAsString = getOrThrowAgentLinkProperty(agentLink.getAddress(), "Bluetooth Mesh Address");
Integer address = toIntegerAddress(addressAsString, attributeRef);
if (address == null) {
return;
}
LOG.info("Linking Bluetooth Mesh attribute: [address: '" + String.format("0x%04X", address) + "', model: '" + modelName + "', appKeyIndex: '" + appKeyIndex + "'] - " + attributeRef);
Class<?> clazz = (attribute == null ? null : attribute.getType().getType());
Consumer<Object> sensorValueConsumer = value -> updateLinkedAttribute(new AttributeState(attributeRef, toAttributeValue(value, clazz)));
sensorValueConsumerMap.put(attributeRef, sensorValueConsumer);
meshNetwork.addMeshModel(address, modelId, appKeyIndex);
meshNetwork.addSensorValueConsumer(address, modelId, sensorValueConsumer);
if (meshNetwork.isConnected()) {
meshNetwork.sendMeshGetCommand(address, modelId);
}
}
Aggregations