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