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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.insteon.internal.utils.Utils;
20 * This class wraps an Insteon Address 'xx.xx.xx'
22 * @author Daniel Pfrommer - Initial contribution
23 * @author Rob Nielsen - Port to openHAB 2 insteon binding
26 public class InsteonAddress {
27 private byte highByte;
28 private byte middleByte;
32 public InsteonAddress() {
39 public InsteonAddress(InsteonAddress a) {
40 highByte = a.highByte;
41 middleByte = a.middleByte;
46 public InsteonAddress(byte high, byte middle, byte low) {
56 * @param address string must have format of e.g. '2a.3c.40' or (for X10) 'H.UU'
58 public InsteonAddress(String address) throws IllegalArgumentException {
59 if (X10.isValidAddress(address)) {
62 lowByte = X10.addressToByte(address);
65 String[] parts = address.split("\\.");
66 if (parts.length != 3) {
67 throw new IllegalArgumentException("Address string must have 3 bytes, has: " + parts.length);
69 highByte = (byte) Utils.fromHexString(parts[0]);
70 middleByte = (byte) Utils.fromHexString(parts[1]);
71 lowByte = (byte) Utils.fromHexString(parts[2]);
77 * Constructor for an InsteonAddress that wraps an X10 address.
78 * Simply stuff the X10 address into the lowest byte.
80 * @param aX10HouseUnit the house and unit number as encoded by the X10 protocol
82 public InsteonAddress(byte aX10HouseUnit) {
85 lowByte = aX10HouseUnit;
89 public void setHighByte(byte h) {
93 public void setMiddleByte(byte m) {
97 public void setLowByte(byte l) {
101 public byte getHighByte() {
105 public byte getMiddleByte() {
109 public byte getLowByte() {
113 public byte getX10HouseCode() {
114 return (byte) ((lowByte & 0xf0) >> 4);
117 public byte getX10UnitCode() {
118 return (byte) ((lowByte & 0x0f));
121 public boolean isX10() {
125 public void storeBytes(byte[] bytes, int offset) {
126 bytes[offset] = getHighByte();
127 bytes[offset + 1] = getMiddleByte();
128 bytes[offset + 2] = getLowByte();
131 public void loadBytes(byte[] bytes, int offset) {
132 setHighByte(bytes[offset]);
133 setMiddleByte(bytes[offset + 1]);
134 setLowByte(bytes[offset + 2]);
138 public String toString() {
141 byte house = (byte) (((getLowByte() & 0xf0) >> 4) & 0xff);
142 byte unit = (byte) ((getLowByte() & 0x0f) & 0xff);
143 s = X10.houseToString(house) + "." + X10.unitToInt(unit);
144 // s = Utils.getHexString(lowByte);
146 s = Utils.getHexString(highByte) + "." + Utils.getHexString(middleByte) + "." + Utils.getHexString(lowByte);
151 @SuppressWarnings("PMD.SimplifyBooleanReturns")
153 public boolean equals(@Nullable Object obj) {
160 if (getClass() != obj.getClass()) {
163 InsteonAddress other = (InsteonAddress) obj;
164 if (highByte != other.highByte) {
167 if (lowByte != other.lowByte) {
170 if (middleByte != other.middleByte) {
173 if (x10 != other.x10) {
180 public int hashCode() {
181 final int prime = 31;
183 result = prime * result + highByte;
184 result = prime * result + lowByte;
185 result = prime * result + middleByte;
186 result = prime * result + (x10 ? 1231 : 1237);
191 * Test if Insteon address is valid
193 * @return true if address is in valid AB.CD.EF or (for X10) H.UU format
195 public static boolean isValid(@Nullable String addr) {
199 if (X10.isValidAddress(addr)) {
202 String[] fields = addr.split("\\.");
203 if (fields.length != 3) {
207 // convert the insteon xx.xx.xx address to integer to test
208 @SuppressWarnings("unused")
209 int test = Integer.parseInt(fields[2], 16) * 65536 + Integer.parseInt(fields[1], 16) * 256
210 + +Integer.parseInt(fields[0], 16);
211 } catch (NumberFormatException e) {
218 * Turn string into address
220 * @param val the string to convert
221 * @return the corresponding insteon address
223 public static InsteonAddress parseAddress(String val) {
224 return new InsteonAddress(val);