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