Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[upb] Handle device state reports #11352

Merged
merged 4 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void incomingMessage(final UPBMessage msg) {
return;
}

if (msg.getControlWord().isLink() || srcId == dstId) {
if (msg.getControlWord().isLink() || dstId == 0 || srcId == dstId) {
thingHnd.onMessageReceived(msg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private void processBuffer(final InputStream in, final int len) {
return;
}
if (logger.isDebugEnabled()) {
logger.debug("UPB Message: {}", HexUtils.bytesToHex(buf));
logger.debug("UPB Message: {}", formatMessage(buf));
}
final UPBMessage msg;
try {
Expand Down Expand Up @@ -244,6 +244,24 @@ public void terminate() {
}
}

// 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 static final int MAX_RETRIES = 3;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
*/
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;

Expand Down Expand Up @@ -165,6 +169,7 @@ public void onMessageReceived(final UPBMessage msg) {

private void handleDirectMessage(final UPBMessage msg) {
final State state;
byte[] args = msg.getArguments();
switch (msg.getCommand()) {
case ACTIVATE:
state = OnOffType.ON;
Expand All @@ -175,12 +180,12 @@ private void handleDirectMessage(final UPBMessage msg) {
break;

case GOTO:
if (msg.getArguments().length == 0) {
logger.warn("DEV {}: malformed GOTO cmd", unitId);
case DEVICE_STATE:
if (args.length == 0) {
logger.warn("DEV {}: malformed {} cmd", unitId, msg.getCommand());
return;
}
final int level = msg.getArguments()[0];
state = new PercentType(level);
state = new PercentType(args[0]);
break;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
*/
package org.openhab.binding.upb.internal.message;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* An enum of possible commands.
*
* @author cvanorman - Initial contribution
*/
@NonNullByDefault
public enum Command {
NULL(0),
ACTIVATE(0x20),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public enum Type {
/**
* 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}
*/
public static Type forPrefix(final byte[] buf) {
Expand Down Expand Up @@ -80,7 +80,7 @@ private UPBMessage(final Type type) {
/**
* Converts a hex string into a {@link UPBMessage}.
*
* @param commandString
* @param buf
* the string as returned by the modem.
* @return a new UPBMessage.
*/
Expand Down