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