<commit_before>/**
* Copyright 2015 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of iotagent project.
*
* iotagent is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* iotagent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with iotagent. If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with iot_support at tid dot es
*/
#ifndef SRC_UTIL_DEVICE_H_
#define SRC_UTIL_DEVICE_H_

#include "timer.h"
#include "store_const.h"
#include "rest/riot_conf.h"
#include <boost/functional/hash.hpp>
#include <boost/shared_ptr.hpp>

namespace iota {

struct Device : public virtual Timer {

  explicit Device(std::string name, std::string service): Timer(),
    _name(name), _service(service) {
      _active = INT_MIN;
      _duration_cb = LONG_MIN;
      _protocol="";
  };

  explicit Device(std::string entity_name, std::string entity_type,
                  std::string service):
    Timer(), _entity_name(entity_name), _entity_type(entity_type),
    _service(service) {
    _active = INT_MIN;
    _duration_cb = LONG_MIN;
    _protocol="";
  };

  Device(const Device& dev):
    Timer() {
    if (!dev._name.empty()) {
      _name = dev._name;
    }
    if (!dev._service.empty()) {
      _service = dev._service;
    }
    if (!dev._service_path.empty()) {
      _service_path = dev._service_path;
    }
    if (!dev._entity_type.empty()) {
      _entity_type = dev._entity_type;
    }
    if (!dev._entity_name.empty()) {
      _entity_name = dev._entity_name;
    }
    if (!dev._timezone.empty()) {
      _timezone = dev._timezone;
    }
    if (!dev._endpoint.empty()) {
      _endpoint = dev._endpoint;
    }
    if (!dev._model.empty()) {
      _model = dev._model;
    }
    if (!dev._registration_id.empty()) {
      _registration_id = dev._registration_id;
    }

    if (!dev._protocol.empty()) {
      _protocol = dev._protocol;
    }

    if (dev._duration_cb != LONG_MIN) {
      _duration_cb = dev._duration_cb;
    }else{
      _duration_cb = LONG_MIN;
    }

    std::map<std::string, std::string>::const_iterator iter;
    for (iter = dev._attributes.begin(); iter != dev._attributes.end(); ++iter) {
      _attributes.insert(std::pair<std::string, std::string>(iter->first,
                         iter->second));
    }
    for (iter = dev._commands.begin(); iter != dev._commands.end(); ++iter) {
      _commands.insert(std::pair<std::string, std::string>(iter->first,
                       iter->second));
    }
    for (iter = dev._static_attributes.begin();
         iter != dev._static_attributes.end(); ++iter) {
      _static_attributes.insert(std::pair<std::string, std::string>(iter->first,
                                iter->second));
    }

    if (dev._active != INT_MIN) {
      _active = dev._active;
    }else{
       _active = INT_MIN;
    }
  };


  virtual ~Device() {};

  bool operator==(const Device& a) const {
    if ((_name.compare(a._name) == 0) &&
        (_service.compare(a._service) == 0)) {
      return true;
    }
    return false;
  }

  bool operator==(const boost::shared_ptr<Device>& a) const {
    if ((_name.compare(a->_name) == 0) &&
        (_service.compare(a->_service) == 0)) {
      return true;
    }
    return false;
  }

  bool operator()(const Device& one, const Device& two) const {
    std::string one_full(one._name);
    one_full.append(one._service);
    std::string two_full(two._name);
    two_full.append(two._service);
    return (one_full < two_full);
  }

  bool operator()(const boost::shared_ptr<Device>& one,
                  const boost::shared_ptr<Device>& two) const {
    std::string one_full(one->_name);
    one_full.append(one->_service);
    std::string two_full(two->_name);
    two_full.append(two->_service);
    return (one_full < two_full);
  }


  friend std::size_t hash_value(boost::shared_ptr<Device> const& item) {

    std::size_t seed = 0;
    boost::hash_combine(seed, item->_name);
    boost::hash_combine(seed, item->_service);
    boost::hash_combine(seed, item->_service_path);
    return seed;
  }

  friend std::size_t hash_value(Device const& item) {

    std::size_t seed = 0;
    boost::hash_combine(seed, item._name);
    boost::hash_combine(seed, item._service);
    boost::hash_combine(seed, item._service_path);
    return seed;
  }

  std::string unique_entity() const {
    return _service+":"+_service_path + ":"+
           _entity_name+":"+_entity_type;
  }

  std::string unique_name() const {
    return _name;
  }

  std::string get_real_name() const {
    if (_entity_name.empty()) {
      return _name;
    }
    else {
      return _entity_name;
    }
  }<commit_msg>FIX IDAS-20512: IoTA response to updateContext is wrong<commit_after>/**
* Copyright 2015 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of iotagent project.
*
* iotagent is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* iotagent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with iotagent. If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with iot_support at tid dot es
*/
#ifndef SRC_UTIL_DEVICE_H_
#define SRC_UTIL_DEVICE_H_

#include "timer.h"
#include "store_const.h"
#include "rest/riot_conf.h"
#include <boost/functional/hash.hpp>
#include <boost/shared_ptr.hpp>

namespace iota {

struct Device : public virtual Timer {

  explicit Device(std::string name, std::string service): Timer(),
    _name(name), _service(service) {
      _active = INT_MIN;
      _duration_cb = LONG_MIN;
      _protocol="";
  };

  explicit Device(std::string entity_name, std::string entity_type,
                  std::string service):
    Timer(), _entity_name(entity_name), _entity_type(entity_type),
    _service(service) {
    _active = INT_MIN;
    _duration_cb = LONG_MIN;
    _protocol="";
  };

  Device(const Device& dev):
    Timer() {
    if (!dev._name.empty()) {
      _name = dev._name;
    }
    if (!dev._service.empty()) {
      _service = dev._service;
    }
    if (!dev._service_path.empty()) {
      _service_path = dev._service_path;
    }
    if (!dev._entity_type.empty()) {
      _entity_type = dev._entity_type;
    }
    if (!dev._entity_name.empty()) {
      _entity_name = dev._entity_name;
    }
    if (!dev._timezone.empty()) {
      _timezone = dev._timezone;
    }
    if (!dev._endpoint.empty()) {
      _endpoint = dev._endpoint;
    }
    if (!dev._model.empty()) {
      _model = dev._model;
    }
    if (!dev._registration_id.empty()) {
      _registration_id = dev._registration_id;
    }

    if (!dev._protocol.empty()) {
      _protocol = dev._protocol;
    }

    if (dev._duration_cb != LONG_MIN) {
      _duration_cb = dev._duration_cb;
    }else{
      _duration_cb = LONG_MIN;
    }

    std::map<std::string, std::string>::const_iterator iter;
    for (iter = dev._attributes.begin(); iter != dev._attributes.end(); ++iter) {
      _attributes.insert(std::pair<std::string, std::string>(iter->first,
                         iter->second));
    }
    for (iter = dev._commands.begin(); iter != dev._commands.end(); ++iter) {
      _commands.insert(std::pair<std::string, std::string>(iter->first,
                       iter->second));
    }
    for (iter = dev._static_attributes.begin();
         iter != dev._static_attributes.end(); ++iter) {
      _static_attributes.insert(std::pair<std::string, std::string>(iter->first,
                                iter->second));
    }

    if (dev._active != INT_MIN) {
      _active = dev._active;
    }else{
       _active = INT_MIN;
    }
  };


  virtual ~Device() {};

  bool operator==(const Device& a) const {
    if ((_name.compare(a._name) == 0) &&
        (_service.compare(a._service) == 0)) {
      return true;
    }
    return false;
  }

  bool operator==(const boost::shared_ptr<Device>& a) const {
    if ((_name.compare(a->_name) == 0) &&
        (_service.compare(a->_service) == 0)) {
      return true;
    }
    return false;
  }

  bool operator()(const Device& one, const Device& two) const {
    std::string one_full(one._name);
    one_full.append(one._service);
    std::string two_full(two._name);
    two_full.append(two._service);
    return (one_full < two_full);
  }

  bool operator()(const boost::shared_ptr<Device>& one,
                  const boost::shared_ptr<Device>& two) const {
    std::string one_full(one->_name);
    one_full.append(one->_service);
    std::string two_full(two->_name);
    two_full.append(two->_service);
    return (one_full < two_full);
  }


  friend std::size_t hash_value(boost::shared_ptr<Device> const& item) {

    std::size_t seed = 0;
    boost::hash_combine(seed, item->_name);
    boost::hash_combine(seed, item->_service);
    boost::hash_combine(seed, item->_service_path);
    return seed;
  }

  friend std::size_t hash_value(Device const& item) {

    std::size_t seed = 0;
    boost::hash_combine(seed, item._name);
    boost::hash_combine(seed, item._service);
    boost::hash_combine(seed, item._service_path);
    return seed;
  }

  std::string unique_entity() const {
    return _service+":"+_service_path + ":"+
           _entity_name+":"+_entity_type;
  }

  std::string unique_name() const {
    return _name;
  }

  std::string get_real_name() const {
    if (_entity_name.empty()) {
      if (_entity_type.empty()){
        return "thing:"+_name;
      }else{
        return _entity_type+":"+_name;
      }
    }
    else {
      return _entity_name;
    }
  }
{'fraction_non_alphanumeric': 0.13615516911905218, 'fraction_numerical': 0.0025797821517294095, 'mean_word_length': 3.0165003837298543, 'pattern_counts': {'":': 7, '<': 27, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5855398', 'n_tokens_mistral': 3422, 'n_tokens_neox': 3319, 'n_words': 794}