Skip to content

Commit

Permalink
Extract Interactive CLI code in a dedicated class (leshan-client-demo)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Aug 3, 2021
1 parent 80e072e commit a5ca5c7
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.security.interfaces.ECPublicKey;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import org.eclipse.californium.core.network.config.NetworkConfig;
import org.eclipse.californium.elements.Connector;
Expand All @@ -45,6 +44,7 @@
import org.eclipse.leshan.client.demo.cli.ExecutionExceptionHandler;
import org.eclipse.leshan.client.demo.cli.LeshanClientDemoCLI;
import org.eclipse.leshan.client.demo.cli.ShortErrorMessageHandler;
import org.eclipse.leshan.client.demo.cli.interactive.InteractiveCLI;
import org.eclipse.leshan.client.engine.DefaultRegistrationEngineFactory;
import org.eclipse.leshan.client.object.Server;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
Expand Down Expand Up @@ -118,18 +118,40 @@ public static void main(String[] args) {
LeshanClientDemoCLI cli = new LeshanClientDemoCLI();
CommandLine command = new CommandLine(cli).setParameterExceptionHandler(new ShortErrorMessageHandler())
.setExecutionExceptionHandler(new ExecutionExceptionHandler());
// handle exit code error
// Handle exit code error
int exitCode = command.execute(args);
if (exitCode != 0)
System.exit(exitCode);
// handle help or version command
// Handle help or version command
if (command.isUsageHelpRequested() || command.isVersionHelpRequested())
System.exit(0);

// create and start client
try {
createAndStartClient(cli);
// Create Client
LwM2mModel model = createModel(cli);
final LeshanClient client = createClient(cli, model);

// Print commands help
InteractiveCLI console = new InteractiveCLI(client, model);
console.showHelp();

// Start the client
client.start();

// De-register on shutdown and stop client.
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
client.destroy(true); // send de-registration request before destroy
}
});

// Start interactive console
console.start();

} catch (Exception e) {

// Handler Execution Error
PrintWriter printer = command.getErr();
printer.print(command.getColorScheme().errorText("Unable to create and start client ..."));
printer.printf("%n%n");
Expand All @@ -139,20 +161,23 @@ public static void main(String[] args) {
}
}

public static void createAndStartClient(LeshanClientDemoCLI cli) throws Exception {
// create Leshan client from command line option
final MyLocation locationInstance = new MyLocation(cli.location.position.latitude,
cli.location.position.longitude, cli.location.scaleFactor);
private static LwM2mModel createModel(LeshanClientDemoCLI cli) throws Exception {

// Initialize model
List<ObjectModel> models = ObjectLoader.loadDefault();
models.addAll(ObjectLoader.loadDdfResources("/models", modelPaths));
if (cli.main.modelsFolder != null) {
models.addAll(ObjectLoader.loadObjectsFromDir(cli.main.modelsFolder, true));
}

return new StaticModel(models);
}

public static LeshanClient createClient(LeshanClientDemoCLI cli, LwM2mModel model) throws Exception {
// create Leshan client from command line option
final MyLocation locationInstance = new MyLocation(cli.location.position.latitude,
cli.location.position.longitude, cli.location.scaleFactor);

// Initialize object list
final LwM2mModel model = new StaticModel(models);
final ObjectsInitializer initializer = new ObjectsInitializer(model);
if (cli.main.bootstrap) {
if (cli.identity.isPSK()) {
Expand Down Expand Up @@ -369,84 +394,6 @@ public void objectAdded(LwM2mObjectEnabler object) {
Hex.encodeHexString(cli.identity.getX509().cprik.getEncoded()));
}

// Print commands help
StringBuilder commandsHelp = new StringBuilder("Commands available :");
commandsHelp.append(System.lineSeparator());
commandsHelp.append(System.lineSeparator());
commandsHelp.append(" - create <objectId> : to enable a new object.");
commandsHelp.append(System.lineSeparator());
commandsHelp.append(" - delete <objectId> : to disable a new object.");
commandsHelp.append(System.lineSeparator());
commandsHelp.append(" - update : to trigger a registration update.");
commandsHelp.append(System.lineSeparator());
commandsHelp.append(" - w : to move to North.");
commandsHelp.append(System.lineSeparator());
commandsHelp.append(" - a : to move to East.");
commandsHelp.append(System.lineSeparator());
commandsHelp.append(" - s : to move to South.");
commandsHelp.append(System.lineSeparator());
commandsHelp.append(" - d : to move to West.");
commandsHelp.append(System.lineSeparator());
LOG.info(commandsHelp.toString());

// Start the client
client.start();

// De-register on shutdown and stop client.
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
client.destroy(true); // send de-registration request before destroy
}
});

// Change the location through the Console
try (Scanner scanner = new Scanner(System.in)) {
List<Character> wasdCommands = Arrays.asList('w', 'a', 's', 'd');
while (scanner.hasNext()) {
String command = scanner.next();
if (command.startsWith("create")) {
try {
int objectId = scanner.nextInt();
if (client.getObjectTree().getObjectEnabler(objectId) != null) {
LOG.info("Object {} already enabled.", objectId);
}
if (model.getObjectModel(objectId) == null) {
LOG.info("Unable to enable Object {} : there no model for this.", objectId);
} else {
ObjectsInitializer objectsInitializer = new ObjectsInitializer(model);
objectsInitializer.setDummyInstancesForObject(objectId);
LwM2mObjectEnabler object = objectsInitializer.create(objectId);
client.getObjectTree().addObjectEnabler(object);
}
} catch (Exception e) {
// skip last token
scanner.next();
LOG.info("Invalid syntax, <objectid> must be an integer : create <objectId>");
}
} else if (command.startsWith("delete")) {
try {
int objectId = scanner.nextInt();
if (objectId == 0 || objectId == 0 || objectId == 3) {
LOG.info("Object {} can not be disabled.", objectId);
} else if (client.getObjectTree().getObjectEnabler(objectId) == null) {
LOG.info("Object {} is not enabled.", objectId);
} else {
client.getObjectTree().removeObjectEnabler(objectId);
}
} catch (Exception e) {
// skip last token
scanner.next();
LOG.info("\"Invalid syntax, <objectid> must be an integer : delete <objectId>");
}
} else if (command.startsWith("update")) {
client.triggerRegistrationUpdate();
} else if (command.length() == 1 && wasdCommands.contains(command.charAt(0))) {
locationInstance.moveLocation(command);
} else {
LOG.info("Unknown command '{}'", command);
}
}
}
return client;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*******************************************************************************
* Copyright (c) 2021 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.client.demo.cli.interactive;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import org.eclipse.leshan.client.californium.LeshanClient;
import org.eclipse.leshan.client.demo.MyLocation;
import org.eclipse.leshan.client.resource.LwM2mInstanceEnabler;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.core.LwM2mId;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class InteractiveCLI {

private static final Logger LOG = LoggerFactory.getLogger(InteractiveCLI.class);

private LeshanClient client;
private LwM2mModel model;

public InteractiveCLI(LeshanClient client, LwM2mModel model) throws IOException {
this.client = client;
this.model = model;
}

public void showHelp() {
// Print commands help
StringBuilder commandsHelp = new StringBuilder("Commands available :");
commandsHelp.append(System.lineSeparator());
commandsHelp.append(System.lineSeparator());
commandsHelp.append(" - create <objectId> : to enable a new object.");
commandsHelp.append(System.lineSeparator());
commandsHelp.append(" - delete <objectId> : to disable a new object.");
commandsHelp.append(System.lineSeparator());
commandsHelp.append(" - update : to trigger a registration update.");
commandsHelp.append(System.lineSeparator());
commandsHelp.append(" - w : to move to North.");
commandsHelp.append(System.lineSeparator());
commandsHelp.append(" - a : to move to East.");
commandsHelp.append(System.lineSeparator());
commandsHelp.append(" - s : to move to South.");
commandsHelp.append(System.lineSeparator());
commandsHelp.append(" - d : to move to West.");
commandsHelp.append(System.lineSeparator());
LOG.info(commandsHelp.toString());
}

public void start() throws IOException {
// Change the location through the Console
try (Scanner scanner = new Scanner(System.in)) {
List<Character> wasdCommands = Arrays.asList('w', 'a', 's', 'd');
while (scanner.hasNext()) {
String command = scanner.next();
if (command.startsWith("create")) {
try {
int objectId = scanner.nextInt();
if (client.getObjectTree().getObjectEnabler(objectId) != null) {
LOG.info("Object {} already enabled.", objectId);
}
if (model.getObjectModel(objectId) == null) {
LOG.info("Unable to enable Object {} : there no model for this.", objectId);
} else {
ObjectsInitializer objectsInitializer = new ObjectsInitializer(model);
objectsInitializer.setDummyInstancesForObject(objectId);
LwM2mObjectEnabler object = objectsInitializer.create(objectId);
client.getObjectTree().addObjectEnabler(object);
}
} catch (Exception e) {
// skip last token
scanner.next();
LOG.info("Invalid syntax, <objectid> must be an integer : create <objectId>");
}
} else if (command.startsWith("delete")) {
try {
int objectId = scanner.nextInt();
if (objectId == 0 || objectId == 0 || objectId == 3) {
LOG.info("Object {} can not be disabled.", objectId);
} else if (client.getObjectTree().getObjectEnabler(objectId) == null) {
LOG.info("Object {} is not enabled.", objectId);
} else {
client.getObjectTree().removeObjectEnabler(objectId);
}
} catch (Exception e) {
// skip last token
scanner.next();
LOG.info("\"Invalid syntax, <objectid> must be an integer : delete <objectId>");
}
} else if (command.startsWith("update")) {
client.triggerRegistrationUpdate();
} else if (command.length() == 1 && wasdCommands.contains(command.charAt(0))) {
LwM2mObjectEnabler objectEnabler = client.getObjectTree().getObjectEnabler(LwM2mId.LOCATION);
if (objectEnabler != null && objectEnabler instanceof ObjectEnabler) {
LwM2mInstanceEnabler instance = ((ObjectEnabler) objectEnabler).getInstance(0);
if (instance instanceof MyLocation) {
((MyLocation) instance).moveLocation(command);
}
}
} else {
LOG.info("Unknown command '{}'", command);
}
}
}
}
}

0 comments on commit a5ca5c7

Please sign in to comment.