]> git.basschouten.com Git - openhab-addons.git/blob
3edec5c9c387cc469a0984abf33b130f8810a2e1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.synopanalyzer.internal;
14
15 import static org.openhab.binding.synopanalyzer.internal.SynopAnalyzerBindingConstants.THING_SYNOP;
16
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;
23 import java.util.Set;
24
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;
44
45 import com.google.gson.Gson;
46
47 /**
48  * The {@link SynopAnalyzerHandlerFactory} is responsible for creating things and thing
49  * handlers.
50  *
51  * @author GaĆ«l L'hopital - Initial contribution
52  */
53
54 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.synopanalyzer")
55 @NonNullByDefault
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;
63
64     @Activate
65     public SynopAnalyzerHandlerFactory(@Reference LocationProvider locationProvider) {
66         this.locationProvider = locationProvider;
67         this.gson = new Gson();
68     }
69
70     @Override
71     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
73     }
74
75     @Override
76     protected @Nullable ThingHandler createHandler(Thing thing) {
77         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
78
79         return thingTypeUID.equals(THING_SYNOP) ? new SynopAnalyzerHandler(thing, locationProvider, stationDB) : null;
80     }
81
82     @Override
83     protected void activate(ComponentContext componentContext) {
84         super.activate(componentContext);
85
86         try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("/db/stations.json");
87                 Reader reader = new InputStreamReader(is, "UTF-8");) {
88
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();
95         }
96     }
97
98     @Override
99     protected void deactivate(ComponentContext componentContext) {
100         unregisterDiscoveryService();
101         super.deactivate(componentContext);
102     }
103
104     private void registerDiscoveryService() {
105         SynopAnalyzerDiscoveryService discoveryService = new SynopAnalyzerDiscoveryService(stationDB, locationProvider);
106
107         serviceReg = bundleContext.registerService(DiscoveryService.class.getName(), discoveryService,
108                 new Hashtable<>());
109     }
110
111     private void unregisterDiscoveryService() {
112         if (serviceReg != null) {
113             serviceReg.unregister();
114             serviceReg = null;
115         }
116     }
117 }