]> git.basschouten.com Git - openhab-addons.git/blob
2558ba89196ad73ea597a0a670460caaa2477781
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.bluetooth.daikinmadoka.internal.model;
14
15 import java.nio.ByteBuffer;
16 import java.nio.ByteOrder;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * This class is in charge of serializing/deserializing values from a message
23  *
24  * @author Benjamin Lafois - Initial contribution
25  */
26 @NonNullByDefault
27 public class MadokaValue {
28
29     private int id;
30     private int size;
31     private byte @Nullable [] rawValue;
32
33     public MadokaValue(int id, int size, byte[] rawValue) {
34         this.id = id;
35         this.size = size;
36         this.rawValue = rawValue;
37     }
38
39     public MadokaValue() {
40     }
41
42     public int getId() {
43         return id;
44     }
45
46     public void setId(int id) {
47         this.id = id;
48     }
49
50     public int getSize() {
51         return size;
52     }
53
54     public void setSize(int size) {
55         this.size = size;
56     }
57
58     public byte @Nullable [] getRawValue() {
59         return rawValue;
60     }
61
62     public void setRawValue(byte[] rawValue) {
63         this.rawValue = rawValue;
64     }
65
66     /**
67      * For backward compatibility
68      *
69      * @return
70      */
71     public long getComputedValue() {
72         return getComputedValue(ByteOrder.BIG_ENDIAN);
73     }
74
75     public long getComputedValue(ByteOrder e) {
76         byte[] v = rawValue;
77         if (v != null) {
78             ByteBuffer bb;
79             switch (size) {
80                 case 1:
81                     return v[0];
82                 case 2:
83                     bb = ByteBuffer.wrap(v, 0, 2);
84                     bb.order(e);
85                     return bb.getShort();
86                 case 4:
87                     bb = ByteBuffer.wrap(v, 0, 4);
88                     bb.order(e);
89                     return bb.getInt();
90                 default:
91                     // unsupported
92                     break;
93             }
94
95         }
96         return 0;
97     }
98 }