2 * Copyright (c) 2010-2024 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.insteon.internal.device;
15 import java.util.HashMap;
17 import java.util.Map.Entry;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
22 * This class has utilities related to the X10 protocol.
24 * @author Bernd Pfrommer - Initial contribution
25 * @author Rob Nielsen - Port to openHAB 2 insteon binding
30 * Enumerates the X10 command codes.
32 * @author Bernd Pfrommer - openHAB 1 insteonplm binding
41 HAIL_ACKNOWLEDGE(0x9),
53 private final byte code;
65 * converts house code to clear text
67 * @param c house code as per X10 spec
68 * @return clear text house code, i.e letter A-P
70 public static String houseToString(byte c) {
71 String s = houseCodeToString.get(c & 0xff);
72 return (s == null) ? "X" : s;
76 * converts unit code to regular integer
78 * @param c unit code per X10 spec
79 * @return decoded integer, i.e. number 0-16
81 public static int unitToInt(byte c) {
82 Integer i = unitCodeToInt.get(c & 0xff);
83 return (i == null) ? -1 : i;
87 * Test if string has valid X10 address of form "H.U", e.g. A.10
89 * @param s string to test
90 * @return true if is valid X10 address
92 public static boolean isValidAddress(String s) {
93 String[] parts = s.split("\\.");
94 if (parts.length != 2) {
97 return parts[0].matches("[A-P]") && parts[1].matches("\\d{1,2}");
101 * Turn clear text address ("A.10") to byte code
103 * @param addr clear text address
104 * @return byte that encodes house + unit code
106 public static byte addressToByte(String addr) {
107 String[] parts = addr.split("\\.");
108 int ih = houseStringToCode(parts[0]);
109 int iu = unitStringToCode(parts[1]);
110 int itot = ih << 4 | iu;
111 return (byte) (itot & 0xff);
115 * converts String to house byte code
117 * @param s clear text house string
118 * @return coded house byte
120 public static int houseStringToCode(String s) {
121 for (Entry<Integer, String> entry : houseCodeToString.entrySet()) {
122 if (s.equals(entry.getValue())) {
123 return entry.getKey();
130 * converts unit string to unit code
132 * @param s string with clear text integer inside
133 * @return encoded unit byte
135 public static int unitStringToCode(String s) {
137 int i = Integer.parseInt(s);
138 for (Entry<Integer, Integer> entry : unitCodeToInt.entrySet()) {
139 if (i == entry.getValue()) {
140 return entry.getKey();
143 } catch (NumberFormatException e) {
149 * Map between 4-bit X10 code and the house code.
151 private static Map<Integer, String> houseCodeToString = new HashMap<>();
153 * Map between 4-bit X10 code and the unit code.
155 private static Map<Integer, Integer> unitCodeToInt = new HashMap<>();
158 houseCodeToString.put(0x6, "A");
159 unitCodeToInt.put(0x6, 1);
160 houseCodeToString.put(0xe, "B");
161 unitCodeToInt.put(0xe, 2);
162 houseCodeToString.put(0x2, "C");
163 unitCodeToInt.put(0x2, 3);
164 houseCodeToString.put(0xa, "D");
165 unitCodeToInt.put(0xa, 4);
166 houseCodeToString.put(0x1, "E");
167 unitCodeToInt.put(0x1, 5);
168 houseCodeToString.put(0x9, "F");
169 unitCodeToInt.put(0x9, 6);
170 houseCodeToString.put(0x5, "G");
171 unitCodeToInt.put(0x5, 7);
172 houseCodeToString.put(0xd, "H");
173 unitCodeToInt.put(0xd, 8);
174 houseCodeToString.put(0x7, "I");
175 unitCodeToInt.put(0x7, 9);
176 houseCodeToString.put(0xf, "J");
177 unitCodeToInt.put(0xf, 10);
178 houseCodeToString.put(0x3, "K");
179 unitCodeToInt.put(0x3, 11);
180 houseCodeToString.put(0xb, "L");
181 unitCodeToInt.put(0xb, 12);
182 houseCodeToString.put(0x0, "M");
183 unitCodeToInt.put(0x0, 13);
184 houseCodeToString.put(0x8, "N");
185 unitCodeToInt.put(0x8, 14);
186 houseCodeToString.put(0x4, "O");
187 unitCodeToInt.put(0x4, 15);
188 houseCodeToString.put(0xc, "P");
189 unitCodeToInt.put(0xc, 16);