]> git.basschouten.com Git - openhab-addons.git/blob
97f0696cee9994f94cdaa3a876c668fcab061827
[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.onewire.internal.device;
14
15 import java.util.HashSet;
16 import java.util.Set;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.onewire.internal.OwException;
20 import org.openhab.binding.onewire.internal.SensorId;
21 import org.openhab.binding.onewire.internal.handler.OwBaseThingHandler;
22 import org.openhab.binding.onewire.internal.handler.OwserverBridgeHandler;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.types.State;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * The {@link AbstractOwClass} class defines an abstract onewire device
30  *
31  * @author Jan N. Klug - Initial contribution
32  */
33 @NonNullByDefault
34 public abstract class AbstractOwDevice {
35     private final Logger logger = LoggerFactory.getLogger(AbstractOwDevice.class);
36
37     protected SensorId sensorId;
38     protected OwSensorType sensorType;
39     protected OwBaseThingHandler callback;
40     protected Boolean isConfigured = false;
41
42     protected Set<String> enabledChannels = new HashSet<>();
43
44     /**
45      * constructor for the onewire device
46      *
47      * @param sensorId onewire ID of the sensor
48      * @param callback ThingHandler callback for posting updates
49      */
50     public AbstractOwDevice(SensorId sensorId, OwBaseThingHandler callback) {
51         this.sensorId = sensorId;
52         this.callback = callback;
53         this.sensorType = OwSensorType.UNKNOWN;
54     }
55
56     public AbstractOwDevice(SensorId sensorId, OwSensorType sensorType, OwBaseThingHandler callback) {
57         this.sensorId = sensorId;
58         this.callback = callback;
59         this.sensorType = sensorType;
60     }
61
62     /**
63      * configures the onewire devices channels
64      *
65      */
66     public abstract void configureChannels() throws OwException;
67
68     /**
69      * refresh this sensor
70      *
71      * @param bridgeHandler for sending requests
72      * @param forcedRefresh post update even if state did not change
73      * @throws OwException in case of communication error
74      */
75     public abstract void refresh(OwserverBridgeHandler owBridgeHandler, Boolean forcedRefresh) throws OwException;
76
77     /**
78      * enables a channel on this device
79      *
80      * @param channelID the channels channelID
81      */
82     public void enableChannel(String channelID) {
83         if (!enabledChannels.contains(channelID)) {
84             enabledChannels.add(channelID);
85         }
86     }
87
88     /**
89      * disables a channel on this device
90      *
91      * @param channelID the channels channelID
92      */
93     public void disableChannel(String channelID) {
94         if (enabledChannels.contains(channelID)) {
95             enabledChannels.remove(channelID);
96         }
97     }
98
99     /**
100      * get onewire ID of this sensor
101      *
102      * @return sensor ID
103      */
104     public SensorId getSensorId() {
105         return sensorId;
106     }
107
108     /**
109      * check sensor presence and update thing state
110      *
111      * @param owServerConnection
112      * @return sensors presence state
113      */
114
115     public Boolean checkPresence(OwserverBridgeHandler bridgeHandler) {
116         try {
117             State present = bridgeHandler.checkPresence(sensorId);
118             callback.updatePresenceStatus(present);
119             return OnOffType.ON.equals(present);
120         } catch (OwException e) {
121             logger.debug("error refreshing presence {} on bridge {}: {}", this.sensorId,
122                     bridgeHandler.getThing().getUID(), e.getMessage());
123             return false;
124         }
125     }
126
127     /**
128      * get this sensors type
129      *
130      * @param bridgeHandler bridge handler to request from if type formerly unknown
131      * @return this sensors type
132      * @throws OwException
133      */
134     public OwSensorType getSensorType(OwserverBridgeHandler bridgeHandler) throws OwException {
135         if (sensorType == OwSensorType.UNKNOWN) {
136             sensorType = bridgeHandler.getType(sensorId);
137         }
138         return sensorType;
139     }
140 }