Skip to content

Commit 6ebe335

Browse files
authored
[systeminfo] Reduce code complexity as well garbage collection (openhab#16838)
* [systeminfo] Avoid thing type change as well memory re-allocations Signed-off-by: Alexander Falkenstern <[email protected]>
1 parent c2e051e commit 6ebe335

File tree

6 files changed

+86
-132
lines changed

6 files changed

+86
-132
lines changed

bundles/org.openhab.binding.systeminfo/src/main/java/org/openhab/binding/systeminfo/internal/SystemInfoBindingConstants.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public class SystemInfoBindingConstants {
2828

2929
public static final String BINDING_ID = "systeminfo";
3030

31-
public static final String THING_TYPE_COMPUTER_ID = "computer";
32-
public static final ThingTypeUID THING_TYPE_COMPUTER = new ThingTypeUID(BINDING_ID, THING_TYPE_COMPUTER_ID);
31+
public static final ThingTypeUID THING_TYPE_COMPUTER = new ThingTypeUID(BINDING_ID, "computer");
32+
public static final ThingTypeUID THING_TYPE_COMPUTER_IMPL = new ThingTypeUID(BINDING_ID, "computer-impl");
3333

3434
// Thing properties
3535
/**

bundles/org.openhab.binding.systeminfo/src/main/java/org/openhab/binding/systeminfo/internal/SystemInfoHandlerFactory.java

+13-10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import static org.openhab.binding.systeminfo.internal.SystemInfoBindingConstants.*;
1616

17+
import java.util.Set;
18+
1719
import org.eclipse.jdt.annotation.NonNullByDefault;
1820
import org.eclipse.jdt.annotation.Nullable;
1921
import org.openhab.binding.systeminfo.internal.handler.SystemInfoHandler;
@@ -41,24 +43,25 @@ public class SystemInfoHandlerFactory extends BaseThingHandlerFactory {
4143
private @NonNullByDefault({}) SystemInfoInterface systeminfo;
4244
private @NonNullByDefault({}) SystemInfoThingTypeProvider thingTypeProvider;
4345

46+
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_COMPUTER,
47+
THING_TYPE_COMPUTER_IMPL);
48+
4449
@Override
4550
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
46-
return BINDING_ID.equals(thingTypeUID.getBindingId())
47-
&& thingTypeUID.getId().startsWith(THING_TYPE_COMPUTER_ID);
51+
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
4852
}
4953

5054
@Override
5155
protected @Nullable ThingHandler createHandler(Thing thing) {
52-
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
53-
if (supportsThingType(thingTypeUID)) {
54-
String extString = "-" + thing.getUID().getId();
55-
ThingTypeUID extThingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_COMPUTER_ID + extString);
56-
if (thingTypeProvider.getThingType(extThingTypeUID, null) == null) {
57-
thingTypeProvider.createThingType(extThingTypeUID);
58-
thingTypeProvider.storeChannelsConfig(thing); // Save the current channels configs, will be restored
59-
// after thing type change.
56+
if (THING_TYPE_COMPUTER.equals(thing.getThingTypeUID())) {
57+
if (thingTypeProvider.getThingType(THING_TYPE_COMPUTER_IMPL, null) == null) {
58+
thingTypeProvider.createThingType(THING_TYPE_COMPUTER_IMPL);
59+
// Save the current channels configs, will be restored after thing type change.
60+
thingTypeProvider.storeChannelsConfig(thing);
6061
}
6162
return new SystemInfoHandler(thing, thingTypeProvider, systeminfo);
63+
} else if (THING_TYPE_COMPUTER_IMPL.equals(thing.getThingTypeUID())) {
64+
return new SystemInfoHandler(thing, thingTypeProvider, systeminfo);
6265
}
6366
return null;
6467
}

bundles/org.openhab.binding.systeminfo/src/main/java/org/openhab/binding/systeminfo/internal/SystemInfoThingTypeProvider.java

+29-56
Original file line numberDiff line numberDiff line change
@@ -83,82 +83,54 @@ public SystemInfoThingTypeProvider(@Reference ThingTypeRegistry thingTypeRegistr
8383
* Create thing type with the provided typeUID and add it to the thing type registry.
8484
*
8585
* @param typeUID
86-
* @return false if base type UID `systeminfo:computer` cannot be found in the thingTypeRegistry
8786
*/
88-
public boolean createThingType(ThingTypeUID typeUID) {
87+
public void createThingType(ThingTypeUID typeUID) {
8988
logger.trace("Creating thing type {}", typeUID);
90-
return updateThingType(typeUID, getChannelGroupDefinitions(typeUID));
89+
updateThingType(typeUID, getChannelGroupDefinitions(typeUID));
9190
}
9291

9392
/**
9493
* Update `ThingType`with `typeUID`, replacing the channel group definitions with `groupDefs`.
9594
*
9695
* @param typeUID
97-
* @param groupDefs
98-
* @return false if `typeUID` or its base type UID `systeminfo:computer` cannot be found in the thingTypeRegistry
96+
* @param channelGroupDefinitions
9997
*/
100-
public boolean updateThingType(ThingTypeUID typeUID, List<ChannelGroupDefinition> groupDefs) {
98+
public void updateThingType(ThingTypeUID typeUID, List<ChannelGroupDefinition> channelGroupDefinitions) {
10199
ThingType baseType = thingTypeRegistry.getThingType(typeUID);
102100
if (baseType == null) {
103101
baseType = thingTypeRegistry.getThingType(THING_TYPE_COMPUTER);
104102
if (baseType == null) {
105103
logger.warn("Could not find base thing type in registry.");
106-
return false;
104+
return;
107105
}
108106
}
109-
ThingTypeBuilder builder = createThingTypeBuilder(typeUID, baseType.getUID());
110-
if (builder != null) {
111-
logger.trace("Adding channel group definitions to thing type");
112-
ThingType type = builder.withChannelGroupDefinitions(groupDefs).build();
113107

114-
putThingType(type);
115-
return true;
116-
} else {
117-
logger.debug("Error adding channel groups");
118-
return false;
119-
}
120-
}
121-
122-
/**
123-
* Return a {@link ThingTypeBuilder} that can create an exact copy of the `ThingType` with `baseTypeUID`.
124-
* Further build steps can be performed on the returned object before recreating the `ThingType` from the builder.
125-
*
126-
* @param newTypeUID
127-
* @param baseTypeUID
128-
* @return the ThingTypeBuilder, null if `baseTypeUID` cannot be found in the thingTypeRegistry
129-
*/
130-
private @Nullable ThingTypeBuilder createThingTypeBuilder(ThingTypeUID newTypeUID, ThingTypeUID baseTypeUID) {
131-
ThingType type = thingTypeRegistry.getThingType(baseTypeUID);
108+
final ThingTypeBuilder builder = ThingTypeBuilder.instance(THING_TYPE_COMPUTER_IMPL, baseType.getLabel());
109+
builder.withChannelGroupDefinitions(baseType.getChannelGroupDefinitions());
110+
builder.withChannelDefinitions(baseType.getChannelDefinitions());
111+
builder.withExtensibleChannelTypeIds(baseType.getExtensibleChannelTypeIds());
112+
builder.withSupportedBridgeTypeUIDs(baseType.getSupportedBridgeTypeUIDs());
113+
builder.withProperties(baseType.getProperties()).isListed(false);
132114

133-
if (type == null) {
134-
return null;
135-
}
136-
137-
ThingTypeBuilder result = ThingTypeBuilder.instance(newTypeUID, type.getLabel())
138-
.withChannelGroupDefinitions(type.getChannelGroupDefinitions())
139-
.withChannelDefinitions(type.getChannelDefinitions())
140-
.withExtensibleChannelTypeIds(type.getExtensibleChannelTypeIds())
141-
.withSupportedBridgeTypeUIDs(type.getSupportedBridgeTypeUIDs()).withProperties(type.getProperties())
142-
.isListed(false);
143-
144-
String representationProperty = type.getRepresentationProperty();
115+
final String representationProperty = baseType.getRepresentationProperty();
145116
if (representationProperty != null) {
146-
result = result.withRepresentationProperty(representationProperty);
117+
builder.withRepresentationProperty(representationProperty);
147118
}
148-
URI configDescriptionURI = type.getConfigDescriptionURI();
119+
final URI configDescriptionURI = baseType.getConfigDescriptionURI();
149120
if (configDescriptionURI != null) {
150-
result = result.withConfigDescriptionURI(configDescriptionURI);
121+
builder.withConfigDescriptionURI(configDescriptionURI);
151122
}
152-
String category = type.getCategory();
123+
final String category = baseType.getCategory();
153124
if (category != null) {
154-
result = result.withCategory(category);
125+
builder.withCategory(category);
155126
}
156-
String description = type.getDescription();
127+
final String description = baseType.getDescription();
157128
if (description != null) {
158-
result = result.withDescription(description);
129+
builder.withDescription(description);
159130
}
160131

161-
return result;
132+
logger.trace("Adding channel group definitions to thing type");
133+
putThingType(builder.withChannelGroupDefinitions(channelGroupDefinitions).build());
162134
}
163135

164136
/**
@@ -224,18 +196,19 @@ public List<ChannelGroupDefinition> getChannelGroupDefinitions(ThingTypeUID type
224196
channelTypeUID != null ? channelTypeUID.getId() : "null");
225197
return null;
226198
}
227-
ThingUID thingUID = thing.getUID();
199+
228200
String index = String.valueOf(i);
229-
ChannelUID channelUID = new ChannelUID(thingUID, channelID + index);
230-
ChannelBuilder builder = ChannelBuilder.create(channelUID).withType(channelTypeUID)
231-
.withConfiguration(baseChannel.getConfiguration());
201+
ChannelUID channelUID = new ChannelUID(thing.getUID(), channelID + index);
202+
ChannelBuilder builder = ChannelBuilder.create(channelUID).withType(channelTypeUID);
203+
builder.withConfiguration(baseChannel.getConfiguration());
232204
builder.withLabel(channelType.getLabel() + " " + index);
233205
builder.withDefaultTags(channelType.getTags());
234-
String description = channelType.getDescription();
206+
207+
final String description = channelType.getDescription();
235208
if (description != null) {
236209
builder.withDescription(description);
237210
}
238-
String itemType = channelType.getItemType();
211+
final String itemType = channelType.getItemType();
239212
if (itemType != null) {
240213
builder.withAcceptedItemType(itemType);
241214
}
@@ -252,7 +225,7 @@ public List<ChannelGroupDefinition> getChannelGroupDefinitions(ThingTypeUID type
252225
*/
253226
public void storeChannelsConfig(Thing thing) {
254227
Map<String, Configuration> channelsConfig = thing.getChannels().stream()
255-
.collect(Collectors.toMap(c -> c.getUID().getId(), c -> c.getConfiguration()));
228+
.collect(Collectors.toMap(c -> c.getUID().getId(), Channel::getConfiguration));
256229
thingChannelsConfig.put(thing.getUID(), channelsConfig);
257230
}
258231

bundles/org.openhab.binding.systeminfo/src/main/java/org/openhab/binding/systeminfo/internal/discovery/SystemInfoDiscoveryService.java

+12-9
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class SystemInfoDiscoveryService extends AbstractDiscoveryService {
4848

4949
private static final int DISCOVERY_TIME_SECONDS = 30;
5050
private static final String THING_UID_VALID_CHARS = "A-Za-z0-9_-";
51-
private static final String HOST_NAME_SEPERATOR = "_";
51+
private static final String HOST_NAME_SEPARATOR = "_";
5252

5353
public SystemInfoDiscoveryService() {
5454
super(SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIME_SECONDS);
@@ -57,28 +57,31 @@ public SystemInfoDiscoveryService() {
5757
@Override
5858
protected void startScan() {
5959
logger.debug("Starting system information discovery !");
60-
String hostname;
6160

61+
String hostname;
6262
try {
6363
hostname = getHostName();
6464
if (hostname.isEmpty()) {
6565
throw new UnknownHostException();
6666
}
67-
if (!hostname.matches("[" + THING_UID_VALID_CHARS + "]*")) {
68-
hostname = hostname.replaceAll("[^" + THING_UID_VALID_CHARS + "]", HOST_NAME_SEPERATOR);
69-
}
7067
} catch (UnknownHostException ex) {
7168
hostname = DEFAULT_THING_ID;
7269
logger.info("Hostname can not be resolved. Computer name will be set to the default one: {}",
7370
DEFAULT_THING_ID);
7471
}
7572

76-
ThingUID computer = new ThingUID(THING_TYPE_COMPUTER, hostname);
77-
thingDiscovered(DiscoveryResultBuilder.create(computer).withLabel(DEFAULT_THING_LABEL).build());
73+
String thingId = hostname;
74+
if (!thingId.matches("[" + THING_UID_VALID_CHARS + "]*")) {
75+
thingId = thingId.replaceAll("[^" + THING_UID_VALID_CHARS + "]", HOST_NAME_SEPARATOR);
76+
}
77+
78+
final ThingUID computer = new ThingUID(THING_TYPE_COMPUTER, thingId);
79+
final DiscoveryResultBuilder builder = DiscoveryResultBuilder.create(computer);
80+
builder.withLabel(DEFAULT_THING_LABEL);
81+
thingDiscovered(builder.build());
7882
}
7983

8084
protected String getHostName() throws UnknownHostException {
81-
InetAddress addr = InetAddress.getLocalHost();
82-
return addr.getHostName();
85+
return InetAddress.getLocalHost().getHostName();
8386
}
8487
}

0 commit comments

Comments
 (0)