Skip to content

Commit

Permalink
Refactor ThingManagerImpl (openhab#3330)
Browse files Browse the repository at this point in the history
* Refactor ThingManagerImpl

This

- moves config description URI to type-base class
- decouples the thing manager from bundle loading
- makes sure that all thing/channel-types and config descriptions are available when the thing is initialized
- enables thing type updates

Signed-off-by: Jan N. Klug <github@klug.nrw>
GitOrigin-RevId: d7fa553
  • Loading branch information
J-N-K authored and splatch committed Jul 12, 2023
1 parent ed5636f commit 3d02426
Show file tree
Hide file tree
Showing 27 changed files with 2,028 additions and 762 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,14 @@ public static Object normalizeType(Object value, @Nullable ConfigDescriptionPara
* @param configuration the configuration to be normalized
* @param configDescriptions the configuration descriptions that should be applied (must not be empty).
* @return the normalized configuration or null if given configuration was null
* @throws IllegalArgumentExcetpion if given config description is null
* @throws IllegalArgumentException if given config description is null
*/
public static @Nullable Map<String, Object> normalizeTypes(@Nullable Map<String, Object> configuration,
public static Map<String, Object> normalizeTypes(Map<String, Object> configuration,
List<ConfigDescription> configDescriptions) {
if (configDescriptions.isEmpty()) {
throw new IllegalArgumentException("Config description must not be empty.");
}

if (configuration == null) {
return null;
}

Map<String, Object> convertedConfiguration = new HashMap<>();

Map<String, ConfigDescriptionParameter> configParams = new HashMap<>();
Expand Down
49 changes: 49 additions & 0 deletions bundles/org.opensmarthouse.core.thing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<artifactId>org.openhab.core.io.console</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.test</artifactId>
Expand All @@ -33,4 +38,48 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.15.2</version>
<configuration>
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<noFileHeader>true</noFileHeader>
<locale>en</locale>
<episode>false</episode>
<extension>true</extension>
<args>
<arg>-Xxew</arg>
<arg>-Xxew:instantiate early</arg>
</args>
<plugins>
<plugin>
<groupId>com.github.jaxb-xew-plugin</groupId>
<artifactId>jaxb-xew-plugin</artifactId>
<version>1.10</version>
</plugin>
</plugins>
</configuration>
<dependencies>
<dependency>
<!-- Required for JDK 17 compatibility, see: /~https://github.com/highsource/maven-jaxb2-plugin/issues/207 -->
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.6</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>generate-jaxb-sources</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
@NonNullByDefault
public enum ThingStatusDetail {
NONE,
NOT_YET_READY,
HANDLER_MISSING_ERROR,
HANDLER_REGISTERING_ERROR,
HANDLER_INITIALIZING_ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -74,6 +76,19 @@ public static ThingBuilder create(ThingTypeUID thingTypeUID, ThingUID thingUID)
return new ThingBuilder(thingTypeUID, thingUID);
}

/**
* Create a new thing {@link ThingBuilder} for a copy of the given thing
*
* @param thing the {@link Thing} to create this builder from
* @return the created {@link ThingBuilder}
*
*/
public static ThingBuilder create(Thing thing) {
return ThingBuilder.create(thing.getThingTypeUID(), thing.getUID()).withBridge(thing.getBridgeUID())
.withChannels(thing.getChannels()).withConfiguration(thing.getConfiguration())
.withLabel(thing.getLabel()).withLocation(thing.getLocation()).withProperties(thing.getProperties());
}

/**
* Build the thing
*
Expand Down Expand Up @@ -200,6 +215,20 @@ public ThingBuilder withBridge(@Nullable ThingUID bridgeUID) {
return this;
}

/**
* Set / replace a single property for this thing
*
* @param key the key / name of the property
* @param value the value of the property
* @return the {@link ThingBuilder} itself
*/
public ThingBuilder withProperty(String key, String value) {
Map<String, String> oldProperties = Objects.requireNonNullElse(this.properties, Map.of());
Map<String, String> newProperties = new HashMap<>(oldProperties);
newProperties.put(key, value);
return withProperties(newProperties);
}

/**
* Set/replace the properties for this thing
*
Expand Down
Loading

0 comments on commit 3d02426

Please sign in to comment.