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 request = require('request');
let http = require('http');
let path = require('path');
let fs = require('fs');
let socket = require('socket.io-client')(config.master + '/pi');
let WMStrm = require('./lib/memWrite.js');
let mustBe = 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 = {
config: config,
name: config.name
status: 0,
error: -1
}
//no dupes
delete status.config.name;
let errors = ['Camera Disconnected', 'YoutTube Disconnected', 'Wrong ffmpeg executable.'];
let cmd;
//custom compiled
ffmpeg.setFfmpegPath(config.ffmpegPath)
const cmd = ffmpeg({
let spawn = function () {
source = 'rtsp://' + config.camIP + ':' + config.camPort + '/' + config.camProfile;
ffmpeg.setFfmpegPath(config.ffmpegPath)
delete cmd;
cmd = ffmpeg({
source: source,
niceness: -20,
stdoutLines: 20
@ -30,7 +32,7 @@ const cmd = ffmpeg({
.audioFrequency(11025)
.audioCodec('libmp3lame')
.on('start', function (commandLine) {
status.running = true
status.running = 0;
wLog('Spawned Ffmpeg with command: ' + commandLine, 4);
})
.on('end', function (o, e) {
@ -38,28 +40,31 @@ const cmd = ffmpeg({
})
.on('error', function (err, o, e) {
console.log(err);
if (err.message.indexOf(source + ': No route to host') > -1 || err.message.indexOf(source + ': Connection refused') > -1)
criticalProblem('Camera Disconnected', handleDisc, config.camIP, config.camPort)
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(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)
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
imDead(err.message, e);
})
.outputFormat('flv')
.outputOptions(['-bufsize 50000k', '-tune film'])
.output('rtmp://a.rtmp.youtube.com/live2/' + config.key);
let spawn = function () {
status.error = -1;
socket.emit('change', {
type: 'startStop',
change: {
running: true,
running: 0,
error: -1
}
});
cmd.run();
}
let getSnap = function (cb) {
snapSource = 'rtsp://' + config.camIP + ':' + config.camPort + '/' + config.snapProfile;
let picBuff = new WMStrm();
recCmd = ffmpeg(snapSource)
.on('start', function (commandLine) {
@ -82,11 +87,11 @@ let getSnap = function (cb) {
}
function imDead(why, e = '') {
status.running = false
status.running = 1;
socket.emit('change', {
type: 'startStop',
change: {
running: false,
running: 1,
}
});
if (restart) {
@ -103,13 +108,13 @@ function imDead(why, e = '') {
}
function criticalProblem(err, handler, ...args) {
status.running = false
status.running = 2
status.error = err
wLog('Critical Problem: ' + err, 3);
wLog('Critical Problem: ' + errors[err], 3);
socket.emit('change', {
type: 'error',
change: {
running: false,
running: 2,
error: err
}
});
@ -143,20 +148,17 @@ function isReachable(host, port, callback) {
});
}
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 commandHandlers = function commandHandlers(command, cb) {
var handlers = {
getPanel: function () {
console.log(command.request);
request.get('http://admin:admin@' + config.camIP, function (err, res, body) {
cb(body);
});
},
startStop: function () {
if (status.running) {
if (status.running !== 2)
if (status.running === 0) {
wLog("Stop Command!", 1);
mustBe = true
cmd.kill();
@ -165,7 +167,6 @@ var commandHandlers = function commandHandlers(command) {
spawn();
}
},
snap: function () {
getSnap(snap => {
socket.emit('data', {
@ -174,8 +175,58 @@ var commandHandlers = function commandHandlers(command) {
}, 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 () {
if (status.running) {
if (status.running === 0) {
wLog("Restart Command!", 1);
mustBe = true;
restart = true;
@ -230,4 +281,40 @@ process.on('SIGTERM', function () {
// });
//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