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 ElroConnectsDeviceGenericAlarm} is representing a generic ELRO Connects Alarm device.
37 * @author Mark Herwege - Initial contribution
40 public class ElroConnectsDeviceGenericAlarm extends ElroConnectsDevice {
42 private final Logger logger = LoggerFactory.getLogger(ElroConnectsDeviceGenericAlarm.class);
45 private static final String STAT_ALARM = "55";
46 private static final String STAT_TEST = "BB";
47 private static final String STAT_FAULT = "11";
48 private static final String STAT_SILENCE = "50";
49 private static final String STAT_NORMAL = "AA";
51 private static final Set<String> T_ALARM = Set.of(STAT_ALARM);
52 private static final Set<String> T_TEST = Set.of(STAT_TEST);
53 private static final Set<String> T_FAULT = Set.of(STAT_FAULT);
54 private static final Set<String> T_SILENCE = Set.of(STAT_SILENCE);
55 private static final Set<String> T_NORMAL = Set.of(STAT_NORMAL);
57 private static final Map<ElroDeviceStatus, Set<String>> DEVICE_STATUS_MAP = Map.ofEntries(
58 Map.entry(ElroDeviceStatus.NORMAL, T_NORMAL), Map.entry(ElroDeviceStatus.TRIGGERED, T_ALARM),
59 Map.entry(ElroDeviceStatus.TEST, T_TEST), Map.entry(ElroDeviceStatus.SILENCE, T_SILENCE),
60 Map.entry(ElroDeviceStatus.FAULT, T_FAULT));
63 protected static final String CMD_TEST = "BB000000";
64 protected static final String CMD_SILENCE = "50000000";
66 public ElroConnectsDeviceGenericAlarm(int deviceId, ElroConnectsBridgeHandler bridge) {
67 super(deviceId, bridge);
68 statusMap = DEVICE_STATUS_MAP.entrySet().stream()
69 .flatMap(e -> e.getValue().stream().map(v -> Map.entry(v, e.getKey())))
70 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
74 public void muteAlarm() {
76 bridge.deviceControl(deviceId, CMD_SILENCE);
77 } catch (IOException e) {
78 logger.debug("Failed to control device: {}", e.getMessage());
83 public void testAlarm() {
85 ElroDeviceStatus elroStatus = getStatus();
86 if (!(ElroDeviceStatus.TRIGGERED.equals(elroStatus) || ElroDeviceStatus.TEST.equals(elroStatus))) {
87 bridge.deviceControl(deviceId, CMD_TEST);
89 } catch (IOException e) {
90 logger.debug("Failed to control device: {}", e.getMessage());
95 public void updateState() {
96 ElroConnectsDeviceHandler handler = getHandler();
97 if (handler == null) {
101 ElroDeviceStatus elroStatus = getStatus();
102 int signalStrength = 0;
103 int batteryLevel = 0;
104 String deviceStatus = this.deviceStatus;
105 if (deviceStatus.length() >= 6) {
106 signalStrength = Integer.parseInt(deviceStatus.substring(0, 2), 16);
107 signalStrength = (signalStrength > 4) ? 4 : ((signalStrength < 0) ? 0 : signalStrength);
108 batteryLevel = Integer.parseInt(deviceStatus.substring(2, 4), 16);
110 elroStatus = ElroDeviceStatus.FAULT;
111 logger.debug("Could not decode device status: {}", deviceStatus);
114 switch (elroStatus) {
116 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
117 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
118 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
119 String msg = String.format("@text/offline.device-not-syncing [ \"%d\" ]", deviceId);
120 handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, msg);
123 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
124 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
125 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
126 msg = String.format("@text/offline.device-fault [ \"%d\" ]", deviceId);
127 handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, msg);
130 handler.updateState(SIGNAL_STRENGTH, new DecimalType(signalStrength));
131 handler.updateState(BATTERY_LEVEL, new DecimalType(batteryLevel));
132 handler.updateState(LOW_BATTERY, (batteryLevel < 15) ? OnOffType.ON : OnOffType.OFF);
133 handler.updateStatus(ThingStatus.ONLINE);
138 public void switchState(boolean state) {