]> git.basschouten.com Git - openhab-addons.git/blob
29db6ffc7dcfe74c94f0adfd2101580355487f0f
[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 AbstractOwDevice} 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 owBridgeHandler 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         enabledChannels.add(channelID);
84     }
85
86     /**
87      * disables a channel on this device
88      *
89      * @param channelID the channels channelID
90      */
91     public void disableChannel(String channelID) {
92         enabledChannels.remove(channelID);
93     }
94
95     /**
96      * get onewire ID of this sensor
97      *
98      * @return sensor ID
99      */
100     public SensorId getSensorId() {
101         return sensorId;
102     }
103
104     /**
105      * check sensor presence and update thing state
106      *
107      * @param bridgeHandler
108      * @return sensors presence state
109      */
110     public Boolean checkPresence(OwserverBridgeHandler bridgeHandler) {
111         try {
112             State present = bridgeHandler.checkPresence(sensorId);
113             callback.updatePresenceStatus(present);
114             return OnOffType.ON.equals(present);
115         } catch (OwException e) {
116             logger.debug("error refreshing presence {} on bridge {}: {}", this.sensorId,
117                     bridgeHandler.getThing().getUID(), e.getMessage());
118             return false;
119         }
120     }
121 }