]> git.basschouten.com Git - openhab-addons.git/blob
12830098d75b2069675d8620e927a701289fc9f3
[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 ElroConnectsDeviceGenericAlarm} is representing a generic ELRO Connects Alarm device.
36  *
37  * @author Mark Herwege - Initial contribution
38  */
39 @NonNullByDefault
40 public class ElroConnectsDeviceGenericAlarm extends ElroConnectsDevice {
41
42     private final Logger logger = LoggerFactory.getLogger(ElroConnectsDeviceGenericAlarm.class);
43
44     // device states
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";
50
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);
56
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));
61
62     // device commands
63     protected static final String CMD_TEST = "BB000000";
64     protected static final String CMD_SILENCE = "50000000";
65
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));
71     }
72
73     @Override
74     public void muteAlarm() {
75         try {
76             bridge.deviceControl(deviceId, CMD_SILENCE);
77         } catch (IOException e) {
78             logger.debug("Failed to control device: {}", e.getMessage());
79         }
80     }
81
82     @Override
83     public void testAlarm() {
84         try {
85             ElroDeviceStatus elroStatus = getStatus();
86             if (!(ElroDeviceStatus.TRIGGERED.equals(elroStatus) || ElroDeviceStatus.TEST.equals(elroStatus))) {
87                 bridge.deviceControl(deviceId, CMD_TEST);
88             }
89         } catch (IOException e) {
90             logger.debug("Failed to control device: {}", e.getMessage());
91         }
92     }
93
94     @Override
95     public void updateState() {
96         ElroConnectsDeviceHandler handler = getHandler();
97         if (handler == null) {
98             return;
99         }
100
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);
109         } else {
110             elroStatus = ElroDeviceStatus.FAULT;
111             logger.debug("Could not decode device status: {}", deviceStatus);
112         }
113
114         switch (elroStatus) {
115             case UNDEF:
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);
121                 break;
122             case FAULT:
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);
128                 break;
129             default:
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);
134         }
135     }
136
137     @Override
138     public void switchState(boolean state) {
139         // nothing
140     }
141 }