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 java.util.stream.Collectors;
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;
35 * The {@link ElroConnectsDeviceCxsmAlarm} is representing an ELRO Connects Cxsm Alarm device.
37 * @author Mark Herwege - Initial contribution
40 public class ElroConnectsDeviceCxsmAlarm extends ElroConnectsDevice {
42 private final Logger logger = LoggerFactory.getLogger(ElroConnectsDeviceCxsmAlarm.class);
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";
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);
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));
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";
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));
76 public void muteAlarm() {
78 ElroDeviceStatus elroStatus = getStatus();
79 if (ElroDeviceStatus.FAULT.equals(elroStatus)) {
80 bridge.deviceControl(deviceId, CMD_SILENCE_2);
82 bridge.deviceControl(deviceId, CMD_SILENCE_1);
84 } catch (IOException e) {
85 logger.debug("Failed to control device: {}", e.getMessage());
90 public void testAlarm() {
92 ElroDeviceStatus elroStatus = getStatus();
93 if (!(ElroDeviceStatus.TRIGGERED.equals(elroStatus) || ElroDeviceStatus.TEST.equals(elroStatus))) {
94 bridge.deviceControl(deviceId, CMD_TEST);
96 } catch (IOException e) {
97 logger.debug("Failed to control device: {}", e.getMessage());
102 public void updateState() {
103 ElroConnectsDeviceHandler handler = getHandler();
104 if (handler == null) {
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);
117 elroStatus = ElroDeviceStatus.FAULT;
118 logger.debug("Could not decode device status: {}", deviceStatus);
121 switch (elroStatus) {
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);
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);
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);
145 public void switchState(boolean state) {