2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.velux.internal.things;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
18 * This class support handling of Serial Number used with Velux.
20 * <LI>{@link #UNKNOWN} defines an unknown serial number as constant,</LI>
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>
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>
35 * @author Guenther Schreiner - Initial contribution
38 public final class VeluxProductSerialNo {
41 * ***************************
42 * ***** Private Objects *****
45 private static final String HEXBYTE_SEPARATOR = ":";
46 private static final char SUFFIX_MARKER = '*';
49 * **************************
50 * ***** Public Objects *****
53 public static final String UNKNOWN = "00:00:00:00:00:00:00:00";
56 * ************************
57 * ***** Constructors *****
60 // Suppress default constructor for creating a non-instantiable class.
62 private VeluxProductSerialNo() {
63 throw new AssertionError();
67 * ***************************
68 * ***** Utility Methods *****
72 * Returns the complete serial number as human-readable sequence of hex bytes each separated by the given separator.
74 * @param serialNumber as array of Type byte.
75 * @param separator as of Type String.
76 * @return <b>serialNumberString</b> of type String.
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));
84 if (sb.lastIndexOf(separator) > 0) {
85 sb.deleteCharAt(sb.lastIndexOf(separator));
87 return (sb.toString());
91 * Returns the complete serial number as human-readable sequence of hex bytes each separated by a colon.
93 * @param serialNumber as array of Type byte.
94 * @return <b>serialNumberString</b> of type String.
96 public static String toString(byte[] serialNumber) {
97 return toString(serialNumber, HEXBYTE_SEPARATOR);
101 * Evaluates whether the given serial number is valid.
103 * @param serialNumber as array of type {@link byte},
104 * @return <b>invalid</B> of type {@link boolean}.
106 public static boolean isInvalid(byte[] serialNumber) {
107 if (serialNumber.length != 8) {
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));
116 * Evaluates a given serial number to determine whether any item value should be handled inverted.
118 * @param serialNumberString as type {@link String},
119 * @return <b>isInverted</B> of type {@link boolean}.
121 public static boolean indicatesRevertedValues(String serialNumberString) {
122 return (serialNumberString.length() == 0) ? false
123 : serialNumberString.charAt(serialNumberString.length() - 1) == SUFFIX_MARKER;
127 * Converts a given serial number into a plain serial number without any inversion markers being left.
129 * @param serialNumberString as type {@link String},
130 * @return <b>cleanedSerialNumberString</B> of type {@link String}.
132 public static String cleaned(String serialNumberString) {
133 return indicatesRevertedValues(serialNumberString)
134 ? serialNumberString.substring(0, serialNumberString.length() - 1)
135 : serialNumberString;