use of org.openremote.model.attribute.AttributeState in project openremote by openremote.
the class HTTPProtocol method onPollingResponse.
protected void onPollingResponse(HttpClientRequest request, Response response, AttributeRef attributeRef, HTTPAgentLink agentLink) {
int responseCode = response != null ? response.getStatus() : 500;
Object value = null;
if (response != null && response.hasEntity() && response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) {
try {
boolean binaryMode = agent.getMessageConvertBinary().orElse(agentLink.isMessageConvertBinary());
boolean hexMode = agent.getMessageConvertHex().orElse(agentLink.isMessageConvertHex());
if (hexMode || binaryMode) {
byte[] bytes = response.readEntity(byte[].class);
value = hexMode ? ProtocolUtil.bytesToHexString(bytes) : ProtocolUtil.bytesToBinaryString(bytes);
} else {
value = response.readEntity(String.class);
}
} catch (Exception e) {
LOG.log(Level.WARNING, "Error occurred whilst trying to read response body", e);
response.close();
}
} else {
LOG.fine(prefixLogMessage("Request returned an un-successful response code (" + responseCode + "):" + request.requestTarget.getUriBuilder().build().toString()));
return;
}
if (attributeRef != null) {
updateLinkedAttribute(new AttributeState(attributeRef, value));
// Look for any attributes that also want to use this polling response
synchronized (pollingLinkedAttributeMap) {
Set<AttributeRef> linkedRefs = pollingLinkedAttributeMap.get(attributeRef);
if (linkedRefs != null) {
Object finalValue = value;
linkedRefs.forEach(ref -> updateLinkedAttribute(new AttributeState(ref, finalValue)));
}
}
}
}
use of org.openremote.model.attribute.AttributeState 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.AttributeState in project openremote by openremote.
the class ProtocolUtil method createGenericAttributeMessageConsumer.
public static Consumer<String> createGenericAttributeMessageConsumer(String assetId, Attribute<?> attribute, AgentLink<?> agentLink, Supplier<Long> currentMillisSupplier, Consumer<AttributeState> stateConsumer) {
ValueFilter[] matchFilters = agentLink.getMessageMatchFilters().orElse(null);
ValuePredicate matchPredicate = agentLink.getMessageMatchPredicate().orElse(null);
if (matchPredicate == null) {
return null;
}
return message -> {
if (!TextUtil.isNullOrEmpty(message)) {
Object messageFiltered = applyValueFilters(message, matchFilters);
if (messageFiltered != null) {
if (matchPredicate.asPredicate(currentMillisSupplier).test(messageFiltered)) {
Protocol.LOG.finest("Inbound message meets attribute matching meta so writing state to state consumer for attribute: asssetId=" + assetId + ", attribute=" + attribute.getName());
stateConsumer.accept(new AttributeState(assetId, attribute.getName(), message));
}
}
}
};
}
Aggregations