|
12 | 12 | */
|
13 | 13 | package org.openhab.core.io.rest.core.internal.discovery;
|
14 | 14 |
|
| 15 | +import static org.openhab.core.config.discovery.inbox.InboxPredicates.forThingUID; |
| 16 | + |
| 17 | +import java.net.URI; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Set; |
15 | 22 | import java.util.stream.Stream;
|
16 | 23 |
|
17 | 24 | import javax.annotation.security.RolesAllowed;
|
|
33 | 40 | import org.eclipse.jdt.annotation.NonNullByDefault;
|
34 | 41 | import org.eclipse.jdt.annotation.Nullable;
|
35 | 42 | import org.openhab.core.auth.Role;
|
| 43 | +import org.openhab.core.config.core.ConfigDescription; |
| 44 | +import org.openhab.core.config.core.ConfigDescriptionParameter; |
| 45 | +import org.openhab.core.config.core.ConfigDescriptionRegistry; |
| 46 | +import org.openhab.core.config.core.ConfigUtil; |
| 47 | +import org.openhab.core.config.core.Configuration; |
36 | 48 | import org.openhab.core.config.discovery.DiscoveryResult;
|
37 | 49 | import org.openhab.core.config.discovery.DiscoveryResultFlag;
|
38 | 50 | import org.openhab.core.config.discovery.dto.DiscoveryResultDTO;
|
|
44 | 56 | import org.openhab.core.io.rest.Stream2JSONInputStream;
|
45 | 57 | import org.openhab.core.thing.Thing;
|
46 | 58 | import org.openhab.core.thing.ThingUID;
|
| 59 | +import org.openhab.core.thing.binding.ThingFactory; |
| 60 | +import org.openhab.core.thing.syntaxgenerator.SyntaxGeneratorsService; |
| 61 | +import org.openhab.core.thing.type.ThingType; |
| 62 | +import org.openhab.core.thing.type.ThingTypeRegistry; |
47 | 63 | import org.osgi.service.component.annotations.Activate;
|
48 | 64 | import org.osgi.service.component.annotations.Component;
|
49 | 65 | import org.osgi.service.component.annotations.Reference;
|
@@ -96,10 +112,18 @@ public class InboxResource implements RESTResource {
|
96 | 112 | public static final String PATH_INBOX = "inbox";
|
97 | 113 |
|
98 | 114 | private final Inbox inbox;
|
| 115 | + private final ThingTypeRegistry thingTypeRegistry; |
| 116 | + private final ConfigDescriptionRegistry configDescRegistry; |
| 117 | + private final SyntaxGeneratorsService syntaxGeneratorsService; |
99 | 118 |
|
100 | 119 | @Activate
|
101 |
| - public InboxResource(final @Reference Inbox inbox) { |
| 120 | + public InboxResource(final @Reference Inbox inbox, final @Reference ThingTypeRegistry thingTypeRegistry, |
| 121 | + final @Reference ConfigDescriptionRegistry configDescRegistry, |
| 122 | + final @Reference SyntaxGeneratorsService syntaxGeneratorsService) { |
102 | 123 | this.inbox = inbox;
|
| 124 | + this.thingTypeRegistry = thingTypeRegistry; |
| 125 | + this.configDescRegistry = configDescRegistry; |
| 126 | + this.syntaxGeneratorsService = syntaxGeneratorsService; |
103 | 127 | }
|
104 | 128 |
|
105 | 129 | @POST
|
@@ -182,4 +206,60 @@ public Response unignore(@PathParam("thingUID") @Parameter(description = "thingU
|
182 | 206 | inbox.setFlag(new ThingUID(thingUID), DiscoveryResultFlag.NEW);
|
183 | 207 | return Response.ok(null, MediaType.TEXT_PLAIN).build();
|
184 | 208 | }
|
| 209 | + |
| 210 | + @GET |
| 211 | + @Path("/{thingUID}/filesyntax") |
| 212 | + @Produces(MediaType.TEXT_PLAIN) |
| 213 | + @Operation(operationId = "generateSyntaxForDiscoveryResult", summary = "Generate file syntax for the thing associated to the discovery result.", responses = { |
| 214 | + @ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = String.class))), |
| 215 | + @ApiResponse(responseCode = "400", description = "Unsupported syntax format."), |
| 216 | + @ApiResponse(responseCode = "404", description = "Discovery result not found in the inbox or thing type not found.") }) |
| 217 | + public Response generateSyntaxForDiscoveryResult( |
| 218 | + @HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @Parameter(description = "language") @Nullable String language, |
| 219 | + @PathParam("thingUID") @Parameter(description = "thingUID") String thingUID, |
| 220 | + @DefaultValue("DSL") @QueryParam("format") @Parameter(description = "syntax format") String format) { |
| 221 | + if (!syntaxGeneratorsService.isGeneratorForThingAvailable(format)) { |
| 222 | + String message = "No syntax available for format " + format + "!"; |
| 223 | + return Response.status(Response.Status.BAD_REQUEST).entity(message).build(); |
| 224 | + } |
| 225 | + |
| 226 | + List<DiscoveryResult> results = inbox.getAll().stream().filter(forThingUID(new ThingUID(thingUID))).toList(); |
| 227 | + if (results.isEmpty()) { |
| 228 | + String message = "Discovery result for thing with UID " + thingUID + " not found in the inbox!"; |
| 229 | + return Response.status(Response.Status.NOT_FOUND).entity(message).build(); |
| 230 | + } |
| 231 | + DiscoveryResult result = results.get(0); |
| 232 | + ThingType thingType = thingTypeRegistry.getThingType(result.getThingTypeUID()); |
| 233 | + if (thingType == null) { |
| 234 | + String message = "Thing type with UID " + result.getThingTypeUID() + " does not exist!"; |
| 235 | + return Response.status(Response.Status.NOT_FOUND).entity(message).build(); |
| 236 | + } |
| 237 | + Configuration config = buildThingConfiguration(thingType, result.getProperties()); |
| 238 | + Thing thing = ThingFactory.createThing(thingType, result.getThingUID(), config, result.getBridgeUID(), |
| 239 | + configDescRegistry); |
| 240 | + |
| 241 | + return Response.ok(syntaxGeneratorsService.generateSyntaxForThings(format, Set.of(thing), false)).build(); |
| 242 | + } |
| 243 | + |
| 244 | + private Configuration buildThingConfiguration(ThingType thingType, Map<String, Object> properties) { |
| 245 | + Map<String, Object> configParams = new HashMap<>(); |
| 246 | + for (ConfigDescriptionParameter param : getConfigDescriptionParameters(thingType)) { |
| 247 | + Object value = properties.get(param.getName()); |
| 248 | + if (value != null) { |
| 249 | + configParams.put(param.getName(), ConfigUtil.normalizeType(value, param)); |
| 250 | + } |
| 251 | + } |
| 252 | + return new Configuration(configParams); |
| 253 | + } |
| 254 | + |
| 255 | + private List<ConfigDescriptionParameter> getConfigDescriptionParameters(ThingType thingType) { |
| 256 | + URI descURI = thingType.getConfigDescriptionURI(); |
| 257 | + if (descURI != null) { |
| 258 | + ConfigDescription desc = configDescRegistry.getConfigDescription(descURI); |
| 259 | + if (desc != null) { |
| 260 | + return desc.getParameters(); |
| 261 | + } |
| 262 | + } |
| 263 | + return List.of(); |
| 264 | + } |
185 | 265 | }
|
0 commit comments