This commit is contained in:
Hiro Protagonist 2016-08-29 17:10:01 +12:00
parent f19860cf16
commit 388fc29cad
2 changed files with 4309 additions and 69 deletions

163
main.js
View file

@ -1,25 +1,27 @@
let config = require('./config.js') let sock = require('socket.io-client');
let ffmpeg = require('fluent-ffmpeg'); let ffmpeg = require('fluent-ffmpeg');
let request = require('request');
let http = require('http'); let http = require('http');
let path = require('path');
let fs = require('fs'); let fs = require('fs');
let socket = require('socket.io-client')(config.master + '/pi');
let WMStrm = require('./lib/memWrite.js'); let WMStrm = require('./lib/memWrite.js');
let mustBe = false; let mustBe = false;
let restart = false; let restart = false;
var config, source, snapSource;
let source = 'rtsp://' + config.camIP + ':' + config.camPort + '/' + config.camProfile;
let snapSource = 'rtsp://' + config.camIP + ':' + config.camPort + '/' + config.snapProfile;
let status = { let status = {
config: config, status: 0,
name: config.name error: -1
} }
//no dupes let errors = ['Camera Disconnected', 'YoutTube Disconnected', 'Wrong ffmpeg executable.'];
delete status.config.name; let cmd;
//custom compiled let spawn = function () {
ffmpeg.setFfmpegPath(config.ffmpegPath) source = 'rtsp://' + config.camIP + ':' + config.camPort + '/' + config.camProfile;
const cmd = ffmpeg({ ffmpeg.setFfmpegPath(config.ffmpegPath)
delete cmd;
cmd = ffmpeg({
source: source, source: source,
niceness: -20, niceness: -20,
stdoutLines: 20 stdoutLines: 20
@ -30,7 +32,7 @@ const cmd = ffmpeg({
.audioFrequency(11025) .audioFrequency(11025)
.audioCodec('libmp3lame') .audioCodec('libmp3lame')
.on('start', function (commandLine) { .on('start', function (commandLine) {
status.running = true status.running = 0;
wLog('Spawned Ffmpeg with command: ' + commandLine, 4); wLog('Spawned Ffmpeg with command: ' + commandLine, 4);
}) })
.on('end', function (o, e) { .on('end', function (o, e) {
@ -38,28 +40,31 @@ const cmd = ffmpeg({
}) })
.on('error', function (err, o, e) { .on('error', function (err, o, e) {
console.log(err); console.log(err);
if (err.message.indexOf(source + ': No route to host') > -1 || err.message.indexOf(source + ': Connection refused') > -1) if (err.message.indexOf(source + ': No route to host') > -1 || err.message.indexOf(source + ': Connection refused') > -1 || err.message.indexOf(source + ': Input/output error') > -1)
criticalProblem('Camera Disconnected', handleDisc, config.camIP, config.camPort) criticalProblem(0, handleDisc, config.camIP, config.camPort)
else if (err.message.indexOf(source + 'Input/output error') > -1 || err.message.indexOf('rtmp://a.rtmp.youtube.com/live2/' + config.key + ': Network is unreachable') > -1) else if (err.message.indexOf(source + 'Input/output error') > -1 || err.message.indexOf('rtmp://a.rtmp.youtube.com/live2/' + config.key + ': Network is unreachable') > -1)
criticalProblem('YoutTube Disconnected', handleDisc, 'rtmp://a.rtmp.youtube.com/live2/', 1935); criticalProblem(1, handleDisc, 'a.rtmp.youtube.com/live2/', 1935);
else if (err.message.indexOf('spawn') > -1 || err.message.indexOf('niceness') > -1)
criticalProblem(2, function () {});
else else
imDead(err.message, e); imDead(err.message, e);
}) })
.outputFormat('flv') .outputFormat('flv')
.outputOptions(['-bufsize 50000k', '-tune film']) .outputOptions(['-bufsize 50000k', '-tune film'])
.output('rtmp://a.rtmp.youtube.com/live2/' + config.key); .output('rtmp://a.rtmp.youtube.com/live2/' + config.key);
status.error = -1;
let spawn = function () {
socket.emit('change', { socket.emit('change', {
type: 'startStop', type: 'startStop',
change: { change: {
running: true, running: 0,
error: -1
} }
}); });
cmd.run(); cmd.run();
} }
let getSnap = function (cb) { let getSnap = function (cb) {
snapSource = 'rtsp://' + config.camIP + ':' + config.camPort + '/' + config.snapProfile;
let picBuff = new WMStrm(); let picBuff = new WMStrm();
recCmd = ffmpeg(snapSource) recCmd = ffmpeg(snapSource)
.on('start', function (commandLine) { .on('start', function (commandLine) {
@ -82,11 +87,11 @@ let getSnap = function (cb) {
} }
function imDead(why, e = '') { function imDead(why, e = '') {
status.running = false status.running = 1;
socket.emit('change', { socket.emit('change', {
type: 'startStop', type: 'startStop',
change: { change: {
running: false, running: 1,
} }
}); });
if (restart) { if (restart) {
@ -103,13 +108,13 @@ function imDead(why, e = '') {
} }
function criticalProblem(err, handler, ...args) { function criticalProblem(err, handler, ...args) {
status.running = false status.running = 2
status.error = err status.error = err
wLog('Critical Problem: ' + err, 3); wLog('Critical Problem: ' + errors[err], 3);
socket.emit('change', { socket.emit('change', {
type: 'error', type: 'error',
change: { change: {
running: false, running: 2,
error: err error: err
} }
}); });
@ -143,20 +148,17 @@ function isReachable(host, port, callback) {
}); });
} }
var commandHandlers = function commandHandlers(command, cb) {
socket.on('connect', function () {
wLog('Connected to Master: ' + config.master + '.');
socket.emit('meta', status);
});
socket.on('command', (command) => {
commandHandlers(command);
});
var commandHandlers = function commandHandlers(command) {
var handlers = { var handlers = {
getPanel: function () {
console.log(command.request);
request.get('http://admin:admin@' + config.camIP, function (err, res, body) {
cb(body);
});
},
startStop: function () { startStop: function () {
if (status.running) { if (status.running !== 2)
if (status.running === 0) {
wLog("Stop Command!", 1); wLog("Stop Command!", 1);
mustBe = true mustBe = true
cmd.kill(); cmd.kill();
@ -165,7 +167,6 @@ var commandHandlers = function commandHandlers(command) {
spawn(); spawn();
} }
}, },
snap: function () { snap: function () {
getSnap(snap => { getSnap(snap => {
socket.emit('data', { socket.emit('data', {
@ -174,8 +175,58 @@ var commandHandlers = function commandHandlers(command) {
}, command.sender); }, command.sender);
}); });
}, },
config: function () {
socket.emit('data', {
type: 'config',
data: config,
}, command.sender);
},
changeSettings: function () {
for (let set in command.data) {
if (config[set])
config[set] = command.data[set];
}
let oldConfigured;
if (config.configured)
oldConfigured = true;
config.configured = true;
fs.writeFile('./config.js', JSON.stringify(config, undefined, 2), function (err) {
if (err) {
socket.emit('data', {
type: 'message',
data: {
title: 'Error',
type: 'error',
text: 'Can\'t save the Settings!\n' + err.message
}
}, command.sender);
} else {
socket.emit('data', {
type: 'message',
data: {
title: 'Success',
type: 'success',
text: 'Settings Saved!'
}
}, command.sender);
if (oldConfigured) {
socket.emit('change', {
type: 'settings',
change: {
config: command.data
}
});
cmd.kill();
spawn();
} else {
socket.disconnect();
init();
}
}
});
},
restart: function () { restart: function () {
if (status.running) { if (status.running === 0) {
wLog("Restart Command!", 1); wLog("Restart Command!", 1);
mustBe = true; mustBe = true;
restart = true; restart = true;
@ -230,4 +281,40 @@ process.on('SIGTERM', function () {
// }); // });
//let's go //let's go
spawn() function init() {
config = readConfig('./config.js');
if (config.configured) {
socket = sock(config.master + '/pi');
initSocket();
status.name = config.name;
spawn();
} else {
socket = sock(config.master + '/pi', {
query: "unconfigured=true"
});
status.running = 2;
initSocket();
}
}
function initSocket() {
socket.on('connect', function () {
wLog('Connected to Master: ' + config.master + '.');
socket.emit('meta', status);
});
socket.on('disconnect', function () {
socket.disconnect();
init();
});
socket.on('command', (command, cb) => {
commandHandlers(command, cb);
});
}
function readConfig() {
return JSON.parse(fs.readFileSync('./config.js'));
}
init();;

File diff suppressed because it is too large Load diff