domotz-remote-pawn
Version:
Domotz Agent
316 lines (299 loc) • 11 kB
JavaScript
/**
* This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
*
* Created by Tommaso Latini <tommaso@domotz.com> on 19/12/15.
*
*/
;
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'production';
}
var MODULE = 'MAIN';
var VALUE = 0,
TYPE = 1,
ACTION = 2;
var ARGUMENTS = [
[
['-p', '--passive'],
{
help: 'Runs in passive mode: no active actions will be performed ' + 'unless externally triggered',
action: 'storeTrue',
},
null,
],
[
['-e', '--env-file'],
{
help:
'Environment file location, defaults to /etc/domotz.env --\n' +
'NOTE: when passing the file this way, its contents override the ' +
'current env values. If this param is not specified, the default ' +
'file is used but its contents are only used as a fallback of the ' +
'current environment. Priorities for arguments: ' +
'1) Command Line Options ' +
'2) Custom Environment File ' +
'3) Current Environment ' +
'4) Default Environment (File: /etc/domotz.env) ',
defaultValue: null,
},
function (args) {
var envFile = args.env_file;
var isEnvFilePassed = !!envFile;
if (envFile) {
console.warn('%s - Using custom domotz.env file: %s', MODULE, envFile);
}
require('./setup/env').load(envFile, isEnvFilePassed);
},
],
[
['-c', '--conf-file'],
{
help: 'Configuration file absolute path, defaults to /opt/domotz/etc/domotz.json',
defaultValue: null,
},
function (args) {
if (args.conf_file) {
process.env.DOMOTZ_JSON = args.conf_file;
console.warn('%s - Using custom domotz.json file: %s', MODULE, process.env.DOMOTZ_JSON);
}
},
],
[
['-l', '--log-level'],
{
help: 'Sets logging level, default to ${DLEVEL}, ' + "valid values: 'debug', 'verbose', 'silly'",
defaultValue: null,
},
function (args) {
if (args.log_level) {
process.env.DLEVEL = args.log_level;
console.warn('%s - Forcing log level to %s', MODULE, args.log_level);
}
},
],
[
['-d', '--daemon-script'],
{
help: 'Location of init script. Useful only in daemon mode. Defaults ${DOMOTZ_SS_LINK}',
defaultValue: null,
},
function (args) {
if (args.daemon_script) {
process.env.DOMOTZ_SS_LINK = args.daemon_script;
console.warn('%s - Daemon script location: %s', MODULE, process.env.DOMOTZ_SS_LINK);
}
},
],
[
['-r', '--root-dir'],
{
help: 'Sets root directory for the subsided package. Default ${DOMOTZ_ROOT_DIR}',
defaultValue: null,
},
function (args) {
if (args.root_dir) {
process.env.DOMOTZ_ROOT_DIR = args.root_dir;
console.warn('Using file-system root directory: %s', process.env.DOMOTZ_ROOT_DIR);
}
},
],
[
['--cache-dir'],
{
help: 'Sets cache directory for the subsided package. Default ${DOMOTZ_CACHE_DIR}',
defaultValue: null,
},
function (args) {
if (args.cache_dir) {
process.env.DOMOTZ_CACHE_DIR = args.cache_dir;
console.warn('Using cache directory: %s', process.env.DOMOTZ_CACHE_DIR);
}
},
],
[
['--sbin-dir'],
{
help: 'System binaries folder. Default ${DOMOTZ_ROOT_DIR}/sbin',
defaultValue: null,
},
function (args) {
if (args.sbin_dir) {
process.env.DOMOTZ_SBIN_DIR = args.sbin_dir;
console.warn('Using cache directory: %s', process.env.DOMOTZ_SBIN_DIR);
}
},
],
[
['--bin-dir'],
{
help: 'Binaries folder. Default ${DOMOTZ_ROOT_DIR}/bin',
defaultValue: null,
},
function (args) {
if (args.bin_dir) {
process.env.DOMOTZ_BIN_DIR = args.bin_dir;
console.warn('Using cache directory: %s', process.env.DOMOTZ_BIN_DIR);
}
},
],
[
['--lib-dir'],
{
help: 'Libraries folder. Default ${DOMOTZ_ROOT_DIR}/lib',
defaultValue: null,
},
function (args) {
if (args.lib_dir) {
process.env.DOMOTZ_LIB_DIR = args.lib_dir;
console.warn('Using cache directory: %s', process.env.DOMOTZ_LIB_DIR);
}
},
],
[
['--conf-dir'],
{
help: 'Libraries folder. Default ${DOMOTZ_ROOT_DIR}/etc',
defaultValue: null,
},
function (args) {
if (args.conf_dir) {
process.env.DOMOTZ_CONF_DIR = args.conf_dir;
console.warn('Using cache directory: %s', process.env.DOMOTZ_CONF_DIR);
}
},
],
[
['--data-dir'],
{
help: 'Libraries folder. Default ${DOMOTZ_ROOT_DIR}/share',
defaultValue: null,
},
function (args) {
if (args.data_dir) {
process.env.DOMOTZ_CONF_DIR = args.data_dir;
console.warn('Using cache directory: %s', process.env.DOMOTZ_CONF_DIR);
}
},
],
[
['--log-dir'],
{
help: 'Libraries folder. Default ${DOMOTZ_ROOT_DIR}/var/log/domotz',
defaultValue: null,
},
function (args) {
if (args.log_dir) {
process.env.DOMOTZ_LOG_DIR = args.log_dir;
console.warn('Using cache directory: %s', process.env.DOMOTZ_LOG_DIR);
}
},
],
];
var argparse = require('argparse');
// Debug methods must exists in standard console
console.debug =
console.verbose =
console.silly =
process.env.DNAME === 'domotz'
? function () {}
: function () {
console.log.apply(console, arguments);
};
module.exports = function (onInitSuccess, onInitFailure) {
var version = require('../package.json').version;
console.info('%s - Start agent [%s] at %s', MODULE, version, new Date());
// Create Parser
var parser = new argparse.ArgumentParser({
version: version,
addHelp: true,
description: 'Domotz Remote Pawn',
});
// Add arguments
for (var i = 0; i < ARGUMENTS.length; i++) {
var arg = ARGUMENTS[i][VALUE];
var type = ARGUMENTS[i][TYPE];
console.debug('%s - Add argument. Short option: "%s". Long Option: "%s"', MODULE, arg.length === 2 ? arg[0] : 'NA', arg[arg.length - 1]);
parser.addArgument(arg, type);
}
// Parse CLI options and set properly the environment variables
var args = parser.parseArgs();
for (var j = 0; j < ARGUMENTS.length; j++) {
(ARGUMENTS[j][ACTION] || function () {})(args);
}
// Log Environment
console.info('%s - Domotz Environment:', MODULE);
Object.keys(process.env)
.sort()
.forEach(function (key) {
console.info('%s - --> %s=%s', MODULE, key, process.env[key]);
});
if (process.version.charAt(1) === '0') {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
}
// Domotz App
var resourceLocator = require('./setup/resourceLocator');
function onSuccess(resourceLocator) {
var myConsole = resourceLocator.log.decorateLogs();
if (process.env.BOX_REGISTRATION_OUTCOME !== 'failure') {
if (resourceLocator.status.isPassive()) {
myConsole.warn('%s - Running in PASSIVE mode, periodic tasks disabled', MODULE);
if (resourceLocator.configuration && resourceLocator.settingsManager.getSetting('configuration.settings.amqp.enabled', true)) {
myConsole.info('AMQP is enabled');
resourceLocator.amqpListener.listen();
} else {
myConsole.info('AMQP is not enabled');
}
} else {
resourceLocator.periodicalTasks.start();
}
if (resourceLocator.status.isConfigured()) {
myConsole.info('Agent configured, initialized advanced features');
resourceLocator.upnp.httpServer.start();
if (resourceLocator.settingsManager.getSetting('configuration.settings.amqp.enabled', true)) {
myConsole.info('AMQP is enabled');
resourceLocator.amqpListener.listen();
} else {
myConsole.info('AMQP is not enabled');
}
resourceLocator.socketListener.listen();
if (resourceLocator.lodash.get(resourceLocator, 'configuration.settings.dhcp_server.enabled', true)) {
if (['syno', 'openwrt'].indexOf(process.env.DPLATFORM) === -1) {
try {
resourceLocator.discovery.dhcp_discovery.listenForLeasePackets();
} catch (e) {
myConsole.error('disable dhcp discovery for this device %s', e.toString());
}
}
} else {
myConsole.info('DHCP server listener disabled from settings');
}
resourceLocator.stallWatcher.start();
}
}
resourceLocator.webapp.startServing();
if (onInitSuccess) {
onInitSuccess();
}
}
function onFailure(error) {
console.log('Error initializing resource locator ' + error.toString());
if (onInitFailure) {
onInitFailure(error);
}
}
resourceLocator.create(null, args.passive ? 'PASSIVE' : null, onSuccess, onFailure);
};