<commit_before>var Service, Characteristic;
const http_request = require('http').request;
const https_request = require('https').request;

const CELSIUS_UNITS = 'C',
      FAHRENHEIT_UNITS = 'F';
const DEF_MIN_TEMPERATURE = -100,
      DEF_MAX_TEMPERATURE = 130,
      DEF_UNITS = CELSIUS_UNITS,
      DEF_TIMEOUT = 5000,
      DEF_INTERVAL = 120000; //120s

module.exports = function (homebridge) {
   Service = homebridge.hap.Service;
   Characteristic = homebridge.hap.Characteristic;
   homebridge.registerAccessory("homebridge-http-temperature", "HttpTemperature", HttpTemperature);
}


function HttpTemperature(log, config) {
   this.log = log;

   this.name = config.name;
   this.url = config.url;
   const protocol = config.http_protocol ? config.protocol.toLowerCase() :
      (this.url.indexOf('http://') !== -1 ? 'http' : 'https');
   this.request = protocol === 'http' ? http_request : https_request;
   this.request_opts = {
      method: config.http_method || "GET",
      timeout: config.timeout || DEF_TIMEOUT,
   };
   this.manufacturer = config["manufacturer"] || "@metbosch";
   this.model = config["model"] || "Model not available";
   this.serial = config["serial"] || "Non-defined serial";
   this.fieldName = ( config["field_name"] != null ? config["field_name"] : "temperature" );
   this.minTemperature = config["min_temp"] || DEF_MIN_TEMPERATURE;
   this.maxTemperature = config["max_temp"] || DEF_MAX_TEMPERATURE;
   this.units = config["units"] || DEF_UNITS;
   this.update_interval = Number( config["update_interval"] || DEF_INTERVAL );
   this.debug = config["debug"] || false;

   //Check auth option
   if (config.auth && config.auth.user !== undefined && config.auth.pass !== undefined) {
      this.request_opts.auth = config.auth.user + ':' + config.auth.pass;
   } else if (config.auth) {
      this.log('Ignoring invalid auth options. "user" and "pass" must be provided');
   }

   //Check headers option
   if (config.http_headers) {
      this.request_opts.headers = config.http_headers;
   }

   //Check if units field is valid
   this.units = this.units.toUpperCase()
   if (this.units !== CELSIUS_UNITS && this.units !== FAHRENHEIT_UNITS) {
      this.log('Bad temperature units : "' + this.units + '" (assuming Celsius)');
      this.units = CELSIUS_UNITS;
   }

   // Internal variables
   this.last_value = null;
   this.waiting_response = false;
}

HttpTemperature.prototype = {

   logDebug: function (str) {
      if (this.debug) {
         this.log(str)
      }
   },

   updateState: function () {
      //Ensure previous call finished
      if (this.waiting_response) {
         this.logDebug('Avoid updateState as previous response does not arrived yet');
         return;
      }
      this.waiting_response = true;
      this.last_value = new Promise((resolve, reject) => {
         this.logDebug('Requesting temperature on "' + this.url);
         this.request(this.url, this.request_opts, (res) => {
            let body = '';
            res.on('data', (chunk) => {
               body += chunk;
            });
            res.on('end', () => {
               let value = null;
               try {
                  const value_str = this.fieldName === '' ? body : this.getFromObject(JSON.parse(body), this.fieldName, '');
                  value_str = value_str.replace(/[^0-9.]/g, '');
                  value = Number(value_str);
                  if (value_str === '' || isNaN(value)) {
                     throw new Error('Received value is not a number: "' + value_str + '" ("' + body.substring(0, 100) + '")');
                  } else if (value < this.minTemperature || value > this.maxTemperature) {
                     var msg = 'Received value is out of bounds: "' + value + '". min=' + this.minTemperature +
                               ', max= ' + this.maxTemperature;
                     throw new Error(msg);
                  }
                  this.logDebug('HTTP successful response: ' + value);
                  if (this.units === FAHRENHEIT_UNITS) {
                     value = (value - 32)/1.8;
                     this.logDebug('Converted Fahrenheit temperature to celsius: ' + value);
                  }
               } catch (error) {
                  reject(error);
               }
               resolve(value);
            });
         }).on('error', reject).end();
      });
      this.last_value.then((value) => {
         this.temperatureService
            .getCharacteristic(Characteristic.CurrentTemperature).updateValue(value);
         this.temperatureService
            .getCharacteristic(Characteristic.StatusFault).updateValue(Characteristic.StatusFault.NO_FAULT);
         this.waiting_response = false;
      }).catch((error) => {<commit_msg>change const to let<commit_after>var Service, Characteristic;
const http_request = require('http').request;
const https_request = require('https').request;

const CELSIUS_UNITS = 'C',
      FAHRENHEIT_UNITS = 'F';
const DEF_MIN_TEMPERATURE = -100,
      DEF_MAX_TEMPERATURE = 130,
      DEF_UNITS = CELSIUS_UNITS,
      DEF_TIMEOUT = 5000,
      DEF_INTERVAL = 120000; //120s

module.exports = function (homebridge) {
   Service = homebridge.hap.Service;
   Characteristic = homebridge.hap.Characteristic;
   homebridge.registerAccessory("homebridge-http-temperature", "HttpTemperature", HttpTemperature);
}


function HttpTemperature(log, config) {
   this.log = log;

   this.name = config.name;
   this.url = config.url;
   const protocol = config.http_protocol ? config.protocol.toLowerCase() :
      (this.url.indexOf('http://') !== -1 ? 'http' : 'https');
   this.request = protocol === 'http' ? http_request : https_request;
   this.request_opts = {
      method: config.http_method || "GET",
      timeout: config.timeout || DEF_TIMEOUT,
   };
   this.manufacturer = config["manufacturer"] || "@metbosch";
   this.model = config["model"] || "Model not available";
   this.serial = config["serial"] || "Non-defined serial";
   this.fieldName = ( config["field_name"] != null ? config["field_name"] : "temperature" );
   this.minTemperature = config["min_temp"] || DEF_MIN_TEMPERATURE;
   this.maxTemperature = config["max_temp"] || DEF_MAX_TEMPERATURE;
   this.units = config["units"] || DEF_UNITS;
   this.update_interval = Number( config["update_interval"] || DEF_INTERVAL );
   this.debug = config["debug"] || false;

   //Check auth option
   if (config.auth && config.auth.user !== undefined && config.auth.pass !== undefined) {
      this.request_opts.auth = config.auth.user + ':' + config.auth.pass;
   } else if (config.auth) {
      this.log('Ignoring invalid auth options. "user" and "pass" must be provided');
   }

   //Check headers option
   if (config.http_headers) {
      this.request_opts.headers = config.http_headers;
   }

   //Check if units field is valid
   this.units = this.units.toUpperCase()
   if (this.units !== CELSIUS_UNITS && this.units !== FAHRENHEIT_UNITS) {
      this.log('Bad temperature units : "' + this.units + '" (assuming Celsius)');
      this.units = CELSIUS_UNITS;
   }

   // Internal variables
   this.last_value = null;
   this.waiting_response = false;
}

HttpTemperature.prototype = {

   logDebug: function (str) {
      if (this.debug) {
         this.log(str)
      }
   },

   updateState: function () {
      //Ensure previous call finished
      if (this.waiting_response) {
         this.logDebug('Avoid updateState as previous response does not arrived yet');
         return;
      }
      this.waiting_response = true;
      this.last_value = new Promise((resolve, reject) => {
         this.logDebug('Requesting temperature on "' + this.url);
         this.request(this.url, this.request_opts, (res) => {
            let body = '';
            res.on('data', (chunk) => {
               body += chunk;
            });
            res.on('end', () => {
               let value = null;
               try {
                  let value_str = this.fieldName === '' ? body : this.getFromObject(JSON.parse(body), this.fieldName, '');
                  value_str = value_str.replace(/[^0-9.]/g, '');
                  value = Number(value_str);
                  if (value_str === '' || isNaN(value)) {
                     throw new Error('Received value is not a number: "' + value_str + '" ("' + body.substring(0, 100) + '")');
                  } else if (value < this.minTemperature || value > this.maxTemperature) {
                     var msg = 'Received value is out of bounds: "' + value + '". min=' + this.minTemperature +
                               ', max= ' + this.maxTemperature;
                     throw new Error(msg);
                  }
                  this.logDebug('HTTP successful response: ' + value);
                  if (this.units === FAHRENHEIT_UNITS) {
                     value = (value - 32)/1.8;
                     this.logDebug('Converted Fahrenheit temperature to celsius: ' + value);
                  }
               } catch (error) {
                  reject(error);
               }
               resolve(value);
            });
         }).on('error', reject).end();
      });
      this.last_value.then((value) => {
         this.temperatureService
            .getCharacteristic(Characteristic.CurrentTemperature).updateValue(value);
         this.temperatureService
            .getCharacteristic(Characteristic.StatusFault).updateValue(Characteristic.StatusFault.NO_FAULT);
         this.waiting_response = false;
      }).catch((error) => {
{'fraction_non_alphanumeric': 0.13934426229508196, 'fraction_numerical': 0.006305170239596469, 'mean_word_length': 2.367657466383581, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '543628', 'n_tokens_mistral': 2850, 'n_tokens_neox': 2747, 'n_words': 567}