]> git.basschouten.com Git - openhab-addons.git/blob
b12ec2a079e6d7c6eedda6fc54a477d0a506a99e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.ambientweather.internal.processor;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.core.thing.Thing;
18
19 import com.google.gson.Gson;
20
21 /**
22  * The {@link ProcessorFactory} is responsible for returning the right
23  * processor for handling info update and weather data events.
24  *
25  * There is one processor for each supported station type.
26  * To add support for a new station type, this class needs to be modified to
27  * return a processor for the new thing type. In addition, an
28  * AmbientWeatherXXXXXProcessor class needs to be created for the new station.
29  *
30  * @author Mark Hilbush - Initial contribution
31  */
32 @NonNullByDefault
33 public class ProcessorFactory {
34     // Single Gson instance shared by processors
35     private static final Gson GSON = new Gson();
36
37     // Supported weather stations
38     private @Nullable static Ws1400ipProcessor WS1400IP_PROCESSOR;
39     private @Nullable static Ws2902aProcessor WS2902A_PROCESSOR;
40     private @Nullable static Ws8482Processor WS8482_PROCESSOR;
41     private @Nullable static Ws0900ipProcessor WS0900IP_PROCESSOR;
42     private @Nullable static Ws0265Processor WS0265_PROCESSOR;
43
44     /**
45      * Individual weather station processors use this one Gson instance,
46      * rather than create their own.
47      *
48      * @return instance of a Gson object
49      */
50     public static Gson getGson() {
51         return (GSON);
52     }
53
54     /**
55      * Get a processor for a specific weather station type.
56      *
57      * @param thing
58      * @return instance of a weather station processor
59      * @throws ProcessorNotFoundException
60      */
61     public static AbstractProcessor getProcessor(Thing thing) throws ProcessorNotFoundException {
62         // Return the processor for this thing type
63         String thingType = thing.getThingTypeUID().getAsString().toLowerCase();
64         switch (thingType) {
65             case "ambientweather:ws1400ip": {
66                 Ws1400ipProcessor processor = WS1400IP_PROCESSOR;
67                 if (processor == null) {
68                     processor = new Ws1400ipProcessor();
69                     WS1400IP_PROCESSOR = processor;
70                 }
71                 return processor;
72             }
73             case "ambientweather:ws2902a": {
74                 Ws2902aProcessor processor = WS2902A_PROCESSOR;
75                 if (processor == null) {
76                     processor = new Ws2902aProcessor();
77                     WS2902A_PROCESSOR = processor;
78                 }
79                 return processor;
80             }
81             case "ambientweather:ws8482": {
82                 Ws8482Processor processor = WS8482_PROCESSOR;
83                 if (processor == null) {
84                     processor = new Ws8482Processor();
85                     WS8482_PROCESSOR = processor;
86                 }
87                 return processor;
88             }
89             case "ambientweather:ws0900ip": {
90                 Ws0900ipProcessor processor = WS0900IP_PROCESSOR;
91                 if (processor == null) {
92                     processor = new Ws0900ipProcessor();
93                     WS0900IP_PROCESSOR = processor;
94                 }
95                 return processor;
96             }
97             case "ambientweather:ws0265": {
98                 Ws0265Processor processor = WS0265_PROCESSOR;
99                 if (processor == null) {
100                     processor = new Ws0265Processor();
101                     WS0265_PROCESSOR = processor;
102                 }
103                 return processor;
104             }
105         }
106         throw new ProcessorNotFoundException("No processor for thing type " + thingType);
107     }
108 }