<commit_before>"use strict";

var request = require("request");
const EventEmitter = require("events");

var ThermostatItem = function(widget,platform,homebridge) {
    //General
    this.temperatureDisplayUnits = homebridge.hap.Characteristic.TemperatureDisplayUnits.FAHRENHEIT;

    //CurrentTemperature
    this.itemCurrentTemperature = undefined;
    this.listenerCurrentTemperature = undefined;
    this.wsCurrentTemperature = undefined;

    //TargetTemperature
    this.itemTargetTemperature = undefined;
    this.setTargetTemperatureInitialState = false;
    this.targetTemperatureArr = [];
    this.targetTemperatureInProgress = false;
    this.targetTemperatureTimeout = 1000;

    //CurrentHeatingCoolingState
    this.itemCurrentHeatingCoolingState = undefined;
    this.listenerCurrentHeatingCoolingState = undefined;
    this.wsCurrentHeatingCoolingState = undefined;

    //TargetHeatingCoolingState
    this.itemTargetHeatingCoolingState = undefined;
    this.listenerTargetHeatingCoolingState = undefined;
    this.wsTargetHeatingCoolingState = undefined;

    //CoolingThresholdTemperature
    this.itemCoolingThresholdTemperature = undefined;
    this.listenerCoolingThresholdTemperature = undefined;
    this.wsCoolingThresholdTemperature = undefined;

    //HeatingThresholdTemperature
    this.itemHeatingThresholdTemperature = undefined;
    this.listenerHeatingThresholdTemperature = undefined;
    this.wsHeatingThresholdTemperature = undefined;

    this.setFromOpenHAB = false;
    this.setInitialState = true;

    ThermostatItem.super_.call(this, widget,platform,homebridge);
};

//CurrentTemperature
ThermostatItem.prototype.setCurrentTemperatureItem = function(item){
    this.itemCurrentTemperature = item;
    this.temperatureDisplayUnits = this.homebridge.hap.Characteristic.TemperatureDisplayUnits.CELSIUS;
};
//TargetHeatingCoolingState
ThermostatItem.prototype.setTargetHeatingCoolingStateItem = function(item){
    this.itemTargetHeatingCoolingState = item;
};
//CoolingThresholdTemperature
ThermostatItem.prototype.setCoolingThresholdTemperatureItem = function(item){
    this.itemCoolingThresholdTemperature = item;
};
//HeatingThresholdTemperature
ThermostatItem.prototype.setHeatingThresholdTemperatureItem = function(item){
    this.itemHeatingThresholdTemperature = item;
};

/**
 * Init all thermostat listener
 */
ThermostatItem.prototype.initListener = function() {
    if ((typeof this.itemCurrentTemperature) == 'undefined'){
        throw new Error(this.name + " needs CurrentTemperatureItem!");
    }

    //CurrentTemperature
    this.listenerCurrentTemperature = this.listenerFactory(
        this.itemCurrentTemperature.name,
        this.itemCurrentTemperature.link,
        this.wsCurrentTemperature,
        this.log,
        this.updateCurrentTemperature.bind(this)
    );

    //TargetHeatingCoolingState
    this.listenerTargetHeatingCoolingState = this.listenerFactory(
        this.itemTargetHeatingCoolingState.name,
        this.itemTargetHeatingCoolingState.link,
        this.wsTargetHeatingCoolingState,
        this.log,
        this.updateTargetHeatingCoolingState.bind(this)
    );

    //CoolingThresholdTemperature
    this.listenerCoolingThresholdTemperature = this.listenerFactory(
        this.itemCoolingThresholdTemperature.name,
        this.itemCoolingThresholdTemperature.link,
        this.wsCoolingThresholdTemperature,
        this.log,
        this.updateCoolingThresholdTemperature.bind(this)
    );

    //HeatingThresholdTemperature
    this.listenerHeatingThresholdTemperature = this.listenerFactory(
        this.itemHeatingThresholdTemperature.name,
        this.itemHeatingThresholdTemperature.link,
        this.wsHeatingThresholdTemperature,
        this.log,
        this.updateHeatingThresholdTemperature.bind(this)
    );
};

/**
 * Initiialize Others Services needed for ThermostatItem
 * @returns {*}
 */
ThermostatItem.prototype.getOtherServices = function() {
    var otherService = new this.homebridge.hap.Service.Thermostat();

    //Init CurrentTemperature Characteristic
    otherService.getCharacteristic(this.homebridge.hap.Characteristic.CurrentTemperature)
        .on('get', this.getCurrentTemperatureState.bind(this))
        .setValue(this.checkTemperatureState(this.itemCurrentTemperature.state));

    //Init TargetTemperature Characteristic
    otherService.getCharacteristic(this.homebridge.hap.Characteristic.TargetTemperature)
        .on('get', this.getTargetTemperatureState.bind(this))
        .setValue(this.checkTemperatureState(this.itemTargetTemperature.state));

    //Init CurrentHeatingCoolingState Characteristic
    otherService.getCharacteristic(this.homebridge.hap.Characteristic.CurrentHeatingCoolingState)
        .on('get', this.getCurrentHeatingCoolingState.bind(this))
        .setValue(this.checkCurrentHeatingCoolingState(this.itemTargetHeatingCoolingState.state));

    //Init TargetHeatingCoolingState Characteristic
    otherService.getCharacteristic(this.homebridge.hap.Characteristic.TargetHeatingCoolingState)
        .on('set', this.setTargetHeatingCoolingState.bind(this))
        .on('get', this.getTargetHeatingCoolingState.bind(this))
        .setValue(this.checkTargetHeatingCoolingState(this.itemTargetHeatingCoolingState.state));

    //Init CoolingThresholdTemperature Characteristic
    otherService.getCharacteristic(this.homebridge.hap.Characteristic.CoolingThresholdTemperature)
        .on('set', this.setCoolingThresholdTemperature.bind(this))
        .on('get', this.getCoolingThresholdTemperature.bind(this))
        .setValue(this.checkTemperatureState(this.itemCoolingThresholdTemperature.state));

    //Init HeatingThresholdTemperature Characteristic
    otherService.getCharacteristic(this.homebridge.hap.Characteristic.HeatingThresholdTemperature)
        .on('set', this.setHeatingThresholdTemperature.bind(this))
        .on('get', this.getHeatingThresholdTemperature.bind(this))
        .setValue(this.checkTemperatureState(this.itemHeatingThresholdTemperature.state));

    otherService.getCharacteristic(this.homebridge.hap.Characteristic.TemperatureDisplayUnits)
        .on('get', this.getTemperatureDisplayUnits.bind(this))
        .setValue(this.temperatureDisplayUnits);

    this.setInitialState = false;

    return otherService;
};

//CurrentTemperature
ThermostatItem.prototype.updateCurrentTemperature = function(message) {
    this.otherService
        .getCharacteristic(this.homebridge.hap.Characteristic.CurrentTemperature)
        .setValue(this.checkTemperatureState(message));
};
ThermostatItem.prototype.getCurrentTemperatureState = function(callback) {
    var self = this;
    this.log("iOS - request current temperature state from " + this.itemCurrentTemperature.name + " (" + (self.name)+")");
    request(self.itemCurrentTemperature.link + '/state?type=json', function (error, response, body) {
        if (!error && response.statusCode == 200) {
            self.log("OpenHAB HTTP - response from " + self.itemCurrentTemperature.name + " (" + (self.name)+"): " + body);
            callback(undefined,self.checkTemperatureState(body));
        } else {
            self.log("OpenHAB HTTP - error from " + self.itemCurrentTemperature.name + " (" + (self.name)+"): " + error);
        }
    });
};


//TargetTemperature
ThermostatItem.prototype.getTargetTemperatureState = funtion(callback) {
    var self = this;
    //Cooling or off or auto
    var temperatureItem = this.itemCoolingThresholdTemperature;
    if (this.itemCurrentHeatingCoolingState.state == '1') {<commit_msg>functions are funtion<commit_after>"use strict";

var request = require("request");
const EventEmitter = require("events");

var ThermostatItem = function(widget,platform,homebridge) {
    //General
    this.temperatureDisplayUnits = homebridge.hap.Characteristic.TemperatureDisplayUnits.FAHRENHEIT;

    //CurrentTemperature
    this.itemCurrentTemperature = undefined;
    this.listenerCurrentTemperature = undefined;
    this.wsCurrentTemperature = undefined;

    //TargetTemperature
    this.itemTargetTemperature = undefined;
    this.setTargetTemperatureInitialState = false;
    this.targetTemperatureArr = [];
    this.targetTemperatureInProgress = false;
    this.targetTemperatureTimeout = 1000;

    //CurrentHeatingCoolingState
    this.itemCurrentHeatingCoolingState = undefined;
    this.listenerCurrentHeatingCoolingState = undefined;
    this.wsCurrentHeatingCoolingState = undefined;

    //TargetHeatingCoolingState
    this.itemTargetHeatingCoolingState = undefined;
    this.listenerTargetHeatingCoolingState = undefined;
    this.wsTargetHeatingCoolingState = undefined;

    //CoolingThresholdTemperature
    this.itemCoolingThresholdTemperature = undefined;
    this.listenerCoolingThresholdTemperature = undefined;
    this.wsCoolingThresholdTemperature = undefined;

    //HeatingThresholdTemperature
    this.itemHeatingThresholdTemperature = undefined;
    this.listenerHeatingThresholdTemperature = undefined;
    this.wsHeatingThresholdTemperature = undefined;

    this.setFromOpenHAB = false;
    this.setInitialState = true;

    ThermostatItem.super_.call(this, widget,platform,homebridge);
};

//CurrentTemperature
ThermostatItem.prototype.setCurrentTemperatureItem = function(item){
    this.itemCurrentTemperature = item;
    this.temperatureDisplayUnits = this.homebridge.hap.Characteristic.TemperatureDisplayUnits.CELSIUS;
};
//TargetHeatingCoolingState
ThermostatItem.prototype.setTargetHeatingCoolingStateItem = function(item){
    this.itemTargetHeatingCoolingState = item;
};
//CoolingThresholdTemperature
ThermostatItem.prototype.setCoolingThresholdTemperatureItem = function(item){
    this.itemCoolingThresholdTemperature = item;
};
//HeatingThresholdTemperature
ThermostatItem.prototype.setHeatingThresholdTemperatureItem = function(item){
    this.itemHeatingThresholdTemperature = item;
};

/**
 * Init all thermostat listener
 */
ThermostatItem.prototype.initListener = function() {
    if ((typeof this.itemCurrentTemperature) == 'undefined'){
        throw new Error(this.name + " needs CurrentTemperatureItem!");
    }

    //CurrentTemperature
    this.listenerCurrentTemperature = this.listenerFactory(
        this.itemCurrentTemperature.name,
        this.itemCurrentTemperature.link,
        this.wsCurrentTemperature,
        this.log,
        this.updateCurrentTemperature.bind(this)
    );

    //TargetHeatingCoolingState
    this.listenerTargetHeatingCoolingState = this.listenerFactory(
        this.itemTargetHeatingCoolingState.name,
        this.itemTargetHeatingCoolingState.link,
        this.wsTargetHeatingCoolingState,
        this.log,
        this.updateTargetHeatingCoolingState.bind(this)
    );

    //CoolingThresholdTemperature
    this.listenerCoolingThresholdTemperature = this.listenerFactory(
        this.itemCoolingThresholdTemperature.name,
        this.itemCoolingThresholdTemperature.link,
        this.wsCoolingThresholdTemperature,
        this.log,
        this.updateCoolingThresholdTemperature.bind(this)
    );

    //HeatingThresholdTemperature
    this.listenerHeatingThresholdTemperature = this.listenerFactory(
        this.itemHeatingThresholdTemperature.name,
        this.itemHeatingThresholdTemperature.link,
        this.wsHeatingThresholdTemperature,
        this.log,
        this.updateHeatingThresholdTemperature.bind(this)
    );
};

/**
 * Initiialize Others Services needed for ThermostatItem
 * @returns {*}
 */
ThermostatItem.prototype.getOtherServices = function() {
    var otherService = new this.homebridge.hap.Service.Thermostat();

    //Init CurrentTemperature Characteristic
    otherService.getCharacteristic(this.homebridge.hap.Characteristic.CurrentTemperature)
        .on('get', this.getCurrentTemperatureState.bind(this))
        .setValue(this.checkTemperatureState(this.itemCurrentTemperature.state));

    //Init TargetTemperature Characteristic
    otherService.getCharacteristic(this.homebridge.hap.Characteristic.TargetTemperature)
        .on('get', this.getTargetTemperatureState.bind(this))
        .setValue(this.checkTemperatureState(this.itemTargetTemperature.state));

    //Init CurrentHeatingCoolingState Characteristic
    otherService.getCharacteristic(this.homebridge.hap.Characteristic.CurrentHeatingCoolingState)
        .on('get', this.getCurrentHeatingCoolingState.bind(this))
        .setValue(this.checkCurrentHeatingCoolingState(this.itemTargetHeatingCoolingState.state));

    //Init TargetHeatingCoolingState Characteristic
    otherService.getCharacteristic(this.homebridge.hap.Characteristic.TargetHeatingCoolingState)
        .on('set', this.setTargetHeatingCoolingState.bind(this))
        .on('get', this.getTargetHeatingCoolingState.bind(this))
        .setValue(this.checkTargetHeatingCoolingState(this.itemTargetHeatingCoolingState.state));

    //Init CoolingThresholdTemperature Characteristic
    otherService.getCharacteristic(this.homebridge.hap.Characteristic.CoolingThresholdTemperature)
        .on('set', this.setCoolingThresholdTemperature.bind(this))
        .on('get', this.getCoolingThresholdTemperature.bind(this))
        .setValue(this.checkTemperatureState(this.itemCoolingThresholdTemperature.state));

    //Init HeatingThresholdTemperature Characteristic
    otherService.getCharacteristic(this.homebridge.hap.Characteristic.HeatingThresholdTemperature)
        .on('set', this.setHeatingThresholdTemperature.bind(this))
        .on('get', this.getHeatingThresholdTemperature.bind(this))
        .setValue(this.checkTemperatureState(this.itemHeatingThresholdTemperature.state));

    otherService.getCharacteristic(this.homebridge.hap.Characteristic.TemperatureDisplayUnits)
        .on('get', this.getTemperatureDisplayUnits.bind(this))
        .setValue(this.temperatureDisplayUnits);

    this.setInitialState = false;

    return otherService;
};

//CurrentTemperature
ThermostatItem.prototype.updateCurrentTemperature = function(message) {
    this.otherService
        .getCharacteristic(this.homebridge.hap.Characteristic.CurrentTemperature)
        .setValue(this.checkTemperatureState(message));
};
ThermostatItem.prototype.getCurrentTemperatureState = function(callback) {
    var self = this;
    this.log("iOS - request current temperature state from " + this.itemCurrentTemperature.name + " (" + (self.name)+")");
    request(self.itemCurrentTemperature.link + '/state?type=json', function (error, response, body) {
        if (!error && response.statusCode == 200) {
            self.log("OpenHAB HTTP - response from " + self.itemCurrentTemperature.name + " (" + (self.name)+"): " + body);
            callback(undefined,self.checkTemperatureState(body));
        } else {
            self.log("OpenHAB HTTP - error from " + self.itemCurrentTemperature.name + " (" + (self.name)+"): " + error);
        }
    });
};


//TargetTemperature
ThermostatItem.prototype.getTargetTemperatureState = function(callback) {
    var self = this;
    //Cooling or off or auto
    var temperatureItem = this.itemCoolingThresholdTemperature;
    if (this.itemCurrentHeatingCoolingState.state == '1') {
{'fraction_non_alphanumeric': 0.09516267405794232, 'fraction_numerical': 0.00105589652214083, 'mean_word_length': 4.512550018188432, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '320159', 'n_tokens_mistral': 4267, 'n_tokens_neox': 4116, 'n_words': 532}