]> git.basschouten.com Git - openhab-addons.git/blob
3dcdd3bef1f61f74f6aa2b4d6b25a66498373e79
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 Ws2902bProcessor WS2902B_PROCESSOR;
41     private @Nullable static Ws8482Processor WS8482_PROCESSOR;
42     private @Nullable static Ws0900ipProcessor WS0900IP_PROCESSOR;
43     private @Nullable static Ws0265Processor WS0265_PROCESSOR;
44
45     /**
46      * Individual weather station processors use this one Gson instance,
47      * rather than create their own.
48      *
49      * @return instance of a Gson object
50      */
51     public static Gson getGson() {
52         return (GSON);
53     }
54
55     /**
56      * Get a processor for a specific weather station type.
57      *
58      * @param thing
59      * @return instance of a weather station processor
60      * @throws ProcessorNotFoundException
61      */
62     public static AbstractProcessor getProcessor(Thing thing) throws ProcessorNotFoundException {
63         // Return the processor for this thing type
64         String thingType = thing.getThingTypeUID().getAsString().toLowerCase();
65         switch (thingType) {
66             case "ambientweather:ws1400ip": {
67                 Ws1400ipProcessor processor = WS1400IP_PROCESSOR;
68                 if (processor == null) {
69                     processor = new Ws1400ipProcessor();
70                     WS1400IP_PROCESSOR = processor;
71                 }
72                 return processor;
73             }
74             case "ambientweather:ws2902a": {
75                 Ws2902aProcessor processor = WS2902A_PROCESSOR;
76                 if (processor == null) {
77                     processor = new Ws2902aProcessor();
78                     WS2902A_PROCESSOR = processor;
79                 }
80                 return processor;
81             }
82             case "ambientweather:ws2902b": {
83                 Ws2902bProcessor processor = WS2902B_PROCESSOR;
84                 if (processor == null) {
85                     processor = new Ws2902bProcessor();
86                     WS2902B_PROCESSOR = processor;
87                 }
88                 return processor;
89             }
90             case "ambientweather:ws8482": {
91                 Ws8482Processor processor = WS8482_PROCESSOR;
92                 if (processor == null) {
93                     processor = new Ws8482Processor();
94                     WS8482_PROCESSOR = processor;
95                 }
96                 return processor;
97             }
98             case "ambientweather:ws0900ip": {
99                 Ws0900ipProcessor processor = WS0900IP_PROCESSOR;
100                 if (processor == null) {
101                     processor = new Ws0900ipProcessor();
102                     WS0900IP_PROCESSOR = processor;
103                 }
104                 return processor;
105             }
106             case "ambientweather:ws0265": {
107                 Ws0265Processor processor = WS0265_PROCESSOR;
108                 if (processor == null) {
109                     processor = new Ws0265Processor();
110                     WS0265_PROCESSOR = processor;
111                 }
112                 return processor;
113             }
114         }
115         throw new ProcessorNotFoundException("No processor for thing type " + thingType);
116     }
117 }