<commit_before>/**
 *      Admin backend
 *
 *      Controls Adapter-Processes
 *
 *      Copyright 2014-2020 bluefox <<EMAIL>>,
 *      MIT License
 *
 */

/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
'use strict';

const adapterName = require('./package.json').name.split('.').pop();
const utils       = require('@iobroker/adapter-core'); // Get common adapter utils
const tools 	  = require(utils.controllerDir + '/lib/tools.js');
const SocketIO    = require('./lib/socket');
const Web         = require('./lib/web');
const semver      = require('semver');

const ONE_HOUR_MS = 3600000;
const ERROR_PERMISSION = 'permissionError';

let socket        = null;
let webServer     = null;

let objects       = {};
let secret        = 'Zgfr56gFe87jJOM'; // Will be generated by first start
let adapter;

function startAdapter(options) {
    options = options || {};
	Object.assign(options, {
	    name:           adapterName, // adapter name
	    dirname:        __dirname,   // say own position
	    logTransporter: true,        // receive the logs
	    systemConfig:   true,
	    install:        callback => typeof callback === 'function' && callback()
	});

    adapter = new utils.Adapter(options);

    adapter.on('objectChange', (id, obj) => {
        if (obj) {
            //console.log('objectChange: ' + id);
            objects[id] = obj;

            if (id === 'system.repositories') {
                writeUpdateInfo(adapter);
            }
        } else {
            //console.log('objectDeleted: ' + id);
            if (objects[id]) {
                delete objects[id];
            }
        }

        // TODO Build in some threshold of messages
        socket && socket.objectChange(id, obj);
    });

    adapter.on('stateChange', (id, state) => {
        socket && socket.stateChange(id, state);
    });

    adapter.on('ready', () => {
        adapter.getForeignObject('system.config', (err, obj) => {
            if (!err && obj) {
                obj.native = obj.native || {};
                if (!obj.native.secret) {
                    require('crypto').randomBytes(24, (ex, buf) => {
                        adapter.config.secret = buf.toString('hex');
                        adapter.extendForeignObject('system.config', {native: {secret: adapter.config.secret}});
                        main(adapter);
                    });
                } else {
                    adapter.config.secret = obj.native.secret;
                    main(adapter);
                }
            } else {
                adapter.config.secret = secret;
                adapter.log.error('Cannot find object system.config');
            }
        });
    });

    adapter.on('message', obj => {
        if (!obj || !obj.message) {
            return false;
        }

        socket && socket.sendCommand(obj);

        return true;
    });

    adapter.on('unload', callback => {
        // unsubscribe all
        socket && socket.unsubscribeAll();

        try {
            adapter.log.info('terminating http' + (adapter.config.secure ? 's' : '') + ' server on port ' + adapter.config.port);
            webServer.close();
            callback();
        } catch (e) {
            callback();
        }
    });

// obj = {message: msg, severity: level, from: this.namespace, ts: (new Date()).getTime()}
    adapter.on('log', obj => socket && socket.sendLog(obj));

    return adapter;
}

function createUpdateInfo(adapter) {
    // create connected object and state
    let updatesNumberObj = objects[adapter.namespace + '.info.updatesNumber'];<commit_msg>clear timeout in on unload<commit_after>/**
 *      Admin backend
 *
 *      Controls Adapter-Processes
 *
 *      Copyright 2014-2020 bluefox <<EMAIL>>,
 *      MIT License
 *
 */

/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
'use strict';

const adapterName = require('./package.json').name.split('.').pop();
const utils       = require('@iobroker/adapter-core'); // Get common adapter utils
const tools 	  = require(utils.controllerDir + '/lib/tools.js');
const SocketIO    = require('./lib/socket');
const Web         = require('./lib/web');
const semver      = require('semver');

const ONE_HOUR_MS = 3600000;
const ERROR_PERMISSION = 'permissionError';

let socket        = null;
let webServer     = null;

let objects       = {};
let secret        = '<KEY>'; // Will be generated by first start
let adapter;

function startAdapter(options) {
    options = options || {};
	Object.assign(options, {
	    name:           adapterName, // adapter name
	    dirname:        __dirname,   // say own position
	    logTransporter: true,        // receive the logs
	    systemConfig:   true,
	    install:        callback => typeof callback === 'function' && callback()
	});

    adapter = new utils.Adapter(options);

    adapter.on('objectChange', (id, obj) => {
        if (obj) {
            //console.log('objectChange: ' + id);
            objects[id] = obj;

            if (id === 'system.repositories') {
                writeUpdateInfo(adapter);
            }
        } else {
            //console.log('objectDeleted: ' + id);
            if (objects[id]) {
                delete objects[id];
            }
        }

        // TODO Build in some threshold of messages
        socket && socket.objectChange(id, obj);
    });

    adapter.on('stateChange', (id, state) => {
        socket && socket.stateChange(id, state);
    });

    adapter.on('ready', () => {
        adapter.getForeignObject('system.config', (err, obj) => {
            if (!err && obj) {
                obj.native = obj.native || {};
                if (!obj.native.secret) {
                    require('crypto').randomBytes(24, (ex, buf) => {
                        adapter.config.secret = buf.toString('hex');
                        adapter.extendForeignObject('system.config', {native: {secret: adapter.config.secret}});
                        main(adapter);
                    });
                } else {
                    adapter.config.secret = obj.native.secret;
                    main(adapter);
                }
            } else {
                adapter.config.secret = secret;
                adapter.log.error('Cannot find object system.config');
            }
        });
    });

    adapter.on('message', obj => {
        if (!obj || !obj.message) {
            return false;
        }

        socket && socket.sendCommand(obj);

        return true;
    });

    adapter.on('unload', callback => {
        // unsubscribe all
        socket && socket.unsubscribeAll();
        adapter.timerRepo && clearTimeout(adapter.timerRepo);

        try {
            adapter.log.info('terminating http' + (adapter.config.secure ? 's' : '') + ' server on port ' + adapter.config.port);
            webServer.close();
            callback();
        } catch (e) {
            callback();
        }
    });

// obj = {message: msg, severity: level, from: this.namespace, ts: (new Date()).getTime()}
    adapter.on('log', obj => socket && socket.sendLog(obj));

    return adapter;
}

function createUpdateInfo(adapter) {
    // create connected object and state
    let updatesNumberObj = objects[adapter.namespace + '.info.updatesNumber'];
{'fraction_non_alphanumeric': 0.14608288585983753, 'fraction_numerical': 0.006058102712377805, 'mean_word_length': 1.8375, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 26, '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': '838342', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1945, 'n_words': 455}