<commit_before>'use strict';

const util = require('util');
const log4js = require('log4js');
const http = require('http');
// const moment = require('moment');
// const async = require('async');
const path = require('path');
const walk = require('walk');
const fs = require('fs');
const mqtt = require('mqtt');
const _ = require('underscore');
const mqttAppender = require('./log4js-mqtt-appender');
var ON_DEATH = require('death');

// Constants
const HOSTPATH = '/rest/items';
const ITEMTYPES = [ 'Group', 'Switch', 'Dimmer', 'DateTime', 'Number', 'Contact', 'String', 'Location' ];
const UTILITIESDIR = path.resolve(__dirname, './OHUtilities');

var ohClasses = {}

_.each(ITEMTYPES, function(element, index, list)
{
    ohClasses[element] = require('./OHItemClasses/OHItem' + element);
});

class OHRuleServer
{
    constructor(config)
    {
        module.exports.Config = config;
        this._config = config;
        this._mqttClient = null;
        this._items = {};
        this._modules = [];
        this._repeaters = [];
        this._repeaterHandle = 0;
        this._mqttServer = null;
        
        log4js.configure(
            {
                appenders: 
                {
                    out: 
                    { 
                        type: 'console' 
                    },
                    rolling: 
                    { 
                        type: 'dateFile', 
                        filename: this._config.rollingLogger.logFile || 'logs/ohrules_temp.log',    // There really is no good default...
                        pattern: '.yyyy-MM-dd', 
                        alwaysIncludePattern: true, 
                        daysToKeep: 7, 
                        keepFileExt: true 
                    },
                    mqtt: 
                    { 
                        type: mqttAppender, 
                        host: this._config.mqttLogger.host || 'mqtt://localhost:1883', 
                        auth: this._config.mqttLogger.auth || null
                    }
                },
                categories:
                {
                    default: 
                    { 
                        appenders: [ 'out', 'rolling', 'mqtt' ], 
                        level: this._config.logging.logLevel || 'debug', 
                        layout: { type: 'coloured' } 
                    },
                },
            });

        this._logger = log4js.getLogger();

        ON_DEATH((signal, err) =>
        {
            this.logger.info('OHRuleServer::Death - encountered exiting');
            mqttAppender.end();

            if (this._mqttClient && this._mqttClient.connected)
            {
                this._mqttClient.end();
            }<commit_msg>Add local MQTT server shutdown<commit_after>'use strict';

const util = require('util');
const log4js = require('log4js');
const http = require('http');
// const moment = require('moment');
// const async = require('async');
const path = require('path');
const walk = require('walk');
const fs = require('fs');
const mqtt = require('mqtt');
const _ = require('underscore');
const mqttAppender = require('./log4js-mqtt-appender');
var ON_DEATH = require('death');

// Constants
const HOSTPATH = '/rest/items';
const ITEMTYPES = [ 'Group', 'Switch', 'Dimmer', 'DateTime', 'Number', 'Contact', 'String', 'Location' ];
const UTILITIESDIR = path.resolve(__dirname, './OHUtilities');

var ohClasses = {}

_.each(ITEMTYPES, function(element, index, list)
{
    ohClasses[element] = require('./OHItemClasses/OHItem' + element);
});

class OHRuleServer
{
    constructor(config)
    {
        module.exports.Config = config;
        this._config = config;
        this._mqttClient = null;
        this._items = {};
        this._modules = [];
        this._repeaters = [];
        this._repeaterHandle = 0;
        this._mqttServer = null;
        
        log4js.configure(
            {
                appenders: 
                {
                    out: 
                    { 
                        type: 'console' 
                    },
                    rolling: 
                    { 
                        type: 'dateFile', 
                        filename: this._config.rollingLogger.logFile || 'logs/ohrules_temp.log',    // There really is no good default...
                        pattern: '.yyyy-MM-dd', 
                        alwaysIncludePattern: true, 
                        daysToKeep: 7, 
                        keepFileExt: true 
                    },
                    mqtt: 
                    { 
                        type: mqttAppender, 
                        host: this._config.mqttLogger.host || 'mqtt://localhost:1883', 
                        auth: this._config.mqttLogger.auth || null
                    }
                },
                categories:
                {
                    default: 
                    { 
                        appenders: [ 'out', 'rolling', 'mqtt' ], 
                        level: this._config.logging.logLevel || 'debug', 
                        layout: { type: 'coloured' } 
                    },
                },
            });

        this._logger = log4js.getLogger();

        ON_DEATH((signal, err) =>
        {
            this.logger.info('OHRuleServer::Death - encountered exiting');
            mqttAppender.end();

            if (this._mqttClient && this._mqttClient.connected)
            {
                this._mqttClient.end();
            }

            if (this._mqttServer != null)
            {
                await ((server) => 
                {
                    return new Promise((resolve, reject) =>
                    {
                        server.close(() =>
                        {
                            resolve();
                        });
                    });
                })(this._mqttServer);
            }

{'fraction_non_alphanumeric': 0.12425734170768969, 'fraction_numerical': 0.003734510269903242, 'mean_word_length': 1.2557427258805514, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '195582', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1443, 'n_words': 292}