Example #1
0
  /**
   * Moves the device from managed state to unmanaged state
   *
   * <p>A device uses this request when it no longer needs to be managed. This means IoTF will no
   * longer send new device management requests to this device and device management requests from
   * this device will be rejected apart from a Manage device request
   *
   * @return True if the unmanage command is successful
   * @throws MqttException
   */
  public boolean unmanage() throws MqttException {

    final String METHOD = "unmanage";
    boolean success = false;
    DeviceTopic topic = DeviceTopic.UNMANAGE;

    JsonObject jsonPayload = new JsonObject();
    JsonObject jsonResponse = sendAndWait(topic, jsonPayload, REGISTER_TIMEOUT_VALUE);
    if (jsonResponse != null
        && jsonResponse.get("rc").getAsInt() == ResponseCode.DM_SUCCESS.getCode()) {
      success = true;
    }

    terminate();
    DMListener.stop(this);
    DMRequestHandler.clearRequestHandlers(this);
    this.deviceData.terminateHandlers();
    this.supportsDeviceActions = false;
    this.supportsFirmwareActions = false;

    if (responseSubscription != null) {
      this.unsubscribe(this.responseSubscription);
      responseSubscription = null;
    }

    LoggerUtility.log(Level.FINE, CLASS_NAME, METHOD, "Success (" + success + ")");
    if (success) {
      bManaged = false;
    }
    return success;
  }
Example #2
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;
  }