2018-11-07 12:05:14 -07:00
|
|
|
const semver = require('semver');
|
|
|
|
const engines = require('./package.json').engines;
|
2021-02-06 08:32:20 -07:00
|
|
|
const indexJsRestart = 'indexjs.restart';
|
2018-11-07 12:05:14 -07:00
|
|
|
|
2021-02-06 08:32:20 -07:00
|
|
|
let controller;
|
|
|
|
let stopping = false;
|
|
|
|
|
|
|
|
async function restart() {
|
|
|
|
await stop(indexJsRestart);
|
|
|
|
await start();
|
2018-11-07 12:05:14 -07:00
|
|
|
}
|
2018-04-09 08:37:21 -07:00
|
|
|
|
2021-02-06 08:32:20 -07:00
|
|
|
async function exit(code, reason) {
|
|
|
|
if (reason !== indexJsRestart) {
|
|
|
|
process.exit(code);
|
2020-07-23 10:17:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 08:32:20 -07:00
|
|
|
async function start() {
|
|
|
|
const version = engines.node;
|
|
|
|
if (!semver.satisfies(process.version, version)) {
|
|
|
|
console.log(`\t\tZigbee2MQTT requires node version ${version}, you are running ${process.version}!\n`); // eslint-disable-line
|
|
|
|
}
|
2018-04-14 07:17:25 -07:00
|
|
|
|
2021-02-06 08:32:20 -07:00
|
|
|
// Validate settings
|
|
|
|
const settings = require('./lib/util/settings');
|
2021-03-09 11:50:05 -07:00
|
|
|
settings.reRead();
|
2021-02-06 08:32:20 -07:00
|
|
|
const errors = settings.validate();
|
|
|
|
if (errors.length > 0) {
|
|
|
|
console.log(`\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`);
|
|
|
|
console.log(' READ THIS CAREFULLY\n');
|
|
|
|
console.log(`Refusing to start because configuration is not valid, found the following errors:`);
|
|
|
|
for (const error of errors) {
|
|
|
|
console.log(`- ${error}`);
|
|
|
|
}
|
|
|
|
console.log(`\nIf you don't know how to solve this, read https://www.zigbee2mqtt.io/information/configuration.html`); // eslint-disable-line
|
|
|
|
console.log(`\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n`);
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-04-08 06:00:36 -07:00
|
|
|
|
2021-02-06 08:32:20 -07:00
|
|
|
const Controller = require('./lib/controller');
|
|
|
|
controller = new Controller(restart, exit);
|
|
|
|
await controller.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function stop(reason=null) {
|
|
|
|
await controller.stop(reason);
|
|
|
|
}
|
2019-05-20 11:43:25 -07:00
|
|
|
|
2021-02-06 08:32:20 -07:00
|
|
|
async function handleQuit() {
|
|
|
|
if (!stopping && controller) {
|
2019-05-20 11:43:25 -07:00
|
|
|
stopping = true;
|
2021-02-06 08:32:20 -07:00
|
|
|
await stop();
|
2019-05-20 11:43:25 -07:00
|
|
|
}
|
2018-05-17 08:20:46 -07:00
|
|
|
}
|
2021-02-06 08:32:20 -07:00
|
|
|
|
|
|
|
process.on('SIGINT', handleQuit);
|
|
|
|
process.on('SIGTERM', handleQuit);
|
|
|
|
|
|
|
|
start();
|