]> git.basschouten.com Git - openhab-addons.git/blob
69fce0358c8e8a5015dc29c2d6e1b582caa1ae38
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.binding.mqtt.generic.internal.handler;
14
15 import static org.openhab.binding.mqtt.generic.internal.MqttBindingConstants.*;
16
17 import java.math.BigDecimal;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.mqtt.generic.internal.MqttBindingConstants;
25 import org.openhab.core.config.core.Configuration;
26 import org.openhab.core.thing.Channel;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.builder.ChannelBuilder;
30 import org.openhab.core.thing.type.ChannelTypeUID;
31
32 /**
33  * Static test definitions, like thing, bridge and channel definitions
34  *
35  * @author David Graeff - Initial contribution
36  */
37 @NonNullByDefault
38 public class ThingChannelConstants {
39     // Common ThingUID and ChannelUIDs
40     public static final ThingUID testGenericThing = new ThingUID(GENERIC_MQTT_THING, "genericthing");
41
42     public static final ChannelTypeUID textChannel = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.STRING);
43     public static final ChannelTypeUID textWithJsonChannel = new ChannelTypeUID(BINDING_ID,
44             MqttBindingConstants.STRING);
45     public static final ChannelTypeUID onoffChannel = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.SWITCH);
46     public static final ChannelTypeUID numberChannel = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.NUMBER);
47     public static final ChannelTypeUID percentageChannel = new ChannelTypeUID(BINDING_ID, MqttBindingConstants.DIMMER);
48     public static final ChannelTypeUID unknownChannel = new ChannelTypeUID(BINDING_ID, "unknown");
49
50     public static final ChannelUID textChannelUID = new ChannelUID(testGenericThing, "mytext");
51
52     public static final String jsonPathJSON = "{ \"device\": { \"status\": { \"temperature\": 23.2 }}}";
53     public static final String jsonPathPattern = "$.device.status.temperature";
54
55     public static final List<Channel> thingChannelList = new ArrayList<>();
56     public static final List<Channel> thingChannelListWithJson = new ArrayList<>();
57
58     /**
59      * Create a channel with exact the parameters we need for the tests
60      *
61      * @param id Channel ID
62      * @param acceptedType Accept type
63      * @param config The configuration
64      * @param channelTypeUID ChannelTypeUID provided by the static definitions
65      * @return
66      */
67     public static Channel cb(String id, String acceptedType, Configuration config, ChannelTypeUID channelTypeUID) {
68         return ChannelBuilder.create(new ChannelUID(testGenericThing, id), acceptedType).withConfiguration(config)
69                 .withType(channelTypeUID).build();
70     }
71
72     static {
73         thingChannelList.add(cb("mytext", "TextItemType", textConfiguration(), textChannel));
74         thingChannelList.add(cb("onoff", "OnOffType", onoffConfiguration(), onoffChannel));
75         thingChannelList.add(cb("num", "NumberType", numberConfiguration(), numberChannel));
76         thingChannelList.add(cb("percent", "NumberType", percentageConfiguration(), percentageChannel));
77
78         thingChannelListWithJson.add(cb("mytext", "TextItemType", textConfigurationWithJson(), textWithJsonChannel));
79         thingChannelListWithJson.add(cb("onoff", "OnOffType", onoffConfiguration(), onoffChannel));
80         thingChannelListWithJson.add(cb("num", "NumberType", numberConfiguration(), numberChannel));
81         thingChannelListWithJson.add(cb("percent", "NumberType", percentageConfiguration(), percentageChannel));
82     }
83
84     static Configuration textConfiguration() {
85         Map<String, Object> data = new HashMap<>();
86         data.put("stateTopic", "test/state");
87         data.put("commandTopic", "test/command");
88         return new Configuration(data);
89     }
90
91     static Configuration textConfigurationWithJson() {
92         Map<String, Object> data = new HashMap<>();
93         data.put("stateTopic", "test/state");
94         data.put("commandTopic", "test/command");
95         data.put("transformationPattern", "JSONPATH:" + jsonPathPattern);
96         return new Configuration(data);
97     }
98
99     private static Configuration numberConfiguration() {
100         Map<String, Object> data = new HashMap<>();
101         data.put("stateTopic", "test/state");
102         data.put("commandTopic", "test/command");
103         data.put("min", BigDecimal.valueOf(1));
104         data.put("max", BigDecimal.valueOf(99));
105         data.put("step", BigDecimal.valueOf(2));
106         data.put("isDecimal", true);
107         return new Configuration(data);
108     }
109
110     private static Configuration percentageConfiguration() {
111         Map<String, Object> data = new HashMap<>();
112         data.put("stateTopic", "test/state");
113         data.put("commandTopic", "test/command");
114         data.put("on", "ON");
115         data.put("off", "OFF");
116         return new Configuration(data);
117     }
118
119     private static Configuration onoffConfiguration() {
120         Map<String, Object> data = new HashMap<>();
121         data.put("stateTopic", "test/state");
122         data.put("commandTopic", "test/command");
123         data.put("on", "ON");
124         data.put("off", "OFF");
125         data.put("inverse", true);
126         return new Configuration(data);
127     }
128 }