@@ -83,54 +83,82 @@ public SystemInfoThingTypeProvider(@Reference ThingTypeRegistry thingTypeRegistr
83
83
* Create thing type with the provided typeUID and add it to the thing type registry.
84
84
*
85
85
* @param typeUID
86
+ * @return false if base type UID `systeminfo:computer` cannot be found in the thingTypeRegistry
86
87
*/
87
- public void createThingType (ThingTypeUID typeUID ) {
88
+ public boolean createThingType (ThingTypeUID typeUID ) {
88
89
logger .trace ("Creating thing type {}" , typeUID );
89
- updateThingType (typeUID , getChannelGroupDefinitions (typeUID ));
90
+ return updateThingType (typeUID , getChannelGroupDefinitions (typeUID ));
90
91
}
91
92
92
93
/**
93
94
* Update `ThingType`with `typeUID`, replacing the channel group definitions with `groupDefs`.
94
95
*
95
96
* @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
97
99
*/
98
- public void updateThingType (ThingTypeUID typeUID , List <ChannelGroupDefinition > channelGroupDefinitions ) {
100
+ public boolean updateThingType (ThingTypeUID typeUID , List <ChannelGroupDefinition > groupDefs ) {
99
101
ThingType baseType = thingTypeRegistry .getThingType (typeUID );
100
102
if (baseType == null ) {
101
103
baseType = thingTypeRegistry .getThingType (THING_TYPE_COMPUTER );
102
104
if (baseType == null ) {
103
105
logger .warn ("Could not find base thing type in registry." );
104
- return ;
106
+ return false ;
105
107
}
106
108
}
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 ();
107
113
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 );
114
132
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 ();
116
145
if (representationProperty != null ) {
117
- builder .withRepresentationProperty (representationProperty );
146
+ result = result .withRepresentationProperty (representationProperty );
118
147
}
119
- final URI configDescriptionURI = baseType .getConfigDescriptionURI ();
148
+ URI configDescriptionURI = type .getConfigDescriptionURI ();
120
149
if (configDescriptionURI != null ) {
121
- builder .withConfigDescriptionURI (configDescriptionURI );
150
+ result = result .withConfigDescriptionURI (configDescriptionURI );
122
151
}
123
- final String category = baseType .getCategory ();
152
+ String category = type .getCategory ();
124
153
if (category != null ) {
125
- builder .withCategory (category );
154
+ result = result .withCategory (category );
126
155
}
127
- final String description = baseType .getDescription ();
156
+ String description = type .getDescription ();
128
157
if (description != null ) {
129
- builder .withDescription (description );
158
+ result = result .withDescription (description );
130
159
}
131
160
132
- logger .trace ("Adding channel group definitions to thing type" );
133
- putThingType (builder .withChannelGroupDefinitions (channelGroupDefinitions ).build ());
161
+ return result ;
134
162
}
135
163
136
164
/**
@@ -196,19 +224,18 @@ public List<ChannelGroupDefinition> getChannelGroupDefinitions(ThingTypeUID type
196
224
channelTypeUID != null ? channelTypeUID .getId () : "null" );
197
225
return null ;
198
226
}
199
-
227
+ ThingUID thingUID = thing . getUID ();
200
228
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 ());
204
232
builder .withLabel (channelType .getLabel () + " " + index );
205
233
builder .withDefaultTags (channelType .getTags ());
206
-
207
- final String description = channelType .getDescription ();
234
+ String description = channelType .getDescription ();
208
235
if (description != null ) {
209
236
builder .withDescription (description );
210
237
}
211
- final String itemType = channelType .getItemType ();
238
+ String itemType = channelType .getItemType ();
212
239
if (itemType != null ) {
213
240
builder .withAcceptedItemType (itemType );
214
241
}
@@ -225,7 +252,7 @@ public List<ChannelGroupDefinition> getChannelGroupDefinitions(ThingTypeUID type
225
252
*/
226
253
public void storeChannelsConfig (Thing thing ) {
227
254
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 () ));
229
256
thingChannelsConfig .put (thing .getUID (), channelsConfig );
230
257
}
231
258
0 commit comments