forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SolarForecastHandlerFactory.java
104 lines (95 loc) · 4.39 KB
/
SolarForecastHandlerFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.solarforecast.internal;
import static org.openhab.binding.solarforecast.internal.SolarForecastBindingConstants.*;
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.solarforecast.internal.forecastsolar.handler.ForecastSolarBridgeHandler;
import org.openhab.binding.solarforecast.internal.forecastsolar.handler.ForecastSolarPlaneHandler;
import org.openhab.binding.solarforecast.internal.solcast.handler.SolcastBridgeHandler;
import org.openhab.binding.solarforecast.internal.solcast.handler.SolcastPlaneHandler;
import org.openhab.core.i18n.LocationProvider;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.library.types.PointType;
import org.openhab.core.persistence.PersistenceService;
import org.openhab.core.persistence.PersistenceServiceRegistry;
import org.openhab.core.persistence.QueryablePersistenceService;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The {@link SolarForecastHandlerFactory} is responsible for creating things and thing
* handlers.
*
* @author Bernd Weymann - Initial contribution
*/
@NonNullByDefault
@Component(configurationPid = "binding.solarforecast", service = ThingHandlerFactory.class)
public class SolarForecastHandlerFactory extends BaseThingHandlerFactory {
private final Logger logger = LoggerFactory.getLogger(SolarForecastHandlerFactory.class);
private final TimeZoneProvider timeZoneProvider;
private final ItemRegistry itemRegistry;
private final HttpClient httpClient;
private final PointType location;
private Optional<QueryablePersistenceService> qps = Optional.empty();
@Activate
public SolarForecastHandlerFactory(final @Reference HttpClientFactory hcf, final @Reference LocationProvider lp,
final @Reference PersistenceServiceRegistry psr, final @Reference ItemRegistry ir,
final @Reference TimeZoneProvider tzp) {
itemRegistry = ir;
timeZoneProvider = tzp;
httpClient = hcf.getCommonHttpClient();
PointType pt = lp.getLocation();
if (pt != null) {
location = pt;
} else {
location = PointType.valueOf("0.0,0.0");
}
PersistenceService s = psr.getDefault();
if (s instanceof QueryablePersistenceService) {
qps = Optional.of((QueryablePersistenceService) s);
} else {
logger.info("Persistence {} cannot be queried. Feature Solcast Tuning will not work", s);
}
}
@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return SolarForecastBindingConstants.SUPPORTED_THING_SET.contains(thingTypeUID);
}
@Override
protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (FORECAST_SOLAR_MULTI_STRING.equals(thingTypeUID)) {
return new ForecastSolarBridgeHandler((Bridge) thing, location);
} else if (FORECAST_SOLAR_PART_STRING.equals(thingTypeUID)) {
return new ForecastSolarPlaneHandler(thing, httpClient);
} else if (SOLCAST_BRIDGE_STRING.equals(thingTypeUID)) {
return new SolcastBridgeHandler((Bridge) thing, timeZoneProvider);
} else if (SOLCAST_PART_STRING.equals(thingTypeUID)) {
return new SolcastPlaneHandler(thing, httpClient, qps, itemRegistry);
}
return null;
}
}