]> git.basschouten.com Git - openhab-addons.git/blob
00737b38f0b6a68ebbe59f98fc1e2e2b4bea4162
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.modbus.e3dc.internal.dto;
14
15 import java.nio.ByteBuffer;
16 import java.nio.charset.StandardCharsets;
17 import java.util.BitSet;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20
21 /**
22  * The {@link DataConverter} Helper class to convert bytes from modbus into desired data format
23  *
24  * @author Bernd Weymann - Initial contribution
25  */
26 @NonNullByDefault
27 public class DataConverter {
28     private static final long MAX_INT32 = (long) Math.pow(2, Integer.SIZE);
29
30     /**
31      * Get unit16 value from 2 bytes
32      *
33      * @param wrap
34      * @return int
35      */
36     public static int getUInt16Value(ByteBuffer wrap) {
37         return Short.toUnsignedInt(wrap.getShort());
38     }
39
40     /**
41      * Get unit32 value from 4 bytes
42      *
43      * @param wrap
44      * @return long
45      */
46     public static long getLongValue(ByteBuffer wrap) {
47         return Integer.toUnsignedLong(wrap.getInt());
48     }
49
50     /**
51      * Get double value from 2 bytes with correction factor
52      *
53      * @param wrap
54      * @return double
55      */
56     public static double getUDoubleValue(ByteBuffer wrap, double factor) {
57         return round(getUInt16Value(wrap) * factor, 2);
58     }
59
60     /**
61      * Conversion done according to E3DC Modbus Specification V1.7
62      *
63      * @param wrap
64      * @return decoded long value, Long.MIN_VALUE otherwise
65      */
66     public static long getInt32Swap(ByteBuffer wrap) {
67         long a = getUInt16Value(wrap);
68         long b = getUInt16Value(wrap);
69         if (b < 32768) {
70             return b * 65536 + a;
71         } else {
72             return (MAX_INT32 - b * 65536 - a) * -1;
73         }
74     }
75
76     public static String getString(byte[] bArray) {
77         return new String(bArray, StandardCharsets.US_ASCII).trim();
78     }
79
80     public static int toInt(BitSet bitSet) {
81         int intValue = 0;
82         for (int bit = 0; bit < bitSet.length(); bit++) {
83             if (bitSet.get(bit)) {
84                 intValue |= (1 << bit);
85             }
86         }
87         return intValue;
88     }
89
90     public static double round(double value, int places) {
91         if (places < 0) {
92             throw new IllegalArgumentException();
93         }
94
95         long factor = (long) Math.pow(10, places);
96         value = value * factor;
97         long tmp = Math.round(value);
98         return (double) tmp / factor;
99     }
100 }