]> git.basschouten.com Git - openhab-addons.git/blob
8fa1aeebc404c2f6f803d2b41a0c0d48045c1d94
[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.velux.internal.things;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
18  * This class support handling of Serial Number used with Velux.
19  * <UL>
20  * <LI>{@link #UNKNOWN} defines an unknown serial number as constant,</LI>
21  * </UL>
22  * <UL>
23  * <LI>{@link #toString} converts the serial number as array of bytes into a human-readable String,</LI>
24  * <LI>{@link #isInvalid} evaluates whether the given serial number is valid,</LI>
25  * <LI>{@link #indicatesRevertedValues} returns a flag whether the serial number indicates inversion,</LI>
26  * <LI>{@link #cleaned} returns a plain serial number without any inverse indicators.</LI>
27  * </UL>
28  * <P>
29  * Example:
30  * <UL>
31  * <LI><I>12:23:34:45:56:67:78:89</I> represents a normal serial number,</LI>
32  * <LI><I>12:23:34:45:56:67:78:89*</I> represents a serial number which leads to an inverted value handling.</LI>
33  * </UL>
34  *
35  * @author Guenther Schreiner - Initial contribution
36  */
37 @NonNullByDefault
38 public final class VeluxProductSerialNo {
39
40     /*
41      * ***************************
42      * ***** Private Objects *****
43      */
44
45     private static final String HEXBYTE_SEPARATOR = ":";
46     private static final char SUFFIX_MARKER = '*';
47
48     /*
49      * **************************
50      * ***** Public Objects *****
51      */
52
53     public static final String UNKNOWN = "00:00:00:00:00:00:00:00";
54
55     /*
56      * ************************
57      * ***** Constructors *****
58      */
59
60     // Suppress default constructor for creating a non-instantiable class.
61
62     private VeluxProductSerialNo() {
63         throw new AssertionError();
64     }
65
66     /*
67      * ***************************
68      * ***** Utility Methods *****
69      */
70
71     /**
72      * Returns the complete serial number as human-readable sequence of hex bytes each separated by the given separator.
73      *
74      * @param serialNumber as array of Type byte.
75      * @param separator as of Type String.
76      * @return <b>serialNumberString</b> of type String.
77      */
78     public static String toString(byte[] serialNumber, String separator) {
79         StringBuilder sb = new StringBuilder();
80         for (byte b : serialNumber) {
81             sb.append(String.format("%02X", b));
82             sb.append(separator);
83         }
84         if (sb.lastIndexOf(separator) > 0) {
85             sb.deleteCharAt(sb.lastIndexOf(separator));
86         }
87         return (sb.toString());
88     }
89
90     /**
91      * Returns the complete serial number as human-readable sequence of hex bytes each separated by a colon.
92      *
93      * @param serialNumber as array of Type byte.
94      * @return <b>serialNumberString</b> of type String.
95      */
96     public static String toString(byte[] serialNumber) {
97         return toString(serialNumber, HEXBYTE_SEPARATOR);
98     }
99
100     /**
101      * Evaluates whether the given serial number is valid.
102      *
103      * @param serialNumber as array of type {@link byte},
104      * @return <b>invalid</B> of type {@link boolean}.
105      */
106     public static boolean isInvalid(byte[] serialNumber) {
107         if (serialNumber.length != 8) {
108             return true;
109         }
110         return ((serialNumber[0] == 0) && (serialNumber[1] == 0) && (serialNumber[2] == 0) && (serialNumber[3] == 0)
111                 && (serialNumber[4] == 0) && (serialNumber[5] == 0) && (serialNumber[6] == 0)
112                 && (serialNumber[7] == 0));
113     }
114
115     /**
116      * Evaluates a given serial number to determine whether any item value should be handled inverted.
117      *
118      * @param serialNumberString as type {@link String},
119      * @return <b>isInverted</B> of type {@link boolean}.
120      */
121     public static boolean indicatesRevertedValues(String serialNumberString) {
122         return (serialNumberString.length() == 0) ? false
123                 : serialNumberString.charAt(serialNumberString.length() - 1) == SUFFIX_MARKER;
124     }
125
126     /**
127      * Converts a given serial number into a plain serial number without any inversion markers being left.
128      *
129      * @param serialNumberString as type {@link String},
130      * @return <b>cleanedSerialNumberString</B> of type {@link String}.
131      */
132     public static String cleaned(String serialNumberString) {
133         return indicatesRevertedValues(serialNumberString)
134                 ? serialNumberString.substring(0, serialNumberString.length() - 1)
135                 : serialNumberString;
136     }
137 }