[upb] Handle device state reports (#11352)

* [upb] Fix javadoc

Signed-off-by: Marcus Better <marcus@better.se>

* [upb] Handle Device State Report messages

Signed-off-by: Marcus Better <marcus@better.se>

* More useful debug logging of received messages

Signed-off-by: Marcus Better <marcus@better.se>

* Null annotation

Signed-off-by: Marcus Better <marcus@better.se>
This commit is contained in:
Marcus Better 2021-10-21 01:30:40 -04:00 committed by GitHub
parent 9cc3f441fd
commit daea6481a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 9 deletions

View File

@ -67,7 +67,7 @@ public class UPBController {
return; return;
} }
if (msg.getControlWord().isLink() || srcId == dstId) { if (msg.getControlWord().isLink() || dstId == 0 || srcId == dstId) {
thingHnd.onMessageReceived(msg); thingHnd.onMessageReceived(msg);
} }

View File

@ -152,7 +152,7 @@ public class SerialIoThread extends Thread {
return; return;
} }
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("UPB Message: {}", HexUtils.bytesToHex(buf)); logger.debug("UPB Message: {}", formatMessage(buf));
} }
final UPBMessage msg; final UPBMessage msg;
try { try {
@ -244,6 +244,24 @@ public class SerialIoThread extends Thread {
} }
} }
// format a message for debug logging, include only printable characters
private static String formatMessage(byte[] buf) {
final int len;
// omit the final newline
if (buf[buf.length - 1] == '\r') {
len = buf.length - 1;
} else {
len = buf.length;
}
final String s = new String(buf, 0, len, US_ASCII);
if (s.chars().allMatch(c -> c >= 32 && c < 127)) {
return s;
} else {
// presence of non-printable characters is either noise or a misconfiguration, log it in hex
return HexUtils.bytesToHex(buf);
}
}
private class WriteRunnable implements Runnable { private class WriteRunnable implements Runnable {
private static final int MAX_RETRIES = 3; private static final int MAX_RETRIES = 3;

View File

@ -12,7 +12,11 @@
*/ */
package org.openhab.binding.upb.internal.handler; package org.openhab.binding.upb.internal.handler;
import static org.openhab.binding.upb.internal.message.Command.*; import static org.openhab.binding.upb.internal.message.Command.ACTIVATE;
import static org.openhab.binding.upb.internal.message.Command.DEACTIVATE;
import static org.openhab.binding.upb.internal.message.Command.GOTO;
import static org.openhab.binding.upb.internal.message.Command.NULL;
import static org.openhab.binding.upb.internal.message.Command.REPORT_STATE;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -165,6 +169,7 @@ public class UPBThingHandler extends BaseThingHandler {
private void handleDirectMessage(final UPBMessage msg) { private void handleDirectMessage(final UPBMessage msg) {
final State state; final State state;
byte[] args = msg.getArguments();
switch (msg.getCommand()) { switch (msg.getCommand()) {
case ACTIVATE: case ACTIVATE:
state = OnOffType.ON; state = OnOffType.ON;
@ -175,12 +180,12 @@ public class UPBThingHandler extends BaseThingHandler {
break; break;
case GOTO: case GOTO:
if (msg.getArguments().length == 0) { case DEVICE_STATE:
logger.warn("DEV {}: malformed GOTO cmd", unitId); if (args.length == 0) {
logger.warn("DEV {}: malformed {} cmd", unitId, msg.getCommand());
return; return;
} }
final int level = msg.getArguments()[0]; state = new PercentType(args[0]);
state = new PercentType(level);
break; break;
default: default:

View File

@ -12,11 +12,14 @@
*/ */
package org.openhab.binding.upb.internal.message; package org.openhab.binding.upb.internal.message;
import org.eclipse.jdt.annotation.NonNullByDefault;
/** /**
* An enum of possible commands. * An enum of possible commands.
* *
* @author cvanorman - Initial contribution * @author cvanorman - Initial contribution
*/ */
@NonNullByDefault
public enum Command { public enum Command {
NULL(0), NULL(0),
ACTIVATE(0x20), ACTIVATE(0x20),

View File

@ -48,7 +48,7 @@ public class UPBMessage {
/** /**
* Returns the message type for a message buffer. * Returns the message type for a message buffer.
* *
* @param prefix the byte array to check for a matching type prefix * @param buf the byte array to check for a matching type prefix
* @return the matching message type, or {@code NONE} * @return the matching message type, or {@code NONE}
*/ */
public static Type forPrefix(final byte[] buf) { public static Type forPrefix(final byte[] buf) {
@ -80,7 +80,7 @@ public class UPBMessage {
/** /**
* Converts a hex string into a {@link UPBMessage}. * Converts a hex string into a {@link UPBMessage}.
* *
* @param commandString * @param buf
* the string as returned by the modem. * the string as returned by the modem.
* @return a new UPBMessage. * @return a new UPBMessage.
*/ */