]> git.basschouten.com Git - openhab-addons.git/blob
44e3fb34dfbf19e0b305055bf9c01662df972351
[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.echonetlite.internal;
14
15 import static org.openhab.binding.echonetlite.internal.HexUtil.hex;
16 import static org.openhab.binding.echonetlite.internal.LangUtil.b;
17
18 import java.nio.ByteBuffer;
19 import java.nio.ByteOrder;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.Objects;
23 import java.util.concurrent.TimeUnit;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.types.StringType;
31 import org.openhab.core.library.unit.SIUnits;
32 import org.openhab.core.library.unit.Units;
33 import org.openhab.core.types.State;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * @author Michael Barker - Initial contribution
39  */
40 @NonNullByDefault
41 public interface StateCodec extends StateEncode, StateDecode {
42
43     class OnOffCodec implements StateCodec {
44         private final int on;
45         private final int off;
46
47         public OnOffCodec(int on, int off) {
48             this.on = on;
49             this.off = off;
50         }
51
52         public State decodeState(final ByteBuffer edt) {
53             return b(on) == edt.get() ? OnOffType.ON : OnOffType.OFF;
54         }
55
56         public void encodeState(final State state, final ByteBuffer edt) {
57             final OnOffType onOff = (OnOffType) state;
58             edt.put(onOff == OnOffType.ON ? b(on) : b(off));
59         }
60
61         public String itemType() {
62             return "Switch";
63         }
64     }
65
66     enum StandardVersionInformationCodec implements StateDecode {
67
68         INSTANCE;
69
70         public State decodeState(final ByteBuffer edt) {
71             final int pdc = edt.remaining();
72             if (pdc != 4) {
73                 return StringType.EMPTY;
74             }
75
76             return new StringType("" + (char) edt.get(edt.position() + 2));
77         }
78
79         public String itemType() {
80             return "String";
81         }
82     }
83
84     enum HexStringCodec implements StateDecode {
85
86         INSTANCE;
87
88         public State decodeState(final ByteBuffer edt) {
89             return new StringType(hex(edt, "", "", "", ""));
90         }
91
92         public String itemType() {
93             return "String";
94         }
95     }
96
97     enum OperatingTimeDecode implements StateDecode {
98         INSTANCE;
99
100         public State decodeState(final ByteBuffer edt) {
101             // Specification isn't explicit about byte order, but seems to be work with testing.
102             edt.order(ByteOrder.BIG_ENDIAN);
103
104             final int b0 = edt.get() & 0xFF;
105             final long time = edt.getInt() & 0xFFFFFFFFL;
106
107             final TimeUnit timeUnit;
108             switch (b0) {
109                 case 0x42:
110                     timeUnit = TimeUnit.MINUTES;
111                     break;
112
113                 case 0x43:
114                     timeUnit = TimeUnit.HOURS;
115                     break;
116
117                 case 0x44:
118                     timeUnit = TimeUnit.DAYS;
119                     break;
120
121                 case 0x41:
122                 default:
123                     timeUnit = TimeUnit.SECONDS;
124                     break;
125             }
126
127             return new QuantityType<>(timeUnit.toSeconds(time), Units.SECOND);
128         }
129
130         public String itemType() {
131             return "Number:Time";
132         }
133     }
134
135     class Option {
136         final String name;
137         final int value;
138         final StringType state;
139
140         public Option(final String name, final int value) {
141             this.name = name;
142             this.value = value;
143             this.state = new StringType(name);
144         }
145     }
146
147     class OptionCodec implements StateCodec {
148
149         private final Logger logger = LoggerFactory.getLogger(OptionCodec.class);
150         private final Map<String, Option> optionByName = new HashMap<>();
151         private final Option[] optionByValue = new Option[256]; // All options values are single bytes on the wire
152         private final StringType unknown = new StringType("Unknown");
153
154         public OptionCodec(Option... options) {
155             for (Option option : options) {
156                 optionByName.put(option.name, option);
157                 optionByValue[option.value] = option;
158             }
159         }
160
161         public String itemType() {
162             return "String";
163         }
164
165         public State decodeState(final ByteBuffer edt) {
166             final int value = edt.get() & 0xFF;
167             final Option option = optionByValue[value];
168             return null != option ? option.state : unknown;
169         }
170
171         public void encodeState(final State state, final ByteBuffer edt) {
172             final Option option = optionByName.get(state.toFullString());
173             if (null != option) {
174                 edt.put(b(option.value));
175             } else {
176                 logger.warn("No option specified for: {}", state);
177             }
178         }
179     }
180
181     enum Decimal8bitCodec implements StateCodec {
182
183         INSTANCE;
184
185         public String itemType() {
186             return "Number";
187         }
188
189         public State decodeState(final ByteBuffer edt) {
190             final int value = edt.get(); // Should expand to typed value (mask excluded)
191             return new DecimalType(value);
192         }
193
194         public void encodeState(final State state, final ByteBuffer edt) {
195             edt.put((byte) (((DecimalType) state).intValue()));
196         }
197     }
198
199     enum Temperature8bitCodec implements StateCodec {
200         INSTANCE;
201
202         public State decodeState(final ByteBuffer edt) {
203             final int value = edt.get();
204             return new QuantityType<>(value, SIUnits.CELSIUS);
205         }
206
207         public String itemType() {
208             return "Number:Temperature";
209         }
210
211         public void encodeState(final State state, final ByteBuffer edt) {
212             final @Nullable QuantityType<?> tempCelsius = ((QuantityType<?>) state).toUnit(SIUnits.CELSIUS);
213             edt.put((byte) (Objects.requireNonNull(tempCelsius).intValue()));
214         }
215     }
216 }