/**
   * This method builds the device objects required to create the ManagedClient
   *
   * @param propertiesFile
   * @throws Exception
   */
  private void createManagedClient(String propertiesFile) throws Exception {
    /** Load device properties */
    Properties deviceProps = loadPropertiesFile(propertiesFile);

    /**
     * To create a DeviceData object, we will need the following objects: - DeviceInfo -
     * DeviceMetadata - DeviceLocation (optional) - DiagnosticErrorCode (optional) - DiagnosticLog
     * (optional) - DeviceFirmware (optional)
     */
    DeviceInfo deviceInfo =
        new DeviceInfo.Builder()
            .serialNumber(trimedValue(deviceProps.getProperty("DeviceInfo.serialNumber")))
            .manufacturer(trimedValue(deviceProps.getProperty("DeviceInfo.manufacturer")))
            .model(trimedValue(deviceProps.getProperty("DeviceInfo.model")))
            .deviceClass(trimedValue(deviceProps.getProperty("DeviceInfo.deviceClass")))
            .description(trimedValue(deviceProps.getProperty("DeviceInfo.description")))
            .fwVersion(trimedValue(deviceProps.getProperty("DeviceInfo.swVersion")))
            .hwVersion(trimedValue(deviceProps.getProperty("DeviceInfo.hwVersion")))
            .descriptiveLocation(
                trimedValue(deviceProps.getProperty("DeviceInfo.descriptiveLocation")))
            .build();

    /** Build the ErrorCode & DeviceDiagnostic Object */
    DiagnosticErrorCode errorCode = new DiagnosticErrorCode(0);
    errorCode.waitForResponse(false);

    /** Create a DeviceMetadata object */
    JsonObject data = new JsonObject();
    data.addProperty("customField", "customValue");
    DeviceMetadata metadata = new DeviceMetadata(data);

    this.deviceData =
        new DeviceData.Builder()
            .deviceInfo(deviceInfo)
            .deviceErrorCode(errorCode)
            .metadata(metadata)
            .build();

    // create the location updater thread
    this.ecUpdaterThread = new ErrorCodeUpdaterThread();
    this.ecUpdaterThread.diag = errorCode;

    // Options to connect to IoT Foundation
    Properties options = new Properties();
    options.setProperty("Organization-ID", trimedValue(deviceProps.getProperty("Organization-ID")));
    options.setProperty("Device-Type", trimedValue(deviceProps.getProperty("Device-Type")));
    options.setProperty("Device-ID", trimedValue(deviceProps.getProperty("Device-ID")));
    options.setProperty(
        "Authentication-Method", trimedValue(deviceProps.getProperty("Authentication-Method")));
    options.setProperty(
        "Authentication-Token", trimedValue(deviceProps.getProperty("Authentication-Token")));

    dmClient = new ManagedDevice(options, deviceData);
  }
    public void run() {

      for (int i = 0; i < 1000; i++) {
        // Don't check for response code as we don't wait
        // for response from the IoT Foundation server
        diag.append(random.nextInt(500));
        System.out.println("Current Errorcode (" + diag + ")");

        if (i % 25 == 0) {
          diag.clear();
        }
        try {
          Thread.sleep(5000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }