<commit_before>var Service, Characteristic;
var request = require('sync-request');
var JSONPath = require('JSONPath');

var temperatureService;
var humidityService;
var url
var humidity = 0;
var temperature = 0;

module.exports = function (homebridge) {
    Service = homebridge.hap.Service;
    Characteristic = homebridge.hap.Characteristic;
    homebridge.registerAccessory("homebridge-httptemperaturehumidity", "HttpTemphum", HttpTemphum);
}

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

    // url info
    this.url = config["url"];
    this.http_method = config["http_method"] || "GET";
    this.sendimmediately = config["sendimmediately"] || "";
    this.name = config["name"];
    this.manufacturer = config["manufacturer"] || "Luca Manufacturer";
    this.model = config["model"] || "Luca Model";
    this.serial = config["serial"] || "Luca Serial";
    this.temperaturePath = config["temperaturePath"];
    this.temperatureMultiplier = config["temperatureMultiplier"] || 1.0;
}

HttpTemphum.prototype = {

    httpRequest: function (url, body, method, username, password, sendimmediately, callback) {
        request({
            url: url,
            body: body,
            method: method,
            rejectUnauthorized: false
        },
            function (error, response, body) {
                callback(error, response, body)
            })
    },

    getStateHumidity: function (callback) {
        callback(null, this.humidity);
    },

    getState: function (callback) {
        var body;

        var res = request(this.http_method, this.url, {});
        if (res.statusCode > 400) {
            this.log('HTTP request failed');
            callback(error);
        } else {
            this.log('HTTP request succeeded!');

            var info = JSON.parse(res.body);
            this.log(info);

            var me = this;
            if (this.temperaturePath) {
                JSONPath({
                    json: info, path: this.temperaturePath, callback: function (value) {
                        me.log("Parse-Result: " + value);
                        var temperature =  me.temperatureMultiplier * value;
                        me.log("Format-Result: " + temperature);
                        temperatureService.setCharacteristic(Characteristic.CurrentTemperature, temperature);
                        callback(null, temperature);
                        me.temperature = temperature;
                    }
                });
            }
        }
    },

    identify: function (callback) {
        this.log("Identify requested!");
        callback(); // success
    },

    getServices: function () {
        var informationService = new Service.AccessoryInformation();
        informationService
            .setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
            .setCharacteristic(Characteristic.Model, this.model)
            .setCharacteristic(Characteristic.SerialNumber, this.serial);

        temperatureService = new Service.TemperatureSensor(this.name);
        temperatureService
            .getCharacteristic(Characteristic.CurrentTemperature)
            .on('get', this.getState.bind(this));

        return [informationService, temperatureService];
    }
};<commit_msg>Integrated a cache per url to save load time for multiple devices references same url<commit_after>var Service, Characteristic;
var request = require('sync-request');
var JSONPath = require('JSONPath');

var temperatureService;
var url
var temperature = 0;

module.exports = function (homebridge) {
    Service = homebridge.hap.Service;
    Characteristic = homebridge.hap.Characteristic;
    homebridge.registerAccessory("homebridge-httptemperaturehumidity", "HttpTemphum", HttpTemphum);
}

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

    // url info
    this.url = config["url"];
    this.http_method = config["http_method"] || "GET";
    this.sendimmediately = config["sendimmediately"] || "";
    this.name = config["name"];
    this.manufacturer = config["manufacturer"] || "Luca Manufacturer";
    this.model = config["model"] || "Luca Model";
    this.serial = config["serial"] || "Luca Serial";
    this.temperaturePath = config["temperaturePath"];
    this.temperatureMultiplier = config["temperatureMultiplier"] || 1.0;
    
    var temperatureCache = GLOBAL.temperatureCache;
    if (!temperatureCache) {
        temperatureCache = {};
        GLOBAL.temperatureCache = temperatureCache;
    }

    this.myTempCache = temperatureCache[this.url];
    if (!this.myTempCache) {
        this.isMaster = true;
        this.myTempCache = { lastFetchTime: null, lastFetchResult : 0 };
        temperatureCache[this.url] = this.myTempCache;
        this.log("is master for " + this.url);
    } else {
        this.log("is not the master for " + this.url);
        this.isMaster = false;
    }
}

HttpTemphum.prototype = {

    getStateCached: function (callback) {
        var me = this;
        var cache = this.myTempCache;
        
        var cacheIsGood = cache.lastFetchResult && (new Date().getTime() - cache.lastFetchTime < 60000);
        if (cacheIsGood) {
            this.log("Using cached result: " +  cache.lastFetchResult);
            me.getTemperatureFromJson(cache.lastFetchResult, function(temperature) {
                //temperatureService.setCharacteristic(Characteristic.CurrentTemperature, temperature);
                callback(null, temperature);
            });
        } else {
            this.log("Cache expired. Fetching new data... ");
            this.getJSON(function(error, json) {
                if (error) {
                    callback(error);
                } else {
                    cache.lastFetchResult = json;
                    cache.lastFetchTime = new Date().getTime();

                    me.getTemperatureFromJson(json, function(temperature) {
                        //temperatureService.setCharacteristic(Characteristic.CurrentTemperature, temperature);
                        callback(null, temperature);
                    });
                }
            });
        }
    },

    getTemperatureFromJson: function (json, callback) {
        var me = this;
        if (this.temperaturePath) {
            JSONPath({
                json: json, path: this.temperaturePath, callback: function (value) {
                    me.log("Parse-Result: " + value + " - Format-Result: " + temperature);
                    var temperature =  me.temperatureMultiplier * value;
                    callback(temperature);
                }
            });
        }
    },

    getJSON: function(callback) {
        var res = request(this.http_method, this.url, {});

        if (res.statusCode > 400) {
            this.log('HTTP request failed');
            callback(error);
        } else {
            this.log('HTTP request succeeded!!');
            var info = JSON.parse(res.body);
            this.log(info);
            callback(null, info);
        }
    },

    getState: function (callback) {
        var me = this;
        this.getJSON(function(error, json) {
            if (error) {
                callback(error);
            } else {
                me.getTemperatureFromJson(json, function(temperature) {
                    temperatureService.setCharacteristic(Characteristic.CurrentTemperature, temperature);
                    callback(null, temperature);
                });
            }
        });
    },

    identify: function (callback) {
        this.log("Identify requested!");
        callback(); // success
    },

    getServices: function () {
        var informationService = new Service.AccessoryInformation();
        informationService
            .setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
            .setCharacteristic(Characteristic.Model, this.model)
            .setCharacteristic(Characteristic.SerialNumber, this.serial);

        temperatureService = new Service.TemperatureSensor(this.name);
        temperatureService
            .getCharacteristic(Characteristic.CurrentTemperature)
            .on('get', this.getStateCached.bind(this));

        return [informationService, temperatureService];
    }
};
{'fraction_non_alphanumeric': 0.11373599610326351, 'fraction_numerical': 0.0023136872868972235, 'mean_word_length': 2.2007014809041308, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, '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': '429438', 'n_tokens_mistral': 2170, 'n_tokens_neox': 2071, 'n_words': 432}