public boolean removeThingUnreachable(String sThingName) {
    boolean bCorrect = true;
    try {
      mLogger.debug(
          "Component CM perform operation removeThingUnreachable. Item: " + sThingName + ".");
      String sThingServiceName = cmservice.sparqlRemoveDevice(sThingName);

      try {
        qosservice.thingRemoved(sThingServiceName);
      } catch (Exception e) {
        mLogger.error(
            "Component CM perform operation removeThing. It has not been executed correctly the call to qosservice.thingRemoved("
                + sInstanceID
                + "). Exception: "
                + e.getMessage()
                + ".");
      }

      List<String> sThingServiceList = new ArrayList<String>();
      sThingServiceList.add(sThingServiceName);

      try {
        rmservice.deleteThingServices(sThingServiceList);
      } catch (Exception e) {
        mLogger.error(
            "Component CM perform operation removeThing. It has not been executed correctly the call to TaaSRM.deleteThingServices("
                + sThingServiceList.get(0)
                + "). Exception: "
                + e.getMessage()
                + ".");
      }

    } catch (Exception e) {
      mLogger.error(
          "Component CM perform operation removeThing. It has not been executed correctly the call to QoSservice.thingRemoved or TaaSRM.deleteThingServices. Exception: "
              + e.getMessage()
              + ".");
      oConfigOSGi.sendData(
          "The Thing Service " + sThingName + " has been removed unsucessfully!.",
          "error",
          "AdaptationCM");
    }
    oConfigOSGi.sendData(
        "The Thing Service " + sThingName + " has been removed sucessfully!.",
        "info",
        "AdaptationCM");
    return bCorrect;
  }
  public String publishThing_local(ArrayList<ThingsData> oThingsDataList, String sMode)
      throws Exception {
    boolean bResults = true;
    boolean bSubscribe = false;
    JsonObject jResultType = null;
    String sNewThingServiceName = "";

    bdservice = oConfigOSGi.getBdservice();
    rmservice = oConfigOSGi.getRmservice();
    qosservice = oConfigOSGi.getQosservice();
    sGwIdLocal = oConfigOSGi.getGwId();

    Date date = new Date();
    Timestamp ts = new Timestamp(date.getTime());
    String sEnvironmentOnto = null;

    try {

      ThingsData oThingsData;
      for (int i = 0; i < oThingsDataList.size(); i++) {
        oThingsData = oThingsDataList.get(i);
        boolean bOutput = oThingsData.isOutput();
        boolean bDigital = oThingsData.isDigital();
        String sMaximumResponseTime = oThingsData.getMaximumResponseTime();
        String sMemoryStatus = oThingsData.getMemoryStatus();
        String sComputationalCost = oThingsData.getComputationalCost();
        String sBatteryLevel = oThingsData.getBatteryLevel();
        String sBatteryCost = oThingsData.getBatteryCost();
        String sMeasurement = oThingsData.getMeasurement();
        String sProtocol = oThingsData.getProtocol();
        String sDeviceID = oThingsData.getDeviceID();
        String sThingID = oThingsData.getThingId();
        sThingID = oThingsData.getThingId();
        String sType = oThingsData.getType().toLowerCase();
        String sTypeClass = sType.substring(0, 1).toUpperCase() + sType.substring(1);
        String sUnit = oThingsData.getUnit().toLowerCase();
        boolean bEnvironment = oThingsData.getEnvironment(); // PUBLIC 1 = true
        // PRIVATE 0 = false
        if (bEnvironment) sEnvironmentOnto = PUBLIC;
        else sEnvironmentOnto = PRIVATE;
        String sLatitude = oThingsData.getLatitude().replace(" ", "");
        String sLongitude = oThingsData.getLongitude().replace(" ", "");
        String sAltitude = oThingsData.getAltitude().replace(" ", "");
        String sFloor = oThingsData.getFloor();
        String sLocationKeyword = oThingsData.getLocationKeyword().replace(" ", "");
        sLocationKeyword = sLocationKeyword.toLowerCase();
        sLocationKeyword =
            sLocationKeyword.substring(0, 1).toUpperCase() + sLocationKeyword.substring(1);
        String sLocationIdentifier = oThingsData.getLocationIdentifier().replace(" ", "");
        sLocationIdentifier = sLocationIdentifier.toLowerCase();
        if (!sLocationIdentifier.isEmpty()) {
          sLocationIdentifier =
              sLocationIdentifier.substring(0, 1).toUpperCase() + sLocationIdentifier.substring(1);
        }
        mLogger.info(
            "Component CM perform operation publishThing. Data comming from TA:"
                + "          Output: "
                + oThingsData.isOutput()
                + ".          Digital: "
                + oThingsData.isDigital()
                + ".          MaximumResponseTime: "
                + oThingsData.getMaximumResponseTime()
                + ".          MemoryStatus: "
                + oThingsData.getMemoryStatus()
                + ".          ComputationalCost: "
                + oThingsData.getComputationalCost()
                + ".          BatteryLevel: "
                + oThingsData.getBatteryLevel()
                + ".          BatteryCost: "
                + oThingsData.getBatteryCost()
                + ".          Measurement: "
                + oThingsData.getMeasurement()
                + ".          Protocol: "
                + oThingsData.getProtocol()
                + ".          DeviceID: "
                + oThingsData.getDeviceID()
                + ".          ThingID: "
                + oThingsData.getThingId()
                + ".          Type: "
                + oThingsData.getType()
                + ".          Unit: "
                + oThingsData.getUnit()
                + ".          Environment: "
                + oThingsData.getEnvironment()
                + ".          Latitude: "
                + oThingsData.getLatitude()
                + ".          Longitude: "
                + oThingsData.getLongitude()
                + ".          Altitude: "
                + oThingsData.getAltitude()
                + ".          Floor: "
                + oThingsData.getFloor()
                + ".          LocationKeyword: "
                + oThingsData.getLocationKeyword()
                + ".          LocationIdentifier: "
                + oThingsData.getLocationIdentifier()
                + ".");

        String sCurrentDateTime = getCurrentDateTime("DTL_GMT", "DTF_DEC");

        checkBlankThingsData(
            bOutput,
            bDigital,
            sMaximumResponseTime,
            sMemoryStatus,
            sComputationalCost,
            sBatteryLevel,
            sBatteryCost,
            sMeasurement,
            sProtocol,
            sDeviceID,
            sThingID,
            sType,
            sUnit,
            bEnvironment,
            sLatitude,
            sLongitude,
            sAltitude,
            sFloor,
            sLocationKeyword,
            sLocationIdentifier);

        mLogger.info("Checking Thing Type families on the ontology for the type: " + sTypeClass);
        jResultType = cmservice.checkThingType(sType, oThingsData.isOutput()); // TODO

        mLogger.info(
            "Checking Location families on the ontology for the locationKeyword: "
                + sLocationKeyword);
        JsonObject jResultLocation = cmservice.checkThingLocation(sLocationKeyword); // TODO

        String sPrefixThingService;
        if (bOutput) sPrefixThingService = "get";
        else sPrefixThingService = "set";

        String sID = sThingID + "_" + sDeviceID;
        String sPointInstance = "point_" + sID;
        String sHWInstance = "hwProperties_" + sID;
        String sSensorInstance = "sensor_" + sID;
        String sObservationInstanceID = "observation_" + sID;
        sInstanceID = sObservationInstanceID;

        String sDeviceInstance = "device_" + sDeviceID;
        String sFloorInstance = "floor_" + sFloor; // "_1" vs "floor_1"
        String sRoomInstance =
            "_" + sLocationIdentifier.toLowerCase() + sLocationKeyword.toLowerCase();
        String sArea = "_" + sLocationIdentifier.toLowerCase() + sLocationKeyword.toLowerCase();

        String sThingServiceName =
            sPrefixThingService
                + sLocationIdentifier
                + sLocationKeyword
                + sTypeClass; // setMainKitchenPresence
        sNewThingServiceName =
            sThingServiceName + "_" + sDeviceID + "_" + sGwIdLocal; // setMainKitchenPresence_999_01

        if (sMode.equals(CHECK)) {
          bSubscribe = cmservice.checkSubscribeService(sNewThingServiceName);
        } else bSubscribe = true;

        if (bSubscribe) {
          if (!sMode.equals(INIT)) {
            String subject = "http://www.betaas.eu/2013/betaasOnt#" + sInstanceID;
            cmservice.sparqlRemoveStatement(subject);
          }

          if (bEnvironment) {
            if (!sLatitude.equals("")) lat = Double.parseDouble(sLatitude);

            if (!sLongitude.equals("")) lng = Double.parseDouble(sLongitude);

            if (!(sLatitude.equals("")
                || sLongitude.equals("")
                || sLatitude.equals("0.0") && sLongitude.equals("0.0")))
              sPhysicalPlace = getPhysicalPlaceOnGeonames(lat, lng);
          }

          cmservice.addResource(sTypeClass);

          sQueryUpdate =
              ""
                  + "PREFIX BETaaS: <http://www.betaas.eu/2013/betaasOnt#> "
                  + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
                  + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
                  + "INSERT DATA { "
                  + "BETaaS:"
                  + sObservationInstanceID
                  + " a BETaaS:Observation; "
                  + "BETaaS:observation_result_time \""
                  + sCurrentDateTime
                  + "\"^^xsd:dateTime; "
                  + "BETaaS:measurement '"
                  + sMeasurement
                  + "'; "
                  + "BETaaS:observedProperty BETaaS:"
                  + sType
                  + "; "
                  + "BETaaS:hasUnit '"
                  + sUnit
                  + "'; ";
          if (bEnvironment) {
            // sQueryUpdate = sQueryUpdate + "BETaaS:hasPoint " + sPointInstance
            // + "/point>; "
            sQueryUpdate =
                sQueryUpdate
                    + "BETaaS:hasPoint BETaaS:"
                    + sPointInstance
                    + "; "
                    + "BETaaS:hasLocation BETaaS:"
                    + sArea
                    + "; ";
            if (!sPhysicalPlace.isEmpty())
              sQueryUpdate = sQueryUpdate + "BETaaS:nearTo " + sPhysicalPlace + "; ";
          } else {
            sQueryUpdate =
                sQueryUpdate
                    // + "BETaaS:hasLocation BETaaS:" + sFloorInstance +"; "
                    + "BETaaS:hasLocation BETaaS:"
                    + sRoomInstance
                    + "; ";
          }
          sQueryUpdate =
              sQueryUpdate
                  + "BETaaS:observedBy BETaaS:"
                  + sSensorInstance
                  + ". "
                  + "BETaaS:"
                  + sSensorInstance
                  + " a <"
                  + PREFIX_BETAAS
                  + "#"
                  + sTypeClass
                  + "Sensor>; " // OJO
                  + "BETaaS:thingID '"
                  + sThingID
                  + "'; "
                  + "BETaaS:maximum_response_time '"
                  + sMaximumResponseTime
                  + "'; "
                  + "BETaaS:digital '"
                  + bDigital
                  + "'; "
                  + "BETaaS:output '"
                  + bOutput
                  + "'; "
                  + "BETaaS:hasHwProperties BETaaS:"
                  + sHWInstance
                  + "; "
                  + "BETaaS:onPlatform BETaaS:"
                  + sDeviceInstance
                  + "; "
                  + "BETaaS:hasService BETaaS:"
                  + sNewThingServiceName
                  + ". ";
          if (bEnvironment) {
            sQueryUpdate =
                sQueryUpdate
                    + "BETaaS:"
                    + sPointInstance
                    + " a BETaaS:GeographicalPoint; "
                    + "BETaaS:latitude \'"
                    + sLatitude
                    + "'; "
                    + "BETaaS:longitude \'"
                    + sLongitude
                    + "'; "
                    + "BETaaS:altitude \'"
                    + sAltitude
                    + "'. ";
            if (!sPhysicalPlace.isEmpty()) {
              sQueryUpdate =
                  sQueryUpdate
                      + sPhysicalPlace
                      + "a BETaaS:GeographicalPlace; "
                      + "BETaaS:label "
                      + sPhysicalPlace
                      + ". ";
            }
            sQueryUpdate =
                sQueryUpdate
                    + "BETaaS:"
                    + sArea
                    + " a BETaaS:Area; "
                    + "BETaaS:location_keyword '"
                    + sLocationKeyword
                    + "'; "
                    + "BETaaS:location_identifier '"
                    + sLocationIdentifier
                    + "'. ";
          } else
            sQueryUpdate =
                sQueryUpdate
                    + "BETaaS:"
                    + sFloorInstance
                    + " a BETaaS:Floor; "
                    + "BETaaS:level '"
                    + sFloor
                    + "'; "
                    + "BETaaS:hasRoom BETaaS:"
                    + sRoomInstance
                    + ". "
                    + "BETaaS:"
                    + sRoomInstance
                    + " a BETaaS:Room; "
                    + "BETaaS:location_keyword '"
                    + sLocationKeyword
                    + "'; "
                    + "BETaaS:location_identifier '"
                    + sLocationIdentifier
                    + "'; "
                    + "BETaaS:isInFloor BETaaS:"
                    + sFloorInstance
                    + ". ";

          sQueryUpdate =
              sQueryUpdate
                  + "BETaaS:"
                  + sHWInstance
                  + " a BETaaS:HwProperties; "
                  + "BETaaS:connection_description '"
                  + sProtocol
                  + "'; "
                  + "BETaaS:memory_status '"
                  + sMemoryStatus
                  + "'; "
                  + "BETaaS:computational_cost '"
                  + sComputationalCost
                  + "'; "
                  + "BETaaS:battery_level '"
                  + sBatteryLevel
                  + "'; "
                  + "BETaaS:battery_cost '"
                  + sBatteryCost
                  + "'. "
                  + "BETaaS:"
                  + sDeviceInstance
                  + " a BETaaS:Platform; "
                  + "BETaaS:deviceID '"
                  + sDeviceID
                  + "'. ";
          if (bOutput)
            sQueryUpdate =
                sQueryUpdate + "BETaaS:" + sNewThingServiceName + " a BETaaS:SensorService. ";
          // + "BETaaS:SensorService rdfs:subClassOf* BETaaS:PhysicalService. ";
          else
            sQueryUpdate =
                sQueryUpdate + "BETaaS:" + sNewThingServiceName + " a BETaaS:ActuatorService. ";
          // +
          // "BETaaS:ActuatorService rdfs:subClassOf* BETaaS:PhysicalService. ";
          sQueryUpdate =
              sQueryUpdate
                  + "BETaaS:"
                  + sNewThingServiceName
                  + " BETaaS:environment '"
                  + sEnvironmentOnto
                  + "'. ";
          if (sMode.equals(INIT))
            sQueryUpdate =
                sQueryUpdate + "BETaaS:" + sNewThingServiceName + " BETaaS:subscription 'false'. ";
          sQueryUpdate =
              sQueryUpdate
                  + "BETaaS:"
                  + sNewThingServiceName
                  + " BETaaS:thingserviceID '"
                  + sNewThingServiceName
                  + "'. "
                  + " }";

          //          String sNameThread = "ontology update";
          //          mLogger.debug("Component CM perform operation publishThing, starting
          // "+sNameThread+" thread");
          cmservice.sparqlUpdate(sQueryUpdate);

          // Check for null values
          if (!((oThingsData.getDeviceID() == null) && (oThingsData.getThingId() == null))) {
            JsonObject thing = new JsonObject();
            thing.addProperty("timestamp", ts.toString());
            thing.addProperty("is_output", oThingsData.isOutput());
            thing.addProperty("is_digital", oThingsData.isDigital());
            thing.addProperty("maximum_response_time", oThingsData.getMaximumResponseTime());
            thing.addProperty("memory_status", oThingsData.getMemoryStatus());
            thing.addProperty("computational_cost", oThingsData.getComputationalCost());
            thing.addProperty("battery_level", oThingsData.getBatteryLevel());
            thing.addProperty("battery_cost", oThingsData.getBatteryCost());
            thing.addProperty("measurement", oThingsData.getMeasurement());
            thing.addProperty("protocol", oThingsData.getProtocol());
            thing.addProperty("deviceID", oThingsData.getDeviceID());
            thing.addProperty("thingID", oThingsData.getThingId());
            thing.addProperty("type", oThingsData.getType());
            thing.addProperty("unit", oThingsData.getUnit());
            thing.addProperty("environment", oThingsData.getEnvironment());
            thing.addProperty("latitude", oThingsData.getLatitude());
            thing.addProperty("longitude", oThingsData.getLongitude());
            thing.addProperty("altitude", oThingsData.getAltitude());
            thing.addProperty("floor", oThingsData.getFloor());
            thing.addProperty("location_keyword", oThingsData.getLocationKeyword());
            thing.addProperty("location_identifier", oThingsData.getLocationIdentifier());

            try {
              mLogger.debug(
                  "Component CM call service ITaasBigDataManager.setThingsBDM. sDeviceID: "
                      + sDeviceID
                      + ", thing: "
                      + thing.toString()
                      + ".");
              bdservice.setThingsBDM(sDeviceID, thing);
            } catch (Exception e) {
              bResults = false;
              mLogger.error(
                  "Component CM perform operation publishThingInit. Exception comming from ITaasBigDataManager.setThingsBDM: "
                      + e.getMessage()
                      + ".");
            }
          } else {
            bResults = false;
            mLogger.error(
                "Component CM perform operation publishThing. NOT call service ITaasBigDataManager.setThingsBDM FUNCTION because some parameters are null!.");
          }

          if (sMode.equals(INIT)) {
            try {
              mLogger.debug(
                  "Component CM call service TaaSRMservice.registerThingsServices. ThingID: "
                      + sThingID
                      + ", ThingService: "
                      + sNewThingServiceName
                      + ".");
              rmservice.registerThingsServices(sThingID, sNewThingServiceName);
            } catch (Exception e) {
              bResults = false;
              mLogger.error(
                  "Component CM perform operation publishThingInit. Exception comming from TaaSRMservice.registerThingsServices: "
                      + e.getMessage()
                      + ".");
            }
          } else {
            try {
              mLogger.debug(
                  "Component CM call service TaaSRMservice.notifyNewMeasurement. ThingID: "
                      + sThingID
                      + ", ThingService: "
                      + sNewThingServiceName
                      + ".");
              rmservice.notifyNewMeasurement(sNewThingServiceName, oThingsData);
            } catch (Exception e) {
              bResults = false;
              mLogger.error(
                  "Component CM perform operation publishThingInit. Exception comming from TaaSRMservice.registerThingsServices: "
                      + e.getMessage()
                      + ".");
            }
          }

        } // subscribe
        else {
          mLogger.error(
              "Component CM perform operation publishThingCheck. NO SUBSCRIPTION: "
                  + sNewThingServiceName);
        }
      } // for
    } catch (InterruptedException ie) {

    } catch (Exception e) {
      bResults = false;
      mLogger.error(
          "Component CM perform operation publishThingInit. It has not been executed correctly. Exception: "
              + e.getMessage()
              + ".");
    }
    mLogger.debug("Component CM perform operation publishThingInit. Results: " + bResults);
    if (bResults)
      oConfigOSGi.sendData(
          "The Thing Service " + sNewThingServiceName + " has been published sucessfully!.",
          "info",
          "AdaptationCM");
    else
      oConfigOSGi.sendData(
          "The Thing Service " + sNewThingServiceName + " has not been published sucessfully!.",
          "error",
          "AdaptationCM");
    return jResultType.toString();
  }