Search in sources :

Example 36 with AttributeRef

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);
}
Also used : HTTPProtocol(org.openremote.agent.protocol.http.HTTPProtocol) AttributeRef(org.openremote.model.attribute.AttributeRef) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ControllerCommandBasic(org.openremote.agent.protocol.controller.command.ControllerCommandBasic)

Example 37 with AttributeRef

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));
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef)

Example 38 with 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));
    }
}
Also used : AttributeState(org.openremote.model.attribute.AttributeState) AttributeRef(org.openremote.model.attribute.AttributeRef) Instant(java.time.Instant) Duration(java.time.Duration)

Example 39 with AttributeRef

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));
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef)

Example 40 with 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);
    }
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef)

Aggregations

AttributeRef (org.openremote.model.attribute.AttributeRef)42 Logger (java.util.logging.Logger)9 AttributeEvent (org.openremote.model.attribute.AttributeEvent)8 AttributeState (org.openremote.model.attribute.AttributeState)8 Attribute (org.openremote.model.attribute.Attribute)7 List (java.util.List)6 Level (java.util.logging.Level)6 Container (org.openremote.model.Container)6 SyslogCategory (org.openremote.model.syslog.SyslogCategory)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 ScheduledFuture (java.util.concurrent.ScheduledFuture)5 Consumer (java.util.function.Consumer)5 Asset (org.openremote.model.asset.Asset)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 HashMap (java.util.HashMap)4 TimeUnit (java.util.concurrent.TimeUnit)4 AbstractProtocol (org.openremote.agent.protocol.AbstractProtocol)4 AssetStorageService (org.openremote.manager.asset.AssetStorageService)4 ConnectionStatus (org.openremote.model.asset.agent.ConnectionStatus)4