2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.synopanalyzer.internal;
15 import static org.openhab.binding.synopanalyzer.internal.SynopAnalyzerBindingConstants.THING_SYNOP;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.InputStreamReader;
20 import java.io.Reader;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Hashtable;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.synopanalyzer.internal.discovery.SynopAnalyzerDiscoveryService;
27 import org.openhab.binding.synopanalyzer.internal.handler.SynopAnalyzerHandler;
28 import org.openhab.binding.synopanalyzer.internal.synop.StationDB;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.i18n.LocationProvider;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.framework.ServiceRegistration;
37 import org.osgi.service.component.ComponentContext;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 import com.google.gson.Gson;
47 * The {@link SynopAnalyzerHandlerFactory} is responsible for creating things and thing
50 * @author Gaƫl L'hopital - Initial contribution
53 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.synopanalyzer")
55 public class SynopAnalyzerHandlerFactory extends BaseThingHandlerFactory {
56 private final Logger logger = LoggerFactory.getLogger(SynopAnalyzerHandlerFactory.class);
57 private final LocationProvider locationProvider;
58 private final Gson gson = new Gson();
59 private @Nullable StationDB stationDB;
60 private @Nullable ServiceRegistration<?> serviceReg;
63 public SynopAnalyzerHandlerFactory(@Reference LocationProvider locationProvider) {
64 this.locationProvider = locationProvider;
68 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
69 return THING_SYNOP.equals(thingTypeUID);
73 protected @Nullable ThingHandler createHandler(Thing thing) {
74 return supportsThingType(thing.getThingTypeUID()) ? new SynopAnalyzerHandler(thing, locationProvider, stationDB)
79 protected void activate(ComponentContext componentContext) {
80 super.activate(componentContext);
82 try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("/db/stations.json");
83 Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);) {
85 StationDB stations = gson.fromJson(reader, StationDB.class);
86 registerDiscoveryService(stations);
87 this.stationDB = stations;
88 logger.debug("Discovery service for Synop Stations registered.");
89 } catch (IOException e) {
90 logger.warn("Unable to read synop stations database");
95 protected void deactivate(ComponentContext componentContext) {
96 unregisterDiscoveryService();
97 super.deactivate(componentContext);
100 private void registerDiscoveryService(StationDB stations) {
101 SynopAnalyzerDiscoveryService discoveryService = new SynopAnalyzerDiscoveryService(stations, locationProvider);
103 serviceReg = bundleContext.registerService(DiscoveryService.class.getName(), discoveryService,
107 private void unregisterDiscoveryService() {
108 if (serviceReg != null) {
109 serviceReg.unregister();