]> git.basschouten.com Git - openhab-addons.git/blob
1f804bbfe94446fa8cee6de40ca5ad14ad65e970
[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.elroconnects.internal.devices;
14
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.*;
16
17 import java.io.IOException;
18 import java.util.Map;
19
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;
29
30 /**
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.
33  *
34  * @author Mark Herwege - Initial contribution
35  */
36 @NonNullByDefault
37 public abstract class ElroConnectsDevice {
38
39     private final Logger logger = LoggerFactory.getLogger(ElroConnectsDevice.class);
40
41     // minimum data to create an instance of the class
42     protected int deviceId;
43     protected ElroConnectsBridgeHandler bridge;
44
45     protected volatile String deviceName = "";
46     protected volatile String deviceType = "";
47     protected volatile String deviceStatus = "";
48
49     protected volatile Map<String, ElroDeviceStatus> statusMap = Map.of();
50
51     /**
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.
55      *
56      * @param deviceId
57      * @param bridge
58      */
59     public ElroConnectsDevice(int deviceId, ElroConnectsBridgeHandler bridge) {
60         this.deviceId = deviceId;
61         this.bridge = bridge;
62     }
63
64     /**
65      * Get the current status of the device.
66      *
67      * @return status
68      */
69     protected ElroDeviceStatus getStatus() {
70         String deviceStatus = this.deviceStatus;
71         ElroDeviceStatus elroStatus = ElroDeviceStatus.UNDEF;
72
73         if (deviceStatus.length() >= 6) {
74             elroStatus = statusMap.getOrDefault(deviceStatus.substring(4, 6), ElroDeviceStatus.UNDEF);
75         }
76
77         return elroStatus;
78     }
79
80     public void setDeviceName(String deviceName) {
81         this.deviceName = deviceName;
82     }
83
84     public void updateDeviceName(String deviceName) {
85         try {
86             if (!ElroConnectsUtil.equals(getDeviceName(), deviceName, 15)) {
87                 bridge.renameDevice(deviceId, deviceName);
88                 setDeviceName(deviceName);
89             }
90         } catch (IOException e) {
91             logger.debug("Failed to update device name: {}", e.getMessage());
92         }
93     }
94
95     public void setDeviceType(String deviceType) {
96         this.deviceType = deviceType;
97     }
98
99     public void setDeviceStatus(String deviceStatus) {
100         this.deviceStatus = deviceStatus;
101     }
102
103     public String getDeviceName() {
104         String typeName = null;
105         ElroDeviceType type = TYPE_MAP.get(getDeviceType());
106         if (type != null) {
107             typeName = TYPE_NAMES.get(type);
108         }
109         if (typeName == null) {
110             typeName = getDeviceType();
111         }
112
113         return deviceName.isEmpty() ? typeName + "-" + String.valueOf(deviceId) : deviceName;
114     }
115
116     public String getDeviceType() {
117         return deviceType;
118     }
119
120     /**
121      * Retrieve the {@link ElroConnectsDeviceHandler} for device.
122      *
123      * @return handler for the device.
124      */
125     protected @Nullable ElroConnectsDeviceHandler getHandler() {
126         return bridge.getDeviceHandler(deviceId);
127     }
128
129     /**
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.
132      */
133     public abstract void updateState();
134
135     /**
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.
138      */
139     public abstract void testAlarm();
140
141     /**
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.
144      */
145     public abstract void muteAlarm();
146
147     /**
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.
150      */
151     public abstract void switchState(boolean state);
152 }