/**
  * バッテリー全属性取得テストを行う.
  *
  * <pre>
  * 【HTTP通信】
  * Method: GET
  * Path: /battery?deviceid=xxxx
  * </pre>
  *
  * <pre>
  * 【期待する動作】
  * ・resultに0が返ってくること。
  * ・chargingがfalseで返ってくること。
  * ・chargingtimeが50000で返ってくること。
  * ・dischargingtimeが10000で返ってくること。
  * ・levelが0.5で返ってくること。
  * </pre>
  */
 public void testGetBattery() {
   URIBuilder builder = TestURIBuilder.createURIBuilder();
   builder.setProfile(BatteryProfileConstants.PROFILE_NAME);
   builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
   builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
   try {
     HttpUriRequest request = new HttpGet(builder.toString());
     JSONObject root = sendRequest(request);
     assertResultOK(root);
     assertEquals(
         "charging is not equals.",
         TestBatteryProfileConstants.CHARGING,
         root.getBoolean(BatteryProfileConstants.ATTRIBUTE_CHARGING));
     assertEquals(
         "chargingtime is not equals.",
         TestBatteryProfileConstants.CHARGING_TIME,
         root.getDouble(BatteryProfileConstants.ATTRIBUTE_CHARGING_TIME));
     assertEquals(
         "dischargingtime is not equals.",
         TestBatteryProfileConstants.DISCHARGING_TIME,
         root.getDouble(BatteryProfileConstants.ATTRIBUTE_DISCHARGING_TIME));
     assertEquals(
         "level is not equals.",
         TestBatteryProfileConstants.LEVEL,
         root.getDouble(BatteryProfileConstants.ATTRIBUTE_LEVEL));
   } catch (JSONException e) {
     fail("Exception in JSONObject." + e.getMessage());
   }
 }
 /**
  * バッテリーonchargingchangeを解除するテストを行う.
  *
  * <pre>
  * 【HTTP通信】
  * Method: DELETE
  * Path: /battery/onchargingchange?deviceid=xxxx&session_key=xxxx
  * </pre>
  *
  * <pre>
  * 【期待する動作】
  * ・resultに0が返ってくること。
  * </pre>
  */
 public void testDeleteBatteryOnChargingChange() {
   URIBuilder builder = TestURIBuilder.createURIBuilder();
   builder.setProfile(BatteryProfileConstants.PROFILE_NAME);
   builder.setAttribute(BatteryProfileConstants.ATTRIBUTE_ON_CHARGING_CHANGE);
   builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
   builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId());
   builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
   try {
     HttpUriRequest request = new HttpDelete(builder.toString());
     JSONObject root = sendRequest(request);
     assertResultOK(root);
   } catch (JSONException e) {
     fail("Exception in JSONObject." + e.getMessage());
   }
 }
 /**
  * onbatterychange属性のコールバック登録テストを行う.
  *
  * <pre>
  * 【HTTP通信】
  * Method: PUT
  * Path: /battery/onbatterychange?deviceid=xxxx&session_key=xxxx
  * </pre>
  *
  * <pre>
  * 【期待する動作】
  * ・resultに0が返ってくること。
  * </pre>
  */
 public void testPutBatteryOnBatteryChange() {
   URIBuilder builder = TestURIBuilder.createURIBuilder();
   builder.setProfile(BatteryProfileConstants.PROFILE_NAME);
   builder.setAttribute(BatteryProfileConstants.ATTRIBUTE_ON_BATTERY_CHANGE);
   builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, getDeviceId());
   builder.addParameter(DConnectProfileConstants.PARAM_SESSION_KEY, getClientId());
   builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
   try {
     HttpUriRequest request = new HttpPut(builder.toString());
     JSONObject root = sendRequest(request);
     assertResultOK(root);
     // イベントメッセージを受け取る
     JSONObject resp = waitForEvent();
     assertNotNull("response is null.", resp);
     assertEquals(
         BatteryProfileConstants.PROFILE_NAME, resp.getString(DConnectMessage.EXTRA_PROFILE));
     assertEquals(
         BatteryProfileConstants.ATTRIBUTE_ON_BATTERY_CHANGE,
         resp.getString(DConnectMessage.EXTRA_ATTRIBUTE));
   } catch (JSONException e) {
     fail("Exception in JSONObject." + e.getMessage());
   }
 }
    @Override
    protected List<SmartDevice> doInBackground(final Void... params) {
      mLogger.entering(getClass().getName(), "doInBackground", params);

      List<SmartDevice> devices = new ArrayList<SmartDevice>();

      DConnectMessage message = null;

      try {
        URIBuilder builder = new URIBuilder();
        builder.setProfile(ServiceDiscoveryProfileConstants.PROFILE_NAME);

        SharedPreferences prefs =
            PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        if (prefs.getBoolean(getString(R.string.key_settings_dconn_ssl), false)) {
          builder.setScheme("https");
        } else {
          builder.setScheme("http");
        }

        builder.setHost(
            prefs.getString(
                getString(R.string.key_settings_dconn_host), getString(R.string.default_host)));

        builder.setPort(
            Integer.parseInt(
                prefs.getString(
                    getString(R.string.key_settings_dconn_port),
                    getString(R.string.default_port))));

        builder.addParameter(DConnectMessage.EXTRA_ACCESS_TOKEN, getAccessToken());

        HttpUriRequest request = new HttpGet(builder.build());
        request.addHeader(DConnectMessage.HEADER_GOTAPI_ORIGIN, getPackageName());
        mLogger.info(request.getMethod() + " " + request.getURI());
        HttpResponse response = mDConnectClient.execute(request);
        message = (new HttpMessageFactory()).newDConnectMessage(response);
      } catch (URISyntaxException e) {
        e.printStackTrace();

        mLogger.exiting(getClass().getName(), "doInBackground", devices);
        return devices;
      } catch (IOException e) {
        e.printStackTrace();

        mLogger.exiting(getClass().getName(), "doInBackground", devices);
        return devices;
      }

      if (message == null) {
        mLogger.exiting(getClass().getName(), "doInBackground", devices);
        return devices;
      }

      int result = message.getInt(DConnectMessage.EXTRA_RESULT);
      if (result == DConnectMessage.RESULT_ERROR) {
        mLogger.exiting(getClass().getName(), "doInBackground", devices);
        return devices;
      }

      List<Object> services = message.getList(ServiceDiscoveryProfileConstants.PARAM_SERVICES);
      if (services != null) {
        for (Object object : services) {
          @SuppressWarnings("unchecked")
          Map<String, Object> service = (Map<String, Object>) object;
          SmartDevice device =
              new SmartDevice(
                  service.get(ServiceDiscoveryProfileConstants.PARAM_ID).toString(),
                  service.get(ServiceDiscoveryProfileConstants.PARAM_NAME).toString());
          devices.add(device);
          mLogger.info("Found smart device: " + device.getId());
        }
      }

      mLogger.exiting(getClass().getName(), "doInBackground", devices);
      return devices;
    }