]> git.basschouten.com Git - openhab-addons.git/blob
80aea70206f020104cb03b11a13b71bf176de3b3
[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.networkupstools.internal;
14
15 import static org.openhab.binding.networkupstools.internal.NUTBindingConstants.BINDING_ID;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.library.CoreItemFactory;
20 import org.openhab.core.library.types.QuantityType;
21 import org.openhab.core.thing.Channel;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.binding.builder.ChannelBuilder;
24 import org.openhab.core.thing.type.ChannelType;
25 import org.openhab.core.thing.type.ChannelTypeBuilder;
26 import org.openhab.core.thing.type.ChannelTypeUID;
27 import org.openhab.core.types.StateDescriptionFragmentBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Factory class to enrich dynamic created channels with additional configurations.
33  *
34  * @author Hilbrand Bouwkamp - Initial contribution
35  */
36 @NonNullByDefault
37 class NUTDynamicChannelFactory {
38
39     private static final String QUANTITY_ITEM_TYPE_PREFIX = CoreItemFactory.NUMBER + ':';
40
41     private final Logger logger = LoggerFactory.getLogger(NUTDynamicChannelFactory.class);
42
43     private final NUTChannelTypeProvider channelTypeProvider;
44
45     NUTDynamicChannelFactory(final NUTChannelTypeProvider channelTypeProvider) {
46         this.channelTypeProvider = channelTypeProvider;
47     }
48
49     /**
50      * Enriches the channel from the given channel and returns the newly created channel.
51      *
52      * @param channel Channel to enrich
53      * @param channelConfig channel configuration
54      * @return new created channel or null if it was not possible to create a new channel due to missing information or
55      *         otherwise
56      */
57     public @Nullable Channel createChannel(final Channel channel, final NUTDynamicChannelConfiguration channelConfig) {
58         final String acceptedItemType = channel.getAcceptedItemType();
59
60         if (acceptedItemType == null) {
61             return null;
62         }
63         final ChannelTypeUID channelTypeUID;
64
65         if (acceptedItemType.startsWith(QUANTITY_ITEM_TYPE_PREFIX)) {
66             channelTypeUID = createQuantityTypeChannel(channel, acceptedItemType, channelConfig);
67         } else {
68             channelTypeUID = getChannelTypeUID(acceptedItemType, channel.getUID());
69         }
70         return channelTypeUID == null ? null : ChannelBuilder.create(channel).withType(channelTypeUID).build();
71     }
72
73     /**
74      * Returns the {@link ChannelTypeUID} for channels types that are supported and have a channel-type definition in
75      * the binding thing xml.
76      *
77      * @param itemType item type to get the channel type for
78      * @param channelUID ChannelUID for which the channel type is determined
79      * @return channel type or null if not supported
80      */
81     private @Nullable ChannelTypeUID getChannelTypeUID(final String itemType, final ChannelUID channelUID) {
82         switch (itemType) {
83             case CoreItemFactory.NUMBER:
84                 return NUTBindingConstants.CHANNEL_TYPE_DYNAMIC_NUMBER;
85             case CoreItemFactory.STRING:
86                 return NUTBindingConstants.CHANNEL_TYPE_DYNAMIC_STRING;
87             case CoreItemFactory.SWITCH:
88                 return NUTBindingConstants.CHANNEL_TYPE_DYNAMIC_SWITCH;
89             default:
90                 logger.info("Dynamic channel '{}' is ignored because the type '{}' is not supported.", channelUID,
91                         itemType);
92                 return null;
93         }
94     }
95
96     /**
97      * Creates a new {@link ChannelTypeUID} for dynamically created {@link QuantityType} channels.
98      * It registers the new channel type with the channel type provider.
99      *
100      * @param channel Channel to enrich
101      * @param itemType item type to get the channel type for
102      * @param channelConfig channel configuration
103      * @return channel type or null if not supported
104      */
105     private @Nullable ChannelTypeUID createQuantityTypeChannel(final Channel channel, final String itemType,
106             final NUTDynamicChannelConfiguration channelConfig) {
107         if (channelConfig.unit == null || channelConfig.unit.isEmpty()) {
108             logger.info("Dynamic Channel '{}' is ignored because it's a QuantityType without a 'unit' property.",
109                     channel.getUID());
110             return null;
111         }
112         final StateDescriptionFragmentBuilder sdb = StateDescriptionFragmentBuilder.create();
113         final ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, channel.getUID().getId() + "Type");
114         final String label = channel.getLabel();
115         final ChannelType channelType = ChannelTypeBuilder.state(channelTypeUID, label == null ? "" : label, itemType)
116                 .withStateDescriptionFragment(sdb.withReadOnly(Boolean.TRUE).build())
117                 .withConfigDescriptionURI(NUTBindingConstants.DYNAMIC_CHANNEL_CONFIG_QUANTITY_TYPE).build();
118         channelTypeProvider.addChannelType(channelType);
119         return channelTypeUID;
120     }
121 }