]> git.basschouten.com Git - openhab-addons.git/blob
d8752c19a0d12e3f5250a7ba75c22c0f9e4f7219
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.voice.actiontemplatehli.internal.configuration;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.items.Metadata;
22
23 import com.fasterxml.jackson.annotation.JsonProperty;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26
27 /**
28  * The {@link ActionTemplateConfiguration} class represents each configured action
29  *
30  * @author Miguel Álvarez - Initial contribution
31  */
32 @NonNullByDefault
33 public class ActionTemplateConfiguration {
34     @JsonProperty("type")
35     public String type = "tokens";
36     @JsonProperty("read")
37     public boolean read = false;
38     @JsonProperty(value = "template", required = true)
39     public String template = "";
40     @JsonProperty("value")
41     public @Nullable Object value = null;
42     @JsonProperty("emptyValue")
43     public String emptyValue = "";
44     @JsonProperty("placeholders")
45     public List<ActionTemplatePlaceholder> placeholders = List.of();
46     @JsonProperty("requiredTags")
47     public String[] requiredItemTags = new String[] {};
48     @JsonProperty("silent")
49     public boolean silent = false;
50     @JsonProperty("memberTargets")
51     public @Nullable ActionTemplateGroupTargets memberTargets = null;
52
53     public static ActionTemplateConfiguration[] fromMetadata(Metadata metadata) throws JsonProcessingException {
54         var configuration = metadata.getConfiguration();
55         var multipleValues = configuration.get("multiple");
56         ObjectMapper mapper = new ObjectMapper();
57         if (multipleValues != null) {
58             return mapper.readValue(mapper.writeValueAsString(multipleValues), ActionTemplateConfiguration[].class);
59         } else {
60             var actionConfig = mapper.readValue(mapper.writeValueAsString(configuration),
61                     ActionTemplateConfiguration.class);
62             return new ActionTemplateConfiguration[] { actionConfig };
63         }
64     }
65
66     public static ActionTemplateConfiguration[] fromJSON(File jsonFile) throws IOException {
67         ObjectMapper mapper = new ObjectMapper();
68         return mapper.readValue(jsonFile, ActionTemplateConfiguration[].class);
69     }
70 }