]> git.basschouten.com Git - openhab-addons.git/blob
300c598d2b3bbe5b33ab7a71b920b2020c087eca
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.resol.internal.providers;
14
15 import java.util.Collection;
16 import java.util.Locale;
17 import java.util.Map;
18 import java.util.concurrent.ConcurrentHashMap;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.resol.internal.ResolBindingConstants;
23 import org.openhab.core.thing.type.ChannelType;
24 import org.openhab.core.thing.type.ChannelTypeBuilder;
25 import org.openhab.core.thing.type.ChannelTypeProvider;
26 import org.openhab.core.thing.type.ChannelTypeUID;
27 import org.openhab.core.types.StateDescriptionFragmentBuilder;
28 import org.osgi.service.component.annotations.Component;
29
30 import de.resol.vbus.Specification;
31 import de.resol.vbus.SpecificationFile.Unit;
32
33 /**
34  * @author Raphael Mack - Initial Contribution
35  *
36  */
37 @Component(service = { ChannelTypeProvider.class, ResolChannelTypeProvider.class })
38 @NonNullByDefault
39 public class ResolChannelTypeProvider implements ChannelTypeProvider {
40     private Map<ChannelTypeUID, ChannelType> channelTypes = new ConcurrentHashMap<ChannelTypeUID, ChannelType>();
41
42     public ResolChannelTypeProvider() {
43         // let's add all channel types from known by the resol-vbus java library
44
45         Specification spec = Specification.getDefaultSpecification();
46
47         Unit[] units = spec.getUnits();
48         for (Unit u : units) {
49             ChannelTypeUID channelTypeUID = new ChannelTypeUID(ResolBindingConstants.BINDING_ID, u.getUnitCodeText());
50
51             // maybe we could use pfv.getPacketFieldSpec().getPrecision() here
52             int precision = 1;
53             if (u.getUnitId() >= 0) {
54                 ChannelType ctype = ChannelTypeBuilder
55                         .state(channelTypeUID, u.getUnitFamily().toString(), itemTypeForUnit(u))
56                         .withStateDescriptionFragment(StateDescriptionFragmentBuilder.create()
57                                 .withPattern("%." + precision + "f " + u.getUnitTextText().replace("%", "%%"))
58                                 .withReadOnly(true).build())
59                         .build();
60
61                 channelTypes.put(channelTypeUID, ctype);
62             }
63         }
64     }
65
66     @Override
67     public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
68         return channelTypes.values();
69     }
70
71     @Override
72     public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
73         if (channelTypes.containsKey(channelTypeUID)) {
74             return channelTypes.get(channelTypeUID);
75         } else {
76             return null;
77         }
78     }
79
80     public static String itemTypeForUnit(Unit u) {
81         String itemType = "Number";
82         switch (u.getUnitFamily()) {
83             case Temperature:
84                 itemType += ":Temperature";
85                 break;
86             case Energy:
87                 itemType += ":Energy";
88                 break;
89             case VolumeFlow:
90                 itemType += ":VolumetricFlowRate";
91                 break;
92             case Pressure:
93                 itemType += ":Pressure";
94                 break;
95             case Volume:
96                 itemType += ":Volume";
97                 break;
98             case Time:
99                 itemType += ":Time";
100                 break;
101             case Power:
102                 itemType += ":Power";
103                 break;
104             case None:
105                 switch (u.getUnitCodeText()) {
106                     case "Hertz":
107                         itemType += ":Frequency";
108                         break;
109                     case "Hectopascals":
110                         itemType += ":Pressure";
111                         break;
112                     case "MetersPerSecond":
113                         itemType += ":Speed";
114                         break;
115                     case "Milliamperes":
116                         itemType += ":ElectricCurrent";
117                         break;
118                     case "Milliseconds":
119                         itemType += ":Time";
120                         break;
121                     case "Ohms":
122                         itemType += ":ElectricResistance";
123                         break;
124                     case "Percent":
125                         itemType += ":Dimensionless";
126                         break;
127                     case "PercentRelativeHumidity":
128                         itemType += ":Dimensionless";
129                         break;
130                     case "Volts":
131                         itemType += ":ElectricPotential";
132                         break;
133                     case "WattsPerSquareMeter":
134                         itemType += ":Intensity";
135                         break;
136                 }
137                 break;
138             default:
139         }
140         return itemType;
141     }
142 }