Skip to content

Commit 4af270e

Browse files
committed
Generate sitemap yaml
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
1 parent 0e1892d commit 4af270e

7 files changed

Lines changed: 702 additions & 2 deletions

File tree

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ public class FileFormatResource implements RESTResource {
189189
}
190190
""";
191191

192+
private static final String YAML_SITEMAPS_EXAMPLE = """
193+
version: 2
194+
sitemaps:
195+
MySitemap:
196+
label: Label
197+
widgets:
198+
MyWidget:
199+
type: Switch
200+
label: Label
201+
item: MyItem
202+
""";
203+
192204
private final Logger logger = LoggerFactory.getLogger(FileFormatResource.class);
193205

194206
private final ItemRegistry itemRegistry;
@@ -343,11 +355,12 @@ public Response createFileFormatForThings(final @Context HttpHeaders httpHeaders
343355
@RolesAllowed({ Role.ADMIN })
344356
@Path("/sitemaps")
345357
@Consumes(MediaType.APPLICATION_JSON)
346-
@Produces({ "text/vnd.openhab.dsl.sitemap" })
358+
@Produces({ "text/vnd.openhab.dsl.sitemap", "application/yaml" })
347359
@Operation(operationId = "createFileFormatForSitemaps", summary = "Create file format for a list of sitemaps in registry.", security = {
348360
@SecurityRequirement(name = "oauth2", scopes = { "admin" }) }, responses = {
349361
@ApiResponse(responseCode = "200", description = "OK", content = {
350-
@Content(mediaType = "text/vnd.openhab.dsl.sitemap", schema = @Schema(example = DSL_SITEMAPS_EXAMPLE)) }),
362+
@Content(mediaType = "text/vnd.openhab.dsl.sitemap", schema = @Schema(example = DSL_SITEMAPS_EXAMPLE)),
363+
@Content(mediaType = "application/yaml", schema = @Schema(example = YAML_SITEMAPS_EXAMPLE)) }),
351364
@ApiResponse(responseCode = "404", description = "One or more sitemaps not found in registry."),
352365
@ApiResponse(responseCode = "415", description = "Unsupported media type.") })
353366
public Response createFileFormatForSitemaps(final @Context HttpHeaders httpHeaders,

bundles/org.openhab.core.model.yaml/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,15 @@
3535
<artifactId>org.openhab.core.thing</artifactId>
3636
<version>${project.version}</version>
3737
</dependency>
38+
<dependency>
39+
<groupId>org.openhab.core.bundles</groupId>
40+
<artifactId>org.openhab.core.model.sitemap</artifactId>
41+
<version>${project.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.openhab.core.bom</groupId>
45+
<artifactId>org.openhab.core.bom.compile-model</artifactId>
46+
<type>pom</type>
47+
</dependency>
3848
</dependencies>
3949
</project>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.model.yaml.internal.sitemaps;
14+
15+
import java.util.List;
16+
import java.util.Objects;
17+
18+
import org.eclipse.jdt.annotation.NonNull;
19+
import org.eclipse.jdt.annotation.Nullable;
20+
import org.openhab.core.model.yaml.YamlElement;
21+
import org.openhab.core.model.yaml.YamlElementName;
22+
23+
/**
24+
* The {@link YamlMappingDTO} is a data transfer object used to serialize a sitemap in a YAML configuration file.
25+
*
26+
* @author Mark Herwege - Initial contribution
27+
*/
28+
@YamlElementName("mapping")
29+
public class YamlMappingDTO implements YamlElement, Cloneable {
30+
31+
public String uid;
32+
public String cmd;
33+
public String releaseCmd;
34+
public String label;
35+
public String icon;
36+
37+
public YamlMappingDTO() {
38+
}
39+
40+
@Override
41+
public @NonNull String getId() {
42+
return uid == null ? "" : uid;
43+
}
44+
45+
@Override
46+
public void setId(@NonNull String id) {
47+
uid = id;
48+
}
49+
50+
@Override
51+
public YamlElement cloneWithoutId() {
52+
YamlMappingDTO copy;
53+
try {
54+
copy = (YamlMappingDTO) super.clone();
55+
copy.uid = null;
56+
return copy;
57+
} catch (CloneNotSupportedException e) {
58+
// Will never happen
59+
return new YamlMappingDTO();
60+
}
61+
}
62+
63+
@Override
64+
public boolean isValid(@Nullable List<@NonNull String> errors, @Nullable List<@NonNull String> warnings) {
65+
return true;
66+
}
67+
68+
@Override
69+
public int hashCode() {
70+
return Objects.hash(uid, cmd, label);
71+
}
72+
73+
@Override
74+
public boolean equals(@Nullable Object obj) {
75+
if (this == obj) {
76+
return true;
77+
} else if (obj == null || getClass() != obj.getClass()) {
78+
return false;
79+
}
80+
YamlMappingDTO other = (YamlMappingDTO) obj;
81+
return Objects.equals(uid, other.uid) && Objects.equals(label, other.label) && Objects.equals(cmd, other.cmd);
82+
}
83+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.model.yaml.internal.sitemaps;
14+
15+
import java.util.List;
16+
import java.util.Objects;
17+
18+
import org.eclipse.jdt.annotation.NonNull;
19+
import org.eclipse.jdt.annotation.Nullable;
20+
import org.openhab.core.model.yaml.YamlElement;
21+
import org.openhab.core.model.yaml.YamlElementName;
22+
23+
/**
24+
* The {@link YamlRuleDTO} is a data transfer object used to serialize a sitemap widget rule in a YAML configuration
25+
* file.
26+
*
27+
* @author Mark Herwege - Initial contribution
28+
*/
29+
@YamlElementName("rule")
30+
public class YamlRuleDTO implements YamlElement, Cloneable {
31+
32+
public String uid;
33+
public List<Condition> conditions;
34+
public String argument;
35+
36+
public YamlRuleDTO() {
37+
}
38+
39+
@Override
40+
public @NonNull String getId() {
41+
return uid == null ? "" : uid;
42+
}
43+
44+
@Override
45+
public void setId(@NonNull String id) {
46+
uid = id;
47+
}
48+
49+
@Override
50+
public YamlElement cloneWithoutId() {
51+
YamlRuleDTO copy;
52+
try {
53+
copy = (YamlRuleDTO) super.clone();
54+
copy.uid = null;
55+
return copy;
56+
} catch (CloneNotSupportedException e) {
57+
// Will never happen
58+
return new YamlRuleDTO();
59+
}
60+
}
61+
62+
@Override
63+
public boolean isValid(@Nullable List<@NonNull String> errors, @Nullable List<@NonNull String> warnings) {
64+
return true;
65+
}
66+
67+
@Override
68+
public int hashCode() {
69+
return Objects.hash(uid, conditions);
70+
}
71+
72+
@Override
73+
public boolean equals(@Nullable Object obj) {
74+
if (this == obj) {
75+
return true;
76+
} else if (obj == null || getClass() != obj.getClass()) {
77+
return false;
78+
}
79+
YamlRuleDTO other = (YamlRuleDTO) obj;
80+
return Objects.equals(uid, other.uid) && Objects.equals(conditions, other.conditions);
81+
}
82+
83+
public class Condition {
84+
public String item;
85+
public String condition;
86+
public String value;
87+
}
88+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.model.yaml.internal.sitemaps;
14+
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.Objects;
18+
19+
import org.eclipse.jdt.annotation.NonNull;
20+
import org.eclipse.jdt.annotation.Nullable;
21+
import org.openhab.core.model.yaml.YamlElement;
22+
import org.openhab.core.model.yaml.YamlElementName;
23+
24+
/**
25+
* The {@link YamlSitemapDTO} is a data transfer object used to serialize a sitemap in a YAML configuration file.
26+
*
27+
* @author Mark Herwege - Initial contribution
28+
*/
29+
@YamlElementName("sitemap")
30+
public class YamlSitemapDTO implements YamlElement, Cloneable {
31+
32+
public String uid;
33+
public String label;
34+
public String icon;
35+
public List<Map.Entry<String, YamlWidgetDTO>> widgets;
36+
37+
public YamlSitemapDTO() {
38+
}
39+
40+
@Override
41+
public @NonNull String getId() {
42+
return uid == null ? "" : uid;
43+
}
44+
45+
@Override
46+
public void setId(@NonNull String id) {
47+
uid = id;
48+
}
49+
50+
@Override
51+
public YamlElement cloneWithoutId() {
52+
YamlSitemapDTO copy;
53+
try {
54+
copy = (YamlSitemapDTO) super.clone();
55+
copy.uid = null;
56+
return copy;
57+
} catch (CloneNotSupportedException e) {
58+
// Will never happen
59+
return new YamlSitemapDTO();
60+
}
61+
}
62+
63+
@Override
64+
public boolean isValid(@Nullable List<@NonNull String> errors, @Nullable List<@NonNull String> warnings) {
65+
return true;
66+
}
67+
68+
@Override
69+
public int hashCode() {
70+
return Objects.hash(uid, label);
71+
}
72+
73+
@Override
74+
public boolean equals(@Nullable Object obj) {
75+
if (this == obj) {
76+
return true;
77+
} else if (obj == null || getClass() != obj.getClass()) {
78+
return false;
79+
}
80+
YamlSitemapDTO other = (YamlSitemapDTO) obj;
81+
return Objects.equals(uid, other.uid);
82+
}
83+
}

0 commit comments

Comments
 (0)