📄 commandlinespotselector.java
字号:
/* * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is * described in this document. In particular, and without limitation, these intellectual property rights may * include one or more of the U.S. patents listed at http://www.sun.com/patents and one or more additional patents * or pending patent applications in the U.S. and in other countries. * * U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. * standard license agreement and applicable provisions of the FAR and its supplements. * * Use is subject to license terms. * * This distribution may include materials developed by third parties. Sun, Sun Microsystems, the Sun logo and * Java are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. * * Copyright (c) 2006 Sun Microsystems, Inc. Tous droits r?serv?s. * * Sun Microsystems, Inc. d?tient les droits de propri?t? intellectuels relatifs ? la technologie incorpor?e dans * le produit qui est d?crit dans ce document. En particulier, et ce sans limitation, ces droits de propri?t? * intellectuelle peuvent inclure un ou plus des brevets am?ricains list?s ? l'adresse http://www.sun.com/patents * et un ou les brevets suppl?mentaires ou les applications de brevet en attente aux Etats - Unis et dans les * autres pays. * * L'utilisation est soumise aux termes du contrat de licence. * * Cette distribution peut comprendre des composants d?velopp?s par des tierces parties. * Sun, Sun Microsystems, le logo Sun et Java sont des marques de fabrique ou des marques d?pos?es de Sun * Microsystems, Inc. aux Etats-Unis et dans d'autres pays. */package com.sun.spot.spotworld.usb.spotselector;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Iterator;/** * The CommandLineSpotSelector class determines the serial port to use from a * given list of suitable ports. If it cannot decide it can ask the user via a * command line prompt to select the device. * * @author Christian P黨ringer */public class CommandLineSpotSelector extends SpotSelector { public CommandLineSpotSelector(String spotPorts) { this(spotPorts, "", false, false, false, false, false); } public CommandLineSpotSelector(String spotPorts, String defaultPort, boolean isFindBaseStation, boolean isScanAllBaseStations, boolean isInteractive, boolean isVerbose, boolean isVeryVerbose) { if ((defaultPort == null) || (defaultPort.equals("") || defaultPort.startsWith(("$")))) { hasSunSpotDefaultPort = false; this.sunSpotDefaultPort = ""; } else { hasSunSpotDefaultPort = true; this.sunSpotDefaultPort = defaultPort; } this.isFindBaseStation = isFindBaseStation; this.isScanAllBaseStations = isScanAllBaseStations; this.isVerbose = isVerbose; if (isVeryVerbose) { this.isVeryVerbose = true; this.isVerbose = true; } if (isVerbose) infoOut.println("CommandLineSpotSelector: Ports: " + spotPorts + " \nDefaultPort:" + defaultPort + "\nisInteractive:" + isInteractive + "\nisFindBaseStation:" + isFindBaseStation + "\nisScanAllBaseStations:" + isScanAllBaseStations); this.portList = spotPorts; this.isInteractive = isInteractive; spotInfoTable = new SpotInfoTable(spotPorts, isVerbose, isVeryVerbose); } private void updateBaseStationLastProperty(String baseStationPort) { (new SunSpotProperties()) .writeProperty( "spotselector.basestation.lastport", baseStationPort, " Don't modify. Property" + " reflects last base station port which was used, and is used by spotfinder to minimize search time."); } protected String getPortOfSpotToUse() { // Filter for basestations if (isFindBaseStation) { String basestationLastPort = System.getProperty("spotselector.basestation.lastport", ""); if (isVerbose) infoOut.println("baseStationLastPort: " + basestationLastPort); spotInfoTable.removeNonBaseStations(basestationLastPort, isScanAllBaseStations); } // TODO: Perhaps allow removing all spots which are base stations. // If we are looking for a base station port, we don't care whether a // an other port was defined as the base station has. // TODO: Perhaps for next releases: allow defining a default basestation // port and a default basestation ID. String portOrErrorMessage; if (isFindBaseStation) { portOrErrorMessage = determinePortOrErrorMessage("Sun SPOT basestation", false, ""); if (portOrErrorMessage.indexOf(ERROR_STRING) == -1) { if (isVeryVerbose) System.out .println("updating spotselector.basestation.lastport in user properties file"); updateBaseStationLastProperty(portOrErrorMessage); } } else portOrErrorMessage = determinePortOrErrorMessage("Sun SPOT device", hasSunSpotDefaultPort, sunSpotDefaultPort); if (isVeryVerbose) infoOut.println("choosen Port:" + portOrErrorMessage); return portOrErrorMessage; } private String determinePortOrErrorMessage(String sunSpotString, boolean hasDefaultPort, String defaultPort) { // No appropriate SunSPOT found. if (spotInfoTable.size() == 0) { if (isVerbose) infoOut.println("No " + sunSpotString + " is connected"); return getNoAppropriateSpotFoundErrorMessage(sunSpotString); } // Exactly one appropriate SunSPOT found (and no default port is // defined) if ((spotInfoTable.size() == 1) && (!hasDefaultPort)) { String serialPort = ((SpotInfo) spotInfoTable.iterator().next()).serialPort; return serialPort; } // An appropriate device with on the specified defaultport was found // if (spotInfoTable.containsPort(defaultPort)) { return defaultPort; } // More than one, or another spot then defined found if (!isInteractive) { // spotselector is not interactive, exit with error. return getNonInteractiveAndUserDecisionRequiredErrorMessage(sunSpotString); } // Interactive // In some environments (in particular eclipse) ant doesn't support // console input, // then the following spotselection will just hang forever. infoOut.println("If the selection does not respond to\n" + "input, cancel the build and set the\n" + "property spotselector.noninteractive=true\n\n"); if (spotInfoTable.size() > 1) { infoOut.println(sunSpotString + "s found on multiple ports:"); } else infoOut.println("No " + sunSpotString + " found on " + defaultPort + ".\nAvailable ports:"); Iterator it = spotInfoTable.iterator(); int i = 1; while (it.hasNext()) { infoOut.println("\t" + i + ". " + it.next()); i++; } BufferedReader inputR = new BufferedReader(new InputStreamReader(System.in)); int indexOfSpotInfo = -1; boolean isOk = false; String selectionPossibilitiesString; if (spotInfoTable.size() == 1) selectionPossibilitiesString = "[1]"; else selectionPossibilitiesString = "[1-" + spotInfoTable.size() + "]"; do { infoOut.println("Input your port selection " + selectionPossibilitiesString + " or 'c' to cancel:"); String input; try { input = inputR.readLine(); } catch (IOException ex) { return ERROR_STRING + ":\n IOException occured when reading console input. \n (" + ex.getMessage() + ")"; } if (input.equalsIgnoreCase("c")) { return getUserCancelledErrorMessage(); } try { indexOfSpotInfo = Integer.parseInt(input); if ((indexOfSpotInfo >= 1) && (indexOfSpotInfo <= spotInfoTable.size())) { isOk = true; infoOut.println("Choosen: " + indexOfSpotInfo); } } catch (NumberFormatException ex) { } } while (!isOk); String serialPort = ((SpotInfo) spotInfoTable.get(indexOfSpotInfo - 1)).serialPort; return serialPort; } private String getNonInteractiveAndUserDecisionRequiredErrorMessage( String sunSpotString) { String errorMessage = ""; if (!hasSunSpotDefaultPort) errorMessage = ERROR_STRING + ":\n Found " + sunSpotString + "s on multiple ports:\n" + spotInfoTable.toString() + ".\n" + "Specify one of these using the spotport\n" + "property (either in a properties file or\n" + "using -Dspotport=XXX on the commandline).\n\n" + "NOTE: Upgrading to ant version >= 1.6.3\n" + "will enable interactive selection of\n" + "available ports. \n(Except ant is executed from within eclipse)"; else { errorMessage = ERROR_STRING + ":\n No Sun SPOT device found on " + sunSpotDefaultPort + "\n" + "Available port(s):\n" + " " + spotInfoTable.toString() + ".\n" + "Specify an available port using the spotport\n" + "property (either in a properties file or\n" + "using -Dspotport=XXX on the commandline).\n\n" + "NOTE: Upgrading to ant version >= 1.6.3\n" + "will enable interactive selection of\n" + "available ports. \n(Except ant is executed from within eclipse)"; } return errorMessage; } private String getUserCancelledErrorMessage() { return ERROR_STRING + ":\n Operation cancelled by user."; } private String getNoAppropriateSpotFoundErrorMessage(String sunSpotString) { String errorString = ERROR_STRING + ":\n No " + sunSpotString + " found. Connect a\n" + sunSpotString + " and retry or, if you\n" + "know that one is already connected,\n" + "specify its port using the \"port\"\n" + "property (either in a properties file\n" + "or using -Dport=XXX on the commandline).\n" + "This will force the build scripts to\n" + "use that port.\n\n"; if (spotInfoTable.containedDevicesInUse) errorString += " At least one connected SunSPOT device\n" + "is in use by another application.\n" + "If this device is your basestation,\n" + "close the other application and retry.\n\n"; errorString += spotInfoTable.getRemovedDevicesString(); return errorString; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -