[insteon] Refactor transport message field type (#18272)

Signed-off-by: Jeremy Setton <jeremy.setton@gmail.com>
pull/18285/head
Jeremy 2025-02-16 18:33:58 -05:00 committed by GitHub
parent 1b6e2376f5
commit 2e8717414c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 26 deletions

View File

@ -32,9 +32,9 @@ import org.openhab.binding.insteon.internal.utils.HexUtils;
public final class Field {
private final String name;
private final int offset;
private final DataType type;
private final FieldType type;
public Field(String name, int offset, DataType type) {
public Field(String name, int offset, FieldType type) {
this.name = name;
this.offset = offset;
this.type = type;
@ -48,7 +48,7 @@ public final class Field {
return offset;
}
private void check(int len, DataType t) throws FieldException {
private void check(int len, FieldType t) throws FieldException {
if (offset + type.getSize() > len) {
throw new FieldException("field write beyond end of msg");
}
@ -79,7 +79,7 @@ public final class Field {
* @throws FieldException
*/
public void setByte(byte[] data, byte b) throws FieldException {
check(data.length, DataType.BYTE);
check(data.length, FieldType.BYTE);
data[offset] = b;
}
@ -92,7 +92,7 @@ public final class Field {
* @throws FieldException
*/
public void setAddress(byte[] data, InsteonAddress address) throws FieldException {
check(data.length, DataType.ADDRESS);
check(data.length, FieldType.ADDRESS);
System.arraycopy(address.getBytes(), 0, data, offset, type.getSize());
}
@ -104,7 +104,7 @@ public final class Field {
* @throws FieldException
*/
public byte getByte(byte[] data) throws FieldException {
check(data.length, DataType.BYTE);
check(data.length, FieldType.BYTE);
return data[offset];
}
@ -116,7 +116,7 @@ public final class Field {
* @throws FieldException
*/
public InsteonAddress getAddress(byte[] data) throws FieldException {
check(data.length, DataType.ADDRESS);
check(data.length, FieldType.ADDRESS);
byte[] address = Arrays.copyOfRange(data, offset, offset + type.getSize());
return new InsteonAddress(address);
}

View File

@ -21,24 +21,24 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/**
* Defines the data types that can be used in the fields of a message.
* The {@link FieldType} represents a field type
*
* @author Daniel Pfrommer - Initial contribution
* @author Rob Nielsen - Port to openHAB 2 insteon binding
* @author Jeremy Setton - Rewrite insteon binding
*/
@NonNullByDefault
public enum DataType {
public enum FieldType {
BYTE("byte", 1),
ADDRESS("address", 3);
private static final Map<String, DataType> NAME_MAP = Arrays.stream(values())
private static final Map<String, FieldType> NAME_MAP = Arrays.stream(values())
.collect(Collectors.toUnmodifiableMap(type -> type.name, Function.identity()));
private final String name;
private final int size;
private DataType(String name, int size) {
private FieldType(String name, int size) {
this.name = name;
this.size = size;
}
@ -51,13 +51,7 @@ public enum DataType {
return size;
}
/**
* Factory method for getting a DataType from the data type name
*
* @param name the data type name
* @return the data type if defined, otherwise null
*/
public static @Nullable DataType get(String name) {
public static @Nullable FieldType get(String name) {
return NAME_MAP.get(name);
}
}

View File

@ -153,7 +153,7 @@ public class MsgDefinitionRegistry extends InsteonResourceLoader {
Element child = (Element) node;
String nodeName = child.getNodeName();
if (!"header".equals(nodeName)) {
// Increment the offset by the field data type length
// Increment the offset by the field type length
offset += parseField(child, offset, data, fields);
} else if (offset == 0) {
headerLength = parseHeader(child, data, fields);
@ -196,7 +196,7 @@ public class MsgDefinitionRegistry extends InsteonResourceLoader {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element child = (Element) node;
// Increment the offset by the field data type length
// Increment the offset by the field type length
offset += parseField(child, offset, data, fields);
}
}
@ -213,16 +213,16 @@ public class MsgDefinitionRegistry extends InsteonResourceLoader {
* @param offset msg offset
* @param data msg data to update
* @param fields fields map to update
* @return field data type length
* @return field type length
* @throws SAXException
*/
private int parseField(Element element, int offset, byte[] data, Map<String, Field> fields) throws SAXException {
String name = element.getAttribute("name");
DataType dataType = DataType.get(element.getNodeName());
if (dataType == null) {
throw new SAXException("invalid field data type");
FieldType fieldType = FieldType.get(element.getNodeName());
if (fieldType == null) {
throw new SAXException("invalid field type");
}
Field field = new Field(name, offset, dataType);
Field field = new Field(name, offset, fieldType);
try {
field.set(data, element.getTextContent());
} catch (FieldException | IllegalArgumentException e) {
@ -231,7 +231,7 @@ public class MsgDefinitionRegistry extends InsteonResourceLoader {
if (!name.isEmpty()) {
fields.put(name, field);
}
return dataType.getSize();
return fieldType.getSize();
}
/**