]> git.basschouten.com Git - openhab-addons.git/blob
57e593e7b837c5cba5426b5bb185dcd1ea3bc323
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.values;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.mqtt.generic.ChannelConfig;
17 import org.openhab.binding.mqtt.generic.internal.MqttBindingConstants;
18 import org.openhab.binding.mqtt.generic.mapping.ColorMode;
19
20 /**
21  * A factory t
22  *
23  * @author David Graeff - Initial contribution
24  */
25 @NonNullByDefault
26 public class ValueFactory {
27     /**
28      * Creates a new channel state value.
29      *
30      * @param config The channel configuration
31      * @param channelTypeID The channel type, for instance TEXT_CHANNEL.
32      */
33     public static Value createValueState(ChannelConfig config, String channelTypeID) throws IllegalArgumentException {
34         Value value;
35         switch (channelTypeID) {
36             case MqttBindingConstants.STRING:
37                 value = config.allowedStates.isBlank() ? new TextValue()
38                         : new TextValue(config.allowedStates.split(","));
39                 break;
40             case MqttBindingConstants.DATETIME:
41                 value = new DateTimeValue();
42                 break;
43             case MqttBindingConstants.IMAGE:
44                 value = new ImageValue();
45                 break;
46             case MqttBindingConstants.LOCATION:
47                 value = new LocationValue();
48                 break;
49             case MqttBindingConstants.NUMBER:
50                 value = new NumberValue(config.min, config.max, config.step, config.unit);
51                 break;
52             case MqttBindingConstants.DIMMER:
53                 value = new PercentageValue(config.min, config.max, config.step, config.on, config.off);
54                 break;
55             case MqttBindingConstants.COLOR_HSB:
56                 value = new ColorValue(ColorMode.HSB, config.on, config.off, config.onBrightness);
57                 break;
58             case MqttBindingConstants.COLOR_RGB:
59                 value = new ColorValue(ColorMode.RGB, config.on, config.off, config.onBrightness);
60                 break;
61             case MqttBindingConstants.COLOR:
62                 value = new ColorValue(ColorMode.valueOf(config.colorMode), config.on, config.off, config.onBrightness);
63                 break;
64             case MqttBindingConstants.SWITCH:
65                 value = new OnOffValue(config.on, config.off);
66                 break;
67             case MqttBindingConstants.CONTACT:
68                 value = new OpenCloseValue(config.on, config.off);
69                 break;
70             case MqttBindingConstants.ROLLERSHUTTER:
71                 value = new RollershutterValue(config.on, config.off, config.stop);
72                 break;
73             case MqttBindingConstants.TRIGGER:
74                 config.trigger = true;
75                 value = new TextValue();
76                 break;
77             default:
78                 throw new IllegalArgumentException("ChannelTypeUID not recognised: " + channelTypeID);
79         }
80         return value;
81     }
82 }