]> git.basschouten.com Git - openhab-addons.git/blob
4b3ead3f644c833f79d3523eb26c5ceabccab633
[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 import java.util.Set;
20 import java.util.stream.Collectors;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.ElroDeviceStatus;
24 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsBridgeHandler;
25 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsDeviceHandler;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.types.UnDefType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The {@link ElroConnectsDeviceCxsmAlarm} is representing an ELRO Connects Cxsm Alarm device.
36  *
37  * @author Mark Herwege - Initial contribution
38  */
39 @NonNullByDefault
40 public class ElroConnectsDeviceCxsmAlarm extends ElroConnectsDevice {
41
42     private final Logger logger = LoggerFactory.getLogger(ElroConnectsDeviceCxsmAlarm.class);
43
44     // device states
45     private static final String STAT_ALARM = "19";
46     private static final String STAT_TEST = "17";
47     private static final String STAT_FAULT = "12";
48     private static final String STAT_SILENCE_1 = "1B";
49     private static final String STAT_SILENCE_2 = "15";
50     private static final String STAT_NORMAL = "AA";
51
52     private static final Set<String> T_ALARM = Set.of(STAT_ALARM);
53     private static final Set<String> T_TEST = Set.of(STAT_TEST);
54     private static final Set<String> T_FAULT = Set.of(STAT_FAULT);
55     private static final Set<String> T_SILENCE = Set.of(STAT_SILENCE_1, STAT_SILENCE_2);
56     private static final Set<String> T_NORMAL = Set.of(STAT_NORMAL);
57
58     private static final Map<ElroDeviceStatus, Set<String>> DEVICE_STATUS_MAP = Map.ofEntries(
59             Map.entry(ElroDeviceStatus.NORMAL, T_NORMAL), Map.entry(ElroDeviceStatus.TRIGGERED, T_ALARM),
60             Map.entry(ElroDeviceStatus.TEST, T_TEST), Map.entry(ElroDeviceStatus.SILENCE, T_SILENCE),
61             Map.entry(ElroDeviceStatus.FAULT, T_FAULT));
62
63     // device commands
64     private static final String CMD_TEST = "17000000";
65     private static final String CMD_SILENCE_1 = "1B000000";
66     private static final String CMD_SILENCE_2 = "15000000";
67
68     public ElroConnectsDeviceCxsmAlarm(int deviceId, ElroConnectsBridgeHandler bridge) {
69         super(deviceId, bridge);
70         statusMap = DEVICE_STATUS_MAP.entrySet().stream()
71                 .flatMap(e -> e.getValue().stream().map(v -> Map.entry(v, e.getKey())))
72                 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
73     }
74
75     @Override
76     public void muteAlarm() {
77         try {
78             ElroDeviceStatus elroStatus = getStatus();
79             if (ElroDeviceStatus.FAULT.equals(elroStatus)) {
80                 bridge.deviceControl(deviceId, CMD_SILENCE_2);
81             } else {
82                 bridge.deviceControl(deviceId, CMD_SILENCE_1);
83             }
84         } catch (IOException e) {
85             logger.debug("Failed to control device: {}", e.getMessage());
86         }
87     }
88
89     @Override
90     public void testAlarm() {
91         try {
92             ElroDeviceStatus elroStatus = getStatus();
93             if (!(ElroDeviceStatus.TRIGGERED.equals(elroStatus) || ElroDeviceStatus.TEST.equals(elroStatus))) {
94                 bridge.deviceControl(deviceId, CMD_TEST);
95             }
96         } catch (IOException e) {
97             logger.debug("Failed to control device: {}", e.getMessage());
98         }
99     }
100
101     @Override
102     public void updateState() {
103         ElroConnectsDeviceHandler handler = getHandler();
104         if (handler == null) {
105             return;
106         }
107
108         ElroDeviceStatus elroStatus = getStatus();
109         int signalStrength = 0;
110         int batteryLevel = 0;
111         String deviceStatus = this.deviceStatus;
112         if (deviceStatus.length() >= 6) {
113             signalStrength = Integer.parseInt(deviceStatus.substring(0, 2), 16);
114             signalStrength = (signalStrength > 4) ? 4 : ((signalStrength < 0) ? 0 : signalStrength);
115             batteryLevel = Integer.parseInt(deviceStatus.substring(2, 4), 16);
116         } else {
117             elroStatus = ElroDeviceStatus.FAULT;
118             logger.debug("Could not decode device status: {}", deviceStatus);
119         }
120
121         switch (elroStatus) {
122             case UNDEF:
123                 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
124                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
125                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
126                 String msg = String.format("@text/offline.device-not-syncing [ \"%d\" ]", deviceId);
127                 handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, msg);
128                 break;
129             case FAULT:
130                 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
131                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
132                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
133                 msg = String.format("@text/offline.device-fault [ \"%d\" ]", deviceId);
134                 handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, msg);
135                 break;
136             default:
137                 handler.updateState(SIGNAL_STRENGTH, new DecimalType(signalStrength));
138                 handler.updateState(BATTERY_LEVEL, new DecimalType(batteryLevel));
139                 handler.updateState(LOW_BATTERY, OnOffType.from(batteryLevel < 15));
140                 handler.updateStatus(ThingStatus.ONLINE);
141         }
142     }
143
144     @Override
145     public void switchState(boolean state) {
146         // nothing
147     }
148 }