Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Improvements to EnergyOptimisationService force charge handling

Rich Turnerb5f90b5523b0
diff --git a/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java b/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java
index 679ecb598..022fd5d05 100644
--- a/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java
+++ b/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java
@@ -189,24 +189,59 @@ public class EnergyOptimisationService extends RouteBuilder implements Container
&& attributeEvent.<Boolean>getValue().orElse(false)) {
// Look for forced charge asset
if (forceChargeAssetIds.remove(attributeEvent.getAssetId())) {
- LOG.info("Previously force charged asset has now been removed: " + attributeEvent.getAssetId());
+ LOG.info("Previously force charged asset has now been disconnected so clearing force charge flag: " + attributeEvent.getAssetId());
}
return;
}
// Check for request to force charge
- if (attributeName.equals(ElectricityStorageAsset.FORCE_CHARGE.getName()) && attributeEvent.<AttributeExecuteStatus>getValue().orElse(null) == AttributeExecuteStatus.REQUEST_START) {
- Asset<?> storageAsset = assetStorageService.find(attributeEvent.getAssetId());
- if (storageAsset instanceof ElectricityStorageAsset) {
- forceChargeAssetIds.add(attributeEvent.getAssetId());
- double powerImportMax = ((ElectricityStorageAsset) storageAsset).getPowerImportMax().orElse(Double.MAX_VALUE);
+ if (attributeName.equals(ElectricityStorageAsset.FORCE_CHARGE.getName())) {
+ Asset<?> asset = assetStorageService.find(attributeEvent.getAssetId());
+ if (!(asset instanceof ElectricityStorageAsset)) {
+ LOG.fine("Request to force charge asset will be ignored as asset not found or is not of type '" + ElectricityStorageAsset.class.getSimpleName() + "': " + attributeEvent.getAssetId());
+ return;
+ }
+
+ ElectricityStorageAsset storageAsset = (ElectricityStorageAsset) asset;
+
+ if (attributeEvent.<AttributeExecuteStatus>getValue().orElse(null) == AttributeExecuteStatus.REQUEST_START) {
+
+ double powerImportMax = storageAsset.getPowerImportMax().orElse(Double.MAX_VALUE);
+ double maxEnergyLevel = getElectricityStorageAssetEnergyLevelMax(storageAsset);
+ double currentEnergyLevel = storageAsset.getEnergyLevel().orElse(0d);
LOG.info("Request to force charge asset '" + attributeEvent.getAssetId() + "': attempting to set powerSetpoint=" + powerImportMax);
+
+ if (forceChargeAssetIds.contains(attributeEvent.getAssetId())) {
+ LOG.info("Request to force charge asset will be ignored as force charge already requested for asset: " + storageAsset);
+ return;
+ }
+
+ if (currentEnergyLevel >= maxEnergyLevel) {
+ LOG.info("Request to force charge asset will be ignored as asset is already at or above maxEnergyLevel: " + storageAsset);
+ return;
+ }
+
+ forceChargeAssetIds.add(attributeEvent.getAssetId());
assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityAsset.POWER_SETPOINT, powerImportMax));
- assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityStorageAsset.FORCE_CHARGE, AttributeExecuteStatus.COMPLETED));
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityStorageAsset.FORCE_CHARGE, AttributeExecuteStatus.RUNNING));
+
+ } else if (attributeEvent.<AttributeExecuteStatus>getValue().orElse(null) == AttributeExecuteStatus.REQUEST_CANCEL) {
+
+ if (forceChargeAssetIds.remove(attributeEvent.getAssetId())) {
+ LOG.info("Request to cancel force charge asset: " + storageAsset.getId());
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityAsset.POWER_SETPOINT, 0d));
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityStorageAsset.FORCE_CHARGE, AttributeExecuteStatus.CANCELLED));
+ }
}
}
}
+ protected double getElectricityStorageAssetEnergyLevelMax(ElectricityStorageAsset asset) {
+ double energyCapacity = asset.getEnergyCapacity().orElse(0d);
+ int maxEnergyLevelPercentage = asset.getEnergyLevelPercentageMax().orElse(100);
+ return energyCapacity * ((1d*maxEnergyLevelPercentage)/100d);
+ }
+
protected void processOptimisationAssetAttributeEvent(OptimisationInstance optimisationInstance, AttributeEvent attributeEvent) {
if (EnergyOptimisationAsset.FINANCIAL_SAVING.getName().equals(attributeEvent.getAttributeName())
@@ -395,14 +430,14 @@ public class EnergyOptimisationService extends RouteBuilder implements Container
IntStream.range(0, intervalCount).forEach(i -> powerNets[i] += powerLevels[i]);
double currentEnergyLevel = asset.getEnergyLevel().orElse(0d);
- double energyCapacity = asset.getEnergyCapacity().orElse(0d);
- int maxEnergyLevelPercentage = asset.getEnergyLevelPercentageMax().orElse(100);
- double maxEnergyLevel = energyCapacity * ((1d*maxEnergyLevelPercentage)/100d);
+ double maxEnergyLevel = getElectricityStorageAssetEnergyLevelMax(asset);
if (currentEnergyLevel >= maxEnergyLevel) {
LOG.info("Force charged asset has reached maxEnergyLevelPercentage so stopping charging: " + asset.getId());
+ forceChargeAssetIds.remove(asset.getId());
assetProcessingService.sendAttributeEvent(
new AttributeEvent(asset.getId(), ElectricityStorageAsset.POWER_SETPOINT, 0d)
);
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(asset.getId(), ElectricityStorageAsset.FORCE_CHARGE, AttributeExecuteStatus.COMPLETED));
}
return false;
}
diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeExecuteStatus.java b/model/src/main/java/org/openremote/model/attribute/AttributeExecuteStatus.java
index 68d40d3a2..bf78b68a7 100644
--- a/model/src/main/java/org/openremote/model/attribute/AttributeExecuteStatus.java
+++ b/model/src/main/java/org/openremote/model/attribute/AttributeExecuteStatus.java
@@ -51,9 +51,14 @@ public enum AttributeExecuteStatus {
/**
* Execution is currently running
*/
- RUNNING(false);
+ RUNNING(false),
- private boolean write;
+ /**
+ * Execution has been cancelled
+ */
+ CANCELLED(false);
+
+ private final boolean write;
// Prevents cloning of values each time fromString is called
private static final AttributeExecuteStatus[] copyOfValues = values();