Пример #1
0
  private void setDeviceData(DeviceData deviceData) throws Exception {
    final String METHOD = "setDeviceData";
    if (deviceData == null) {
      LoggerUtility.log(
          Level.FINE,
          CLASS_NAME,
          METHOD,
          "Could not create Managed Client " + "without DeviceInformations !");
      throw new Exception("Could not create Managed Client without DeviceInformations !");
    }
    String typeId = deviceData.getTypeId();
    String deviceId = deviceData.getDeviceId();

    if (typeId == null || deviceId == null) {
      LoggerUtility.log(
          Level.FINE,
          CLASS_NAME,
          METHOD,
          "Could not create Managed Client " + "without Device Type or Device ID!");
      throw new Exception(
          "Could not create Managed Client without Device Type or Device ID!, "
              + "Please specify the same in DeviceData");
    }
    this.deviceData = deviceData;
  }
Пример #2
0
  /**
   * Constructor that creates a ManagedDevice object, but does not connect to IBM IoT Foundation
   * connect yet
   *
   * @param options List of options to connect to IBM IoT Foundation Connect
   * @param deviceData The Device Model
   * @throws Exception If the essential parameters are not set
   */
  public ManagedDevice(Properties options, DeviceData deviceData) throws Exception {
    super(options);
    final String METHOD = "constructor";
    if (deviceData == null) {
      LoggerUtility.log(
          Level.FINE,
          CLASS_NAME,
          METHOD,
          "Could not create Managed Client " + "without DeviceInformations !");
      throw new Exception("Could not create Managed Client without DeviceInformations !");
    }
    String typeId = this.getDeviceType();
    String deviceId = this.getDeviceId();

    if (typeId == null || deviceId == null) {
      LoggerUtility.log(
          Level.FINE,
          CLASS_NAME,
          METHOD,
          "Could not create Managed Client " + "without Device Type or Device ID !");
      throw new Exception(
          "Could not create Managed Client without Device Type or Device ID!, "
              + "Please specify the same in properties");
    }
    deviceData.setTypeId(typeId);
    deviceData.setDeviceId(deviceId);
    this.deviceData = deviceData;
  }
Пример #3
0
  /**
   * Send a device manage request to IoT Foundation
   *
   * <p>A device uses this request to become a managed device. It should be the first device
   * management request sent by the device after connecting to the Internet of Things Foundation. It
   * would be usual for a device management agent to send this whenever is starts or restarts.
   *
   * <p>This method connects the device to IoT Foundation connect if its not connected already
   *
   * @param lifetime The length of time in seconds within which the device must send another Manage
   *     device request. if set to 0, the managed device will not become dormant. When set, the
   *     minimum supported setting is 3600 (1 hour).
   * @return True if successful
   * @throws MqttException
   */
  public boolean manage(long lifetime) throws MqttException {

    final String METHOD = "manage";

    LoggerUtility.log(Level.FINE, CLASS_NAME, METHOD, "lifetime value (" + lifetime + ")");

    boolean success = false;
    DeviceTopic topic = DeviceTopic.MANAGE;

    if (!this.isConnected()) {
      this.connect();
    }

    JsonObject jsonPayload = new JsonObject();
    if (deviceData.getDeviceInfo() != null || deviceData.getMetadata() != null) {

      JsonObject supports = new JsonObject();
      supports.add("deviceActions", new JsonPrimitive(this.supportsDeviceActions));
      supports.add("firmwareActions", new JsonPrimitive(this.supportsFirmwareActions));

      JsonObject data = new JsonObject();
      data.add("supports", supports);
      if (deviceData.getDeviceInfo() != null) {
        data.add("deviceInfo", deviceData.getDeviceInfo().toJsonObject());
      }
      if (deviceData.getMetadata() != null) {
        data.add("metadata", deviceData.getMetadata().getMetadata());
      }
      data.add("lifetime", new JsonPrimitive(lifetime));
      jsonPayload.add("d", data);
    } else {
      LoggerUtility.log(
          Level.SEVERE,
          CLASS_NAME,
          METHOD,
          "Cannot send manage request " + "as either deviceInfo or metadata is not set !!");

      return false;
    }

    JsonObject jsonResponse = sendAndWait(topic, jsonPayload, REGISTER_TIMEOUT_VALUE);
    if (jsonResponse != null
        && jsonResponse.get("rc").getAsInt() == ResponseCode.DM_SUCCESS.getCode()) {
      DMListener.start(this);
      DMRequestHandler.setRequestHandlers(this);
      publishQueue = new LinkedBlockingQueue<JsonObject>();
      Thread t = new Thread(this);
      t.start();
      /*
       * set the dormant time to a local variable, in case if the connection is
       * lost due to n/w interruption, we need to send another manage request
       * with the dormant time as the lifetime
       */
      if (lifetime > 0) {
        Date currentTime = new Date();
        dormantTime = new Date(currentTime.getTime() + (lifetime * 1000));
      }
      success = true;
    }
    LoggerUtility.log(Level.FINE, CLASS_NAME, METHOD, "Success (" + success + ")");

    bManaged = success;
    return success;
  }