Skip to content

Commit 98961fe

Browse files
committed
Revert "[systeminfo] Reduce code complexity as well garbage collection (openhab#16838)"
This reverts commit 6e77a49.
1 parent e092627 commit 98961fe

File tree

6 files changed

+132
-86
lines changed

6 files changed

+132
-86
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 ThingTypeUID THING_TYPE_COMPUTER = new ThingTypeUID(BINDING_ID, "computer");
32-
public static final ThingTypeUID THING_TYPE_COMPUTER_IMPL = new ThingTypeUID(BINDING_ID, "computer-impl");
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);
3333

3434
// Thing properties
3535
/**

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

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

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

17-
import java.util.Set;
18-
1917
import org.eclipse.jdt.annotation.NonNullByDefault;
2018
import org.eclipse.jdt.annotation.Nullable;
2119
import org.openhab.binding.systeminfo.internal.handler.SystemInfoHandler;
@@ -43,25 +41,24 @@ public class SystemInfoHandlerFactory extends BaseThingHandlerFactory {
4341
private @NonNullByDefault({}) SystemInfoInterface systeminfo;
4442
private @NonNullByDefault({}) SystemInfoThingTypeProvider thingTypeProvider;
4543

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

5450
@Override
5551
protected @Nullable ThingHandler createHandler(Thing thing) {
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);
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.
6160
}
6261
return new SystemInfoHandler(thing, thingTypeProvider, systeminfo);
63-
} else if (THING_TYPE_COMPUTER_IMPL.equals(thing.getThingTypeUID())) {
64-
return new SystemInfoHandler(thing, thingTypeProvider, systeminfo);
6562
}
6663
return null;
6764
}

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

+56-29
Original file line numberDiff line numberDiff line change
@@ -83,54 +83,82 @@ 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
8687
*/
87-
public void createThingType(ThingTypeUID typeUID) {
88+
public boolean createThingType(ThingTypeUID typeUID) {
8889
logger.trace("Creating thing type {}", typeUID);
89-
updateThingType(typeUID, getChannelGroupDefinitions(typeUID));
90+
return updateThingType(typeUID, getChannelGroupDefinitions(typeUID));
9091
}
9192

9293
/**
9394
* Update `ThingType`with `typeUID`, replacing the channel group definitions with `groupDefs`.
9495
*
9596
* @param typeUID
96-
* @param channelGroupDefinitions
97+
* @param groupDefs
98+
* @return false if `typeUID` or its base type UID `systeminfo:computer` cannot be found in the thingTypeRegistry
9799
*/
98-
public void updateThingType(ThingTypeUID typeUID, List<ChannelGroupDefinition> channelGroupDefinitions) {
100+
public boolean updateThingType(ThingTypeUID typeUID, List<ChannelGroupDefinition> groupDefs) {
99101
ThingType baseType = thingTypeRegistry.getThingType(typeUID);
100102
if (baseType == null) {
101103
baseType = thingTypeRegistry.getThingType(THING_TYPE_COMPUTER);
102104
if (baseType == null) {
103105
logger.warn("Could not find base thing type in registry.");
104-
return;
106+
return false;
105107
}
106108
}
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();
107113

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);
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);
114132

115-
final String representationProperty = baseType.getRepresentationProperty();
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();
116145
if (representationProperty != null) {
117-
builder.withRepresentationProperty(representationProperty);
146+
result = result.withRepresentationProperty(representationProperty);
118147
}
119-
final URI configDescriptionURI = baseType.getConfigDescriptionURI();
148+
URI configDescriptionURI = type.getConfigDescriptionURI();
120149
if (configDescriptionURI != null) {
121-
builder.withConfigDescriptionURI(configDescriptionURI);
150+
result = result.withConfigDescriptionURI(configDescriptionURI);
122151
}
123-
final String category = baseType.getCategory();
152+
String category = type.getCategory();
124153
if (category != null) {
125-
builder.withCategory(category);
154+
result = result.withCategory(category);
126155
}
127-
final String description = baseType.getDescription();
156+
String description = type.getDescription();
128157
if (description != null) {
129-
builder.withDescription(description);
158+
result = result.withDescription(description);
130159
}
131160

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

136164
/**
@@ -196,19 +224,18 @@ public List<ChannelGroupDefinition> getChannelGroupDefinitions(ThingTypeUID type
196224
channelTypeUID != null ? channelTypeUID.getId() : "null");
197225
return null;
198226
}
199-
227+
ThingUID thingUID = thing.getUID();
200228
String index = String.valueOf(i);
201-
ChannelUID channelUID = new ChannelUID(thing.getUID(), channelID + index);
202-
ChannelBuilder builder = ChannelBuilder.create(channelUID).withType(channelTypeUID);
203-
builder.withConfiguration(baseChannel.getConfiguration());
229+
ChannelUID channelUID = new ChannelUID(thingUID, channelID + index);
230+
ChannelBuilder builder = ChannelBuilder.create(channelUID).withType(channelTypeUID)
231+
.withConfiguration(baseChannel.getConfiguration());
204232
builder.withLabel(channelType.getLabel() + " " + index);
205233
builder.withDefaultTags(channelType.getTags());
206-
207-
final String description = channelType.getDescription();
234+
String description = channelType.getDescription();
208235
if (description != null) {
209236
builder.withDescription(description);
210237
}
211-
final String itemType = channelType.getItemType();
238+
String itemType = channelType.getItemType();
212239
if (itemType != null) {
213240
builder.withAcceptedItemType(itemType);
214241
}
@@ -225,7 +252,7 @@ public List<ChannelGroupDefinition> getChannelGroupDefinitions(ThingTypeUID type
225252
*/
226253
public void storeChannelsConfig(Thing thing) {
227254
Map<String, Configuration> channelsConfig = thing.getChannels().stream()
228-
.collect(Collectors.toMap(c -> c.getUID().getId(), Channel::getConfiguration));
255+
.collect(Collectors.toMap(c -> c.getUID().getId(), c -> c.getConfiguration()));
229256
thingChannelsConfig.put(thing.getUID(), channelsConfig);
230257
}
231258

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

+9-12
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_SEPARATOR = "_";
51+
private static final String HOST_NAME_SEPERATOR = "_";
5252

5353
public SystemInfoDiscoveryService() {
5454
super(SUPPORTED_THING_TYPES_UIDS, DISCOVERY_TIME_SECONDS);
@@ -57,31 +57,28 @@ public SystemInfoDiscoveryService() {
5757
@Override
5858
protected void startScan() {
5959
logger.debug("Starting system information discovery !");
60-
6160
String hostname;
61+
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+
}
6770
} catch (UnknownHostException ex) {
6871
hostname = DEFAULT_THING_ID;
6972
logger.info("Hostname can not be resolved. Computer name will be set to the default one: {}",
7073
DEFAULT_THING_ID);
7174
}
7275

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());
76+
ThingUID computer = new ThingUID(THING_TYPE_COMPUTER, hostname);
77+
thingDiscovered(DiscoveryResultBuilder.create(computer).withLabel(DEFAULT_THING_LABEL).build());
8278
}
8379

8480
protected String getHostName() throws UnknownHostException {
85-
return InetAddress.getLocalHost().getHostName();
81+
InetAddress addr = InetAddress.getLocalHost();
82+
return addr.getHostName();
8683
}
8784
}

0 commit comments

Comments
 (0)