]> git.basschouten.com Git - openhab-addons.git/blob
4bd697b859ecc2933ba619dc654a521a28567c08
[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.netatmo.internal;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
23 import org.openhab.binding.netatmo.internal.config.BindingConfiguration;
24 import org.openhab.binding.netatmo.internal.deserialization.NADeserializer;
25 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
26 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
27 import org.openhab.binding.netatmo.internal.handler.DeviceHandler;
28 import org.openhab.binding.netatmo.internal.handler.ModuleHandler;
29 import org.openhab.binding.netatmo.internal.handler.capability.AirCareCapability;
30 import org.openhab.binding.netatmo.internal.handler.capability.CameraCapability;
31 import org.openhab.binding.netatmo.internal.handler.capability.Capability;
32 import org.openhab.binding.netatmo.internal.handler.capability.ChannelHelperCapability;
33 import org.openhab.binding.netatmo.internal.handler.capability.DeviceCapability;
34 import org.openhab.binding.netatmo.internal.handler.capability.EventCapability;
35 import org.openhab.binding.netatmo.internal.handler.capability.HomeCapability;
36 import org.openhab.binding.netatmo.internal.handler.capability.MeasureCapability;
37 import org.openhab.binding.netatmo.internal.handler.capability.PersonCapability;
38 import org.openhab.binding.netatmo.internal.handler.capability.PresenceCapability;
39 import org.openhab.binding.netatmo.internal.handler.capability.RoomCapability;
40 import org.openhab.binding.netatmo.internal.handler.capability.WeatherCapability;
41 import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
42 import org.openhab.binding.netatmo.internal.providers.NetatmoDescriptionProvider;
43 import org.openhab.core.config.core.ConfigParser;
44 import org.openhab.core.io.net.http.HttpClientFactory;
45 import org.openhab.core.thing.Bridge;
46 import org.openhab.core.thing.Thing;
47 import org.openhab.core.thing.ThingTypeUID;
48 import org.openhab.core.thing.binding.BaseThingHandler;
49 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
50 import org.openhab.core.thing.binding.ThingHandler;
51 import org.openhab.core.thing.binding.ThingHandlerFactory;
52 import org.osgi.service.component.annotations.Activate;
53 import org.osgi.service.component.annotations.Component;
54 import org.osgi.service.component.annotations.Modified;
55 import org.osgi.service.component.annotations.Reference;
56 import org.osgi.service.http.HttpService;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 /**
61  * The {@link NetatmoHandlerFactory} is responsible for creating things and
62  * thing handlers.
63  *
64  * @author GaĆ«l L'hopital - Initial contribution
65  */
66 @NonNullByDefault
67 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.netatmo")
68 public class NetatmoHandlerFactory extends BaseThingHandlerFactory {
69     private final Logger logger = LoggerFactory.getLogger(NetatmoHandlerFactory.class);
70
71     private final BindingConfiguration configuration = new BindingConfiguration();
72     private final NetatmoDescriptionProvider stateDescriptionProvider;
73     private final NADeserializer deserializer;
74     private final HttpClient httpClient;
75     private final HttpService httpService;
76
77     @Activate
78     public NetatmoHandlerFactory(@Reference NetatmoDescriptionProvider stateDescriptionProvider,
79             @Reference HttpClientFactory factory, @Reference NADeserializer deserializer,
80             @Reference HttpService httpService, Map<String, @Nullable Object> config) {
81         this.stateDescriptionProvider = stateDescriptionProvider;
82         this.httpClient = factory.getCommonHttpClient();
83         this.deserializer = deserializer;
84         this.httpService = httpService;
85         configChanged(config);
86     }
87
88     @Modified
89     public void configChanged(Map<String, @Nullable Object> config) {
90         BindingConfiguration newConf = ConfigParser.configurationAs(config, BindingConfiguration.class);
91         if (newConf != null) {
92             configuration.update(newConf);
93         }
94     }
95
96     @Override
97     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
98         return ModuleType.AS_SET.stream().anyMatch(mt -> mt.thingTypeUID.equals(thingTypeUID));
99     }
100
101     @Override
102     protected @Nullable ThingHandler createHandler(Thing thing) {
103         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
104         return ModuleType.AS_SET.stream().filter(mt -> mt.thingTypeUID.equals(thingTypeUID)).findFirst()
105                 .map(mt -> buildHandler(thing, mt)).orElse(null);
106     }
107
108     private BaseThingHandler buildHandler(Thing thing, ModuleType moduleType) {
109         if (ModuleType.ACCOUNT.equals(moduleType)) {
110             return new ApiBridgeHandler((Bridge) thing, httpClient, deserializer, configuration, httpService);
111         }
112         CommonInterface handler = moduleType.isABridge() ? new DeviceHandler((Bridge) thing) : new ModuleHandler(thing);
113
114         List<ChannelHelper> helpers = new ArrayList<>();
115         moduleType.channelHelpers.forEach(helperClass -> {
116             try {
117                 helpers.add(helperClass.getConstructor().newInstance());
118             } catch (ReflectiveOperationException e) {
119                 logger.warn("Error creating or initializing helper class : {}", e.getMessage());
120             }
121         });
122
123         moduleType.capabilities.forEach(capability -> {
124             Capability newCap = null;
125             if (capability == DeviceCapability.class) {
126                 newCap = new DeviceCapability(handler);
127             } else if (capability == AirCareCapability.class) {
128                 newCap = new AirCareCapability(handler);
129             } else if (capability == EventCapability.class) {
130                 newCap = new EventCapability(handler);
131             } else if (capability == HomeCapability.class) {
132                 newCap = new HomeCapability(handler, stateDescriptionProvider);
133             } else if (capability == WeatherCapability.class) {
134                 newCap = new WeatherCapability(handler);
135             } else if (capability == RoomCapability.class) {
136                 newCap = new RoomCapability(handler);
137             } else if (capability == PersonCapability.class) {
138                 newCap = new PersonCapability(handler, stateDescriptionProvider, helpers);
139             } else if (capability == CameraCapability.class) {
140                 newCap = new CameraCapability(handler, stateDescriptionProvider, helpers);
141             } else if (capability == PresenceCapability.class) {
142                 newCap = new PresenceCapability(handler, stateDescriptionProvider, helpers);
143             } else if (capability == MeasureCapability.class) {
144                 newCap = new MeasureCapability(handler, helpers);
145             } else if (capability == ChannelHelperCapability.class) {
146                 newCap = new ChannelHelperCapability(handler, helpers);
147             }
148             if (newCap != null) {
149                 handler.getCapabilities().put(newCap);
150             } else {
151                 logger.warn("No factory entry defined to create Capability : {}", capability);
152             }
153         });
154
155         return (BaseThingHandler) handler;
156     }
157 }