This commit is contained in:
Hiro Protagonist 2016-09-07 15:15:25 +12:00
parent e849a961a4
commit b0cf51f936
3 changed files with 44 additions and 19 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
config.js config.js
node_modules node_modules
process.log process.log
process.log.*
ssh.pid ssh.pid

View file

@ -7,7 +7,6 @@
"key": "7r76-zyjx-bfdj-e79q", "key": "7r76-zyjx-bfdj-e79q",
"name": "Unconfigured", "name": "Unconfigured",
"master": "http://data.protagon.space:8080", "master": "http://data.protagon.space:8080",
"logPath": "./process.log",
"ffmpegPath": "ffmpeg", "ffmpegPath": "ffmpeg",
"ssh-user": "root", "ssh-user": "root",
"ssh-local-user": "alarm", "ssh-local-user": "alarm",

61
main.js
View file

@ -4,16 +4,45 @@ const http = require('http');
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const WMStrm = require(__dirname + '/lib/memWrite.js'); const WMStrm = require(__dirname + '/lib/memWrite.js');
const mustBe = false;
const restart = false;
const exec = require('child_process').exec; const exec = require('child_process').exec;
const execSync = require('child_process').execSync; const execSync = require('child_process').execSync;
const spawnP = require('child_process').spawn; const spawnP = require('child_process').spawn;
const winston = require('winston');
let mustBe = false;
let restart = false;
let config, source, snapSource; let config, source, snapSource;
const importance = ['', 'info', 'warning', 'danger', 'success']; const importance = ['', 'info', 'warning', 'danger', 'success'];
var customLevels = {
levels: {
normal: 0,
info: 1,
warning: 2,
danger: 3,
success: 4
},
colors: {
normal: 'white',
info: 'blue',
warning: 'orange',
danger: 'red',
success: 'green'
}
};
let winston = require('winston');
let logger = new(winston.Logger)({
levels: customLevels.levels,
transports: [
new(winston.transports.Console)({level: 'success'}),
new(winston.transports.File)({
filename: __dirname + '/process.log',
maxsize: 2048,
maxFiles: 10
})
]
});
winston.addColors(customLevels.colors);
let dir = '/home'; let dir = '/home';
let status = { let status = {
@ -40,7 +69,7 @@ let spawn = function() {
.audioCodec('libmp3lame') .audioCodec('libmp3lame')
.on('start', function(commandLine) { .on('start', function(commandLine) {
status.running = 0; status.running = 0;
winston.log(importance[4], 'Spawned Ffmpeg with command: ' + commandLine); logger.log(importance[4], 'Spawned Ffmpeg with command: ' + commandLine);
}) })
.on('end', function(o, e) { .on('end', function(o, e) {
imDead('Normal Stop.', e); imDead('Normal Stop.', e);
@ -77,7 +106,7 @@ let getSnap = function(cb) {
let picBuff = new WMStrm(); let picBuff = new WMStrm();
recCmd = ffmpeg(snapSource) recCmd = ffmpeg(snapSource)
.on('start', function(commandLine) { .on('start', function(commandLine) {
winston.log(importance[4], 'Snapshot ' + commandLine); logger.log(importance[4], 'Snapshot ' + commandLine);
}) })
.on('error', function(err, o, e) {}) .on('error', function(err, o, e) {})
.outputFormat('mjpeg') .outputFormat('mjpeg')
@ -108,7 +137,7 @@ function imDead(why, e = '') {
restart = false; restart = false;
} }
if (!mustBe) { if (!mustBe) {
winston.log(importance[2], 'Crash! ' + why + ' ' + e); logger.log(importance[2], 'Crash! ' + why + ' ' + e);
setTimeout(function() { setTimeout(function() {
spawn(); spawn();
}, 1000); }, 1000);
@ -120,7 +149,7 @@ function criticalProblem(err, handler, ...args) {
setTimeout(function() { setTimeout(function() {
status.running = 2 status.running = 2
status.error = err status.error = err
winston.log(importance[3], 'Critical Problem: ' + errors[err]); logger.log(importance[3], 'Critical Problem: ' + errors[err]);
socket.emit('change', { socket.emit('change', {
type: 'error', type: 'error',
change: { change: {
@ -164,7 +193,7 @@ var commandHandlers = function commandHandlers(command, cb) {
startStop: function() { startStop: function() {
if (status.running !== 2) if (status.running !== 2)
if (status.running === 0) { if (status.running === 0) {
winston.log(importance[1], "Stop Command!"); logger.log(importance[1], "Stop Command!");
mustBe = true mustBe = true
cmd.kill(); cmd.kill();
socket.emit('data', { socket.emit('data', {
@ -176,7 +205,7 @@ var commandHandlers = function commandHandlers(command, cb) {
} }
}, command.sender); }, command.sender);
} else { } else {
winston.log(importance[1], "Start Command!"); logger.log(importance[1], "Start Command!");
spawn(); spawn();
socket.emit('data', { socket.emit('data', {
type: 'message', type: 'message',
@ -253,12 +282,12 @@ var commandHandlers = function commandHandlers(command, cb) {
}, },
restart: function() { restart: function() {
if (status.running === 0) { if (status.running === 0) {
winston.log(importance[1], "Restart Command!"); logger.log(importance[1], "Restart Command!");
mustBe = true; mustBe = true;
restart = true; restart = true;
cmd.kill(); cmd.kill();
} else { } else {
winston.log(importance[1], "Start Command!"); logger.log(importance[1], "Start Command!");
spawn(); spawn();
} }
socket.emit('data', { socket.emit('data', {
@ -322,7 +351,7 @@ function restartSSH(cb) {
function handleKill() { function handleKill() {
process.stdin.resume(); process.stdin.resume();
winston.log(importance[0], "Received Shutdown Command"); logger.log(importance[0], "Received Shutdown Command");
mustBe = true; mustBe = true;
cmd.kill(); cmd.kill();
process.exit(0); process.exit(0);
@ -335,11 +364,7 @@ process.on('SIGTERM', function() {
//let's go //let's go
function init() { function init() {
config = readConfig(); config = readConfig();
winston.add(winston.transports.File, {
filename: __dirname + '/' + config.logPath,
maxsize: 2048,
maxFiles: 10
});
if (config.configured) { if (config.configured) {
socket = sock(config.master + '/pi'); socket = sock(config.master + '/pi');
initSocket(); initSocket();
@ -423,7 +448,7 @@ function checkSSH(cb) {
function initSocket() { function initSocket() {
socket.on('connect', function() { socket.on('connect', function() {
winston.log(importance[0], 'Connected to Master: ' + config.master + '.'); logger.log(importance[0], 'Connected to Master: ' + config.master + '.');
if (config['ssh-user']) if (config['ssh-user'])
initSSH(err => { initSSH(err => {
if (err) if (err)