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