2 * Copyright (c) 2010-2021 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.util.Collections;
22 import java.util.Hashtable;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.synopanalyser.internal.discovery.SynopAnalyzerDiscoveryService;
28 import org.openhab.binding.synopanalyser.internal.synop.StationDB;
29 import org.openhab.binding.synopanalyzer.internal.handler.SynopAnalyzerHandler;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.i18n.LocationProvider;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerFactory;
37 import org.osgi.framework.ServiceRegistration;
38 import org.osgi.service.component.ComponentContext;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.google.gson.Gson;
48 * The {@link SynopAnalyzerHandlerFactory} is responsible for creating things and thing
51 * @author Gaƫl L'hopital - Initial contribution
54 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.synopanalyzer")
56 public class SynopAnalyzerHandlerFactory extends BaseThingHandlerFactory {
57 private final Logger logger = LoggerFactory.getLogger(SynopAnalyzerHandlerFactory.class);
58 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_SYNOP);
59 private final LocationProvider locationProvider;
60 private final Gson gson;
61 private @NonNullByDefault({}) StationDB stationDB;
62 private @Nullable ServiceRegistration<?> serviceReg;
65 public SynopAnalyzerHandlerFactory(@Reference LocationProvider locationProvider) {
66 this.locationProvider = locationProvider;
67 this.gson = new Gson();
71 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
76 protected @Nullable ThingHandler createHandler(Thing thing) {
77 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
79 return thingTypeUID.equals(THING_SYNOP) ? new SynopAnalyzerHandler(thing, locationProvider, stationDB) : null;
83 protected void activate(ComponentContext componentContext) {
84 super.activate(componentContext);
86 try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("/db/stations.json");
87 Reader reader = new InputStreamReader(is, "UTF-8");) {
89 stationDB = gson.fromJson(reader, StationDB.class);
90 registerDiscoveryService();
91 logger.debug("Discovery service for Synop Stations registered.");
92 } catch (IOException e) {
93 logger.warn("Unable to read synop stations database");
94 stationDB = new StationDB();
99 protected void deactivate(ComponentContext componentContext) {
100 unregisterDiscoveryService();
101 super.deactivate(componentContext);
104 private void registerDiscoveryService() {
105 SynopAnalyzerDiscoveryService discoveryService = new SynopAnalyzerDiscoveryService(stationDB, locationProvider);
107 serviceReg = bundleContext.registerService(DiscoveryService.class.getName(), discoveryService,
111 private void unregisterDiscoveryService() {
112 if (serviceReg != null) {
113 serviceReg.unregister();