]> git.basschouten.com Git - openhab-addons.git/blob
e3a0c5b45d0a4ae441a9ccd878ee6690508fd431
[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.jeelink.internal;
14
15 import java.util.Set;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
18
19 import org.openhab.binding.jeelink.internal.ec3k.Ec3kSensorDefinition;
20 import org.openhab.binding.jeelink.internal.lacrosse.LaCrosseSensorDefinition;
21 import org.openhab.binding.jeelink.internal.lacrosse.LgwSensorDefinition;
22 import org.openhab.binding.jeelink.internal.lacrosse.Tx22SensorDefinition;
23 import org.openhab.binding.jeelink.internal.pca301.Pca301SensorDefinition;
24 import org.openhab.binding.jeelink.internal.revolt.RevoltSensorDefinition;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.binding.ThingHandler;
28
29 /**
30  * Base class for sensor definitions.
31  *
32  * @author Volker Bier - Initial contribution
33  *
34  * @param <R> the Reading type this sensor provides.
35  */
36 public abstract class SensorDefinition<R extends Reading> {
37     public static final String ALL_TYPE = "All";
38
39     private static final Set<SensorDefinition<?>> SENSOR_DEFS = Stream
40             .of(new LaCrosseSensorDefinition(), new Ec3kSensorDefinition(), new Pca301SensorDefinition(),
41                     new Tx22SensorDefinition(), new RevoltSensorDefinition(), new LgwSensorDefinition())
42             .collect(Collectors.toSet());
43     private static final Set<JeeLinkReadingConverter<?>> CONVERTERS = SENSOR_DEFS.stream()
44             .map(SensorDefinition::createConverter).collect(Collectors.toSet());
45
46     protected final String type;
47
48     final ThingTypeUID thingTypeUid;
49     final String name;
50
51     public SensorDefinition(ThingTypeUID thingTypeUid, String name, String type) {
52         this.thingTypeUid = thingTypeUid;
53         this.name = name;
54         this.type = type;
55     }
56
57     public String getName() {
58         return name;
59     }
60
61     public ThingTypeUID getThingTypeUID() {
62         return thingTypeUid;
63     }
64
65     public abstract Class<R> getReadingClass();
66
67     public abstract JeeLinkSensorHandler<R> createHandler(Thing thing);
68
69     public abstract JeeLinkReadingConverter<R> createConverter();
70
71     public static SensorDefinition<?> getSensorDefinition(Reading reading) {
72         for (SensorDefinition<?> sensor : SENSOR_DEFS) {
73             if (sensor.getReadingClass().equals(reading.getClass())) {
74                 return sensor;
75             }
76         }
77
78         return null;
79     }
80
81     public static Set<SensorDefinition<?>> getDefinitions() {
82         return SENSOR_DEFS;
83     }
84
85     public static ThingHandler createHandler(ThingTypeUID thingTypeUid, Thing thing) {
86         for (SensorDefinition<?> sensor : SENSOR_DEFS) {
87             if (sensor.getThingTypeUID().equals(thingTypeUid)) {
88                 return sensor.createHandler(thing);
89             }
90         }
91
92         return null;
93     }
94
95     public static JeeLinkReadingConverter<?> getConverter(String sensorType) {
96         for (SensorDefinition<?> sensor : SENSOR_DEFS) {
97             if (sensor.getSensorType().equals(sensorType)) {
98                 return sensor.createConverter();
99             }
100         }
101         return null;
102     }
103
104     public static Set<JeeLinkReadingConverter<?>> getDiscoveryConverters() {
105         return CONVERTERS;
106     }
107
108     private String getSensorType() {
109         return type;
110     }
111 }