use of org.openremote.model.ValidationFailure in project openremote by openremote.
the class SimulatorProtocol method doLinkAttribute.
@Override
protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
// Get element type from the attribute meta
Optional<String> elementType = getElementType(attribute);
if (!elementType.isPresent()) {
LOG.warning("Can't configure simulator, missing " + SIMULATOR_ELEMENT + " meta item on: " + attribute);
return;
}
AttributeRef configRef = protocolConfiguration.getReferenceOrThrow();
AttributeRef attributeRef = attribute.getReferenceOrThrow();
SimulatorElement element = createElement(elementType.get(), attribute);
if (element == null) {
LOG.warning("Can't simulate element '" + elementType + "': " + attribute);
return;
}
if (attribute.getValue().isPresent()) {
element.setValue(attribute.getValue().get());
List<ValidationFailure> failures = element.getValidationFailures();
if (!failures.isEmpty()) {
element.clearValue();
LOG.warning("Failed to initialize simulator element, initial value validation failures " + failures + ": " + attribute);
return;
}
}
LOG.info("Putting element '" + element + "' for: " + attribute);
elements.put(attributeRef, element);
attributeInstanceMap.put(attributeRef, configRef);
}
use of org.openremote.model.ValidationFailure in project openremote by openremote.
the class SimulatorProtocol method putValue.
/**
* Call this to simulate a send to actuator.
*/
public boolean putValue(AttributeState attributeState) {
Boolean result = withLockReturning(getProtocolName() + "::putValue", () -> {
AttributeRef attributeRef = attributeState.getAttributeRef();
AttributeRef instanceRef = attributeInstanceMap.get(attributeRef);
if (instanceRef == null) {
LOG.warning("Attribute is not referenced by an instance:" + attributeRef);
return false;
}
Instance instance = instances.get(instanceRef);
if (instance == null) {
LOG.warning("No instance found by name '" + instanceRef + "'");
return false;
}
if (!instance.isEnabled()) {
LOG.fine("Simulator protocol configuration is disabled so cannot process request");
return false;
}
LOG.info("Put simulator value: " + attributeState);
SimulatorElement element = elements.get(attributeRef);
if (element == null) {
LOG.warning("No simulated element for: " + attributeRef);
return false;
}
Optional<Value> oldValue = element.getValue();
element.setValue(attributeState.getValue().orElse(null));
List<ValidationFailure> failures = element.getValidationFailures();
if (!failures.isEmpty()) {
// Reset to old value
oldValue.ifPresent(element::setValue);
LOG.warning("Failed to update simulator element, state validation failures " + failures + ": " + attributeRef);
return false;
}
if (instance.getMode() != Mode.MANUAL) {
updateSensor(attributeRef, instance.getMode() == Mode.WRITE_THROUGH_IMMEDIATE ? 0 : instance.getDelayMilliseconds());
}
return true;
});
return result != null ? result : false;
}
use of org.openremote.model.ValidationFailure in project openremote by openremote.
the class TimerConfiguration method validateTimerConfiguration.
public static boolean validateTimerConfiguration(AssetAttribute attribute, AttributeValidationResult result) {
boolean failure = false;
if (!isTimerConfiguration(attribute)) {
failure = true;
if (result != null) {
result.addAttributeFailure(new ValidationFailure(ValueHolder.ValueFailureReason.VALUE_MISMATCH, PROTOCOL_NAME));
}
}
boolean actionFound = false;
boolean cronFound = false;
if (attribute.getMeta() != null && !attribute.getMeta().isEmpty()) {
for (int i = 0; i < attribute.getMeta().size(); i++) {
MetaItem metaItem = attribute.getMeta().get(i);
if (isMetaNameEqualTo(metaItem, META_TIMER_ACTION)) {
actionFound = true;
if (!getAction(metaItem).isPresent()) {
failure = true;
if (result == null) {
break;
}
result.addMetaFailure(i, new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_VALUE_MISMATCH, "Timer Action"));
}
} else if (isMetaNameEqualTo(metaItem, META_TIMER_CRON_EXPRESSION)) {
cronFound = true;
if (!metaItem.getValueAsString().map(TimerProtocol::createCronExpression).isPresent()) {
failure = true;
if (result == null) {
break;
}
result.addMetaFailure(i, new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_VALUE_MISMATCH, "Timer Cron Expression"));
}
}
}
}
if (!cronFound) {
failure = true;
if (result != null) {
result.addMetaFailure(new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_MISSING, META_TIMER_CRON_EXPRESSION));
}
}
if (!actionFound) {
failure = true;
if (result != null) {
result.addMetaFailure(new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_MISSING, META_TIMER_ACTION));
}
}
return !failure;
}
use of org.openremote.model.ValidationFailure in project openremote by openremote.
the class VelbusConfiguration method validateSerialConfiguration.
public static boolean validateSerialConfiguration(AssetAttribute protocolConfiguration, AttributeValidationResult result) {
boolean failure = false;
if (!isSerialConfiguration(protocolConfiguration)) {
failure = true;
if (result != null) {
result.addAttributeFailure(new ValidationFailure(ValueHolder.ValueFailureReason.VALUE_MISMATCH, VelbusSerialProtocol.PROTOCOL_NAME));
}
}
boolean portFound = false;
if (protocolConfiguration.getMeta() != null && !protocolConfiguration.getMeta().isEmpty()) {
for (int i = 0; i < protocolConfiguration.getMeta().size(); i++) {
MetaItem metaItem = protocolConfiguration.getMeta().get(i);
if (isMetaNameEqualTo(metaItem, VelbusSerialProtocol.META_VELBUS_SERIAL_PORT)) {
portFound = true;
if (isNullOrEmpty(metaItem.getValueAsString().orElse(null))) {
failure = true;
if (result == null) {
break;
}
result.addMetaFailure(i, new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_VALUE_IS_REQUIRED, ValueType.STRING.name()));
}
} else if (isMetaNameEqualTo(metaItem, VelbusSerialProtocol.META_VELBUS_SERIAL_BAUDRATE)) {
int baudrate = metaItem.getValueAsInteger().orElse(0);
if (baudrate <= 0) {
failure = true;
if (result == null) {
break;
}
result.addMetaFailure(i, new ValidationFailure(ValueHolder.ValueFailureReason.VALUE_MISMATCH));
}
}
}
}
if (!portFound) {
failure = true;
if (result != null) {
result.addMetaFailure(new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_MISSING, VelbusSocketProtocol.META_VELBUS_SOCKET_PORT));
}
}
return !failure;
}
use of org.openremote.model.ValidationFailure in project openremote by openremote.
the class VelbusConfiguration method validateSocketConfiguration.
public static boolean validateSocketConfiguration(AssetAttribute attribute, AttributeValidationResult result) {
boolean failure = false;
if (!isSocketConfiguration(attribute)) {
failure = true;
if (result != null) {
result.addAttributeFailure(new ValidationFailure(ValueHolder.ValueFailureReason.VALUE_MISMATCH, VelbusSocketProtocol.PROTOCOL_NAME));
}
}
boolean hostFound = false;
boolean portFound = false;
if (attribute.getMeta() != null && !attribute.getMeta().isEmpty()) {
for (int i = 0; i < attribute.getMeta().size(); i++) {
MetaItem metaItem = attribute.getMeta().get(i);
if (isMetaNameEqualTo(metaItem, VelbusSocketProtocol.META_VELBUS_SOCKET_HOST)) {
hostFound = true;
if (isNullOrEmpty(metaItem.getValueAsString().orElse(null))) {
failure = true;
if (result == null) {
break;
}
result.addMetaFailure(i, new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_VALUE_IS_REQUIRED, ValueType.STRING.name()));
}
} else if (isMetaNameEqualTo(metaItem, VelbusSocketProtocol.META_VELBUS_SOCKET_PORT)) {
portFound = true;
int port = metaItem.getValueAsInteger().orElse(0);
if (port <= 0 || port > 65536) {
failure = true;
if (result == null) {
break;
}
result.addMetaFailure(i, new ValidationFailure(ValueHolder.ValueFailureReason.VALUE_MISMATCH, "1-65536"));
}
}
}
}
if (!hostFound) {
failure = true;
if (result != null) {
result.addMetaFailure(new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_MISSING, VelbusSocketProtocol.META_VELBUS_SOCKET_HOST));
}
}
if (!portFound) {
failure = true;
if (result != null) {
result.addMetaFailure(new ValidationFailure(MetaItem.MetaItemFailureReason.META_ITEM_MISSING, VelbusSocketProtocol.META_VELBUS_SOCKET_PORT));
}
}
return !failure;
}
Aggregations