<commit_before>'use strict';

// Netatmo weatherstation for HomeBridge
// Wriiten by planetk (https://github.com/planetk)
//
// Remember to add platform to config.json. Example:
// "platforms": [
//     {
//         "platform": "Netatmo",
//         "name": "Netatmo Weather",
//         "auth": {
//             "client_id": "",
//             "client_secret": "",
//             "username": "",
//             "password": ""
//         }
//     }
// ],
//
// The default code for all HomeBridge accessories is 031-45-154.

var types = require('hap-nodejs/accessories/types.js');

//////////////////////////////////////////////////////////////////////////////
// DECLARE SOME UUIDS WHICH SHOUL BE IN HAP-NODEJS TYPES LIB, BUT ARE NOT YET
// REMOVE WHEN HAP LIB IS UPDATED!!
//////////////////////////////////////////////////////////////////////////////
var stPre = "000000";
var stPost = "-0000-1000-8000-0026BB765291";

types.BATTERY_SERVICE_STYPE = stPre + "96" + stPost;
types.AIR_QUALITY_SENSOR_STYPE = stPre + "8D" + stPost;
types.CARBON_DIOXIDE_SENSOR_STYPE = stPre + "97" + stPost;

types.AIR_PARTICULATE_DENISITY_CTYPE = stPre + "64" + stPost;
types.CARBON_DIOXIDE_DETECTED_CTYPE = stPre + "92" + stPost;
types.CARBON_DIOXIDE_LEVEL_CTYPE = stPre + "93" + stPost;
types.AIR_QUALITY_CTYPE = stPre + "95" + stPost;
//////////////////////////////////////////////////////////////////////////////

var netatmo = require('netatmo');
var NodeCache = require( "node-cache" );

function NetAtmoRepository(log, api) {
  this.api = api;
  this.log = log;
  this.cache = new NodeCache();
}

NetAtmoRepository.prototype = {
  refresh: function(callback) {
    var datasource={
      devices: {},
      modules: {}
    };
    var that = this;
    that.api.getDevicelist(function(err, devices, modules) {
      for (var device of devices) {
        that.log("refreshing device " + device._id + " (" + device.module_name + ")");
        datasource.devices[device._id] = device;
      }
      for (var module of modules) {
        that.log("refreshing module " + module._id + " (" + module.module_name + ")");
        datasource.modules[module._id] = module;
      }
      that.cache.set( "datasource", datasource, 20 );
      callback(datasource);
    });
  },
  load: function(callback) {
    var that = this;
    this.cache.get( "datasource", function( err, datasource ){
      if( !err ){
        if ( datasource == undefined ){
          that.refresh(callback);
        } else {
          callback(datasource)
        }
      }
    });
  }
}

function NetatmoPlatform(log, config) {
  this.log = log;
  var api = new netatmo(config["auth"]);
  this.repository = new NetAtmoRepository(this.log, api);
  api.on("error", function(error) {
    this.log('ERROR - Netatmo: ' + error);
  });
  api.on("warning", function(error) {
    this.log('WARN - Netatmo: ' + error);
  });
}

NetatmoPlatform.prototype = {
  accessories: function(callback) {

    var that = this;
    var foundAccessories = [];

    this.repository.load(function(datasource) {
      for (var id in datasource.devices) {
        var device = datasource.devices[id];
        that.log("Adding accessory for device " + id + " (" + device.module_name + ")");
        var accessory = new NetatmoAccessory(that.log, that.repository, device._id, null, device);
        foundAccessories.push(accessory);
      }
      for (var id in datasource.modules) {
        var module = datasource.modules[id];
        that.log("Adding accessory for module " + module._id + " (" + module.module_name + ")");
        var accessory = new NetatmoAccessory(that.log, that.repository, module.main_device, module._id, module);
        foundAccessories.push(accessory);
      }
      callback(foundAccessories);
    });
  }
}

function NetatmoAccessory(log, repository, deviceId, moduleId, device) {
  this.log = log;
  this.repository = repository;
  this.deviceId = deviceId;
  this.moduleId = moduleId;
  this.serial = deviceId;
  if (moduleId) {
    this.serial = moduleId;
  }
  this.name = device.module_name;
  this.model = device.type;
  this.serviceTypes = device.data_type;
  if (device.battery_vp) {
    this.serviceTypes.push("Battery");
  }
}

NetatmoAccessory.prototype = {

  getData: function(callback) {
    var that = this;
    this.repository.load(function(datasource) {
      if(that.moduleId) {
        callback(datasource.modules[that.moduleId]);
      } else {
        callback(datasource.devices[that.deviceId]);
      }
    });
  },

  getCurrentTemperature: function(callback) {
    this.getData(function(deviceData) {
      callback(deviceData.dashboard_data.Temperature);
    });
  },

  getCurrentHumidity: function(callback) {
    this.getData(function(deviceData) {
      callback(deviceData.dashboard_data.Humidity);
    });
  },

  getAirQuality: function(callback) {
    this.getData(function(deviceData) {
      var level = deviceData.dashboard_data.CO2; 
      var quality = 0;
      if (level > 2000) quality = 5;
      else if (level > 1500) quality = 4;
      else if (level > 1000) quality = 3;
      else if (level > 500) quality = 2;
      else if (level > 250) quality = 1;<commit_msg>fixed netatmo problem with duplicate module names<commit_after>'use strict';

// Netatmo weatherstation for HomeBridge
// Wriiten by planetk (https://github.com/planetk)
//
// Remember to add platform to config.json. Example:
// "platforms": [
//     {
//         "platform": "Netatmo",
//         "name": "Netatmo Weather",
//         "auth": {
//             "client_id": "",
//             "client_secret": "",
//             "username": "",
//             "password": ""
//         }
//     }
// ],
//
// The default code for all HomeBridge accessories is 031-45-154.

var types = require('hap-nodejs/accessories/types.js');

//////////////////////////////////////////////////////////////////////////////
// DECLARE SOME UUIDS WHICH SHOUL BE IN HAP-NODEJS TYPES LIB, BUT ARE NOT YET
// REMOVE WHEN HAP LIB IS UPDATED!!
//////////////////////////////////////////////////////////////////////////////
var stPre = "000000";
var stPost = "-0000-1000-8000-0026BB765291";

types.BATTERY_SERVICE_STYPE = stPre + "96" + stPost;
types.AIR_QUALITY_SENSOR_STYPE = stPre + "8D" + stPost;
types.CARBON_DIOXIDE_SENSOR_STYPE = stPre + "97" + stPost;

types.AIR_PARTICULATE_DENISITY_CTYPE = stPre + "64" + stPost;
types.CARBON_DIOXIDE_DETECTED_CTYPE = stPre + "92" + stPost;
types.CARBON_DIOXIDE_LEVEL_CTYPE = stPre + "93" + stPost;
types.AIR_QUALITY_CTYPE = stPre + "95" + stPost;
//////////////////////////////////////////////////////////////////////////////

var netatmo = require('netatmo');
var NodeCache = require( "node-cache" );

function NetAtmoRepository(log, api) {
  this.api = api;
  this.log = log;
  this.cache = new NodeCache();
}

NetAtmoRepository.prototype = {
  refresh: function(callback) {
    var datasource = {
      devices: {},
      modules: {}
    };
    var that = this;
    that.api.getDevicelist(function(err, devices, modules) {
      for (var device of devices) {
        that.log("refreshing device " + device._id + " (" + device.module_name + ")");
        datasource.devices[device._id] = device;
      }
      for (var module of modules) {
        that.log("refreshing module " + module._id + " (" + module.module_name + ")");
        datasource.modules[module._id] = module;
      }
      that.cache.set( "datasource", datasource, 20 );
      callback(datasource);
    });
  },
  load: function(callback) {
    var that = this;
    this.cache.get( "datasource", function( err, datasource ) {
      if( !err ){
        if ( datasource == undefined ){
          that.refresh(callback);
        } else {
          callback(datasource)
        }
      }
    });
  }
}

function NetatmoPlatform(log, config) {
  this.log = log;
  var api = new netatmo(config["auth"]);
  this.repository = new NetAtmoRepository(this.log, api);
  api.on("error", function(error) {
    this.log('ERROR - Netatmo: ' + error);
  });
  api.on("warning", function(error) {
    this.log('WARN - Netatmo: ' + error);
  });
}

NetatmoPlatform.prototype = {
  accessories: function(callback) {

    var that = this;
    var foundAccessories = [];

    this.repository.load(function(datasource) {
      for (var id in datasource.devices) {
        var device = datasource.devices[id];
        that.log("Adding accessory for device " + id + " (" + device.module_name + ")");
        var accessory = new NetatmoAccessory(that.log, that.repository, device._id, null, device);
        foundAccessories.push(accessory);
      }
      for (var id in datasource.modules) {
        var module = datasource.modules[id];
        that.log("Adding accessory for module " + module._id + " (" + module.module_name + ")");
        var accessory = new NetatmoAccessory(that.log, that.repository, module.main_device, module._id, module);
        foundAccessories.push(accessory);
      }
      callback(foundAccessories);
    });
  }
}

function NetatmoAccessory(log, repository, deviceId, moduleId, device) {
  this.log = log;
  this.repository = repository;
  this.deviceId = deviceId;
  this.moduleId = moduleId;
  this.serial = deviceId;
  if (moduleId) {
    this.serial = moduleId;
  }

  // add station name to devices to avoid duplicate names
  if (device.station_name) {
    this.name = device.station_name + " " + device.module_name;
  } else {
    this.name = device.module_name;
  }

  this.model = device.type;
  this.serviceTypes = device.data_type;
  if (device.battery_vp) {
    this.serviceTypes.push("Battery");
  }
}

NetatmoAccessory.prototype = {

  getData: function(callback) {
    var that = this;
    this.repository.load(function(datasource) {
      if(that.moduleId) {
        callback(datasource.modules[that.moduleId]);
      } else {
        callback(datasource.devices[that.deviceId]);
      }
    });
  },

  getCurrentTemperature: function(callback) {
    this.getData(function(deviceData) {
      callback(deviceData.dashboard_data.Temperature);
    });
  },

  getCurrentHumidity: function(callback) {
    this.getData(function(deviceData) {
      callback(deviceData.dashboard_data.Humidity);
    });
  },

  getAirQuality: function(callback) {
    this.getData(function(deviceData) {
      var level = deviceData.dashboard_data.CO2;
      var quality = 0;
      if (level > 2000) quality = 5;
      else if (level > 1500) quality = 4;
      else if (level > 1000) quality = 3;
      else if (level > 500) quality = 2;
      else if (level > 250) quality = 1;
{'fraction_non_alphanumeric': 0.1792631978731485, 'fraction_numerical': 0.014432206608431447, 'mean_word_length': 2.410945595854922, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '6660609', 'n_tokens_mistral': 3494, 'n_tokens_neox': 3297, 'n_words': 688}