tuwien.auto.calimero.tools.PropClient Maven / Gradle / Ivy
Show all versions of calimero-tools Show documentation
/*
Calimero 2 - A library for KNX network access
Copyright (c) 2006, 2016 B. Malinowsky
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under terms
of your choice, provided that you also meet, for each linked independent
module, the terms and conditions of the license of that module. An
independent module is a module which is not derived from or based on
this library. If you modify this library, you may extend this exception
to your version of the library, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from your
version.
*/
package tuwien.auto.calimero.tools;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.util.StringTokenizer;
import tuwien.auto.calimero.exception.KNXIllegalArgumentException;
import tuwien.auto.calimero.log.LogLevel;
import tuwien.auto.calimero.log.LogManager;
import tuwien.auto.calimero.log.LogStreamWriter;
import tuwien.auto.calimero.log.LogWriter;
import tuwien.auto.calimero.mgmt.PropertyAdapter;
import tuwien.auto.calimero.mgmt.PropertyClient;
/**
* A tool for Calimero showing features of the {@link PropertyClient} used for KNX
* property access.
*
* PropClient is a console based tool implementation for reading and writing KNX
* properties. It supports network access using a KNXnet/IP connection or FT1.2
* connection. To start the PropClient, invoke the main
-method of this class.
* Take a look at the command line options to configure the tool with the desired
* communication settings.
*
* The main part of this tool implementation interacts with the PropertyClient interface,
* which offers high level access to KNX property information. It also shows creation of
* the {@link PropertyAdapter}, necessary for a property client to work. All queried
* property values, as well as occurring problems are written to System.out
*
.
*
* @author B. Malinowsky
*/
public class PropClient implements Runnable
{
class PropertyEx extends tuwien.auto.calimero.tools.Property
{
PropertyEx(final String[] args)
{
super(args);
}
protected void runCommand(final String[] cmd)
{
// ignore any command supplied on command line
options.remove("command");
System.out.println("exit - close connection and exit");
System.out.println();
// show some command info
super.runCommand(new String[] { "?" });
runReaderLoop(PropClient.this);
}
private void runReaderLoop(final PropClient propClient)
{
// create reader for user input
final BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
String[] args;
try {
while ((args = propClient.readLine(r)) != null) {
if (args.length > 0) {
if ("exit".equalsIgnoreCase(args[0]))
break;
super.runCommand(args);
}
}
}
catch (final InterruptedException e) {
System.out.println("received quit (interrupt), closing ...");
}
catch (final InterruptedIOException e) {
System.out.println("received quit (interrupt), closing ...");
}
catch (final IOException e) {
System.out.println("I/O error, " + e.getMessage());
}
}
}
private final PropertyEx property;
/**
* Constructs a new PropClient.
*
*
* @param args options for the property client tool, see {@link #main(String[])}
* @throws KNXIllegalArgumentException on missing or wrong formatted option value
*/
public PropClient(final String[] args)
{
property = new PropertyEx(args);
}
/**
* Entry point for running the PropClient.
*
* An IP host or port identifier has to be supplied to specify the endpoint for the
* KNX network access.
* To show the usage message of this tool on the console, supply the command line
* option -help (or -h).
* Command line options are treated case sensitive. Available options for the property
* client:
*
* -help -h
show help message
* -version
show tool/library version and exit
* -verbose -v
enable verbose status output
* -local -l
local device management
* -remote -r
KNX addr remote property service
* -definitions -d
file use property definition file
* -localhost
id local IP/host name
* -localport
number local UDP port (default system
* assigned)
* -port -p
number UDP port on host (default 3671)
* -nat -n
enable Network Address Translation
* -serial -s
use FT1.2 serial communication
* -tpuart
use TP-UART communication
*
* For local device management these options are available:
*
* -emulatewriteenable -e
check write-enable of a property
*
* For remote property service these options are available:
*
* -routing
use KNXnet/IP routing
* -medium -m
id KNX medium [tp1|p110|rf]
* (defaults to tp1)
* -connect -c
connection oriented mode
* -authorize -a
key authorize key to access KNX device
*
*
* @param args command line options for property client
*/
public static void main(final String[] args)
{
final LogWriter w = LogStreamWriter.newUnformatted(LogLevel.INFO, System.out, true, false);
Property.out.addWriter(w);
try {
final PropClient pc = new PropClient(args);
pc.run();
}
catch (final Throwable t) {
Property.out.error("client error", t);
}
LogManager.getManager().shutdown(true);
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run()
{
property.run();
}
/**
* Writes command prompt and waits for a command request from the user.
*
*
* @param r input reader
* @return array with command and command arguments
* @throws IOException on I/O error
* @throws InterruptedException on interrupted thread
*/
private String[] readLine(final BufferedReader r) throws IOException, InterruptedException
{
System.out.print("> ");
synchronized (this) {
while (!r.ready())
wait(200);
}
final String line = r.readLine();
return line != null ? split(line) : null;
}
private static String[] split(final String text)
{
final StringTokenizer st = new StringTokenizer(text, " \t");
final String[] tokens = new String[st.countTokens()];
for (int i = 0; i < tokens.length; ++i)
tokens[i] = st.nextToken();
return tokens;
}
}