コード例 #1
0
ファイル: ZigBee.java プロジェクト: rossbiro/Arduino
  public Object executeCommand(String command, Object argument, long wait)
      throws ZigBeeException, SerialException {
    command = command.toUpperCase();
    Command c = new Command();
    try {
      c.command = ZigBeeCommands.valueOf(command);
    } catch (IllegalArgumentException iae) {
      c.command = null;
    }
    c.commandString = command;
    c.set = (argument != null);
    c.issued = new Date();
    c.broadcast = (c.command == ZigBeeCommands.NI);
    c.argument = argument;

    return executeCommand(c, wait);
  }
コード例 #2
0
ファイル: ZigBee.java プロジェクト: rossbiro/Arduino
  void processCommandResponse(byte[] packet, int offset) throws ZigBeeException, SerialException {
    String command = new String(packet, offset, 2); // commands are all 2 characters.
    Command c;
    synchronized (outstandingCommands) {
      c = outstandingCommands.get(packet[4]);
    }
    if (c == null) {
      System.err.println("Command " + command + " command already timed out or not issued.");
    } else {
      if (c.command != ZigBeeCommands.ND || packet.length == 9) {
        c.status = new Byte(packet[offset + 2]);
        synchronized (outstandingCommands) {
          outstandingCommands.values().remove(c);
        }
      } else {
        c = null;
      }
    }

    if (packet[offset + 2] != 0) {
      // Need to get the message to the user.
      if (c != null && packet[offset + 2] == 4) {
        if (c.retransmits++ < c.maxRetransmits) {
          executeCommand(c, 0);
          return;
        }
      }
      if (c != null) {
        synchronized (c) {
          c.notifyAll();
        }
      }
      return;
    }

    // Did we get some data back as well?
    if (packet.length > offset + 4) {
      command = command.toUpperCase();
      ZigBeeCommands zbc;
      try {
        zbc = ZigBeeCommands.valueOf(command);
        if (c != null) {
          if (zbc.responseString) {
            c.response = bytesToString(packet, offset + 3, packet.length - offset - 1);
          } else {
            c.response = new Long(bytesToNum(packet, offset + 3, 8, packet.length - 1));
          }
        }
        zbc.process(this, packet, offset + 3);
      } catch (IllegalArgumentException iae) {
        if (c != null) {
          c.response = new Long(bytesToNum(packet, offset + 3, 8, packet.length - 1));
        }
        // unhandled message, just ignore it.
      }
    }

    // let any waiters know we are done.
    if (c != null) {
      synchronized (c) {
        c.notifyAll();
      }
    }
  }