Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"env": {
"browser": true,
"amd": true,
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"rules": {
"indent": ["error", 4, {
"SwitchCase": 1,
"VariableDeclarator": 1,
"outerIIFEBody": 1,
// MemberExpression: null,
// CallExpression: {
// parameters: null,
// },
"FunctionDeclaration": {
"parameters": 1,
"body": 1
},
"FunctionExpression": {
"parameters": 1,
"body": 1
}
}],
"max-len": ["error", 90, 2, {
"ignoreUrls": true,
"ignoreComments": false,
"ignoreStrings": true,
"ignoreTemplateLiterals": true
}],
"space-before-function-paren": ["error", "never"]
}
}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ conf.json
super.json
dbdata
npm-debug.log

# windows
/ffmpeg/*

# vscode
/.vscode/*
463 changes: 245 additions & 218 deletions camera.js

Large diffs are not rendered by default.

63 changes: 37 additions & 26 deletions cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var sql=mysql.createConnection(config.db);
s={lock:{}};
if(config.cron===undefined)config.cron={};
if(config.cron.deleteOld===undefined)config.cron.deleteOld=true;
if(config.cron.deleteOrphans===undefined)config.cron.deleteOrphans=false;
if(config.cron.deleteOrphans===undefined)config.cron.deleteOrphans=true; // default false
if(config.cron.deleteNoVideo===undefined)config.cron.deleteNoVideo=true;
if(config.cron.deleteOverMax===undefined)config.cron.deleteOverMax=true;
if(config.cron.deleteLogs===undefined)config.cron.deleteLogs=true;
Expand Down Expand Up @@ -119,35 +119,46 @@ s.checkFilterRules=function(v){
}
s.checkForOrphanedFiles=function(v){
if(config.cron.deleteOrphans===true){
e={};
sql.query('SELECT * FROM Monitors WHERE ke=?',[v.ke],function(arr,b) {
b.forEach(function(mon,m){
mon.dir=s.
fs.readdir(s.getVideoDirectory(mon), function(err, items) {
e.query=[];
e.filesFound=[mon.ke,mon.mid];
if(items&&items.length>0){
items.forEach(function(v,n){
e.query.push('time=?')
e.filesFound.push(s.nameToTime(v))
})
sql.query('SELECT * FROM Videos WHERE ke=? AND mid=? AND ('+e.query.join(' OR ')+')',e.filesFound,function(arr,r) {
if(!r){r=[]};
e.foundSQLrows=[];
r.forEach(function(v,n){
v.index=e.filesFound.indexOf(s.moment(v.time,'YYYY-MM-DD HH:mm:ss'));
if(v.index>-1){
delete(items[v.index-2]);
}
});
// FIXME:
// mon.dir=s.
(function(mon) {
fs.readdir(s.getVideoDirectory(mon), function(err, items) {
var e = {};
e.query=[];
e.filesFound=[mon.ke,mon.mid];
if(items&&items.length>0){
items.forEach(function(v,n){
if(v&&v!==null){
exec('rm '+s.getVideoDirectory(mon)+v);
}
e.query.push('time=?')
e.filesFound.push(s.nameToTime(v))
})
})
}
})
sql.query('SELECT * FROM Videos WHERE ke=? AND mid=? AND ('+e.query.join(' OR ')+')',
e.filesFound,function(arr,r) {
if(!r){r=[]};
e.foundSQLrows=[];
r.forEach(function(v,n){
v.index=e.filesFound.indexOf(s.moment(v.time,'YYYY-MM-DD HH:mm:ss'));
if(v.index>-1){
delete(items[v.index-2]);
}
});
items.forEach(function(v,n){
if(v&&v!==null){
var filePath = path.join(s.getVideoDirectory(mon), v);
fs.unlink(filePath, function(err) {
if (err) {
console.log('Video Delete Failed');
} else {
console.log('Video Delete Success');
}
});
}
})
})
}
})
})(mon)
});
});
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
"body-parser": "^1.15.2",
"circular-json": "0.3.1",
"connection-tester": "^0.1.1",
"cors": "^2.8.4",
"crypto": "^0.0.3",
"ejs": "^2.5.5",
"express": "^4.14.0",
"jsonfile": "^3.0.1",
"ldapauth-fork": "^4.0.2",
"moment": "^2.17.0",
"mysql": "^2.12.0",
"nodemailer": "^4.0.1",
Expand All @@ -33,8 +35,7 @@
"request": "^2.79.0",
"socket.io": "^1.7.1",
"socket.io-client": "^1.7.2",
"webdav": "^0.3.1",
"ldapauth-fork": "^4.0.2"
"webdav": "^0.3.1"
},
"devDependencies": {}
}
15 changes: 15 additions & 0 deletions tools/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
checkRelativePath(x) {
if (x.charAt(0) !== '/') {
x = process.cwd() + '/' + x;
}
return x;
},
checkCorrectPathEnding(x) {
var length = x.length;
if (x.charAt(length - 1) !== '/') {
x = x + '/';
}
return x.replace('__DIR__', process.cwd());
},
};
13 changes: 13 additions & 0 deletions tools/ffmpeg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var exec = require('child_process').exec;

module.exports = {
ffmpegKill(callback){
var cmd=''
if(process.platform === 'win32'){
cmd="Taskkill /IM ffmpeg.exe /T /F"
}else{
cmd="ps aux | grep -ie ffmpeg | awk '{print $2}' | xargs kill -9"
}
exec(cmd, callback)
},
}
98 changes: 98 additions & 0 deletions tools/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
var crypto = require('crypto');
var moment = require('moment');

function toLong(ip) {
var ipl = 0;
ip.split('.').forEach(function(octet) {
ipl <<= 8;
ipl += parseInt(octet);
});
return ipl >>> 0;
}

module.exports = {
toLong,
md5(x) {
return crypto
.createHash('md5')
.update(x)
.digest('hex');
},
nameToTime(x) {
(x = x.split('.')[0].split('T')), (x[1] = x[1].replace(/-/g, ':'));
x = x.join(' ');
return x;
},
ratio(width, height, ratio) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we maybe do some maths? Or is that over thinking it? Just thinking of other ratios like 16:10, or whatever may come of the future.

var gcd = function(a, b) {
    if ( ! b) {
        return a;
    }

    return gcd(b, a % b);
};

var ratio = function(height, width){
	var divisor = gcd(height, width);
  return height/divisor + ':' + width/divisor;
}

ratio = width / height;
return Math.abs(ratio - 4 / 3) < Math.abs(ratio - 16 / 9)
? '4:3'
: '16:9';
},
gid(x) {
if (!x) {
x = 10;
}
var t = '';
var p =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < x; i++)
t += p.charAt(Math.floor(Math.random() * p.length));
return t;
},
moment_withOffset(e, x) {
if (!e) {
e = new Date();
}
if (!x) {
x = 'YYYY-MM-DDTHH-mm-ss';
}
e = moment(e);
if (config.utcOffset) {
e = e.utcOffset(config.utcOffset);
}
return e.format(x);
},
moment(e, x) {
if (!e) {
e = new Date();
}
if (!x) {
x = 'YYYY-MM-DDTHH-mm-ss';
}
return moment(e).format(x);
},
ipRange(start_ip, end_ip) {
var start_long = toLong(start_ip);
var end_long = toLong(end_ip);
if (start_long > end_long) {
var tmp = start_long;
start_long = end_long;
end_long = tmp;
}
var range_array = [];
var i;
for (i = start_long; i <= end_long; i++) {
range_array.push(s.fromLong(i));
}
return range_array;
},
portRange(lowEnd, highEnd) {
var list = [];
for (var i = lowEnd; i <= highEnd; i++) {
list.push(i);
}
return list;
},
fromLong(ipl) {
return (
(ipl >>> 24) +
'.' +
((ipl >> 16) & 255) +
'.' +
((ipl >> 8) & 255) +
'.' +
(ipl & 255)
);
},
};
6 changes: 6 additions & 0 deletions tools/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var get = require('./get');
var ffmpeg = require('./ffmpeg');
var check = require('./check');
var remove = require('./remove');

module.exports = Object.assign({}, get, ffmpeg, check, remove);
16 changes: 16 additions & 0 deletions tools/io.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var { io } = require('../io');

module.exports = {
tx(z, y, x) {
if (x) {
return x.broadcast.to(y).emit('f', z);
}
io.to(y).emit('f', z);
},
cx(z, y, x) {
if (x) {
return x.broadcast.to(y).emit('c', z);
}
io.to(y).emit('c', z);
},
};
35 changes: 35 additions & 0 deletions tools/remove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var fs = require('fs');
var path = require('path');

var rmDir = function(dirPath, removeSelf) {
if (removeSelf === undefined) {
removeSelf = true;
}
try { var files = fs.readdirSync(dirPath); }
catch(e) { console.log(e); return false; }
try {
if (files.length > 0) {
for (var i = 0; i < files.length; i++) {
var filePath = path.join(dirPath, '/', files[i]);
if (fs.statSync(filePath).isFile()) {
fs.unlinkSync(filePath);
} else {
rmDir(filePath);
}
}
}
if (removeSelf) {
fs.rmdirSync(dirPath);
return true;
}
} catch (error) {
console.log(error);
return false;
}

return true;
};

module.exports = {
rmDir
}