]> git.basschouten.com Git - openhab-addons.git/blob
7e1789e1cd6548aced7b14bd695aef380b8e9afa
[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.webthing.internal.link;
14
15 import java.util.Locale;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.webthing.internal.client.dto.Property;
20 import org.openhab.core.library.CoreItemFactory;
21
22 /**
23  * The {@link TypeMapping} class defines the mapping of Item types <-> WebThing Property types.
24  *
25  * Please consider that changes of 'Item types <-> WebThing Property types' mapping will break the
26  * compatibility to former releases
27  *
28  * @author Gregor Roth - Initial contribution
29  */
30 @NonNullByDefault
31 public class TypeMapping {
32
33     /**
34      * maps a property type to an item type
35      *
36      * @param propertyMetadata the property meta data
37      * @return the associated item type
38      */
39     public static ItemType toItemType(Property propertyMetadata) {
40         String type = CoreItemFactory.STRING;
41         @Nullable
42         String tag = null;
43
44         switch (propertyMetadata.typeKeyword) {
45             case "AlarmProperty":
46             case "BooleanProperty":
47             case "LeakProperty":
48             case "LockedProperty":
49             case "MotionProperty":
50             case "OnOffProperty":
51             case "PushedProperty":
52                 type = CoreItemFactory.SWITCH;
53                 tag = "Switchable";
54                 break;
55             case "CurrentProperty":
56             case "FrequencyProperty":
57             case "InstantaneousPowerProperty":
58             case "VoltageProperty":
59                 type = CoreItemFactory.NUMBER;
60                 break;
61             case "HeatingCoolingProperty":
62             case "ImageProperty":
63             case "VideoProperty":
64                 type = CoreItemFactory.STRING;
65                 break;
66             case "BrightnessProperty":
67             case "HumidityProperty":
68                 type = CoreItemFactory.DIMMER;
69                 break;
70             case "ColorModeProperty":
71                 type = CoreItemFactory.STRING;
72                 tag = "lighting";
73                 break;
74             case "ColorProperty":
75                 type = CoreItemFactory.COLOR;
76                 tag = "Lighting";
77                 break;
78             case "ColorTemperatureProperty":
79                 type = CoreItemFactory.DIMMER;
80                 tag = "Lighting";
81                 break;
82             case "OpenProperty":
83                 type = CoreItemFactory.CONTACT;
84                 tag = "ContactSensor";
85                 break;
86             case "TargetTemperatureProperty":
87                 type = CoreItemFactory.NUMBER;
88                 tag = "TargetTemperature";
89                 break;
90             case "TemperatureProperty":
91                 type = CoreItemFactory.NUMBER;
92                 tag = "CurrentTemperature";
93                 break;
94             case "ThermostatModeProperty":
95                 break;
96             case "LevelProperty":
97                 if ((propertyMetadata.unit != null)
98                         && propertyMetadata.unit.toLowerCase(Locale.ENGLISH).equals("percent")) {
99                     type = CoreItemFactory.DIMMER;
100                 } else {
101                     type = CoreItemFactory.NUMBER;
102                 }
103                 break;
104             default:
105                 switch (propertyMetadata.type.toLowerCase(Locale.ENGLISH)) {
106                     case "boolean":
107                         type = CoreItemFactory.SWITCH;
108                         tag = "Switchable";
109                         break;
110                     case "integer":
111                     case "number":
112                         type = CoreItemFactory.NUMBER;
113                         break;
114                     default:
115                         type = CoreItemFactory.STRING;
116                         break;
117                 }
118                 break;
119         }
120
121         return new ItemType(type, tag);
122     }
123
124     /**
125      * The item type description
126      */
127     public static class ItemType {
128         private final String type;
129         private final @Nullable String tag;
130
131         ItemType(String type, @Nullable String tag) {
132             this.type = type;
133             this.tag = tag;
134         }
135
136         public String getType() {
137             return type;
138         }
139
140         public @Nullable String getTag() {
141             return tag;
142         }
143     }
144 }