Skip to content

Commit 66217d6

Browse files
committed
Merge branch 'main' into fileformat_transform_things_new
2 parents edfb480 + 8d3c357 commit 66217d6

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/fileformat/FileFormatResource.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,9 @@ public Response createFileFormatForItems(final @Context HttpHeaders httpHeaders,
278278
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE)
279279
.entity("Unsupported media type '" + acceptHeader + "'!").build();
280280
}
281-
Collection<Item> items = null;
281+
List<Item> items;
282282
if (itemNames == null || itemNames.isEmpty()) {
283-
items = itemRegistry.getAll();
283+
items = getAllItemsSorted();
284284
} else {
285285
items = new ArrayList<>();
286286
for (String itemname : itemNames) {
@@ -293,7 +293,7 @@ public Response createFileFormatForItems(final @Context HttpHeaders httpHeaders,
293293
}
294294
}
295295
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
296-
generator.generateFileFormat(outputStream, sortItems(items), getMetadata(items), hideDefaultParameters);
296+
generator.generateFileFormat(outputStream, items, getMetadata(items), hideDefaultParameters);
297297
return Response.ok(new String(outputStream.toByteArray())).build();
298298
}
299299

@@ -319,9 +319,9 @@ public Response createFileFormatForThings(final @Context HttpHeaders httpHeaders
319319
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE)
320320
.entity("Unsupported media type '" + acceptHeader + "'!").build();
321321
}
322-
Collection<Thing> things = null;
322+
List<Thing> things;
323323
if (thingUIDs == null || thingUIDs.isEmpty()) {
324-
things = thingRegistry.getAll();
324+
things = getAllThingsSorted();
325325
} else {
326326
try {
327327
things = getThingsOrDiscoveryResult(thingUIDs);
@@ -330,7 +330,7 @@ public Response createFileFormatForThings(final @Context HttpHeaders httpHeaders
330330
}
331331
}
332332
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
333-
generator.generateFileFormat(outputStream, sortThings(things), true, hideDefaultParameters);
333+
generator.generateFileFormat(outputStream, things, true, hideDefaultParameters);
334334
return Response.ok(new String(outputStream.toByteArray())).build();
335335
}
336336

@@ -455,14 +455,15 @@ private Collection<Metadata> getMetadata(Collection<Item> items) {
455455
}
456456

457457
/*
458-
* Sort the items in such a way:
458+
* Get all items from registry sorted in such a way:
459459
* - group items are before non group items
460460
* - group items are sorted to have as much as possible ancestors before their children
461461
* - items not linked to a channel are before items linked to a channel
462462
* - items linked to a channel are grouped by thing UID
463463
* - items linked to the same thing UID are sorted by item name
464464
*/
465-
private List<Item> sortItems(Collection<Item> items) {
465+
private List<Item> getAllItemsSorted() {
466+
Collection<Item> items = itemRegistry.getAll();
466467
List<Item> groups = items.stream().filter(item -> item instanceof GroupItem).sorted((item1, item2) -> {
467468
return item1.getName().compareTo(item2.getName());
468469
}).toList();
@@ -516,12 +517,13 @@ private void fillGroupTree(List<Item> groups, Item item) {
516517
}
517518

518519
/*
519-
* Sort the things in such a way:
520+
* Get all things from registry sorted in such a way:
520521
* - things are grouped by binding, sorted by natural order of binding name
521522
* - all things of a binding are sorted to follow the tree, that is bridge thing is before its sub-things
522523
* - all things of a binding at a certain tree depth are sorted by thing UID
523524
*/
524-
private List<Thing> sortThings(Collection<Thing> things) {
525+
private List<Thing> getAllThingsSorted() {
526+
Collection<Thing> things = thingRegistry.getAll();
525527
List<Thing> thingTree = new ArrayList<>();
526528
Set<String> bindings = things.stream().map(thing -> thing.getUID().getBindingId()).collect(Collectors.toSet());
527529
for (String binding : bindings.stream().sorted().toList()) {

bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/fileconverter/DslItemFileConverter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public String getFileFormatGenerator() {
8282
@Override
8383
public synchronized void generateFileFormat(OutputStream out, List<Item> items, Collection<Metadata> metadata,
8484
boolean hideDefaultParameters) {
85+
if (items.isEmpty()) {
86+
return;
87+
}
8588
ItemModel model = ItemsFactory.eINSTANCE.createItemModel();
8689
for (Item item : items) {
8790
model.getItems().add(buildModelItem(item, getChannelLinks(metadata, item.getName()),

bundles/org.openhab.core.model.thing/src/org/openhab/core/model/thing/internal/fileconverter/DslThingFileConverter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public String getFileFormatGenerator() {
8282
@Override
8383
public synchronized void generateFileFormat(OutputStream out, List<Thing> things, boolean hideDefaultChannels,
8484
boolean hideDefaultParameters) {
85+
if (things.isEmpty()) {
86+
return;
87+
}
8588
ThingModel model = ThingFactory.eINSTANCE.createThingModel();
8689
Set<Thing> handledThings = new HashSet<>();
8790
for (Thing thing : things) {

0 commit comments

Comments
 (0)