use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.
the class ControllerProtocol method doLinkedAttributeWrite.
/**
* Write action on a linked attribute mean we execute a command on the Controller. It induce a HTTP request and
* manage it's return code. (No value is returned from the execution of a command)
*/
@Override
protected void doLinkedAttributeWrite(Attribute<?> attribute, ControllerAgentLink agentLink, AttributeEvent event, Object processedValue) {
LOG.finer("### Process Linked Attribute Write");
AttributeRef attributeRef = event.getAttributeRef();
ControllerCommand controllerCommand = controller.getCommand(attributeRef);
HTTPProtocol.HttpClientRequest request = RequestBuilder.buildCommandRequest(controllerCommand, event, controllerWebTarget);
String body = null;
if (controllerCommand instanceof ControllerCommandBasic) {
body = event.getValue().map(v -> {
ObjectNode objectValue = ValueUtil.JSON.createObjectNode();
objectValue.putPOJO("parameter", processedValue);
return objectValue.toString();
}).orElse(null);
}
executeAttributeWriteRequest(request, body, this::onAttributeWriteResponse);
}
use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.
the class SerialProtocol method doUnlinkAttribute.
@Override
protected void doUnlinkAttribute(String assetId, Attribute<?> attribute, DefaultAgentLink agentLink) {
AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());
protocolMessageConsumers.removeIf(attRefConsumerPair -> attRefConsumerPair.key.equals(attributeRef));
}
use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.
the class StorageSimulatorProtocol method updateStorageAsset.
protected void updateStorageAsset(ElectricityStorageAsset storageAsset) {
if (storageAsset == null) {
LOG.finest("Storage asset not set so skipping update");
return;
}
String assetId = storageAsset.getId();
Instant previousTimestamp = lastUpdateMap.remove(assetId);
ScheduledFuture<?> scheduledFuture = simulationMap.remove(assetId);
if (scheduledFuture != null) {
scheduledFuture.cancel(true);
}
boolean setpointLinked = getLinkedAttributes().containsKey(new AttributeRef(assetId, POWER_SETPOINT.getName()));
boolean powerLinked = getLinkedAttributes().containsKey(new AttributeRef(assetId, POWER.getName()));
boolean levelLinked = getLinkedAttributes().containsKey(new AttributeRef(assetId, ENERGY_LEVEL.getName()));
if (!setpointLinked || !powerLinked || !levelLinked) {
LOG.fine("Not all required attributes are linked or don't have values (setpoint, power and energy level): " + assetId);
return;
}
Instant now = Instant.now();
double setpoint = storageAsset.getPowerSetpoint().orElse(0d);
double capacity = storageAsset.getEnergyCapacity().orElse(0d);
double power = storageAsset.getPower().orElse(0d);
double level = storageAsset.getEnergyLevel().orElse(0d);
int minPercentage = storageAsset.getEnergyLevelPercentageMin().orElse(0);
int maxPercentage = storageAsset.getEnergyLevelPercentageMax().orElse(100);
capacity = Math.max(0d, capacity);
level = Math.max(0d, Math.min(capacity, level));
double maxLevel = (((double) maxPercentage) / 100d) * capacity;
double minLevel = (((double) minPercentage) / 100d) * capacity;
if (capacity <= 0d) {
LOG.info("Storage asset capacity is 0 so not usable: " + assetId);
level = 0d;
setpoint = 0d;
}
if (capacity > 0 && power != 0d && previousTimestamp != null) {
// Calculate energy delta since last execution
Duration duration = Duration.between(previousTimestamp, now);
long seconds = duration.getSeconds();
if (seconds > 0) {
double deltaHours = seconds / 3600d;
// Export efficiency < 1 means more energy is consumed to produce requested power
double efficiency = power > 0 ? ((double) storageAsset.getEfficiencyImport().orElse(100)) / 100d : (1d / (((double) storageAsset.getEfficiencyExport().orElse(100)) / 100d));
double energyDelta = power * deltaHours * efficiency;
double newLevel = Math.max(0d, Math.min(capacity, level + energyDelta));
energyDelta = newLevel - level;
level = newLevel;
if (energyDelta > 0) {
updateLinkedAttribute(new AttributeState(storageAsset.getId(), ElectricityStorageAsset.ENERGY_IMPORT_TOTAL.getName(), storageAsset.getEnergyImportTotal().orElse(0d) + energyDelta));
} else {
updateLinkedAttribute(new AttributeState(storageAsset.getId(), ElectricityStorageAsset.ENERGY_EXPORT_TOTAL.getName(), storageAsset.getEnergyExportTotal().orElse(0d) - energyDelta));
}
}
}
power = (setpoint < 0 && level <= minLevel) || (setpoint > 0 && level >= maxLevel) ? 0d : setpoint;
if (power > 0d && !storageAsset.isSupportsImport().orElse(false)) {
LOG.fine("Setpoint is requesting power import but asset does not support it: " + storageAsset);
power = 0d;
} else if (power < 0d && !storageAsset.isSupportsExport().orElse(false)) {
LOG.fine("Setpoint is requesting power export but asset does not support it: " + storageAsset);
power = 0d;
}
updateLinkedAttribute(new AttributeState(assetId, POWER.getName(), power));
updateLinkedAttribute(new AttributeState(assetId, ENERGY_LEVEL.getName(), level));
updateLinkedAttribute(new AttributeState(assetId, ENERGY_LEVEL_PERCENTAGE.getName(), capacity <= 0d ? 0 : (int) ((level / capacity) * 100)));
if (power != 0d) {
lastUpdateMap.put(assetId, now);
simulationMap.put(assetId, scheduleUpdate(assetId));
}
}
use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.
the class TCPProtocol method doUnlinkAttribute.
@Override
protected void doUnlinkAttribute(String assetId, Attribute<?> attribute, DefaultAgentLink agentLink) {
AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());
protocolMessageConsumers.removeIf(attRefConsumerPair -> attRefConsumerPair.key.equals(attributeRef));
}
use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.
the class SimulatorProtocol method doUnlinkAttribute.
@Override
protected void doUnlinkAttribute(String assetId, Attribute<?> attribute, SimulatorAgentLink agentLink) {
AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());
ScheduledFuture<?> updateValueFuture = replayMap.remove(attributeRef);
if (updateValueFuture != null) {
updateValueFuture.cancel(true);
}
}
Aggregations