]> git.basschouten.com Git - openhab-addons.git/blob
63ad5c71e00c207aed9023e87a5a4b961d9a4950
[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.enocean.internal.eep.D2_06;
14
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
16
17 import java.util.function.Function;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.enocean.internal.eep.Base._VLDMessage;
22 import org.openhab.binding.enocean.internal.messages.ERP1Message;
23 import org.openhab.core.config.core.Configuration;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
29
30 /**
31  *
32  * @author Thomas Lauterbach - Initial contribution
33  */
34 @NonNullByDefault
35 public class D2_06_50 extends _VLDMessage {
36
37     public D2_06_50() {
38         super();
39     }
40
41     public D2_06_50(ERP1Message packet) {
42         super(packet);
43     }
44
45     protected State getWindowSashState() {
46         int sashState = bytes[1] & 0x7f;
47         if (sashState == 0x00) {
48             return UnDefType.UNDEF;
49         }
50         if (sashState < 0x04) {
51             return new StringType("CLOSED");
52         } else if (sashState < 0x07) {
53             return new StringType("OPEN");
54         } else if (sashState < 0x0A) {
55             return new StringType("TILTED");
56         }
57
58         return UnDefType.UNDEF;
59     }
60
61     protected State getWindowHandleState() {
62         int handleState = bytes[1] & 0x7f;
63         if (handleState == 0x01 || handleState == 0x04 || handleState == 0x07) {
64             return new StringType("CLOSED");
65         } else if (handleState == 0x02 || handleState == 0x05 || handleState == 0x08) {
66             return new StringType("OPEN");
67         } else if (handleState == 0x03 || handleState == 0x06 || handleState == 0x09) {
68             return new StringType("TILTED");
69         }
70
71         return UnDefType.UNDEF;
72     }
73
74     protected State getCalibrationState() {
75         int calibrationState = bytes[1] >>> 6;
76         if (calibrationState == 0x00) {
77             return new StringType("OK");
78         } else if (calibrationState == 0x01) {
79             return new StringType("ERROR");
80         } else if (calibrationState == 0x02) {
81             return new StringType("INVALID");
82         }
83
84         return UnDefType.UNDEF;
85     }
86
87     protected State getCalibrationStep() {
88         int calibrationStep = bytes[1] & 0x3F;
89         switch (calibrationStep) {
90             case 0x00:
91                 return new StringType("NONE");
92             case 0x01:
93                 return new StringType("SASH CLOSED HANDLE CLOSED");
94             case 0x02:
95                 return new StringType("SASH CLOSED HANDLE OPEN");
96             case 0x03:
97                 return new StringType("SASH CLOSED HANDLE TILTED");
98             case 0x04:
99                 return new StringType("SASH OPEN HANDLE CLOSED");
100             case 0x05:
101                 return new StringType("SASH OPEN HANDLE OPEN");
102             case 0x06:
103                 return new StringType("SASH OPEN HANDLE TILTED");
104             case 0x07:
105                 return new StringType("SASH TILTED HANDLE CLOSED");
106             case 0x08:
107                 return new StringType("SASH TILTED HANDLE OPEN");
108             case 0x09:
109                 return new StringType("SASH TILTED HANDLE TILTED");
110             case 0x0A:
111                 return new StringType("FRAME MAGNET VALIDATION");
112         }
113
114         return UnDefType.UNDEF;
115     }
116
117     @Override
118     protected @Nullable String convertToEventImpl(String channelId, String channelTypeId, String lastEvent,
119             Configuration config) {
120         // Alarm
121         if (bytes[0] == 0x02) {
122             switch (channelId) {
123                 case CHANNEL_WINDOWBREACHEVENT:
124                     if (bytes[1] == 0x01) {
125                         return "ALARM";
126                     }
127             }
128         }
129         return null;
130     }
131
132     @Override
133     public State convertToStateImpl(String channelId, String channelTypeId, Function<String, State> getCurrentStateFunc,
134             Configuration config) {
135         // Window status
136         if (bytes[0] == 0x01) {
137             switch (channelId) {
138                 case CHANNEL_WINDOWSASHSTATE:
139                     return getWindowSashState();
140                 case CHANNEL_WINDOWHANDLESTATE:
141                     return getWindowHandleState();
142                 case CHANNEL_BATTERY_LEVEL:
143                     return new DecimalType(bytes[6] & 0x7f);
144                 case CHANNEL_BATTERYLOW:
145                     return getBit(bytes[6], 7) ? OnOffType.ON : OnOffType.OFF;
146             }
147         }
148
149         // Calibration
150         if (bytes[0] == 0x11) {
151             switch (channelId) {
152                 case CHANNEL_WINDOWCALIBRATIONSTATE:
153                     return getCalibrationState();
154                 case CHANNEL_WINDOWCALIBRATIONSTEP:
155                     return getCalibrationStep();
156             }
157         }
158
159         return UnDefType.UNDEF;
160     }
161 }