public static void main(String[] args) throws IOException {

    Logging.setLoggingDefaults();

    if (args.length > 1) {
      log.info("Searching for {} device with MAC address: {}", args[0], args[1]);
      MoteType type = MoteType.fromString(args[0]);
      if (type == null) {
        log.error("Unknown node type \"{}\" given.", args[0]);
        System.exit(1);
      }

      Map<String, String> telosBReferenceToMACMap = null;
      if (args.length == 3) {
        telosBReferenceToMACMap = readTelosBReferenceToMACMap(args[2]);
        log.info("Using Telos B USB chip ID to MAC mapping: {}", telosBReferenceToMACMap);
      }

      MoteList moteList = MoteListFactory.create(telosBReferenceToMACMap);
      log.info("Found: {}", moteList.getMotePort(type, StringUtils.parseHexOrDecLong(args[1])));

    } else if (args.length == 1) {

      Map<String, String> telosBReferenceToMACMap = null;
      if (args.length == 3) {
        telosBReferenceToMACMap = readTelosBReferenceToMACMap(args[2]);
        log.info("Using Telos B USB chip ID to MAC mapping: {}", telosBReferenceToMACMap);
      }

      log.info(
          "Displaying all connected devices: \n{}",
          MoteListFactory.create(telosBReferenceToMACMap).getMoteList());

    } else {
      log.info(
          "Displaying all connected devices: \n{}", MoteListFactory.create(null).getMoteList());
    }
  }
  public static void main(String[] args) throws Exception {

    Logging.setLoggingDefaults(Level.WARN);

    CommandLineParser parser = new PosixParser();
    Options options = createCommandLineOptions();

    String deviceType = null;
    String port = null;
    Map<String, String> configuration = newHashMap();
    DeviceMacReferenceMap deviceMacReferenceMap = null;
    boolean use16BitMode = true;

    try {

      CommandLine line = parser.parse(options, args, true);

      if (line.hasOption('h')) {
        printUsageAndExit(DeviceMacReaderCLI.class, options, 0);
      }

      if (line.hasOption('v')) {
        org.apache.log4j.Logger.getRootLogger().setLevel(Level.DEBUG);
      }

      if (line.hasOption('l')) {
        Level level = Level.toLevel(line.getOptionValue('l'));
        org.apache.log4j.Logger.getRootLogger().setLevel(level);
      }

      if (line.hasOption('c')) {
        final String configurationFileString = line.getOptionValue('c');
        final File configurationFile = new File(configurationFileString);
        final Properties configurationProperties = new Properties();
        configurationProperties.load(new FileReader(configurationFile));
        for (Map.Entry<Object, Object> entry : configurationProperties.entrySet()) {
          configuration.put((String) entry.getKey(), (String) entry.getValue());
        }
      }

      if (line.hasOption('r')) {
        deviceMacReferenceMap = readDeviceMacReferenceMap(line.getOptionValue('r'));
      }

      assertParametersPresent(line, 't', 'p');

      deviceType = line.getOptionValue('t');
      port = line.getOptionValue('p');
      use16BitMode = !line.hasOption('x');

    } catch (Exception e) {
      log.error("Invalid command line: " + e);
      printUsageAndExit(DeviceMacReaderCLI.class, options, EXIT_CODE_INVALID_ARGUMENTS);
    }

    ExecutorService executorService =
        Executors.newCachedThreadPool(
            new ThreadFactoryBuilder().setNameFormat("DeviceMacReader %d").build());

    final Injector injector =
        Guice.createInjector(
            new DeviceFactoryModule(),
            new DeviceMacReaderModule(executorService, deviceMacReferenceMap, use16BitMode));

    final DeviceMacReader deviceMacReader = injector.getInstance(DeviceMacReader.class);

    String reference = null;
    if (deviceMacReferenceMap != null) {

      final DeviceObserver deviceObserver = injector.getInstance(DeviceObserver.class);
      final ImmutableList<DeviceEvent> events = deviceObserver.getEvents(null);

      for (DeviceEvent event : events) {
        final boolean samePort = port.equals(event.getDeviceInfo().getPort());
        if (samePort) {
          reference = event.getDeviceInfo().getReference();
        }
      }
    }

    try {

      final MacAddress macAddress =
          deviceMacReader.readMac(port, deviceType, configuration, reference);
      log.info(
          "Read MAC address of {} device at port {}: {}",
          new Object[] {deviceType, port, macAddress});
      System.out.println(macAddress.toHexString());
      System.exit(0);

    } catch (Exception e) {
      log.error("Reading MAC address failed with Exception: " + e, e);
      System.exit(1);
    }
  }
Example #3
0
 static {
   Logging.setLoggingDefaults();
 }