<commit_before>"use strict";

var request = require("request");

var DimmerItem = function(widget,platform,homebridge) {
    DimmerItem.super_.call(this, widget,platform,homebridge);
};

DimmerItem.prototype.getServices = function() {

    this.initListener();
    this.setInitialState = true;

    this.informationService = this.getInformationServices();

    this.otherService = new this.homebridge.hap.Service.Lightbulb();
    this.otherService.getCharacteristic(this.homebridge.hap.Characteristic.On)
        .on('set', this.setItem.bind(this))
        .on('get', this.getItemPowerState.bind(this))
        .setValue(+this.state > 0);

    this.setInitialState = true;

    this.otherService.addCharacteristic(this.homebridge.hap.Characteristic.Brightness)
        .on('set', this.setItem.bind(this))
        .on('get', this.getItemBrightnessState.bind(this))
        .setValue(+this.state);

    return [this.informationService, this.otherService];
};

DimmerItem.prototype.updateCharacteristics = function(message) {

    this.setFromOpenHAB = true;
    var brightness = +message;
    var steps = 2;
    if (brightness >= 0) {
        this.otherService.getCharacteristic(this.homebridge.hap.Characteristic.Brightness)
            .setValue(brightness,
                function() {
                    steps--;
                    if (!steps) {
                        this.setFromOpenHAB = false;
                    }
                }.bind(this));
        this.otherService.getCharacteristic(this.homebridge.hap.Characteristic.On)
            .setValue(brightness > 0,
                function() {
                    steps--;
                    if (!steps) {
                        this.setFromOpenHAB = false;
                    }
                }.bind(this));
    }
};

DimmerItem.prototype.getItemPowerState = function(callback) {

    var self = this;

    this.log("iOS - request power state from " + this.name);
    request(this.url + '/state?type=json', function (error, response, body) {
        if (!error && response.statusCode == 200) {
            self.log("OpenHAB HTTP - power state response from " + self.name + ": " + (+body > 0));
            callback(undefined,+body > 0);
        } else {
            self.log("OpenHAB HTTP - error from " + self.name + ": " + error);
        }
    })
};

DimmerItem.prototype.setItem = function(value, callback) {

    var self = this;

    if (this.setInitialState) {
        this.setInitialState = false;
        callback();
        return;
    }

    if (this.setFromOpenHAB) {
        callback();
        return;
    }

    this.log("iOS - send message to " + this.name + ": " + value);
    var command = "0";
    if (value == '1' && this.otherService.getCharacteristic(this.homebridge.hap.Characteristic.On).value == true) {
        callback();
        return;
    } else if (value == false) {
        command = "0";
    } else {
        command = "" + value;
    }
    if (value == '100' && self.name.indexOf("Volume") !== -1)
    {
        command = "20";
    }

    request.post(
        this.url,
        {
            body: command,
            headers: {'Content-Type': 'text/plain'}
        },
        function (error, response, body) {
            if (!error && response.statusCode == 201) {
                self.log("OpenHAB HTTP - response from " + self.name + ": " + body);
            } else {
                self.log("OpenHAB HTTP - error from " + self.name + ": " + error);
            }
            callback();
        }
    );
};

DimmerItem.prototype.getItemBrightnessState = function(callback) {

    var self = this;

    this.log("iOS - request brightness state from " + this.name);
    request(this.url + '/state?type=json', function (error, response, body) {
        if (!error && response.statusCode == 200) {
            self.log("OpenHAB HTTP - brightness state response from " + self.name + ": " + body);
            callback(undefined,+body);
        } else {
            self.log("OpenHAB HTTP - error from " + self.name + ": " + error);
        }
    })
};

module.exports = DimmerItem;
<commit_msg>Testing homekit behavior<commit_after>"use strict";

var request = require("request");

var DimmerItem = function(widget,platform,homebridge) {
    DimmerItem.super_.call(this, widget,platform,homebridge);
};

DimmerItem.prototype.getServices = function() {

    this.initListener();
    this.setInitialState = true;

    this.informationService = this.getInformationServices();

    this.otherService = new this.homebridge.hap.Service.Lightbulb();
    this.otherService.getCharacteristic(this.homebridge.hap.Characteristic.On)
        .on('set', this.setItem.bind(this))
        .on('get', this.getItemPowerState.bind(this))
        .setValue(+this.state > 0);

    this.setInitialState = true;

    this.otherService.addCharacteristic(this.homebridge.hap.Characteristic.Brightness)
        .on('set', this.setItem.bind(this))
        .on('get', this.getItemBrightnessState.bind(this))
        .setValue(+this.state);

    return [this.informationService, this.otherService];
};

DimmerItem.prototype.updateCharacteristics = function(message) {

    this.setFromOpenHAB = true;
    var brightness = +message;
    var steps = 2;
    if (brightness >= 0) {
        this.otherService.getCharacteristic(this.homebridge.hap.Characteristic.Brightness)
            .setValue(brightness,
                function() {
                    steps--;
                    if (!steps) {
                        this.setFromOpenHAB = false;
                    }
                }.bind(this));
        this.otherService.getCharacteristic(this.homebridge.hap.Characteristic.On)
            .setValue(brightness > 0,
                function() {
                    steps--;
                    if (!steps) {
                        this.setFromOpenHAB = false;
                    }
                }.bind(this));
    }
};

DimmerItem.prototype.getItemPowerState = function(callback) {

    var self = this;

    this.log("iOS - request power state from " + this.name);
    request(this.url + '/state?type=json', function (error, response, body) {
        if (!error && response.statusCode == 200) {
            self.log("OpenHAB HTTP - power state response from " + self.name + ": " + (+body > 0));
            callback(undefined,+body > 0);
        } else {
            self.log("OpenHAB HTTP - error from " + self.name + ": " + error);
        }
    })
};

DimmerItem.prototype.setItem = function(value, callback) {

    var self = this;

    if (this.setInitialState) {
        this.setInitialState = false;
        callback();
        return;
    }

    if (this.setFromOpenHAB) {
        callback();
        return;
    }

    this.log("iOS - send message to " + this.name + ": " + value);
    var command = "0";
    if (value == '1') {
        callback();
        return;
    } else if (value == false) {
        command = "0";
    } else if (value == true) {
        value = '100';
        command = "" + value;
    } else {
        command = "" + value;
    }

    if (value == '100' && self.name.indexOf("Volume") !== -1)
    {
        command = "20";
    }

    request.post(
        this.url,
        {
            body: command,
            headers: {'Content-Type': 'text/plain'}
        },
        function (error, response, body) {
            if (!error && response.statusCode == 201) {
                self.log("OpenHAB HTTP - response from " + self.name + ": " + body);
            } else {
                self.log("OpenHAB HTTP - error from " + self.name + ": " + error);
            }
            callback();
        }
    );
};

DimmerItem.prototype.getItemBrightnessState = function(callback) {

    var self = this;

    this.log("iOS - request brightness state from " + this.name);
    request(this.url + '/state?type=json', function (error, response, body) {
        if (!error && response.statusCode == 200) {
            self.log("OpenHAB HTTP - brightness state response from " + self.name + ": " + body);
            callback(undefined,+body);
        } else {
            self.log("OpenHAB HTTP - error from " + self.name + ": " + error);
        }
    })
};

module.exports = DimmerItem;

{'fraction_non_alphanumeric': 0.14040219378427787, 'fraction_numerical': 0.006215722120658135, 'mean_word_length': 2.014695077149155, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 13, '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': '2053839', 'n_tokens_mistral': 2466, 'n_tokens_neox': 2350, 'n_words': 458}