mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[insteon] Add debug monitoring support for modem and x10 devices
Signed-off-by: Jeremy Setton <jeremy.setton@gmail.com>
This commit is contained in:
parent
26749f0fc1
commit
2a38b95c71
@ -27,6 +27,8 @@ import java.util.stream.Collectors;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
import org.openhab.binding.insteon.internal.device.DeviceAddress;
|
||||||
import org.openhab.binding.insteon.internal.device.InsteonAddress;
|
import org.openhab.binding.insteon.internal.device.InsteonAddress;
|
||||||
import org.openhab.binding.insteon.internal.device.InsteonScene;
|
import org.openhab.binding.insteon.internal.device.InsteonScene;
|
||||||
import org.openhab.binding.insteon.internal.device.X10Address;
|
import org.openhab.binding.insteon.internal.device.X10Address;
|
||||||
@ -82,7 +84,8 @@ public class DebugCommand extends InsteonCommand implements PortListener {
|
|||||||
|
|
||||||
private boolean monitoring = false;
|
private boolean monitoring = false;
|
||||||
private boolean monitorAllDevices = false;
|
private boolean monitorAllDevices = false;
|
||||||
private Set<InsteonAddress> monitoredAddresses = new HashSet<>();
|
private Set<DeviceAddress> monitoredAddresses = new HashSet<>();
|
||||||
|
private @Nullable X10Address lastX10Address;
|
||||||
|
|
||||||
public DebugCommand(InsteonCommandExtension commandExtension) {
|
public DebugCommand(InsteonCommandExtension commandExtension) {
|
||||||
super(NAME, DESCRIPTION, commandExtension);
|
super(NAME, DESCRIPTION, commandExtension);
|
||||||
@ -90,11 +93,11 @@ public class DebugCommand extends InsteonCommand implements PortListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getUsages() {
|
public List<String> getUsages() {
|
||||||
return List.of(buildCommandUsage(LIST_MONITORED, "list monitored device(s)"),
|
return List.of(buildCommandUsage(LIST_MONITORED, "list monitored Insteon/X10 device(s)"),
|
||||||
buildCommandUsage(START_MONITORING + " " + ALL_OPTION + "|<address>",
|
buildCommandUsage(START_MONITORING + " " + ALL_OPTION + "|<address>",
|
||||||
"start logging message events for device(s) in separate file(s)"),
|
"start logging message events for Insteon/X10 device(s) in separate file(s)"),
|
||||||
buildCommandUsage(STOP_MONITORING + " " + ALL_OPTION + "|<address>",
|
buildCommandUsage(STOP_MONITORING + " " + ALL_OPTION + "|<address>",
|
||||||
"stop logging message events for device(s) in separate file(s)"),
|
"stop logging message events for Insteon/X10 device(s) in separate file(s)"),
|
||||||
buildCommandUsage(SEND_BROADCAST_MESSAGE + " <group> <cmd1> <cmd2>",
|
buildCommandUsage(SEND_BROADCAST_MESSAGE + " <group> <cmd1> <cmd2>",
|
||||||
"send an Insteon broadcast message to a group"),
|
"send an Insteon broadcast message to a group"),
|
||||||
buildCommandUsage(SEND_STANDARD_MESSAGE + " <address> <cmd1> <cmd2>",
|
buildCommandUsage(SEND_STANDARD_MESSAGE + " <address> <cmd1> <cmd2>",
|
||||||
@ -196,14 +199,15 @@ public class DebugCommand extends InsteonCommand implements PortListener {
|
|||||||
case START_MONITORING:
|
case START_MONITORING:
|
||||||
strings = monitorAllDevices ? List.of()
|
strings = monitorAllDevices ? List.of()
|
||||||
: Stream.concat(Stream.of(ALL_OPTION),
|
: Stream.concat(Stream.of(ALL_OPTION),
|
||||||
getModem().getDB().getDevices().stream()
|
Stream.concat(Stream.of(getModem().getAddress()),
|
||||||
|
getModem().getDB().getDevices().stream())
|
||||||
.filter(address -> !monitoredAddresses.contains(address))
|
.filter(address -> !monitoredAddresses.contains(address))
|
||||||
.map(InsteonAddress::toString))
|
.map(InsteonAddress::toString))
|
||||||
.toList();
|
.toList();
|
||||||
break;
|
break;
|
||||||
case STOP_MONITORING:
|
case STOP_MONITORING:
|
||||||
strings = monitorAllDevices ? List.of(ALL_OPTION)
|
strings = monitorAllDevices ? List.of(ALL_OPTION)
|
||||||
: monitoredAddresses.stream().map(InsteonAddress::toString).toList();
|
: monitoredAddresses.stream().map(DeviceAddress::toString).toList();
|
||||||
break;
|
break;
|
||||||
case SEND_BROADCAST_MESSAGE:
|
case SEND_BROADCAST_MESSAGE:
|
||||||
strings = getModem().getDB().getBroadcastGroups().stream().map(String::valueOf).toList();
|
strings = getModem().getDB().getBroadcastGroups().stream().map(String::valueOf).toList();
|
||||||
@ -235,54 +239,65 @@ public class DebugCommand extends InsteonCommand implements PortListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void messageReceived(Msg msg) {
|
public void messageReceived(Msg msg) {
|
||||||
try {
|
logMessageEvent(msg);
|
||||||
InsteonAddress address = msg.getInsteonAddress(msg.isReply() ? "toAddress" : "fromAddress");
|
|
||||||
if (monitorAllDevices || monitoredAddresses.contains(address)) {
|
|
||||||
logMessageEvent(address, msg);
|
|
||||||
}
|
|
||||||
} catch (FieldException ignored) {
|
|
||||||
// ignore message with no address field
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void messageSent(Msg msg) {
|
public void messageSent(Msg msg) {
|
||||||
try {
|
logMessageEvent(msg);
|
||||||
InsteonAddress address = msg.getInsteonAddress("toAddress");
|
|
||||||
if (monitorAllDevices || monitoredAddresses.contains(address)) {
|
|
||||||
logMessageEvent(address, msg);
|
|
||||||
}
|
}
|
||||||
} catch (FieldException ignored) {
|
|
||||||
// ignore message with no address field
|
private DeviceAddress getMsgEventAddress(Msg msg) throws FieldException {
|
||||||
|
if (msg.isX10()) {
|
||||||
|
X10Address address = msg.isX10Address() ? msg.getX10Address() : lastX10Address;
|
||||||
|
if (address == null) {
|
||||||
|
throw new FieldException("unknown x10 address");
|
||||||
|
}
|
||||||
|
lastX10Address = address;
|
||||||
|
return address;
|
||||||
|
} else if (msg.isInsteon()) {
|
||||||
|
return msg.isInbound() && !msg.isReply() ? msg.getInsteonAddress("fromAddress")
|
||||||
|
: !msg.isAllLinkBroadcast() ? msg.getInsteonAddress("toAddress") : getModem().getAddress();
|
||||||
|
} else {
|
||||||
|
return getModem().getAddress();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Path getMsgEventsFilePath(String address) {
|
private Path getMsgEventsFilePath(DeviceAddress address) {
|
||||||
return getBindingDataFilePath(MSG_EVENTS_FILE_PREFIX + "-" + address.toLowerCase().replace(".", "") + ".log");
|
String name = address.toString().toLowerCase().replace(".", "");
|
||||||
|
if (address instanceof X10Address) {
|
||||||
|
name = "x10-" + name;
|
||||||
|
}
|
||||||
|
return getBindingDataFilePath(MSG_EVENTS_FILE_PREFIX + "-" + name + ".log");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void clearMonitorFiles(String address) {
|
private void clearMonitorFiles(@Nullable DeviceAddress address) {
|
||||||
String prefix = ALL_OPTION.equals(address) ? MSG_EVENTS_FILE_PREFIX
|
String prefix = address == null ? MSG_EVENTS_FILE_PREFIX
|
||||||
: getMsgEventsFilePath(address).getFileName().toString();
|
: getMsgEventsFilePath(address).getFileName().toString();
|
||||||
|
|
||||||
getBindingDataFilePaths(prefix).map(Path::toFile).forEach(File::delete);
|
getBindingDataFilePaths(prefix).map(Path::toFile).forEach(File::delete);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logMessageEvent(InsteonAddress address, Msg msg) {
|
private void logMessageEvent(Msg msg) {
|
||||||
|
try {
|
||||||
|
DeviceAddress address = getMsgEventAddress(msg);
|
||||||
|
if (monitorAllDevices || monitoredAddresses.contains(address)) {
|
||||||
String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
|
String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
|
||||||
String line = timestamp + " " + msg + System.lineSeparator();
|
String line = timestamp + " " + msg + System.lineSeparator();
|
||||||
Path path = getMsgEventsFilePath(address.toString());
|
Path path = getMsgEventsFilePath(address);
|
||||||
|
|
||||||
try {
|
|
||||||
Files.createDirectories(path.getParent());
|
Files.createDirectories(path.getParent());
|
||||||
Files.writeString(path, line, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
|
Files.writeString(path, line, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
|
||||||
|
}
|
||||||
|
} catch (FieldException e) {
|
||||||
|
logger.warn("failed to parse message", e);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.warn("failed to write to message event file", e);
|
logger.warn("failed to write to message event file", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void listMonitoredDevices(Console console) {
|
private void listMonitoredDevices(Console console) {
|
||||||
String addresses = monitoredAddresses.stream().map(InsteonAddress::toString).collect(Collectors.joining(", "));
|
String addresses = monitoredAddresses.stream().map(DeviceAddress::toString).collect(Collectors.joining(", "));
|
||||||
if (!addresses.isEmpty()) {
|
if (!addresses.isEmpty()) {
|
||||||
console.println("The monitored device(s) are: " + addresses);
|
console.println("The monitored device(s) are: " + addresses);
|
||||||
} else if (monitorAllDevices) {
|
} else if (monitorAllDevices) {
|
||||||
@ -292,30 +307,27 @@ public class DebugCommand extends InsteonCommand implements PortListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startMonitoring(Console console, String address) {
|
private void startMonitoring(Console console, String arg) {
|
||||||
if (ALL_OPTION.equals(address)) {
|
if (monitorAllDevices) {
|
||||||
if (!monitorAllDevices) {
|
console.println("Already monitoring all devices.");
|
||||||
|
} else if (ALL_OPTION.equals(arg)) {
|
||||||
monitorAllDevices = true;
|
monitorAllDevices = true;
|
||||||
monitoredAddresses.clear();
|
monitoredAddresses.clear();
|
||||||
console.println("Started monitoring all devices.");
|
console.println("Started monitoring all devices.");
|
||||||
console.println("Message events logged in " + getBindingDataDirPath());
|
console.println("Message events logged in " + getBindingDataDirPath());
|
||||||
clearMonitorFiles(address);
|
clearMonitorFiles(null);
|
||||||
} else {
|
} else {
|
||||||
console.println("Already monitoring all devices.");
|
DeviceAddress address = InsteonAddress.isValid(arg) ? new InsteonAddress(arg)
|
||||||
}
|
: X10Address.isValid(arg) ? new X10Address(arg) : null;
|
||||||
} else if (InsteonAddress.isValid(address)) {
|
if (address == null) {
|
||||||
if (monitorAllDevices) {
|
console.println("Invalid device address argument: " + arg);
|
||||||
console.println("Already monitoring all devices.");
|
} else if (monitoredAddresses.add(address)) {
|
||||||
} else if (monitoredAddresses.add(new InsteonAddress(address))) {
|
|
||||||
console.println("Started monitoring the device " + address + ".");
|
console.println("Started monitoring the device " + address + ".");
|
||||||
console.println("Message events logged in " + getMsgEventsFilePath(address));
|
console.println("Message events logged in " + getMsgEventsFilePath(address));
|
||||||
clearMonitorFiles(address);
|
clearMonitorFiles(address);
|
||||||
} else {
|
} else {
|
||||||
console.println("Already monitoring the device " + address + ".");
|
console.println("Already monitoring the device " + address + ".");
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
console.println("Invalid device address" + address + ".");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!monitoring) {
|
if (!monitoring) {
|
||||||
@ -324,31 +336,26 @@ public class DebugCommand extends InsteonCommand implements PortListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stopMonitoring(Console console, String address) {
|
private void stopMonitoring(Console console, String arg) {
|
||||||
if (!monitoring) {
|
if (ALL_OPTION.equals(arg)) {
|
||||||
console.println("Not monitoring any devices.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ALL_OPTION.equals(address)) {
|
|
||||||
if (monitorAllDevices) {
|
if (monitorAllDevices) {
|
||||||
monitorAllDevices = false;
|
monitorAllDevices = false;
|
||||||
console.println("Stopped monitoring all devices.");
|
console.println("Stopped monitoring all devices.");
|
||||||
} else {
|
} else {
|
||||||
console.println("Not monitoring all devices.");
|
console.println("Not monitoring all devices.");
|
||||||
}
|
}
|
||||||
} else if (InsteonAddress.isValid(address)) {
|
} else {
|
||||||
|
DeviceAddress address = InsteonAddress.isValid(arg) ? new InsteonAddress(arg)
|
||||||
|
: X10Address.isValid(arg) ? new X10Address(arg) : null;
|
||||||
if (monitorAllDevices) {
|
if (monitorAllDevices) {
|
||||||
console.println("Not monitoring individual devices.");
|
console.println("Not monitoring individual devices.");
|
||||||
} else if (monitoredAddresses.remove(new InsteonAddress(address))) {
|
} else if (address == null) {
|
||||||
|
console.println("Invalid device address argument: " + arg);
|
||||||
|
} else if (monitoredAddresses.remove(address)) {
|
||||||
console.println("Stopped monitoring the device " + address + ".");
|
console.println("Stopped monitoring the device " + address + ".");
|
||||||
} else {
|
} else {
|
||||||
console.println("Not monitoring the device " + address + ".");
|
console.println("Not monitoring the device " + address + ".");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
console.println("Invalid address device address " + address + ".");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!monitorAllDevices && monitoredAddresses.isEmpty()) {
|
if (!monitorAllDevices && monitoredAddresses.isEmpty()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user