]> git.basschouten.com Git - openhab-addons.git/blob
22c7def2dc6ab49538de93592d62868aab14b064
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.solarwatt.internal.channel;
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 javax.measure.Unit;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.solarwatt.internal.SolarwattBindingConstants;
25 import org.openhab.binding.solarwatt.internal.domain.SolarwattChannel;
26 import org.openhab.core.library.CoreItemFactory;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.thing.type.ChannelType;
29 import org.openhab.core.thing.type.ChannelTypeBuilder;
30 import org.openhab.core.thing.type.ChannelTypeProvider;
31 import org.openhab.core.thing.type.ChannelTypeUID;
32 import org.openhab.core.thing.type.StateChannelTypeBuilder;
33 import org.openhab.core.types.StateDescriptionFragmentBuilder;
34 import org.openhab.core.types.util.UnitUtils;
35 import org.osgi.service.component.annotations.Component;
36
37 /**
38  * A {@link ChannelTypeProvider} that creates {@link ChannelType}s according to
39  * the requested tags. It creates one {@link ChannelType} per tag value.
40  *
41  * @author Matthias Steigenberger - Initial contribution
42  * @author Sven Carstens - Adapted to solarwatt binding
43  *
44  */
45 @NonNullByDefault
46 @Component(service = { ChannelTypeProvider.class, SolarwattChannelTypeProvider.class })
47 public class SolarwattChannelTypeProvider implements ChannelTypeProvider {
48
49     private final Map<String, ChannelType> channelMap = new ConcurrentHashMap<>();
50
51     @Override
52     public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
53         return this.channelMap.values();
54     }
55
56     @Override
57     public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
58         return this.channelMap.values().stream().filter(channelType -> channelType.getUID().equals(channelTypeUID))
59                 .findFirst().orElse(null);
60     }
61
62     /**
63      * Assert that the {@link ChannelType} matching our requirements exists.
64      *
65      * Only create once for each tagname supplied via {@link SolarwattChannel}.
66      * 
67      * @param solarwattChannel channeltype requirements
68      * @return UID of existing channeltype
69      */
70     public ChannelTypeUID assertChannelType(SolarwattChannel solarwattChannel) {
71         ChannelType existingChannel = this.channelMap.get(solarwattChannel.getChannelName());
72         if (existingChannel == null) {
73             ChannelType createdChannel = this.getChannelType(solarwattChannel);
74             this.channelMap.put(solarwattChannel.getChannelName(), createdChannel);
75             return createdChannel.getUID();
76         } else {
77             return existingChannel.getUID();
78         }
79     }
80
81     private ChannelType getChannelType(SolarwattChannel solarwattChannel) {
82         StateChannelTypeBuilder stateDescriptionBuilder;
83         Unit<?> unit = solarwattChannel.getUnit();
84         if (unit != null) {
85             if ("switch".equals(solarwattChannel.getCategory())) {
86                 stateDescriptionBuilder = ChannelTypeBuilder
87                         .state(new ChannelTypeUID(SolarwattBindingConstants.BINDING_ID,
88                                 solarwattChannel.getChannelName()), solarwattChannel.getChannelName(),
89                                 CoreItemFactory.SWITCH)
90                         .withCategory(solarwattChannel.getCategory()).isAdvanced(solarwattChannel.getAdvanced())
91                         .withStateDescriptionFragment(
92                                 StateDescriptionFragmentBuilder.create().withReadOnly(true).build());
93             } else {
94                 String dimension = ":" + UnitUtils.getDimensionName(unit);
95                 String unitString = unit.toString();
96
97                 if (Units.PERCENT.equals(unit)) {
98                     // strangely it is Angle
99                     dimension = ":Dimensionless";
100                     unitString = "%%";
101                 }
102
103                 stateDescriptionBuilder = ChannelTypeBuilder
104                         .state(new ChannelTypeUID(SolarwattBindingConstants.BINDING_ID,
105                                 solarwattChannel.getChannelName()), solarwattChannel.getChannelName(),
106                                 CoreItemFactory.NUMBER + dimension)
107                         .withCategory(solarwattChannel.getCategory()).isAdvanced(solarwattChannel.getAdvanced())
108                         .withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().withReadOnly(true)
109                                 .withPattern("%.2f " + unitString).build());
110             }
111         } else {
112             stateDescriptionBuilder = ChannelTypeBuilder
113                     .state(new ChannelTypeUID(SolarwattBindingConstants.BINDING_ID, solarwattChannel.getChannelName()),
114                             solarwattChannel.getChannelName(), CoreItemFactory.STRING)
115                     .withCategory(solarwattChannel.getCategory()).isAdvanced(solarwattChannel.getAdvanced())
116                     .withStateDescriptionFragment(StateDescriptionFragmentBuilder.create().withReadOnly(true).build());
117         }
118         return stateDescriptionBuilder.build();
119     }
120 }