]> git.basschouten.com Git - openhab-addons.git/blob
7bb51383801c01dc0b2e57de00dc99835a31f271
[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.channelGroups
116                 .forEach(channelGroup -> channelGroup.getHelperInstance().ifPresent(helper -> helpers.add(helper)));
117
118         moduleType.capabilities.forEach(capability -> {
119             Capability newCap = null;
120             if (capability == DeviceCapability.class) {
121                 newCap = new DeviceCapability(handler);
122             } else if (capability == AirCareCapability.class) {
123                 newCap = new AirCareCapability(handler);
124             } else if (capability == EventCapability.class) {
125                 newCap = new EventCapability(handler);
126             } else if (capability == HomeCapability.class) {
127                 newCap = new HomeCapability(handler, stateDescriptionProvider);
128             } else if (capability == WeatherCapability.class) {
129                 newCap = new WeatherCapability(handler);
130             } else if (capability == RoomCapability.class) {
131                 newCap = new RoomCapability(handler);
132             } else if (capability == PersonCapability.class) {
133                 newCap = new PersonCapability(handler, stateDescriptionProvider, helpers);
134             } else if (capability == CameraCapability.class) {
135                 newCap = new CameraCapability(handler, stateDescriptionProvider, helpers);
136             } else if (capability == PresenceCapability.class) {
137                 newCap = new PresenceCapability(handler, stateDescriptionProvider, helpers);
138             } else if (capability == MeasureCapability.class) {
139                 newCap = new MeasureCapability(handler, helpers);
140             } else if (capability == ChannelHelperCapability.class) {
141                 newCap = new ChannelHelperCapability(handler, helpers);
142             }
143             if (newCap != null) {
144                 handler.getCapabilities().put(newCap);
145             } else {
146                 logger.warn("No factory entry defined to create Capability : {}", capability);
147             }
148         });
149
150         return (BaseThingHandler) handler;
151     }
152 }