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