<commit_before>/* jshint -W097 */// jshint strict:false
/*jslint node: true */
"use strict";

(function () {

    var mods = {
        'vm':               require('vm'),
        'fs':               require('fs'),
        'dgram':            require('dgram'),
        'crypto':           require('crypto'),
        'dns':              require('dns'),
        'events':           require('events'),
        'http':             require('http'),
        'https':            require('https'),
        'net':              require('net'),
        'os':               require('os'),
        'path':             require('path'),
        'util':             require('util'),
        'child_process':    require('child_process'),

        'coffee-compiler':  require('coffee-compiler'),

        'node-schedule':    require('node-schedule'),
        'suncalc':          require('suncalc'),
        'request':          require('request'),
        'wake_on_lan':      require('wake_on_lan')
    };
    var utils =   require(__dirname + '/lib/utils'); // Get common adapter utils

    var adapter = utils.adapter({

        name: 'javascript',

        objectChange: function (id, obj) {
            if (!obj) {
                if (!objects[id]) return;

                // Script deleted => remove it
                if (objects[id].common.engine == 'system.adapter.' + adapter.namespace) stop(id);

                delete objects[id];
            } else if (!objects[id]) {
                objects[id] = obj;

                if (obj.type != 'script' || obj.common.engine != 'system.adapter.' + adapter.namespace || !obj.common.enabled) return;
                // added new script to this engine

            } else {
                // Object just changed
                if (obj.type != 'script') {
                    objects[id] = obj;
                    return;
                }

                if ((objects[id].common.enabled && !obj.common.enabled) ||
                    (objects[id].common.engine == 'system.adapter.' + adapter.namespace && obj.common.engine != 'system.adapter.' + adapter.namespace)) {
                    // Script disabled
                    if (objects[id].common.enabled && objects[id].common.engine == 'system.adapter.' + adapter.namespace) {
                        // Remove it from executing
                        objects[id] = obj;
                        stop(id);
                    } else {
                        objects[id] = obj;
                    }
                } else
                if ((!objects[id].common.enabled && obj.common.enabled) ||
                    (objects[id].common.engine != 'system.adapter.' + adapter.namespace && obj.common.engine == 'system.adapter.' + adapter.namespace)) {
                    // Script enabled
                    objects[id] = obj;

                    if (objects[id].common.enabled && objects[id].common.engine == 'system.adapter.' + adapter.namespace) {
                        // Start script
                        load(id);
                    }
                } else { //if (obj.common.source != objects[id].common.source) {
                    objects[id] = obj;

                    // Source changed => restart it
                    stop(id, function (res, _id) {
                        load(_id);
                    });
                } /*else {
                    // Something changed or not for us
                    objects[id] = obj;
                }*/
            }
        },

        stateChange: function (id, state) {

            if (id.match(/^messagebox./) || id.match(/^log./)) return;

            var oldState = states[id] || {};
            if (state) {
                states[id] = state;
            } else {
                delete states[id];
                state = {};
            }

            var name;

            var common =        {};
            var nativeObj =     {};

            var channelId =     null;
            var channelName =   null;
            var deviceId =      null;
            var deviceName =    null;

            if (objects[id]) {
                name =          objects[id].common ? objects[id].common.name : null;
                common =        objects[id].common;
                nativeObj =     objects[id].native;
                channelId =     objects[id].parent;
            }

            if (channelId) {
                if (objects[channelId]) {
                    channelName = objects[channelId].common ? objects[channelId].common.name : null;
                    if (objects[channelId].parent) {
                        deviceId = objects[channelId] ? objects[channelId].parent : null;
                        deviceName = objects[channelId] && objects[deviceId].common ? objects[deviceId].common.name : null;
                    }
                }
            }

            getObjectEnums(id, function (enumIds, enumNames) {
                var eventObj = {
                    id:             id,
                    name:           name,
                    common:         common,
                    native:         nativeObj,
                    channelId:      channelId,
                    channelName:    channelName,
                    deviceId:       deviceId,
                    deviceName:     deviceName,
                    enumIds:        enumIds,       // Array of Strings
                    enumNames:      enumNames,     // Array of Strings
                    newState: {
                        val:        state.val,
                        ts:         state.ts,
                        ack:        state.ack,
                        lc:         state.lc,
                        from:       state.from
                    },
                    oldState: {
                        val:        oldState.val,
                        ts:         oldState.ts,
                        ack:        oldState.ack,
                        lc:         oldState.lc,
                        from:       oldState.from
                    }
                };

                for (var i = 0, l = subscriptions.length; i < l; i++) {
                    if (patternMatching(eventObj, subscriptions[i].pattern)) {
                        subscriptions[i].callback(eventObj);
                    }
                }
            });
        },

        unload: function (callback) {
            callback();
        },

        ready: function () {
            installLibraries(function () {
                getData(function () {
                    adapter.subscribeForeignObjects('*');
                    adapter.subscribeForeignStates('*');

                    adapter.objects.getObjectView('script', 'javascript', {}, function (err, doc) {
                        for (var i = 0; i < doc.rows.length; i++) {
                            load(doc.rows[i].value._id);
                        }
                    });

                });
            });
        }
    });

    var objects =           {};
    var states =            {};
    var scripts =           {};
    var subscriptions =     [];
    var enums =             [];
    var cacheObjectEnums =  {};
    var channels =          null;
    var devices =           null;
    var fs =                null;
    var attempts =          [];

    function installNpm(npmLib, callback) {
        var path = __dirname;
        if (typeof npmLib == 'function') {
            callback = npmLib;
            npmLib = undefined;
        }

        var cmd = 'npm install ' + npmLib + ' --production --prefix "' + path + '"';
        adapter.log.info(cmd + ' (System call)');
        // Install node modules as system call

        // System call used for update of js-controller itself,
        // because during installation npm packet will be deleted too, but some files must be loaded even during the install process.<commit_msg>(bluefox) fix error<commit_after>/* jshint -W097 */// jshint strict:false
/*jslint node: true */
"use strict";

(function () {

    var mods = {
        'vm':               require('vm'),
        'fs':               require('fs'),
        'dgram':            require('dgram'),
        'crypto':           require('crypto'),
        'dns':              require('dns'),
        'events':           require('events'),
        'http':             require('http'),
        'https':            require('https'),
        'net':              require('net'),
        'os':               require('os'),
        'path':             require('path'),
        'util':             require('util'),
        'child_process':    require('child_process'),

        'coffee-compiler':  require('coffee-compiler'),

        'node-schedule':    require('node-schedule'),
        'suncalc':          require('suncalc'),
        'request':          require('request'),
        'wake_on_lan':      require('wake_on_lan')
    };
    var utils =   require(__dirname + '/lib/utils'); // Get common adapter utils

    var adapter = utils.adapter({

        name: 'javascript',

        objectChange: function (id, obj) {
            if (!obj) {
                if (!objects[id]) return;

                // Script deleted => remove it
                if (objects[id].common.engine == 'system.adapter.' + adapter.namespace) stop(id);

                delete objects[id];
            } else if (!objects[id]) {
                objects[id] = obj;

                if (obj.type != 'script' || obj.common.engine != 'system.adapter.' + adapter.namespace || !obj.common.enabled) return;
                // added new script to this engine

            } else {
                // Object just changed
                if (obj.type != 'script') {
                    objects[id] = obj;
                    return;
                }

                if ((objects[id].common.enabled && !obj.common.enabled) ||
                    (objects[id].common.engine == 'system.adapter.' + adapter.namespace && obj.common.engine != 'system.adapter.' + adapter.namespace)) {
                    // Script disabled
                    if (objects[id].common.enabled && objects[id].common.engine == 'system.adapter.' + adapter.namespace) {
                        // Remove it from executing
                        objects[id] = obj;
                        stop(id);
                    } else {
                        objects[id] = obj;
                    }
                } else
                if ((!objects[id].common.enabled && obj.common.enabled) ||
                    (objects[id].common.engine != 'system.adapter.' + adapter.namespace && obj.common.engine == 'system.adapter.' + adapter.namespace)) {
                    // Script enabled
                    objects[id] = obj;

                    if (objects[id].common.enabled && objects[id].common.engine == 'system.adapter.' + adapter.namespace) {
                        // Start script
                        load(id);
                    }
                } else { //if (obj.common.source != objects[id].common.source) {
                    objects[id] = obj;

                    // Source changed => restart it
                    stop(id, function (res, _id) {
                        load(_id);
                    });
                } /*else {
                    // Something changed or not for us
                    objects[id] = obj;
                }*/
            }
        },

        stateChange: function (id, state) {

            if (id.match(/^messagebox./) || id.match(/^log./)) return;

            var oldState = states[id] || {};
            if (state) {
                states[id] = state;
            } else {
                delete states[id];
                state = {};
            }

            var name;

            var common =        {};
            var nativeObj =     {};

            var channelId =     null;
            var channelName =   null;
            var deviceId =      null;
            var deviceName =    null;

            if (objects[id]) {
                name =          objects[id].common ? objects[id].common.name : null;
                common =        objects[id].common;
                nativeObj =     objects[id].native;
                channelId =     objects[id].parent;
            }

            if (channelId) {
                if (objects[channelId]) {
                    channelName = objects[channelId].common ? objects[channelId].common.name : null;
                    if (objects[channelId].parent) {
                        deviceId = objects[channelId] ? objects[channelId].parent : null;
                        deviceName = objects[channelId] && objects[deviceId].common ? objects[deviceId].common.name : null;
                    }
                }
            }

            getObjectEnums(id, function (enumIds, enumNames) {
                var eventObj = {
                    id:             id,
                    name:           name,
                    common:         common,
                    native:         nativeObj,
                    channelId:      channelId,
                    channelName:    channelName,
                    deviceId:       deviceId,
                    deviceName:     deviceName,
                    enumIds:        enumIds,       // Array of Strings
                    enumNames:      enumNames,     // Array of Strings
                    newState: {
                        val:        state.val,
                        ts:         state.ts,
                        ack:        state.ack,
                        lc:         state.lc,
                        from:       state.from
                    },
                    oldState: {
                        val:        oldState.val,
                        ts:         oldState.ts,
                        ack:        oldState.ack,
                        lc:         oldState.lc,
                        from:       oldState.from
                    }
                };

                for (var i = 0, l = subscriptions.length; i < l; i++) {
                    if (patternMatching(eventObj, subscriptions[i].pattern)) {
                        subscriptions[i].callback(eventObj);
                    }
                }
            });
        },

        unload: function (callback) {
            callback();
        },

        ready: function () {
            installLibraries(function () {
                getData(function () {
                    adapter.subscribeForeignObjects('*');
                    adapter.subscribeForeignStates('*');

                    adapter.objects.getObjectView('script', 'javascript', {}, function (err, doc) {
                        for (var i = 0; i < doc.rows.length; i++) {
                            load(doc.rows[i].value._id);
                        }
                    });

                });
            });
        }
    });

    var objects =           {};
    var states =            {};
    var scripts =           {};
    var subscriptions =     [];
    var enums =             [];
    var cacheObjectEnums =  {};
    var channels =          null;
    var devices =           null;
    var fs =                null;
    var attempts =          {};

    function installNpm(npmLib, callback) {
        var path = __dirname;
        if (typeof npmLib == 'function') {
            callback = npmLib;
            npmLib = undefined;
        }

        var cmd = 'npm install ' + npmLib + ' --production --prefix "' + path + '"';
        adapter.log.info(cmd + ' (System call)');
        // Install node modules as system call

        // System call used for update of js-controller itself,
        // because during installation npm packet will be deleted too, but some files must be loaded even during the install process.
{'fraction_non_alphanumeric': 0.11730354195759807, 'fraction_numerical': 0.0006347594261774788, 'mean_word_length': 1.057594358103696, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5779978', 'n_tokens_mistral': 3882, 'n_tokens_neox': 3604, 'n_words': 847}