2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.onewire.internal.device;
15 import java.util.HashSet;
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;
29 * The {@link AbstractOwDevice} class defines an abstract onewire device
31 * @author Jan N. Klug - Initial contribution
34 public abstract class AbstractOwDevice {
35 private final Logger logger = LoggerFactory.getLogger(AbstractOwDevice.class);
37 protected SensorId sensorId;
38 protected OwSensorType sensorType;
39 protected OwBaseThingHandler callback;
40 protected Boolean isConfigured = false;
42 protected Set<String> enabledChannels = new HashSet<>();
45 * constructor for the onewire device
47 * @param sensorId onewire ID of the sensor
48 * @param callback ThingHandler callback for posting updates
50 public AbstractOwDevice(SensorId sensorId, OwBaseThingHandler callback) {
51 this.sensorId = sensorId;
52 this.callback = callback;
53 this.sensorType = OwSensorType.UNKNOWN;
56 public AbstractOwDevice(SensorId sensorId, OwSensorType sensorType, OwBaseThingHandler callback) {
57 this.sensorId = sensorId;
58 this.callback = callback;
59 this.sensorType = sensorType;
63 * configures the onewire devices channels
66 public abstract void configureChannels() throws OwException;
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
75 public abstract void refresh(OwserverBridgeHandler owBridgeHandler, Boolean forcedRefresh) throws OwException;
78 * enables a channel on this device
80 * @param channelID the channels channelID
82 public void enableChannel(String channelID) {
83 enabledChannels.add(channelID);
87 * disables a channel on this device
89 * @param channelID the channels channelID
91 public void disableChannel(String channelID) {
92 enabledChannels.remove(channelID);
96 * get onewire ID of this sensor
100 public SensorId getSensorId() {
105 * check sensor presence and update thing state
107 * @param bridgeHandler
108 * @return sensors presence state
110 public Boolean checkPresence(OwserverBridgeHandler bridgeHandler) {
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());