Skip to content

Commit 4591c4f

Browse files
committed
[REST] New API for conversion between file format and JSON
Related to #4585 This PR supports conversion of things and items. It supports DSL and YAML file formats. A first new API (POST /file-format/create) allows to create a file format (DSL or YAML) from a JSON object. A second new API (POST /file-format/parse) allows to parse a file format (DSL or YAML) to a JSON object. These 2 APIs should help Main UI displaying DSL and YAML file formats for things. Signed-off-by: Laurent Garnier <[email protected]>
1 parent 803dac8 commit 4591c4f

File tree

27 files changed

+1688
-326
lines changed

27 files changed

+1688
-326
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2010-2025 Contributors to the openHAB project
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Eclipse Public License 2.0 which is available at
9+
* http://www.eclipse.org/legal/epl-2.0
10+
*
11+
* SPDX-License-Identifier: EPL-2.0
12+
*/
13+
package org.openhab.core.io.rest.core.fileformat;
14+
15+
import java.util.List;
16+
17+
/**
18+
* This is a data transfer object to serialize the different components that can be contained
19+
* in a file format (items, things, ...) including an optional list of warnings.
20+
*
21+
* @author Laurent Garnier - Initial contribution
22+
*/
23+
public class ExtendedFileFormatDTO extends FileFormatDTO {
24+
25+
public List<String> warnings;
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2010-2025 Contributors to the openHAB project
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Eclipse Public License 2.0 which is available at
9+
* http://www.eclipse.org/legal/epl-2.0
10+
*
11+
* SPDX-License-Identifier: EPL-2.0
12+
*/
13+
package org.openhab.core.io.rest.core.fileformat;
14+
15+
import java.util.Map;
16+
17+
/**
18+
* This is a data transfer object to serialize a channel link for an item contained in a file format.
19+
*
20+
* @author Laurent Garnier - Initial contribution
21+
*/
22+
public class FileFormatChannelLinkDTO {
23+
24+
public String channelUID;
25+
public Map<String, Object> configuration;
26+
27+
public FileFormatChannelLinkDTO(String channelUID, Map<String, Object> configuration) {
28+
this.channelUID = channelUID;
29+
this.configuration = configuration;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2010-2025 Contributors to the openHAB project
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Eclipse Public License 2.0 which is available at
9+
* http://www.eclipse.org/legal/epl-2.0
10+
*
11+
* SPDX-License-Identifier: EPL-2.0
12+
*/
13+
package org.openhab.core.io.rest.core.fileformat;
14+
15+
import java.util.List;
16+
17+
import org.openhab.core.thing.dto.ThingDTO;
18+
19+
/**
20+
* This is a data transfer object to serialize the different components that can be contained
21+
* in a file format (items, things, ...).
22+
*
23+
* @author Laurent Garnier - Initial contribution
24+
*/
25+
public class FileFormatDTO {
26+
27+
public List<FileFormatItemDTO> items;
28+
public List<ThingDTO> things;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2010-2025 Contributors to the openHAB project
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Eclipse Public License 2.0 which is available at
9+
* http://www.eclipse.org/legal/epl-2.0
10+
*
11+
* SPDX-License-Identifier: EPL-2.0
12+
*/
13+
package org.openhab.core.io.rest.core.fileformat;
14+
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
import org.openhab.core.items.dto.GroupFunctionDTO;
19+
import org.openhab.core.items.dto.GroupItemDTO;
20+
import org.openhab.core.items.dto.ItemDTO;
21+
import org.openhab.core.items.dto.MetadataDTO;
22+
23+
/**
24+
* This is a data transfer object to serialize an item contained in a file format.
25+
*
26+
* @author Laurent Garnier - Initial contribution
27+
*/
28+
public class FileFormatItemDTO extends ItemDTO {
29+
30+
public String groupType;
31+
public GroupFunctionDTO function;
32+
public Map<String, MetadataDTO> metadata;
33+
public List<FileFormatChannelLinkDTO> channelLinks;
34+
35+
public FileFormatItemDTO(ItemDTO itemDTO, boolean isGroup) {
36+
this.type = itemDTO.type;
37+
this.name = itemDTO.name;
38+
this.label = itemDTO.label;
39+
this.category = itemDTO.category;
40+
this.tags = itemDTO.tags;
41+
this.groupNames = itemDTO.groupNames;
42+
if (isGroup) {
43+
this.groupType = ((GroupItemDTO) itemDTO).groupType;
44+
this.function = ((GroupItemDTO) itemDTO).function;
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright (c) 2010-2025 Contributors to the openHAB project
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Eclipse Public License 2.0 which is available at
9+
* http://www.eclipse.org/legal/epl-2.0
10+
*
11+
* SPDX-License-Identifier: EPL-2.0
12+
*/
13+
package org.openhab.core.io.rest.core.fileformat;
14+
15+
import java.util.ArrayList;
16+
import java.util.Collection;
17+
import java.util.LinkedHashMap;
18+
import java.util.List;
19+
import java.util.Map;
20+
import java.util.Objects;
21+
22+
import org.eclipse.jdt.annotation.NonNullByDefault;
23+
import org.eclipse.jdt.annotation.Nullable;
24+
import org.openhab.core.items.GroupItem;
25+
import org.openhab.core.items.Item;
26+
import org.openhab.core.items.ItemBuilderFactory;
27+
import org.openhab.core.items.Metadata;
28+
import org.openhab.core.items.MetadataKey;
29+
import org.openhab.core.items.dto.GroupItemDTO;
30+
import org.openhab.core.items.dto.ItemDTO;
31+
import org.openhab.core.items.dto.ItemDTOMapper;
32+
import org.openhab.core.items.dto.MetadataDTO;
33+
import org.openhab.core.thing.link.ItemChannelLink;
34+
35+
/**
36+
* The {@link FileFormatItemDTOMapper} is a utility class to map items into file format item data transfer objects
37+
* (DTOs).
38+
*
39+
* @author Laurent Garnier - Initial contribution
40+
*/
41+
@NonNullByDefault
42+
public class FileFormatItemDTOMapper {
43+
44+
/**
45+
* Maps item into file format item DTO object.
46+
*
47+
* @param item the item
48+
* @param metadata some metadata
49+
* @param channelLinks some items channel links
50+
* @return file format item DTO object
51+
*/
52+
public static FileFormatItemDTO map(Item item, Collection<Metadata> metadata,
53+
Collection<ItemChannelLink> channelLinks) {
54+
ItemDTO itemDto = ItemDTOMapper.map(item);
55+
FileFormatItemDTO dto = new FileFormatItemDTO(itemDto, itemDto instanceof GroupItemDTO);
56+
57+
Map<String, MetadataDTO> metadataDTO = new LinkedHashMap<>();
58+
metadata.forEach(md -> {
59+
if (item.getName().equals(md.getUID().getItemName())) {
60+
MetadataDTO mdDTO = new MetadataDTO();
61+
mdDTO.value = md.getValue();
62+
mdDTO.config = md.getConfiguration().isEmpty() ? null : md.getConfiguration();
63+
metadataDTO.put(md.getUID().getNamespace(), mdDTO);
64+
}
65+
});
66+
if (!metadataDTO.isEmpty()) {
67+
dto.metadata = metadataDTO;
68+
}
69+
70+
List<FileFormatChannelLinkDTO> hannelLinksDTO = new ArrayList<>();
71+
channelLinks.forEach(link -> {
72+
if (item.getName().equals(link.getItemName())) {
73+
hannelLinksDTO.add(new FileFormatChannelLinkDTO(link.getLinkedUID().getAsString(),
74+
link.getConfiguration().getProperties().isEmpty() ? null
75+
: link.getConfiguration().getProperties()));
76+
}
77+
});
78+
if (!hannelLinksDTO.isEmpty()) {
79+
dto.channelLinks = hannelLinksDTO;
80+
}
81+
82+
return dto;
83+
}
84+
85+
/**
86+
* Maps file format item DTO object into item.
87+
*
88+
* @param dto the file format item DTO object
89+
* @param itemBuilderFactory the item builder factory
90+
* @return item
91+
*/
92+
public static @Nullable Item map(FileFormatItemDTO dto, ItemBuilderFactory itemBuilderFactory) {
93+
if (GroupItem.TYPE.equals(dto.type)) {
94+
GroupItemDTO groupDto = new GroupItemDTO();
95+
groupDto.type = dto.type;
96+
groupDto.name = dto.name;
97+
groupDto.label = dto.label;
98+
groupDto.category = dto.category;
99+
groupDto.tags = dto.tags;
100+
groupDto.groupNames = dto.groupNames;
101+
groupDto.groupType = dto.groupType;
102+
groupDto.function = dto.function;
103+
return ItemDTOMapper.map(groupDto, itemBuilderFactory);
104+
}
105+
return ItemDTOMapper.map(dto, itemBuilderFactory);
106+
}
107+
108+
/**
109+
* Maps file format item DTO object into a collection of metadata including channels links
110+
* provided through the "channel" namespace.
111+
*
112+
* @param dto the file format item DTO object
113+
* @return the collection of metadata
114+
*/
115+
public static Collection<Metadata> mapMetadata(FileFormatItemDTO dto) {
116+
String name = dto.name;
117+
Collection<Metadata> metadata = new ArrayList<>();
118+
if (dto.channelLinks != null) {
119+
for (FileFormatChannelLinkDTO link : dto.channelLinks) {
120+
MetadataKey key = new MetadataKey("channel", name);
121+
metadata.add(new Metadata(key, link.channelUID, link.configuration));
122+
}
123+
}
124+
if (dto.metadata != null) {
125+
for (Map.Entry<String, MetadataDTO> md : dto.metadata.entrySet()) {
126+
MetadataKey key = new MetadataKey(md.getKey(), name);
127+
metadata.add(new Metadata(key, Objects.requireNonNull(md.getValue().value), md.getValue().config));
128+
}
129+
}
130+
return metadata;
131+
}
132+
}

0 commit comments

Comments
 (0)