]> git.basschouten.com Git - openhab-addons.git/blob
5d2a33fbad3b921e520346446b7779ef2aafba80
[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.digitalstrom.internal.providers;
14
15 import java.net.URI;
16 import java.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.Locale;
23
24 import org.openhab.binding.digitalstrom.internal.DigitalSTROMBindingConstants;
25 import org.openhab.binding.digitalstrom.internal.handler.CircuitHandler;
26 import org.openhab.binding.digitalstrom.internal.handler.DeviceHandler;
27 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringTypeEnum;
28 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringUnitsEnum;
29 import org.openhab.core.i18n.TranslationProvider;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.binding.ThingTypeProvider;
32 import org.openhab.core.thing.type.ChannelDefinition;
33 import org.openhab.core.thing.type.ChannelDefinitionBuilder;
34 import org.openhab.core.thing.type.ChannelTypeUID;
35 import org.openhab.core.thing.type.ThingType;
36 import org.openhab.core.thing.type.ThingTypeBuilder;
37 import org.osgi.service.component.ComponentContext;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Deactivate;
41 import org.osgi.service.component.annotations.Reference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * The {@link DsDeviceThingTypeProvider} creates the {@link ThingType}'s for the subclasses of the
47  * {@link org.openhab.binding.digitalstrom.internal.lib.structure.devices.GeneralDeviceInformation}s.
48  * It also adds the {@link ThingTypeUID}'s to the related handlers. So only the
49  * {@link SupportedThingTypes} enum has to be adjusted, if new device types of digitalSTROM should be supported.
50  * Provided the new digitalSTROM devices uses the same mechanism like now.
51  *
52  * @author Michael Ochel - Initial contribution
53  * @author Matthias Siegele - Initial contribution
54  */
55 @Component(service = ThingTypeProvider.class)
56 public class DsDeviceThingTypeProvider extends BaseDsI18n implements ThingTypeProvider {
57
58     /**
59      * Through the {@link SupportedThingTypes} the {@link ThingType}'s will be created. For that the enum name will be
60      * used as thing type id, the first field will set the responsible handler and the last enum field will set the
61      * supporting of the power sensor refresh configurations (config-description with refresh priority setting or not).
62      *
63      * @author Michael Ochel - Initial contribution
64      * @author Matthias Siegele - Initial contribution
65      */
66     public enum SupportedThingTypes {
67         // ThingType, responsible ThingHanlder, Device config-description with power-sensors
68         GE(DeviceHandler.class.getSimpleName(), true),
69         GR(DeviceHandler.class.getSimpleName(), false),
70         SW(DeviceHandler.class.getSimpleName(), true),
71         BL(DeviceHandler.class.getSimpleName(), true),
72         dSiSens200(DeviceHandler.class.getSimpleName(), false),
73         circuit(CircuitHandler.class.getSimpleName(), false);
74
75         private final String handler;
76         private final boolean havePowerSensors;
77
78         private SupportedThingTypes(String handler, boolean havePowerSensors) {
79             this.handler = handler;
80             this.havePowerSensors = havePowerSensors;
81         }
82     }
83
84     private final Logger logger = LoggerFactory.getLogger(DsDeviceThingTypeProvider.class);
85
86     private static final String DEVICE_WITH_POWER_SENSORS = "thing-type:digitalstrom:deviceWithPowerSensors";
87     private static final String DEVICE_WITHOUT_POWER_SENSORS = "thing-type:digitalstrom:deviceWithoutPowerSensors";
88
89     @Activate
90     @Override
91     protected void activate(ComponentContext componentContext) {
92         super.activate(componentContext);
93     }
94
95     @Deactivate
96     @Override
97     protected void deactivate(ComponentContext componentContext) {
98         super.deactivate(componentContext);
99     }
100
101     @Reference
102     @Override
103     protected void setTranslationProvider(TranslationProvider translationProvider) {
104         super.setTranslationProvider(translationProvider);
105     }
106
107     @Override
108     protected void unsetTranslationProvider(TranslationProvider translationProvider) {
109         super.unsetTranslationProvider(translationProvider);
110     }
111
112     @Override
113     protected void init() {
114         for (SupportedThingTypes supportedThingType : SupportedThingTypes.values()) {
115             if (supportedThingType.handler.equals(DeviceHandler.class.getSimpleName())) {
116                 DeviceHandler.SUPPORTED_THING_TYPES
117                         .add(new ThingTypeUID(DigitalSTROMBindingConstants.BINDING_ID, supportedThingType.toString()));
118             }
119             if (supportedThingType.handler.equals(CircuitHandler.class.getSimpleName())) {
120                 CircuitHandler.SUPPORTED_THING_TYPES
121                         .add(new ThingTypeUID(DigitalSTROMBindingConstants.BINDING_ID, supportedThingType.toString()));
122             }
123         }
124     }
125
126     @Override
127     public Collection<ThingType> getThingTypes(Locale locale) {
128         List<ThingType> thingTypes = new LinkedList<>();
129         for (SupportedThingTypes supportedThingType : SupportedThingTypes.values()) {
130             thingTypes.add(getThingType(
131                     new ThingTypeUID(DigitalSTROMBindingConstants.BINDING_ID, supportedThingType.toString()), locale));
132         }
133         return thingTypes;
134     }
135
136     @Override
137     public ThingType getThingType(ThingTypeUID thingTypeUID, Locale locale) {
138         try {
139             SupportedThingTypes supportedThingType = SupportedThingTypes.valueOf(thingTypeUID.getId());
140             ThingTypeBuilder thingTypeBuilder = ThingTypeBuilder
141                     .instance(thingTypeUID, getLabelText(thingTypeUID.getId(), locale))
142                     .withSupportedBridgeTypeUIDs(
143                             Arrays.asList(DigitalSTROMBindingConstants.THING_TYPE_DSS_BRIDGE.getAsString()))
144                     .withDescription(getDescText(thingTypeUID.getId(), locale));
145             try {
146                 if (supportedThingType.havePowerSensors) {
147                     thingTypeBuilder.withConfigDescriptionURI(new URI(DEVICE_WITH_POWER_SENSORS));
148                 } else {
149                     thingTypeBuilder.withConfigDescriptionURI(new URI(DEVICE_WITHOUT_POWER_SENSORS));
150                 }
151             } catch (URISyntaxException e) {
152                 logger.debug("An URISyntaxException occurred: ", e);
153             }
154             if (SupportedThingTypes.GR.equals(supportedThingType)) {
155                 thingTypeBuilder.withChannelDefinitions(Arrays.asList(new ChannelDefinitionBuilder(
156                         DsChannelTypeProvider.SHADE,
157                         new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, DsChannelTypeProvider.SHADE))
158                         .build()));
159             }
160             if (SupportedThingTypes.circuit.equals(supportedThingType)) {
161                 List<ChannelDefinition> channelDefinitions = new ArrayList<>(3);
162                 for (MeteringTypeEnum meteringType : MeteringTypeEnum.values()) {
163                     channelDefinitions.add(new ChannelDefinitionBuilder(
164                             DsChannelTypeProvider.getMeteringChannelID(meteringType, MeteringUnitsEnum.WH, false),
165                             new ChannelTypeUID(DigitalSTROMBindingConstants.BINDING_ID, DsChannelTypeProvider
166                                     .getMeteringChannelID(meteringType, MeteringUnitsEnum.WH, false)))
167                             .build());
168                 }
169                 thingTypeBuilder.withChannelDefinitions(channelDefinitions);
170             }
171             return thingTypeBuilder.build();
172         } catch (IllegalArgumentException e) {
173             // ignore
174         }
175         return null;
176     }
177 }