Skip to content

Commit

Permalink
Enhance API GET /actions/{thingUID} to return input params as config …
Browse files Browse the repository at this point in the history
…params

Related to #1745

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo committed Sep 28, 2024
1 parent a5c488d commit 7bf450e
Showing 1 changed file with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
import org.openhab.core.automation.type.ModuleTypeRegistry;
import org.openhab.core.automation.type.Output;
import org.openhab.core.automation.util.ModuleBuilder;
import org.openhab.core.config.core.ConfigDescriptionParameter;
import org.openhab.core.config.core.ConfigDescriptionParameterBuilder;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.config.core.dto.ConfigDescriptionDTOMapper;
import org.openhab.core.config.core.dto.ConfigDescriptionParameterDTO;
import org.openhab.core.io.rest.LocaleService;
import org.openhab.core.io.rest.RESTConstants;
import org.openhab.core.io.rest.RESTResource;
Expand All @@ -64,6 +68,8 @@
import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsApplicationSelect;
import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsName;
import org.osgi.service.jaxrs.whiteboard.propertytypes.JaxrsResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -89,6 +95,8 @@
public class ThingActionsResource implements RESTResource {
public static final String PATH_THINGS = "actions";

private final Logger logger = LoggerFactory.getLogger(ThingActionsResource.class);

private final LocaleService localeService;
private final ModuleTypeRegistry moduleTypeRegistry;

Expand Down Expand Up @@ -171,11 +179,24 @@ public Response getActions(@PathParam("thingUID") @Parameter(description = "thin
continue;
}

List<ConfigDescriptionParameter> inputParameters = new ArrayList<>();
for (Input input : actionType.getInputs()) {
ConfigDescriptionParameter parameter = convertToConfigDescriptionParameter(input);
if (parameter != null) {
inputParameters.add(parameter);
} else {
inputParameters = null;
break;
}
}

ThingActionDTO actionDTO = new ThingActionDTO();
actionDTO.actionUid = actionType.getUID();
actionDTO.description = actionType.getDescription();
actionDTO.label = actionType.getLabel();
actionDTO.inputs = actionType.getInputs();
actionDTO.inputsAsConfigParameters = inputParameters == null ? null
: ConfigDescriptionDTOMapper.mapParameters(inputParameters);
actionDTO.outputs = actionType.getOutputs();
actions.add(actionDTO);
}
Expand Down Expand Up @@ -230,6 +251,76 @@ public Response executeThingAction(@PathParam("thingUID") @Parameter(description
}
}

private @Nullable ConfigDescriptionParameter convertToConfigDescriptionParameter(Input input) {
boolean supported = true;
ConfigDescriptionParameter.Type parameterType = ConfigDescriptionParameter.Type.TEXT;
String defaultValue = null;
boolean required = false;
String context = null;
switch (input.getType()) {
case "boolean":
defaultValue = "false";
required = true;
case "java.lang.Boolean":
parameterType = ConfigDescriptionParameter.Type.BOOLEAN;
break;
case "byte":
case "short":
case "int":
case "long":
defaultValue = "0";
required = true;
case "java.lang.Byte":
case "java.lang.Short":
case "java.lang.Integer":
case "java.lang.Long":
parameterType = ConfigDescriptionParameter.Type.INTEGER;
break;
case "float":
case "double":
defaultValue = "0";
required = true;
case "java.lang.Float":
case "java.lang.Double":
parameterType = ConfigDescriptionParameter.Type.DECIMAL;
break;
case "java.lang.String":
parameterType = ConfigDescriptionParameter.Type.TEXT;
break;
case "java.time.LocalDate":
parameterType = ConfigDescriptionParameter.Type.TEXT;
context = "date";
break;
case "java.time.LocalTime":
parameterType = ConfigDescriptionParameter.Type.TEXT;
context = "time";
break;
case "java.time.LocalDateTime":
case "java.time.ZonedDateTime":
parameterType = ConfigDescriptionParameter.Type.TEXT;
context = "datetime";
break;
default:
supported = false;
break;
}
if (!supported) {
logger.warn("Unsupported input parameter '{}' having type {}", input.getName(), input.getType());
return null;
}

ConfigDescriptionParameterBuilder builder = ConfigDescriptionParameterBuilder
.create(input.getName(), parameterType).withLabel(input.getLabel())
.withDescription(input.getDescription()).withReadOnly(false)
.withRequired(required || input.isRequired()).withContext(context);
if (!input.getDefaultValue().isEmpty()) {
builder = builder.withDefault(input.getDefaultValue());
} else if (defaultValue != null) {
builder = builder.withDefault(defaultValue);
}
return builder.build();
}

private @Nullable String getScope(ThingActions actions) {
ThingActionsScope scopeAnnotation = actions.getClass().getAnnotation(ThingActionsScope.class);
if (scopeAnnotation == null) {
Expand All @@ -245,6 +336,9 @@ private static class ThingActionDTO {
public @Nullable String description;

public List<Input> inputs = new ArrayList<>();

public @Nullable List<ConfigDescriptionParameterDTO> inputsAsConfigParameters;

public List<Output> outputs = new ArrayList<>();
}
}

0 comments on commit 7bf450e

Please sign in to comment.