]> git.basschouten.com Git - openhab-addons.git/blob
37da7dd3cc2e0a0d9a0c34d00f1b0bb2ca4096ea
[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.util.Map;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.ElroDeviceStatus;
23 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsBridgeHandler;
24 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsDeviceHandler;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.OpenClosedType;
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 ElroConnectsDeviceEntrySensor} is representing a generic ELRO Connects Entry Sensor device.
36  *
37  * @author Mark Herwege - Initial contribution
38  */
39 @NonNullByDefault
40 public class ElroConnectsDeviceEntrySensor extends ElroConnectsDevice {
41
42     private final Logger logger = LoggerFactory.getLogger(ElroConnectsDeviceEntrySensor.class);
43
44     // device states
45     private static final String STAT_OPEN = "55";
46     private static final String STAT_STILL_OPEN = "66";
47     private static final String STAT_FAULT = "11";
48     private static final String STAT_NORMAL = "AA";
49
50     private static final Set<String> T_OPEN = Set.of(STAT_OPEN, STAT_STILL_OPEN);
51     private static final Set<String> T_FAULT = Set.of(STAT_FAULT);
52     private static final Set<String> T_NORMAL = Set.of(STAT_NORMAL);
53
54     private static final Map<ElroDeviceStatus, Set<String>> DEVICE_STATUS_MAP = Map.ofEntries(
55             Map.entry(ElroDeviceStatus.NORMAL, T_NORMAL), Map.entry(ElroDeviceStatus.OPEN, T_OPEN),
56             Map.entry(ElroDeviceStatus.FAULT, T_FAULT));
57
58     public ElroConnectsDeviceEntrySensor(int deviceId, ElroConnectsBridgeHandler bridge) {
59         super(deviceId, bridge);
60         statusMap = DEVICE_STATUS_MAP.entrySet().stream()
61                 .flatMap(e -> e.getValue().stream().map(v -> Map.entry(v, e.getKey())))
62                 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
63     }
64
65     @Override
66     public void updateState() {
67         ElroConnectsDeviceHandler handler = getHandler();
68         if (handler == null) {
69             return;
70         }
71
72         ElroDeviceStatus elroStatus = getStatus();
73         int signalStrength = 0;
74         int batteryLevel = 0;
75         String deviceStatus = this.deviceStatus;
76         if (deviceStatus.length() >= 6) {
77             signalStrength = Integer.parseInt(deviceStatus.substring(0, 2), 16);
78             signalStrength = (signalStrength > 4) ? 4 : ((signalStrength < 0) ? 0 : signalStrength);
79             batteryLevel = Integer.parseInt(deviceStatus.substring(2, 4), 16);
80         } else {
81             elroStatus = ElroDeviceStatus.FAULT;
82             logger.debug("Could not decode device status: {}", deviceStatus);
83         }
84
85         switch (elroStatus) {
86             case UNDEF:
87                 handler.updateState(ENTRY, UnDefType.UNDEF);
88                 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
89                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
90                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
91                 String msg = String.format("@text/offline.device-not-syncing [ \"%d\" ]", deviceId);
92                 handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, msg);
93                 break;
94             case FAULT:
95                 handler.updateState(ENTRY, UnDefType.UNDEF);
96                 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
97                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
98                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
99                 msg = String.format("@text/offline.device-fault [ \"%d\" ]", deviceId);
100                 handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, msg);
101                 break;
102             default:
103                 handler.updateState(ENTRY,
104                         ElroDeviceStatus.OPEN.equals(elroStatus) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
105                 handler.updateState(SIGNAL_STRENGTH, new DecimalType(signalStrength));
106                 handler.updateState(BATTERY_LEVEL, new DecimalType(batteryLevel));
107                 handler.updateState(LOW_BATTERY, (batteryLevel < 15) ? OnOffType.ON : OnOffType.OFF);
108                 handler.updateStatus(ThingStatus.ONLINE);
109         }
110     }
111
112     @Override
113     public void testAlarm() {
114         // nothing
115     }
116
117     @Override
118     public void muteAlarm() {
119         // nothing
120     }
121
122     @Override
123     public void switchState(boolean state) {
124         // nothing
125     }
126 }