]> git.basschouten.com Git - openhab-addons.git/blob
a87cb6c126cf6dbd52b4735599bf07f28c02f46c
[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.elroconnects.internal.util;
14
15 import java.nio.charset.StandardCharsets;
16 import java.util.Arrays;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.util.HexUtils;
21
22 /**
23  * The {@link ElroConnectsUtil} contains a few utility methods for the ELRO Connects binding.
24  *
25  * @author Mark Herwege - Initial contribution
26  */
27 @NonNullByDefault
28 public final class ElroConnectsUtil {
29
30     private static final int POLYNOMIAL = 0x0000a001; // polynomial for CRC calculation
31
32     public static int encode(int value) {
33         return (((value ^ 0xFFFFFFFF) + 0x10000) ^ 0x123) ^ 0x1234;
34     }
35
36     public static int decode(int value, int msgId) {
37         return (byte) (0xFFFF + ~((value ^ 0x1234) ^ msgId));
38     }
39
40     /**
41      * Encode input string into hex
42      *
43      * @param input
44      * @param length byte length for input string in UTF-8 encoding, further characters will be cut
45      * @return encoded hex string cut to length
46      */
47     public static String encode(String input, int length) {
48         byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
49         String content = "@".repeat((length > bytes.length) ? (length - bytes.length) : 0)
50                 + new String(bytes, StandardCharsets.UTF_8);
51         bytes = Arrays.copyOf(content.getBytes(StandardCharsets.UTF_8), length + 1);
52         bytes[length] = (byte) "$".charAt(0);
53         return HexUtils.bytesToHex(bytes);
54     }
55
56     /**
57      * Decode hex string using UTF-8 encoding and drop leading @ characters and trailing $
58      *
59      * @param input hex string
60      * @return string contained in input
61      */
62     public static String decode(String input) {
63         return (new String(HexUtils.hexToBytes(input), StandardCharsets.UTF_8)).replaceAll("[@$]*", "");
64     }
65
66     /**
67      * Compare first bytes of byte representation of input strings in UTF-8 encoding
68      *
69      * @param string1
70      * @param string2
71      * @param length number of bytes to compare
72      * @return true if equal
73      */
74     public static boolean equals(String string1, String string2, int length) {
75         byte[] bytes1 = Arrays.copyOf(string1.getBytes(StandardCharsets.UTF_8), length);
76         byte[] bytes2 = Arrays.copyOf(string2.getBytes(StandardCharsets.UTF_8), length);
77         return Arrays.equals(bytes1, bytes2);
78     }
79
80     /**
81      * Calculate CRC-16 for input string. The input string should be treated as ASCII characters. The calculation is
82      * based on the MODBUS CRC-16 calculation.
83      *
84      * @param input
85      * @return crc hex format
86      */
87     public static String crc16(String input) {
88         byte[] bytes = input.getBytes(StandardCharsets.US_ASCII);
89         int crc = 0x0000ffff;
90         for (byte curByte : bytes) {
91             crc ^= curByte;
92             for (int i = 0; i < 8; i++) {
93                 if ((crc & 0x00000001) == 1) {
94                     crc >>= 1;
95                     crc ^= POLYNOMIAL;
96                 } else {
97                     crc >>= 1;
98                 }
99             }
100         }
101         return Integer.toHexString(crc);
102     }
103
104     public static String stringOrEmpty(@Nullable String data) {
105         return (data == null ? "" : data);
106     }
107
108     public static int intOrZero(@Nullable Integer data) {
109         return (data == null ? 0 : data);
110     }
111 }