Skip to content

Commit 73057db

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 73057db

File tree

24 files changed

+1428
-282
lines changed

24 files changed

+1428
-282
lines changed
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,126 @@
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+
34+
/**
35+
* The {@link FileFormatItemDTOMapper} is a utility class to map items into file format item data transfer objects
36+
* (DTOs).
37+
*
38+
* @author Laurent Garnier - Initial contribution
39+
*/
40+
@NonNullByDefault
41+
public class FileFormatItemDTOMapper {
42+
43+
/**
44+
* Maps item into file format item DTO object.
45+
*
46+
* @param item the item
47+
* @param metadata some metadata (including channel links)
48+
* @return file format item DTO object
49+
*/
50+
public static FileFormatItemDTO map(Item item, List<Metadata> metadata) {
51+
ItemDTO itemDto = ItemDTOMapper.map(item);
52+
FileFormatItemDTO dto = new FileFormatItemDTO(itemDto, itemDto instanceof GroupItemDTO);
53+
54+
List<FileFormatChannelLinkDTO> hannelLinks = new ArrayList<>();
55+
Map<String, MetadataDTO> metadataDTO = new LinkedHashMap<>();
56+
metadata.forEach(md -> {
57+
if (item.getName().equals(md.getUID().getItemName())) {
58+
if ("channel".equals(md.getUID().getNamespace())) {
59+
hannelLinks.add(new FileFormatChannelLinkDTO(md.getValue(),
60+
md.getConfiguration().isEmpty() ? null : md.getConfiguration()));
61+
} else {
62+
MetadataDTO mdDTO = new MetadataDTO();
63+
mdDTO.value = md.getValue();
64+
mdDTO.config = md.getConfiguration().isEmpty() ? null : md.getConfiguration();
65+
metadataDTO.put(md.getUID().getNamespace(), mdDTO);
66+
}
67+
}
68+
});
69+
if (!hannelLinks.isEmpty()) {
70+
dto.channelLinks = hannelLinks;
71+
}
72+
if (!metadataDTO.isEmpty()) {
73+
dto.metadata = metadataDTO;
74+
}
75+
76+
return dto;
77+
}
78+
79+
/**
80+
* Maps file format item DTO object into item.
81+
*
82+
* @param dto the file format item DTO object
83+
* @param itemBuilderFactory the item builder factory
84+
* @return item
85+
*/
86+
public static @Nullable Item map(FileFormatItemDTO dto, ItemBuilderFactory itemBuilderFactory) {
87+
if (GroupItem.TYPE.equals(dto.type)) {
88+
GroupItemDTO groupDto = new GroupItemDTO();
89+
groupDto.type = dto.type;
90+
groupDto.name = dto.name;
91+
groupDto.label = dto.label;
92+
groupDto.category = dto.category;
93+
groupDto.tags = dto.tags;
94+
groupDto.groupNames = dto.groupNames;
95+
groupDto.groupType = dto.groupType;
96+
groupDto.function = dto.function;
97+
return ItemDTOMapper.map(groupDto, itemBuilderFactory);
98+
}
99+
return ItemDTOMapper.map(dto, itemBuilderFactory);
100+
}
101+
102+
/**
103+
* Maps file format item DTO object into a collection of metadata including channels links
104+
* provided through the "channel" namespace.
105+
*
106+
* @param dto the file format item DTO object
107+
* @return the collection of metadata
108+
*/
109+
public static Collection<Metadata> mapMetadata(FileFormatItemDTO dto) {
110+
String name = dto.name;
111+
Collection<Metadata> metadata = new ArrayList<>();
112+
if (dto.channelLinks != null) {
113+
for (FileFormatChannelLinkDTO link : dto.channelLinks) {
114+
MetadataKey key = new MetadataKey("channel", name);
115+
metadata.add(new Metadata(key, link.channelUID, link.configuration));
116+
}
117+
}
118+
if (dto.metadata != null) {
119+
for (Map.Entry<String, MetadataDTO> md : dto.metadata.entrySet()) {
120+
MetadataKey key = new MetadataKey(md.getKey(), name);
121+
metadata.add(new Metadata(key, Objects.requireNonNull(md.getValue().value), md.getValue().config));
122+
}
123+
}
124+
return metadata;
125+
}
126+
}
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+
}

0 commit comments

Comments
 (0)