]> git.basschouten.com Git - openhab-addons.git/blob
b88e8da1da65974a780a2564f77bd7aa517c23ea
[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.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.nio.charset.StandardCharsets;
22 import java.util.Hashtable;
23
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;
43
44 import com.google.gson.Gson;
45
46 /**
47  * The {@link SynopAnalyzerHandlerFactory} is responsible for creating things and thing
48  * handlers.
49  *
50  * @author GaĆ«l L'hopital - Initial contribution
51  */
52
53 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.synopanalyzer")
54 @NonNullByDefault
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;
61
62     @Activate
63     public SynopAnalyzerHandlerFactory(@Reference LocationProvider locationProvider) {
64         this.locationProvider = locationProvider;
65     }
66
67     @Override
68     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
69         return THING_SYNOP.equals(thingTypeUID);
70     }
71
72     @Override
73     protected @Nullable ThingHandler createHandler(Thing thing) {
74         return supportsThingType(thing.getThingTypeUID()) ? new SynopAnalyzerHandler(thing, locationProvider, stationDB)
75                 : null;
76     }
77
78     @Override
79     protected void activate(ComponentContext componentContext) {
80         super.activate(componentContext);
81
82         try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("/db/stations.json");
83                 Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);) {
84
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");
91         }
92     }
93
94     @Override
95     protected void deactivate(ComponentContext componentContext) {
96         unregisterDiscoveryService();
97         super.deactivate(componentContext);
98     }
99
100     private void registerDiscoveryService(StationDB stations) {
101         SynopAnalyzerDiscoveryService discoveryService = new SynopAnalyzerDiscoveryService(stations, locationProvider);
102
103         serviceReg = bundleContext.registerService(DiscoveryService.class.getName(), discoveryService,
104                 new Hashtable<>());
105     }
106
107     private void unregisterDiscoveryService() {
108         if (serviceReg != null) {
109             serviceReg.unregister();
110             serviceReg = null;
111         }
112     }
113 }