2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.voice.actiontemplatehli.internal.configuration;
16 import java.io.IOException;
17 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.items.Metadata;
23 import com.fasterxml.jackson.annotation.JsonProperty;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
28 * The {@link ActionTemplateConfiguration} class represents each configured action
30 * @author Miguel Álvarez - Initial contribution
33 public class ActionTemplateConfiguration {
35 public String type = "tokens";
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;
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);
60 var actionConfig = mapper.readValue(mapper.writeValueAsString(configuration),
61 ActionTemplateConfiguration.class);
62 return new ActionTemplateConfiguration[] { actionConfig };
66 public static ActionTemplateConfiguration[] fromJSON(File jsonFile) throws IOException {
67 ObjectMapper mapper = new ObjectMapper();
68 return mapper.readValue(jsonFile, ActionTemplateConfiguration[].class);