[insteon] Refactor transport message field type (#18272)
Signed-off-by: Jeremy Setton <jeremy.setton@gmail.com>pull/18285/head
parent
1b6e2376f5
commit
2e8717414c
|
@ -32,9 +32,9 @@ import org.openhab.binding.insteon.internal.utils.HexUtils;
|
||||||
public final class Field {
|
public final class Field {
|
||||||
private final String name;
|
private final String name;
|
||||||
private final int offset;
|
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.name = name;
|
||||||
this.offset = offset;
|
this.offset = offset;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
|
@ -48,7 +48,7 @@ public final class Field {
|
||||||
return offset;
|
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) {
|
if (offset + type.getSize() > len) {
|
||||||
throw new FieldException("field write beyond end of msg");
|
throw new FieldException("field write beyond end of msg");
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ public final class Field {
|
||||||
* @throws FieldException
|
* @throws FieldException
|
||||||
*/
|
*/
|
||||||
public void setByte(byte[] data, byte b) throws FieldException {
|
public void setByte(byte[] data, byte b) throws FieldException {
|
||||||
check(data.length, DataType.BYTE);
|
check(data.length, FieldType.BYTE);
|
||||||
data[offset] = b;
|
data[offset] = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,7 +92,7 @@ public final class Field {
|
||||||
* @throws FieldException
|
* @throws FieldException
|
||||||
*/
|
*/
|
||||||
public void setAddress(byte[] data, InsteonAddress address) 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());
|
System.arraycopy(address.getBytes(), 0, data, offset, type.getSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +104,7 @@ public final class Field {
|
||||||
* @throws FieldException
|
* @throws FieldException
|
||||||
*/
|
*/
|
||||||
public byte getByte(byte[] data) throws FieldException {
|
public byte getByte(byte[] data) throws FieldException {
|
||||||
check(data.length, DataType.BYTE);
|
check(data.length, FieldType.BYTE);
|
||||||
return data[offset];
|
return data[offset];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,7 +116,7 @@ public final class Field {
|
||||||
* @throws FieldException
|
* @throws FieldException
|
||||||
*/
|
*/
|
||||||
public InsteonAddress getAddress(byte[] data) 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());
|
byte[] address = Arrays.copyOfRange(data, offset, offset + type.getSize());
|
||||||
return new InsteonAddress(address);
|
return new InsteonAddress(address);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,24 +21,24 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
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 Daniel Pfrommer - Initial contribution
|
||||||
* @author Rob Nielsen - Port to openHAB 2 insteon binding
|
* @author Rob Nielsen - Port to openHAB 2 insteon binding
|
||||||
* @author Jeremy Setton - Rewrite insteon binding
|
* @author Jeremy Setton - Rewrite insteon binding
|
||||||
*/
|
*/
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public enum DataType {
|
public enum FieldType {
|
||||||
BYTE("byte", 1),
|
BYTE("byte", 1),
|
||||||
ADDRESS("address", 3);
|
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()));
|
.collect(Collectors.toUnmodifiableMap(type -> type.name, Function.identity()));
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final int size;
|
private final int size;
|
||||||
|
|
||||||
private DataType(String name, int size) {
|
private FieldType(String name, int size) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.size = size;
|
this.size = size;
|
||||||
}
|
}
|
||||||
|
@ -51,13 +51,7 @@ public enum DataType {
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static @Nullable FieldType get(String name) {
|
||||||
* 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) {
|
|
||||||
return NAME_MAP.get(name);
|
return NAME_MAP.get(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -153,7 +153,7 @@ public class MsgDefinitionRegistry extends InsteonResourceLoader {
|
||||||
Element child = (Element) node;
|
Element child = (Element) node;
|
||||||
String nodeName = child.getNodeName();
|
String nodeName = child.getNodeName();
|
||||||
if (!"header".equals(nodeName)) {
|
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);
|
offset += parseField(child, offset, data, fields);
|
||||||
} else if (offset == 0) {
|
} else if (offset == 0) {
|
||||||
headerLength = parseHeader(child, data, fields);
|
headerLength = parseHeader(child, data, fields);
|
||||||
|
@ -196,7 +196,7 @@ public class MsgDefinitionRegistry extends InsteonResourceLoader {
|
||||||
Node node = nodes.item(i);
|
Node node = nodes.item(i);
|
||||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
Element child = (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);
|
offset += parseField(child, offset, data, fields);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -213,16 +213,16 @@ public class MsgDefinitionRegistry extends InsteonResourceLoader {
|
||||||
* @param offset msg offset
|
* @param offset msg offset
|
||||||
* @param data msg data to update
|
* @param data msg data to update
|
||||||
* @param fields fields map to update
|
* @param fields fields map to update
|
||||||
* @return field data type length
|
* @return field type length
|
||||||
* @throws SAXException
|
* @throws SAXException
|
||||||
*/
|
*/
|
||||||
private int parseField(Element element, int offset, byte[] data, Map<String, Field> fields) throws SAXException {
|
private int parseField(Element element, int offset, byte[] data, Map<String, Field> fields) throws SAXException {
|
||||||
String name = element.getAttribute("name");
|
String name = element.getAttribute("name");
|
||||||
DataType dataType = DataType.get(element.getNodeName());
|
FieldType fieldType = FieldType.get(element.getNodeName());
|
||||||
if (dataType == null) {
|
if (fieldType == null) {
|
||||||
throw new SAXException("invalid field data type");
|
throw new SAXException("invalid field type");
|
||||||
}
|
}
|
||||||
Field field = new Field(name, offset, dataType);
|
Field field = new Field(name, offset, fieldType);
|
||||||
try {
|
try {
|
||||||
field.set(data, element.getTextContent());
|
field.set(data, element.getTextContent());
|
||||||
} catch (FieldException | IllegalArgumentException e) {
|
} catch (FieldException | IllegalArgumentException e) {
|
||||||
|
@ -231,7 +231,7 @@ public class MsgDefinitionRegistry extends InsteonResourceLoader {
|
||||||
if (!name.isEmpty()) {
|
if (!name.isEmpty()) {
|
||||||
fields.put(name, field);
|
fields.put(name, field);
|
||||||
}
|
}
|
||||||
return dataType.getSize();
|
return fieldType.getSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue