Skip to content

Commit

Permalink
use java util logging for example matter java test app (#23943)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google authored and pull[bot] committed Feb 21, 2024
1 parent 27598e5 commit 3277017
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
import com.matter.controller.commands.discover.*;
import com.matter.controller.commands.pairing.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {
private static Logger logger = Logger.getLogger(Main.class.getName());

private static void registerCommandsDiscover(
ChipDeviceController controller,
CommandManager commandManager,
Expand Down Expand Up @@ -109,9 +113,9 @@ public static void main(String[] args) {
try {
commandManager.run(args);
} catch (IllegalArgumentException e) {
System.out.println("Arguments init failed with exception: " + e.getMessage());
logger.log(Level.INFO, "Arguments init failed with exception: " + e.getMessage());
} catch (Exception e) {
System.out.println("Run command failed with exception: " + e.getMessage());
logger.log(Level.INFO, "Run command failed with exception: " + e.getMessage());
}
controller.shutdownCommissioning();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class CommandManager {
private final ArrayList<Command> mCommandMgr = new ArrayList<Command>();
private final Map<String, ArrayList<Command>> mClusters =
new HashMap<String, ArrayList<Command>>();
private static Logger logger = Logger.getLogger(CommandManager.class.getName());

public final void register(String clusterName, ArrayList<Command> commandsList) {
mClusters.put(clusterName, commandsList);
Expand All @@ -37,20 +40,20 @@ public final void run(String[] args) {
Command command;

if (args.length < 1) {
System.out.println("Missing cluster name");
logger.log(Level.INFO, "Missing cluster name");
showClusters();
return;
}

ArrayList<Command> commands = mClusters.get(args[0]);
if (commands == null) {
System.out.println("Unknown cluster: " + args[0]);
logger.log(Level.INFO, "Unknown cluster: " + args[0]);
showClusters();
return;
}

if (args.length < 2) {
System.out.println("Missing command name");
logger.log(Level.INFO, "Missing command name");
showCluster(args[0], commands);
return;
}
Expand All @@ -64,27 +67,27 @@ public final void run(String[] args) {
}
} else if (isEventCommand(args[1])) {
if (args.length < 3) {
System.out.println("Missing event name");
logger.log(Level.INFO, "Missing event name");
showClusterEvents(args[0], args[1], commands);
throw new IllegalArgumentException();
}

command = getGlobalCommand(commands, args[1], args[2]);
if (command == null) {
System.out.println("Unknown event: " + args[2]);
logger.log(Level.INFO, "Unknown event: " + args[2]);
showClusterEvents(args[0], args[1], commands);
throw new IllegalArgumentException();
}
} else {
if (args.length < 3) {
System.out.println("Missing attribute name");
logger.log(Level.INFO, "Missing attribute name");
showClusterAttributes(args[0], args[1], commands);
throw new IllegalArgumentException();
}

command = getGlobalCommand(commands, args[1], args[2]);
if (command == null) {
System.out.println("Unknown attribute: " + args[2]);
logger.log(Level.INFO, "Unknown attribute: " + args[2]);
showClusterAttributes(args[0], args[1], commands);
throw new IllegalArgumentException();
}
Expand All @@ -100,7 +103,7 @@ public final void run(String[] args) {
System.out.println("Run command failed with exception: " + e.getMessage());
showCommand(args[0], command);
} catch (Exception e) {
System.out.println("Run command failed with exception: " + e.getMessage());
logger.log(Level.INFO, "Run command failed with exception: " + e.getMessage());
}
}

Expand Down Expand Up @@ -140,34 +143,43 @@ private Command getGlobalCommand(
}

private void showClusters() {
System.out.println("Usage:");
System.out.println(" java-matter-controller cluster_name command_name [param1 param2 ...]");
System.out.println("\n");
System.out.println(
logger.log(Level.INFO, "Usage:");
logger.log(
Level.INFO, " java-matter-controller cluster_name command_name [param1 param2 ...]");
logger.log(Level.INFO, "\n");
logger.log(
Level.INFO,
" +-------------------------------------------------------------------------------------+");
System.out.println(
logger.log(
Level.INFO,
" | Clusters: |");
System.out.println(
logger.log(
Level.INFO,
" +-------------------------------------------------------------------------------------+");

for (String key : mClusters.keySet()) {
System.out.printf(" | * %-82s|\n", key.toLowerCase());
}

System.out.println(
logger.log(
Level.INFO,
" +-------------------------------------------------------------------------------------+");
}

private void showCluster(String clusterName, ArrayList<Command> commands) {
System.out.println("Usage:");
System.out.println(
logger.log(Level.INFO, "Usage:");
logger.log(
Level.INFO,
" java-matter-controller " + clusterName + " command_name [param1 param2 ...]");
System.out.println("\n");
System.out.println(
logger.log(Level.INFO, "\n");
logger.log(
Level.INFO,
" +-------------------------------------------------------------------------------------+");
System.out.println(
logger.log(
Level.INFO,
" | Commands: |");
System.out.println(
logger.log(
Level.INFO,
" +-------------------------------------------------------------------------------------+");
boolean readCommand = false;
boolean writeCommand = false;
Expand Down Expand Up @@ -198,57 +210,66 @@ private void showCluster(String clusterName, ArrayList<Command> commands) {
System.out.printf(" | * %-82s|\n", cmdName);
}
}
System.out.println(
logger.log(
Level.INFO,
" +-------------------------------------------------------------------------------------+\n");
}

private void showClusterAttributes(
String clusterName, String commandName, ArrayList<Command> commands) {
System.out.println("Usage:");
logger.log(Level.INFO, "Usage:");
System.out.printf(
" java-matter-controller %s %s attribute-name [param1 param2 ...]\n",
clusterName, commandName);
System.out.println("\n");
System.out.println(
logger.log(Level.INFO, "\n");
logger.log(
Level.INFO,
" +-------------------------------------------------------------------------------------+");
System.out.println(
logger.log(
Level.INFO,
" | Attributes: |");
System.out.println(
logger.log(
Level.INFO,
" +-------------------------------------------------------------------------------------+");
for (Command command : commands) {
if (commandName.equals(command.getName())) {
System.out.printf(" | * %-82s|\n", command.getAttribute().get());
}
}
System.out.println(
logger.log(
Level.INFO,
" +-------------------------------------------------------------------------------------+");
}

private void showClusterEvents(
String clusterName, String commandName, ArrayList<Command> commands) {
System.out.println("Usage:");
logger.log(Level.INFO, "Usage:");
System.out.printf(
" java-matter-controller %s %s event-name [param1 param2 ...]\n",
clusterName, commandName);
System.out.println("\n");
System.out.println(
logger.log(Level.INFO, "\n");
logger.log(
Level.INFO,
" +-------------------------------------------------------------------------------------+");
System.out.println(
logger.log(
Level.INFO,
" | Events: |");
System.out.println(
logger.log(
Level.INFO,
" +-------------------------------------------------------------------------------------+");

for (Command command : commands) {
if (commandName.equals(command.getName())) {
System.out.printf(" | * %-82s|\n", command.getAttribute().get());
}
}
System.out.println(
logger.log(
Level.INFO,
" +-------------------------------------------------------------------------------------+");
}

private void showCommand(String clusterName, Command command) {
System.out.println("Usage:");
logger.log(Level.INFO, "Usage:");

String arguments = command.getName();
String description = "";
Expand Down Expand Up @@ -284,7 +305,7 @@ private void showCommand(String clusterName, Command command) {
}

if (!description.isEmpty()) {
System.out.println(description);
logger.log(Level.INFO, description);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class PairingCommand extends MatterCommand
implements ChipDeviceController.CompletionListener {
Expand All @@ -48,6 +50,8 @@ public abstract class PairingCommand extends MatterCommand
private final StringBuffer mOnboardingPayload = new StringBuffer();
private final StringBuffer mDiscoveryFilterInstanceName = new StringBuffer();

private static Logger logger = Logger.getLogger(PairingCommand.class.getName());

public long getNodeId() {
return mNodeId.get();
}
Expand All @@ -66,30 +70,30 @@ public long getTimeoutMillis() {

@Override
public void onConnectDeviceComplete() {
System.out.println("onConnectDeviceComplete");
logger.log(Level.INFO, "onConnectDeviceComplete");
}

@Override
public void onStatusUpdate(int status) {
System.out.println("onStatusUpdate with status: " + status);
logger.log(Level.INFO, "onStatusUpdate with status: " + status);
}

@Override
public void onPairingComplete(int errorCode) {
System.out.println("onPairingComplete with error code: " + errorCode);
logger.log(Level.INFO, "onPairingComplete with error code: " + errorCode);
if (errorCode != 0) {
setTestResult("Failure");
}
}

@Override
public void onPairingDeleted(int errorCode) {
System.out.println("onPairingDeleted with error code: " + errorCode);
logger.log(Level.INFO, "onPairingDeleted with error code: " + errorCode);
}

@Override
public void onCommissioningComplete(long nodeId, int errorCode) {
System.out.println("onCommissioningComplete with error code: " + errorCode);
logger.log(Level.INFO, "onCommissioningComplete with error code: " + errorCode);
if (errorCode == 0) {
setTestResult("Success");
} else {
Expand All @@ -100,33 +104,33 @@ public void onCommissioningComplete(long nodeId, int errorCode) {
@Override
public void onReadCommissioningInfo(
int vendorId, int productId, int wifiEndpointId, int threadEndpointId) {
System.out.println("onReadCommissioningInfo");
logger.log(Level.INFO, "onReadCommissioningInfo");
}

@Override
public void onCommissioningStatusUpdate(long nodeId, String stage, int errorCode) {
System.out.println("onCommissioningStatusUpdate");
logger.log(Level.INFO, "onCommissioningStatusUpdate");
}

@Override
public void onNotifyChipConnectionClosed() {
System.out.println("onNotifyChipConnectionClosed");
logger.log(Level.INFO, "onNotifyChipConnectionClosed");
}

@Override
public void onCloseBleComplete() {
System.out.println("onCloseBleComplete");
logger.log(Level.INFO, "onCloseBleComplete");
}

@Override
public void onError(Throwable error) {
setTestResult(error.toString());
System.out.println("onError with error: " + error.toString());
logger.log(Level.INFO, "onError with error: " + error.toString());
}

@Override
public void onOpCSRGenerationComplete(byte[] csr) {
System.out.println("onOpCSRGenerationComplete");
logger.log(Level.INFO, "onOpCSRGenerationComplete");
for (int i = 0; i < csr.length; i++) {
System.out.print(csr[i] + " ");
}
Expand Down

0 comments on commit 3277017

Please sign in to comment.