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.elroconnects.internal.devices;
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.*;
17 import java.io.IOException;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.ElroDeviceStatus;
23 import org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.ElroDeviceType;
24 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsBridgeHandler;
25 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsDeviceHandler;
26 import org.openhab.binding.elroconnects.internal.util.ElroConnectsUtil;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link ElroConnectsDevice} is an abstract class representing all basic properties for ELRO Connects devices.
32 * Concrete subclasses will contain specific logic for each device type.
34 * @author Mark Herwege - Initial contribution
37 public abstract class ElroConnectsDevice {
39 private final Logger logger = LoggerFactory.getLogger(ElroConnectsDevice.class);
41 // minimum data to create an instance of the class
42 protected int deviceId;
43 protected ElroConnectsBridgeHandler bridge;
45 protected volatile String deviceName = "";
46 protected volatile String deviceType = "";
47 protected volatile String deviceStatus = "";
49 protected volatile Map<String, ElroDeviceStatus> statusMap = Map.of();
52 * Create a new instance of a subclass of {@link ElroConnectsDevice}. These instances get created by an instance
53 * {@link ElroConnectsBridgeHandler}. The deviceId will be set on creation. Other fields will be set as and when the
54 * information is received from the K1 hub.
59 public ElroConnectsDevice(int deviceId, ElroConnectsBridgeHandler bridge) {
60 this.deviceId = deviceId;
65 * Get the current status of the device.
69 protected ElroDeviceStatus getStatus() {
70 String deviceStatus = this.deviceStatus;
71 ElroDeviceStatus elroStatus = ElroDeviceStatus.UNDEF;
73 if (deviceStatus.length() >= 6) {
74 elroStatus = statusMap.getOrDefault(deviceStatus.substring(4, 6), ElroDeviceStatus.UNDEF);
80 public void setDeviceName(String deviceName) {
81 this.deviceName = deviceName;
84 public void updateDeviceName(String deviceName) {
86 if (!ElroConnectsUtil.equals(getDeviceName(), deviceName, 15)) {
87 bridge.renameDevice(deviceId, deviceName);
88 setDeviceName(deviceName);
90 } catch (IOException e) {
91 logger.debug("Failed to update device name: {}", e.getMessage());
95 public void setDeviceType(String deviceType) {
96 this.deviceType = deviceType;
99 public void setDeviceStatus(String deviceStatus) {
100 this.deviceStatus = deviceStatus;
103 public String getDeviceName() {
104 String typeName = null;
105 ElroDeviceType type = TYPE_MAP.get(getDeviceType());
107 typeName = TYPE_NAMES.get(type);
109 if (typeName == null) {
110 typeName = getDeviceType();
113 return deviceName.isEmpty() ? typeName + "-" + String.valueOf(deviceId) : deviceName;
116 public String getDeviceType() {
121 * Retrieve the {@link ElroConnectsDeviceHandler} for device.
123 * @return handler for the device.
125 protected @Nullable ElroConnectsDeviceHandler getHandler() {
126 return bridge.getDeviceHandler(deviceId);
130 * Update all {@link ElroConnectsDeviceHandler} channel states with information received from the device. This
131 * method needs to be implemented in the concrete subclass when any state updates are received from the device.
133 public abstract void updateState();
136 * Send alarm test message to the device. This method is called from the {@link ElroConnectsDeviceHandler}. The
137 * method needs to be implemented in the concrete subclass when test alarms are supported.
139 public abstract void testAlarm();
142 * Send alarm mute message to the device. This method is called from the {@link ElroConnectsDeviceHandler}. The
143 * method needs to be implemented in the concrete subclass when alarm muting is supported.
145 public abstract void muteAlarm();
148 * Send state switch message to the device. This method is called from the {@link ElroConnectsDeviceHandler}. The
149 * method needs to be implemented in the concrete subclass when switching the state on/off is supported.
151 public abstract void switchState(boolean state);