]> git.basschouten.com Git - openhab-addons.git/blob
49d066f8f1ef4af6e0f45808aa4a2974a304de02
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.internal.kostal.inverter;
14
15 import static org.openhab.binding.internal.kostal.inverter.thirdgeneration.ThirdGenerationBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.jetty.client.HttpClient;
23 import org.openhab.binding.internal.kostal.inverter.firstgeneration.WebscrapeHandler;
24 import org.openhab.binding.internal.kostal.inverter.thirdgeneration.ThirdGenerationHandler;
25 import org.openhab.binding.internal.kostal.inverter.thirdgeneration.ThirdGenerationInverterTypes;
26 import org.openhab.core.io.net.http.HttpClientFactory;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerFactory;
32 import org.osgi.service.component.annotations.Activate;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Reference;
35
36 /**
37  * @author Christian Schneider - Initial contribution (as WebscrapeHandlerFactory.java)
38  * @author RenĂ© Stakemeier - extension for the third generation of KOSTAL inverters
39  */
40 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.kostalinverter")
41 @NonNullByDefault
42 public class KostalInverterFactory extends BaseThingHandlerFactory {
43
44     private static final Map<ThingTypeUID, ThirdGenerationInverterTypes> SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS = new HashMap<>();
45     static {
46         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PIKOIQ42, ThirdGenerationInverterTypes.PIKOIQ_42);
47         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PIKOIQ55, ThirdGenerationInverterTypes.PIKOIQ_55);
48         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PIKOIQ70, ThirdGenerationInverterTypes.PIKOIQ_70);
49         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PIKOIQ85, ThirdGenerationInverterTypes.PIKOIQ_85);
50         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PIKOIQ100, ThirdGenerationInverterTypes.PIKOIQ_100);
51         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PLENTICOREPLUS42WITHBATTERY,
52                 ThirdGenerationInverterTypes.PLENTICORE_PLUS_42_WITH_BATTERY);
53         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PLENTICOREPLUS55WITHBATTERY,
54                 ThirdGenerationInverterTypes.PLENTICORE_PLUS_55_WITH_BATTERY);
55         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PLENTICOREPLUS70WITHBATTERY,
56                 ThirdGenerationInverterTypes.PLENTICORE_PLUS_70_WITH_BATTERY);
57         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PLENTICOREPLUS85WITHBATTERY,
58                 ThirdGenerationInverterTypes.PLENTICORE_PLUS_85_WITH_BATTERY);
59         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PLENTICOREPLUS100WITHBATTERY,
60                 ThirdGenerationInverterTypes.PLENTICORE_PLUS_100_WITH_BATTERY);
61         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PLENTICOREPLUS42WITHOUTBATTERY,
62                 ThirdGenerationInverterTypes.PLENTICORE_PLUS_42_WITHOUT_BATTERY);
63         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PLENTICOREPLUS55WITHOUTBATTERY,
64                 ThirdGenerationInverterTypes.PLENTICORE_PLUS_55_WITHOUT_BATTERY);
65         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PLENTICOREPLUS70WITHOUTBATTERY,
66                 ThirdGenerationInverterTypes.PLENTICORE_PLUS_70_WITHOUT_BATTERY);
67         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PLENTICOREPLUS85WITHOUTBATTERY,
68                 ThirdGenerationInverterTypes.PLENTICORE_PLUS_85_WITHOUT_BATTERY);
69         SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.put(PLENTICOREPLUS100WITHOUTBATTERY,
70                 ThirdGenerationInverterTypes.PLENTICORE_PLUS_100_WITHOUT_BATTERY);
71     }
72
73     public static final ThingTypeUID FIRST_GENERATION_INVERTER = new ThingTypeUID("kostalinverter", "kostalinverter");
74
75     private final HttpClient httpClient;
76
77     @Activate
78     public KostalInverterFactory(@Reference final HttpClientFactory httpClientFactory) {
79         this.httpClient = httpClientFactory.getCommonHttpClient();
80     }
81
82     @Override
83     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
84         return thingTypeUID.equals(FIRST_GENERATION_INVERTER)
85                 || SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS.keySet().contains(thingTypeUID);
86     }
87
88     @Override
89     protected @Nullable ThingHandler createHandler(Thing thing) {
90         // first generation
91         if (FIRST_GENERATION_INVERTER.equals(thing.getThingTypeUID())) {
92             return new WebscrapeHandler(thing);
93         }
94         // third generation
95         ThirdGenerationInverterTypes inverterType = SUPPORTED_THIRD_GENERATION_THING_TYPES_UIDS
96                 .get(thing.getThingTypeUID());
97         if (inverterType != null) {
98             return new ThirdGenerationHandler(thing, httpClient, inverterType);
99         }
100         return null;
101     }
102 }