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