<commit_before>//"use strict";
var Accessory, Service, Characteristic, UUIDGen;
var JSONRequest = require("jsonrequest");
var inherits = require('util').inherits;
var path = require('path');
var fs = require('fs');

module.exports = function (homebridge) {
    console.log("homebridge API version: " + homebridge.version);

    // Accessory must be created from PlatformAccessory Constructor    
    Accessory = homebridge.platformAccessory;

    // Service and Characteristic are from hap-nodejs    
    Service = homebridge.hap.Service;
    Characteristic = homebridge.hap.Characteristic;
    UUIDGen = homebridge.hap.uuid;

    ////////////////////////////// Custom characteristics //////////////////////////////
    Characteristic.EvePowerConsumption = function () {
        Characteristic.call(this, 'Consumption', 'E863F10D-079E-48FF-8F27-9C2605A29F52');
        this.setProps({
            format: Characteristic.Formats.UINT16,
            unit: "watts",
            maxValue: 1000000000,
            minValue: 0,
            minStep: 1,
            perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
        });
        this.value = this.getDefaultValue();
    };
    inherits(Characteristic.EvePowerConsumption, Characteristic);
    	Characteristic.EvePowerConsumption.UUID = 'E863F10D-079E-48FF-8F27-9C2605A29F52';


    Characteristic.EveTotalPowerConsumption = function () {
        Characteristic.call(this, 'Total Consumption', 'E863F10C-079E-48FF-8F27-9C2605A29F52');
        this.setProps({
            format: Characteristic.Formats.FLOAT, // Deviation from Eve Energy observed type
            unit: "kilowatthours",
            maxValue: 1000000000,
            minValue: 0,
            minStep: 0.001,
            perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
        });
        this.value = this.getDefaultValue();
    };
    inherits(Characteristic.EveTotalPowerConsumption, Characteristic);
    	Characteristic.EveTotalPowerConsumption.UUID = 'E863F10C-079E-48FF-8F27-9C2605A29F52';


    Characteristic.EveRoomAirQuality = function () {
        Characteristic.call(this, 'Eve Air Quality', 'E863F10B-079E-48FF-8F27-9C2605A29F52');
        this.setProps({
            format: Characteristic.Formats.UINT16,
            unit: "ppm",
            maxValue: 5000,
            minValue: 0,
            minStep: 1,
            perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
        });
        this.value = this.getDefaultValue();
    };
    inherits(Characteristic.EveRoomAirQuality, Characteristic);
    	Characteristic.EveRoomAirQuality.UUID = 'E863F10B-079E-48FF-8F27-9C2605A29F52';


    Characteristic.EveBatteryLevel = function () {
        Characteristic.call(this, 'Eve Battery Level', 'E863F11B-079E-48FF-8F27-9C2605A29F52');
        this.setProps({
            format: Characteristic.Formats.UINT16,
            unit: "PERCENTAGE",
            maxValue: 100,
            minValue: 0,
            minStep: 1,
            perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
        });
        this.value = this.getDefaultValue();
    };
    inherits(Characteristic.EveBatteryLevel, Characteristic);
    	Characteristic.EveBatteryLevel.UUID = 'E863F11B-079E-48FF-8F27-9C2605A29F52';


    Characteristic.EveAirPressure = function () {
        //todo: only rough guess of extreme values -> use correct min/max if known
        Characteristic.call(this, 'Eve AirPressure', 'E863F10F-079E-48FF-8F27-9C2605A29F52');
        this.setProps({
            format: Characteristic.Formats.UINT16,
            unit: "hPa",
            maxValue: 1085,
            minValue: 870,
            minStep: 1,
            perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
        });
        this.value = this.getDefaultValue();
    };
    inherits(Characteristic.EveAirPressure, Characteristic);
    	Characteristic.EveAirPressure.UUID = 'E863F10F-079E-48FF-8F27-9C2605A29F52';



    ////////////////////////////// Custom services //////////////////////////////
    Service.PowerMeterService = function (displayName, subtype) {
        Service.call(this, displayName, '00000001-0000-1777-8000-775D67EC4377', subtype);
        // Required Characteristics
        this.addCharacteristic(Characteristic.EvePowerConsumption);
        // Optional Characteristics
        this.addOptionalCharacteristic(Characteristic.EveTotalPowerConsumption);
    };
    inherits(Service.PowerMeterService, Service);
    	Service.PowerMeterService.UUID = '00000001-0000-1777-8000-775D67EC4377';


    //Eve service (custom UUID)
    Service.EveRoomService = function (displayName, subtype) {
        Service.call(this, displayName, 'E863F002-079E-48FF-8F27-9C2605A29F52', subtype);
        // Required Characteristics
        this.addCharacteristic(Characteristic.EveRoomAirQuality);
        // Optional Characteristics
        this.addOptionalCharacteristic(Characteristic.CurrentRelativeHumidity);
    };
    inherits(Service.EveRoomService, Service);
    	Service.EveRoomService.UUID = 'E863F002-079E-48FF-8F27-9C2605A29F52';


    /////////////////////////////////////////////////////////////////////////////////////////////
    //Eve service (custom UUID)
    Service.EveWeatherService = function (displayName, subtype) {
        Service.call(this, displayName, 'E863F001-079E-48FF-8F27-9C2605A29F52', subtype);
        // Required Characteristics
        this.addCharacteristic(Characteristic.EveAirPressure);
        // Optional Characteristics
        this.addOptionalCharacteristic(Characteristic.CurrentRelativeHumidity);
        this.addOptionalCharacteristic(Characteristic.CurrentTemperature);
        this.addOptionalCharacteristic(Characteristic.EveBatteryLevel);
    };
    inherits(Service.EveWeatherService, Service);
    	Service.EveWeatherService.UUID = 'E863F001-079E-48FF-8F27-9C2605A29F52';


    // Consider platform plugin as dynamic platform plugin
    homebridge.registerPlatform("homebridge-domotiga", "DomotiGa", DomotigaPlatform, true);
}

function DomotigaPlatform(log, config, api) {
    this.log = log;
    this.config = config;
    this.log("DomotiGa Plugin Version " + this.getVersion());
    this.log("Plugin by Samfox2 https://github.com/samfox2");
    this.log("DomotiGa is a Open Source Home Automation Software for Linux");
    this.log("Please report any issues to https://github.com/samfox2/homebridge-domotiga/issues");

    var self = this;
    self.fetch_npmVersion("homebridge-domotiga", function (npmVersion) {
        npmVersion = npmVersion.replace('\n', '');
        self.log("NPM %s vs Local %s", npmVersion, self.getVersion());
        if (npmVersion > self.getVersion()) {
            self.log.warn("There is a new Version available. Please update with sudo npm -g update homebridge-domotiga");
        }
    });

    if (config) {
        // Global configuration
        this.host = this.config.host || 'localhost';
        this.port = this.config.port || 9090;

        // Device specific configuration
        this.devices = this.config.devices || [];
        this.accessories = {};
        this.polling = {};

        if (api) {
            this.api = api;
            this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this));
        }
    }
}

// Method to restore accessories from cache
DomotigaPlatform.prototype.configureAccessory = function (accessory) {
    this.setService(accessory);
    this.accessories[accessory.context.name] = accessory;
}

// Method to setup accessories from config.json
DomotigaPlatform.prototype.didFinishLaunching = function () {

    if (!this.devices.length) {
        this.log.error("No devices configured. Please check your 'config.json' file!");
    }

    // Add or update accessories defined in config.json
    for (var i in this.devices) this.addAccessory(this.devices[i]);

    // Remove extra accessories in cache
    for (var name in this.accessories) {
        var accessory = this.accessories[name];
        if (!accessory.reachable) this.removeAccessory(accessory);
    }

    // Check number of devices
    var noD = this.accessories.length;
    this.log("Number of mapped devices : " + noD);
    if (noD > 100) {
        this.log.error("********************************************");
        this.log.error("* You are using more than 100 HomeKit      *");
        this.log.error("* devices behind a bridge. At this time    *");
        this.log.error("* HomeKit only supports up to 100 devices. *");
        this.log.error("* This may end up that iOS is not able to  *");
        this.log.error("* connect to the bridge anymore.           *");
        this.log.error("********************************************");
    } else {
        if (noD > 90) {
            this.log.warn("You are using more than 90 HomeKit");
            this.log.warn("devices behind a bridge. At this time");
            this.log.warn("HomeKit only supports up to 100 devices.");
            this.log.warn("This is just a warning. Everything should");
            this.log.warn("work fine until you are below that 100.");
        }
    }
}


// Method to add and update HomeKit accessories
DomotigaPlatform.prototype.addAccessory = function (data) {

    this.log("Initializing platform accessory '" + data.name + "'...");

    // Retrieve accessory from cache
    var accessory = this.accessories[data.name];

    if (!accessory) {

        var uuid = UUIDGen.generate(data.name);

        // Setup accessory category.
        accessory = new Accessory(data.name, uuid, 8);

        // Store and initialize logfile into context
        accessory.context.name = data.name || NA;
        accessory.context.service = data.service;
        accessory.context.device = data.device;
        accessory.context.manufacturer = data.manufacturer || NA;
        accessory.context.model = data.model || NA;<commit_msg>Update index.js<commit_after>//"use strict";
var Accessory, Service, Characteristic, UUIDGen;
var JSONRequest = require("jsonrequest");
var inherits = require('util').inherits;
var path = require('path');
var fs = require('fs');

module.exports = function (homebridge) {
    console.log("homebridge API version: " + homebridge.version);

    // Accessory must be created from PlatformAccessory Constructor    
    Accessory = homebridge.platformAccessory;

    // Service and Characteristic are from hap-nodejs    
    Service = homebridge.hap.Service;
    Characteristic = homebridge.hap.Characteristic;
    UUIDGen = homebridge.hap.uuid;

    ////////////////////////////// Custom characteristics //////////////////////////////
    Characteristic.EvePowerConsumption = function () {
        Characteristic.call(this, 'Consumption', 'E863F10D-079E-48FF-8F27-9C2605A29F52');
        this.setProps({
            format: Characteristic.Formats.UINT16,
            unit: "watts",
            maxValue: 1000000000,
            minValue: 0,
            minStep: 1,
            perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
        });
        this.value = this.getDefaultValue();
    };
    inherits(Characteristic.EvePowerConsumption, Characteristic);
    	Characteristic.EvePowerConsumption.UUID = 'E863F10D-079E-48FF-8F27-9C2605A29F52';


    Characteristic.EveTotalPowerConsumption = function () {
        Characteristic.call(this, 'Total Consumption', 'E863F10C-079E-48FF-8F27-9C2605A29F52');
        this.setProps({
            format: Characteristic.Formats.FLOAT, // Deviation from Eve Energy observed type
            unit: "kilowatthours",
            maxValue: 1000000000,
            minValue: 0,
            minStep: 0.001,
            perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
        });
        this.value = this.getDefaultValue();
    };
    inherits(Characteristic.EveTotalPowerConsumption, Characteristic);
    	Characteristic.EveTotalPowerConsumption.UUID = 'E863F10C-079E-48FF-8F27-9C2605A29F52';


    Characteristic.EveRoomAirQuality = function () {
        Characteristic.call(this, 'Eve Air Quality', 'E863F10B-079E-48FF-8F27-9C2605A29F52');
        this.setProps({
            format: Characteristic.Formats.UINT16,
            unit: "ppm",
            maxValue: 5000,
            minValue: 0,
            minStep: 1,
            perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
        });
        this.value = this.getDefaultValue();
    };
    inherits(Characteristic.EveRoomAirQuality, Characteristic);
    	Characteristic.EveRoomAirQuality.UUID = 'E863F10B-079E-48FF-8F27-9C2605A29F52';


    Characteristic.EveBatteryLevel = function () {
        Characteristic.call(this, 'Eve Battery Level', 'E863F11B-079E-48FF-8F27-9C2605A29F52');
        this.setProps({
            format: Characteristic.Formats.UINT16,
            unit: "PERCENTAGE",
            maxValue: 100,
            minValue: 0,
            minStep: 1,
            perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
        });
        this.value = this.getDefaultValue();
    };
    inherits(Characteristic.EveBatteryLevel, Characteristic);
    	Characteristic.EveBatteryLevel.UUID = 'E863F11B-079E-48FF-8F27-9C2605A29F52';


    Characteristic.EveAirPressure = function () {
        //todo: only rough guess of extreme values -> use correct min/max if known
        Characteristic.call(this, 'Eve AirPressure', 'E863F10F-079E-48FF-8F27-9C2605A29F52');
        this.setProps({
            format: Characteristic.Formats.UINT16,
            unit: "hPa",
            maxValue: 1085,
            minValue: 870,
            minStep: 1,
            perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
        });
        this.value = this.getDefaultValue();
    };
    inherits(Characteristic.EveAirPressure, Characteristic);
    	Characteristic.EveAirPressure.UUID = 'E863F10F-079E-48FF-8F27-9C2605A29F52';



    ////////////////////////////// Custom services //////////////////////////////
    Service.PowerMeterService = function (displayName, subtype) {
        Service.call(this, displayName, '00000001-0000-1777-8000-775D67EC4377', subtype);
        // Required Characteristics
        this.addCharacteristic(Characteristic.EvePowerConsumption);
        // Optional Characteristics
        this.addOptionalCharacteristic(Characteristic.EveTotalPowerConsumption);
    };
    inherits(Service.PowerMeterService, Service);
    	Service.PowerMeterService.UUID = '00000001-0000-1777-8000-775D67EC4377';


    //Eve service (custom UUID)
    Service.EveRoomService = function (displayName, subtype) {
        Service.call(this, displayName, 'E863F002-079E-48FF-8F27-9C2605A29F52', subtype);
        // Required Characteristics
        this.addCharacteristic(Characteristic.EveRoomAirQuality);
        // Optional Characteristics
        this.addOptionalCharacteristic(Characteristic.CurrentRelativeHumidity);
    };
    inherits(Service.EveRoomService, Service);
    	Service.EveRoomService.UUID = 'E863F002-079E-48FF-8F27-9C2605A29F52';


    /////////////////////////////////////////////////////////////////////////////////////////////
    //Eve service (custom UUID)
    Service.EveWeatherService = function (displayName, subtype) {
        Service.call(this, displayName, 'E863F001-079E-48FF-8F27-9C2605A29F52', subtype);
        // Required Characteristics
        this.addCharacteristic(Characteristic.EveAirPressure);
        // Optional Characteristics
        this.addOptionalCharacteristic(Characteristic.CurrentRelativeHumidity);
        this.addOptionalCharacteristic(Characteristic.CurrentTemperature);
        this.addOptionalCharacteristic(Characteristic.EveBatteryLevel);
    };
    inherits(Service.EveWeatherService, Service);
    	Service.EveWeatherService.UUID = 'E863F001-079E-48FF-8F27-9C2605A29F52';


    // Consider platform plugin as dynamic platform plugin
    homebridge.registerPlatform("homebridge-domotiga", "DomotiGa", DomotigaPlatform, true);
}

function DomotigaPlatform(log, config, api) {
    this.log = log;
    this.config = config;
    this.log("DomotiGa Plugin Version " + this.getVersion());
    this.log("Plugin by Samfox2 https://github.com/samfox2");
    this.log("DomotiGa is a Open Source Home Automation Software for Linux");
    this.log("Please report any issues to https://github.com/samfox2/homebridge-domotiga/issues");

    var self = this;
    self.fetch_npmVersion("homebridge-domotiga", function (npmVersion) {
        npmVersion = npmVersion.replace('\n', '');
        self.log("NPM %s vs Local %s", npmVersion, self.getVersion());
        if (npmVersion > self.getVersion()) {
            self.log.warn("There is a new Version available. Please update with sudo npm -g update homebridge-domotiga");
        }
    });

    if (config) {
        // Global configuration
        this.host = this.config.host || 'localhost';
        this.port = this.config.port || 9090;

        // Device specific configuration
        this.devices = this.config.devices || [];
        this.accessories = {};
        this.polling = {};

        if (api) {
            this.api = api;
            this.api.on('didFinishLaunching', this.didFinishLaunching.bind(this));
        }
    }
}

// Method to restore accessories from cache
DomotigaPlatform.prototype.configureAccessory = function (accessory) {
    this.setService(accessory);
    this.accessories[accessory.context.name] = accessory;
}

// Method to setup accessories from config.json
DomotigaPlatform.prototype.didFinishLaunching = function () {

    if (!this.devices.length) {
        this.log.error("No devices configured. Please check your 'config.json' file!");
    }

    // Add or update accessories defined in config.json
    for (var i in this.devices) this.addAccessory(this.devices[i]);

    // Remove extra accessories in cache
    for (var name in this.accessories) {
        var accessory = this.accessories[name];
        if (!accessory.reachable) this.removeAccessory(accessory);
    }

    // Check number of devices
    var noD = this.accessories.length;
    this.log("Number of mapped devices : " + noD);
    if (noD > 100) {
        this.log.error("********************************************");
        this.log.error("* You are using more than 100 HomeKit      *");
        this.log.error("* devices behind a bridge. At this time    *");
        this.log.error("* HomeKit only supports up to 100 devices. *");
        this.log.error("* This may end up that iOS is not able to  *");
        this.log.error("* connect to the bridge anymore.           *");
        this.log.error("********************************************");
    } else {
        if (noD > 90) {
            this.log.warn("You are using more than 90 HomeKit");
            this.log.warn("devices behind a bridge. At this time");
            this.log.warn("HomeKit only supports up to 100 devices.");
            this.log.warn("This is just a warning. Everything should");
            this.log.warn("work fine until you are below that 100.");
        }
    }
}


// Method to add and update HomeKit accessories
DomotigaPlatform.prototype.addAccessory = function (data) {

    this.log("Initializing platform accessory '" + data.name + "'...");

    // Retrieve accessory from cache
    var accessory = this.accessories[data.name];

    if (!accessory) {

        var uuid = UUIDGen.generate(data.name);

        // Setup accessory category.
        accessory = new Accessory(data.name, uuid, 8);

        // Store and initialize logfile into context
        accessory.context.name = data.name || "NA";
        accessory.context.service = data.service;
        accessory.context.device = data.device;
        accessory.context.manufacturer = data.manufacturer || "NA";
        accessory.context.model = data.model || "NA";
{'fraction_non_alphanumeric': 0.14004254457050244, 'fraction_numerical': 0.04568476499189627, 'mean_word_length': 2.623600660671683, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, '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': '543724', 'n_tokens_mistral': 6339, 'n_tokens_neox': 5683, 'n_words': 1144}