2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.resol.internal.providers;
15 import java.util.Collection;
16 import java.util.Locale;
18 import java.util.concurrent.ConcurrentHashMap;
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;
30 import de.resol.vbus.Specification;
31 import de.resol.vbus.SpecificationFile.Unit;
34 * @author Raphael Mack - Initial Contribution
37 @Component(service = { ChannelTypeProvider.class, ResolChannelTypeProvider.class })
39 public class ResolChannelTypeProvider implements ChannelTypeProvider {
40 private Map<ChannelTypeUID, ChannelType> channelTypes = new ConcurrentHashMap<ChannelTypeUID, ChannelType>();
42 public ResolChannelTypeProvider() {
43 // let's add all channel types from known by the resol-vbus java library
45 Specification spec = Specification.getDefaultSpecification();
47 Unit[] units = spec.getUnits();
48 for (Unit u : units) {
49 ChannelTypeUID channelTypeUID = new ChannelTypeUID(ResolBindingConstants.BINDING_ID, u.getUnitCodeText());
51 // maybe we could use pfv.getPacketFieldSpec().getPrecision() here
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())
61 channelTypes.put(channelTypeUID, ctype);
67 public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
68 return channelTypes.values();
72 public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
73 if (channelTypes.containsKey(channelTypeUID)) {
74 return channelTypes.get(channelTypeUID);
80 public static String itemTypeForUnit(Unit u) {
81 String itemType = "Number";
82 switch (u.getUnitFamily()) {
84 itemType += ":Temperature";
87 itemType += ":Energy";
90 itemType += ":VolumetricFlowRate";
93 itemType += ":Pressure";
96 itemType += ":Volume";
102 itemType += ":Power";
105 switch (u.getUnitCodeText()) {
107 itemType += ":Frequency";
110 itemType += ":Pressure";
112 case "MetersPerSecond":
113 itemType += ":Speed";
116 itemType += ":ElectricCurrent";
122 itemType += ":ElectricResistance";
125 itemType += ":Dimensionless";
127 case "PercentRelativeHumidity":
128 itemType += ":Dimensionless";
131 itemType += ":ElectricPotential";
133 case "WattsPerSquareMeter":
134 itemType += ":Intensity";