mirror of
https://github.com/openmhz/trunk-server.git
synced 2026-11-12 08:47:40 -05:00
upgraded most of the Backend
This commit is contained in:
@@ -28,7 +28,7 @@ const System = () => {
|
||||
const { data: systemsData, isSuccess: isSystemsSuccess } = useGetSystemsQuery();
|
||||
const { data: talkgroupsData, isSuccess: isTalkgroupsSuccess } = useGetTalkgroupsQuery(shortName);
|
||||
const { data: groupsData } = useGetGroupsQuery(shortName);
|
||||
const { data: systemErrorData, isSuccess: isErrorsSuccess } = useGetErrorsQuery(shortName);
|
||||
//const { data: systemErrorData, isSuccess: isErrorsSuccess } = useGetErrorsQuery(shortName);
|
||||
const [deleteGroupAPI] = useDeleteGroupMutation();
|
||||
const [reorderGroupsAPI] = useSaveGroupOrderMutation();
|
||||
const [importTalkgroupsAPI] = useImportTalkgroupsMutation();
|
||||
@@ -89,7 +89,10 @@ const System = () => {
|
||||
|
||||
const processStatistics = (statistic) => {
|
||||
var callTotals = [];
|
||||
var errorTotals = [];
|
||||
var uploadErrors= [];
|
||||
let talkgroupStats = [];
|
||||
let decodeErrorsFreq = [];
|
||||
|
||||
const now = new Date();
|
||||
var maxDate = now;
|
||||
var minDate = now;
|
||||
@@ -105,13 +108,13 @@ const System = () => {
|
||||
maxValue = statistic.callTotals[j];
|
||||
callTotals.push({ y: statistic.callTotals[j], x: time });
|
||||
}
|
||||
for (let j = 0; j < statistic.errorTotals.length; j++) {
|
||||
let spotsBack = statistic.errorTotals.length - j;
|
||||
for (let j = 0; j < statistic.uploadErrors.length; j++) {
|
||||
let spotsBack = statistic.uploadErrors.length - j;
|
||||
let time = new Date(now - spotsBack * 15 * MS_PER_MINUTE);
|
||||
if (time < minDate) minDate = time;
|
||||
if (statistic.errorTotals[j] > maxValue)
|
||||
maxValue = statistic.errorTotals[j];
|
||||
errorTotals.push({ x: time, y: statistic.errorTotals[j] });
|
||||
if (statistic.uploadErrors[j] > maxValue)
|
||||
maxValue = statistic.uploadErrors[j];
|
||||
uploadErrors.push({ x: time, y: statistic.uploadErrors[j] });
|
||||
}
|
||||
const callData = {
|
||||
minDate: minDate,
|
||||
@@ -119,7 +122,7 @@ const System = () => {
|
||||
minValue: minValue,
|
||||
maxValue: maxValue,
|
||||
callTotals: callTotals,
|
||||
errorTotals: errorTotals
|
||||
errorTotals: uploadErrors
|
||||
};
|
||||
|
||||
setCallData(callData);
|
||||
@@ -234,12 +237,12 @@ const System = () => {
|
||||
processStatistics(systemsData.stats[shortName])
|
||||
}
|
||||
}, [isSystemsSuccess]);
|
||||
|
||||
/*
|
||||
useEffect(() => {
|
||||
if (isErrorsSuccess) {
|
||||
processErrors(systemErrorData)
|
||||
}
|
||||
}, [systemErrorData]);
|
||||
}, [systemErrorData]);*/
|
||||
|
||||
let fileInput = null;
|
||||
var location = "";
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
var path = require("path");
|
||||
var express = require("express");
|
||||
var session = require("express-session");
|
||||
var bodyParser = require("body-parser");
|
||||
var connectMongo = require("connect-mongo");
|
||||
var secrets = require("./secrets");
|
||||
|
||||
const MongoStore = connectMongo(session)
|
||||
var cookie_domain = process.env['REACT_APP_COOKIE_DOMAIN'] != null ? process.env['REACT_APP_COOKIE_DOMAIN'] : '.openmhz.com'; //'https://s3.amazonaws.com/robotastic';
|
||||
var backend_server = process.env['REACT_APP_BACKEND_SERVER'] != null ? process.env['REACT_APP_BACKEND_SERVER'] : 'https://api.openmhz.com';
|
||||
var frontend_server = process.env['REACT_APP_FRONTEND_SERVER'] != null ? process.env['REACT_APP_FRONTEND_SERVER'] : 'https://openmhz.com';
|
||||
@@ -14,7 +10,7 @@ var admin_server = process.env['REACT_APP_ADMIN_SERVER'] != null ? process.env['
|
||||
var dev_server = frontend_server + ":3000"
|
||||
|
||||
|
||||
module.exports = function(app, passport) {
|
||||
module.exports = function(app) {
|
||||
app.set("port", 3005)
|
||||
//app.set('trust proxy', true);
|
||||
// X-Powered-By header has no functional value.
|
||||
@@ -26,24 +22,6 @@ module.exports = function(app, passport) {
|
||||
app.use(bodyParser.urlencoded({ extended: true }))
|
||||
app.use(express.static(path.join(process.cwd(), 'public')));
|
||||
|
||||
|
||||
const sess = {
|
||||
resave: true,
|
||||
saveUninitialized: true,
|
||||
secret: secrets.sessionSecret,
|
||||
proxy: true,
|
||||
name: "sessionId",
|
||||
cookie: {
|
||||
httpOnly: true,
|
||||
secure: false,
|
||||
domain: cookie_domain
|
||||
},
|
||||
store: new MongoStore({
|
||||
url: secrets.db,
|
||||
autoReconnect: true
|
||||
})
|
||||
}
|
||||
|
||||
var node_env = process.env.NODE_ENV;
|
||||
console.log('--------------------------');
|
||||
console.log('===> 😊 Starting Server . . .');
|
||||
@@ -51,15 +29,8 @@ module.exports = function(app, passport) {
|
||||
if(node_env === 'production') {
|
||||
console.log('===> 🚦 Note: In order for authentication to work in production');
|
||||
console.log('===> you will need a secure HTTPS connection');
|
||||
sess.cookie.secure = true; // Serve secure cookies
|
||||
}
|
||||
|
||||
app.use(session(sess))
|
||||
|
||||
app.use(passport.initialize())
|
||||
app.use(passport.session())
|
||||
|
||||
|
||||
app.use('/*', function(req, res, next) {
|
||||
var allowedOrigins = [ admin_server];
|
||||
allowedOrigins.push(frontend_server);
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
|
||||
var LocalStrategy = require('passport-local').Strategy;
|
||||
var User = require("../../models/user");
|
||||
|
||||
/*
|
||||
By default, LocalStrategy expects to find credentials in parameters named username and password.
|
||||
If your site prefers to name these fields differently, options are available to change the defaults.
|
||||
*/
|
||||
module.exports = new LocalStrategy({
|
||||
usernameField: "email"
|
||||
}, (email, password, done) => {
|
||||
User.findOne({
|
||||
$or: [{
|
||||
email
|
||||
}, {
|
||||
local: {
|
||||
email
|
||||
}
|
||||
}]
|
||||
}, (err, user) => {
|
||||
if(!user) return done(null, false, { message: `Email ${email} not found` })
|
||||
|
||||
user.comparePassword(password, (err, isMatch) => {
|
||||
if (isMatch) {
|
||||
return done(null, user)
|
||||
} else {
|
||||
return done(null, false, { message: "Invalid email or password" })
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,25 +0,0 @@
|
||||
/* Initializing PassportJS */
|
||||
var User = require("../models/user");
|
||||
var local = require("./passport-strategies/local");
|
||||
|
||||
module.exports =function (app, passport) {
|
||||
// Configure Passport authenticated session persistence.
|
||||
//
|
||||
// In order to restore authentication state across HTTP requests, Passport needs
|
||||
// to serialize users into and deserialize users out of the session. The
|
||||
// typical implementation of this is as simple as supplying the user ID when
|
||||
// serializing, and querying the user record by ID from the database when
|
||||
// deserializing.
|
||||
passport.serializeUser((user, done) => {
|
||||
done(null, user.id)
|
||||
})
|
||||
|
||||
passport.deserializeUser((id, done) => {
|
||||
User.findById(id, (err, user) => {
|
||||
done(err, user)
|
||||
})
|
||||
})
|
||||
|
||||
// use the following strategies
|
||||
passport.use(local)
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
var ObjectID = require('mongodb').ObjectID;
|
||||
var db = require('../db');
|
||||
var mongoose = require("mongoose");
|
||||
var {callModel:Call} = require("../models/call");
|
||||
var { callModel: Call } = require("../models/call");
|
||||
var Star = require("../models/star");
|
||||
|
||||
var defaultNumResults = 50;
|
||||
@@ -11,7 +11,7 @@ var defaultNumResults = 50;
|
||||
var channels = {};
|
||||
|
||||
|
||||
function get_calls(query, numResults, res) {
|
||||
async function get_calls(query, numResults, res) {
|
||||
|
||||
var calls = [];
|
||||
var fields = {
|
||||
@@ -27,42 +27,40 @@ function get_calls(query, numResults, res) {
|
||||
url: true
|
||||
};
|
||||
|
||||
db.get().collection('calls', function(err, transCollection) {
|
||||
transCollection.find(query.filter, fields, function(err, cursor) {
|
||||
if (err) {
|
||||
console.warn("Error - get_calls() Could not find item " + err + " filter: " + query.filter);
|
||||
res.send(404, 'Sorry, we cannot find that!');
|
||||
return;
|
||||
}
|
||||
cursor.sort(query.sort_order).limit(numResults).each(function(err, item) {
|
||||
if (item) {
|
||||
call = {
|
||||
_id: item._id.toHexString(),
|
||||
talkgroupNum: item.talkgroupNum,
|
||||
url: item.url,
|
||||
filename: item.path + item.name,
|
||||
time: item.time,
|
||||
srcList: item.srcList,
|
||||
star: item.star,
|
||||
freq: item.freq,
|
||||
len: Math.round(item.len)
|
||||
};
|
||||
calls.push(call);
|
||||
} else {
|
||||
res.contentType('json');
|
||||
res.send(JSON.stringify({
|
||||
calls: calls,
|
||||
direction: query.direction
|
||||
}));
|
||||
}
|
||||
});
|
||||
const transCollection = db.get().collection('calls');
|
||||
const sort = { length: -1 };
|
||||
try {
|
||||
let cursor = transCollection.find(query.filter, fields).sort(sort).limit(numResults);
|
||||
await cursor.forEach(function (item) {
|
||||
|
||||
call = {
|
||||
_id: item._id.toHexString(),
|
||||
talkgroupNum: item.talkgroupNum,
|
||||
url: item.url,
|
||||
filename: item.path + item.name,
|
||||
time: item.time,
|
||||
srcList: item.srcList,
|
||||
star: item.star,
|
||||
freq: item.freq,
|
||||
len: Math.round(item.len)
|
||||
};
|
||||
calls.push(call);
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
res.contentType('json');
|
||||
res.send(JSON.stringify({
|
||||
calls: calls,
|
||||
direction: query.direction
|
||||
}));
|
||||
} catch (err) {
|
||||
console.warn("Error - get_calls() Could not find item " + err + " filter: " + query.filter);
|
||||
res.send(404, 'Sorry, we cannot find that!');
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
function build_filter(filter_type, code, start_time, direction, shortName, numResults, starred, res) {
|
||||
async function build_filter(filter_type, code, start_time, direction, shortName, numResults, starred, res) {
|
||||
var filter = {};
|
||||
var query = {};
|
||||
var FilterType = {
|
||||
@@ -71,12 +69,7 @@ function build_filter(filter_type, code, start_time, direction, shortName, numRe
|
||||
Group: 2,
|
||||
Unit: 3
|
||||
};
|
||||
/* removed to see if it helps queries
|
||||
if (starred) {
|
||||
filter.star = {
|
||||
$gt: 0
|
||||
};
|
||||
}*/
|
||||
|
||||
|
||||
if (start_time) {
|
||||
var start = new Date(start_time);
|
||||
@@ -112,45 +105,32 @@ function build_filter(filter_type, code, start_time, direction, shortName, numRe
|
||||
|
||||
if (filter_type) {
|
||||
if ((filter_type == "group") && code && (code.indexOf(',') == -1)) {
|
||||
db.get().collection("groups", function(err, groupCollection) {
|
||||
if (err || !groupCollection) {
|
||||
console.warn("[" + shortName + "] Error - build_filter() Group Collection does not exist " + err);
|
||||
res.contentType('json');
|
||||
res.send(JSON.stringify( {
|
||||
message: 'That Group ID doesnt exist.'
|
||||
}));
|
||||
return;
|
||||
}
|
||||
groupCollection.findOne({
|
||||
'shortName': shortName,
|
||||
'_id': ObjectID.createFromHexString(code)
|
||||
}, function(err, group) {
|
||||
if (err) {
|
||||
console.warn("[" + shortName + "] Error - build_filter() Group ID does not exist " + err);
|
||||
res.contentType('json');
|
||||
res.send(JSON.stringify( {
|
||||
message: 'That Group ID doesnt exist.'
|
||||
}));
|
||||
} else if (!group) {
|
||||
console.warn("[" + shortName + "] Error - build_filter() group is null " + err);
|
||||
res.contentType('json');
|
||||
res.send(JSON.stringify({
|
||||
message: 'That Group ID doesnt exist.'
|
||||
}));
|
||||
} else{
|
||||
filter.talkgroupNum = {
|
||||
$in: group.talkgroups
|
||||
};
|
||||
query['filter'] = filter;
|
||||
const groupCollection = db.get().collection("groups");
|
||||
|
||||
get_calls(query, numResults, res);
|
||||
}
|
||||
});
|
||||
|
||||
const group = await groupCollection.findOne({
|
||||
'shortName': shortName,
|
||||
'_id': ObjectID.createFromHexString(code)
|
||||
});
|
||||
if (!group) {
|
||||
console.warn("[" + shortName + "] Error - build_filter() group is null " + err);
|
||||
res.contentType('json');
|
||||
res.status(404);
|
||||
res.send(JSON.stringify({
|
||||
message: 'That Group ID doesnt exist.'
|
||||
}));
|
||||
}
|
||||
filter.talkgroupNum = {
|
||||
$in: group.talkgroups
|
||||
};
|
||||
query['filter'] = filter;
|
||||
|
||||
get_calls(query, numResults, res);
|
||||
|
||||
} else {
|
||||
if ((filter_type == "talkgroup") || (filter_type == "group")) {
|
||||
if (code) {
|
||||
var codeArray = code.split(',').map(function(item) {
|
||||
var codeArray = code.split(',').map(function (item) {
|
||||
return parseInt(item, 10);
|
||||
});
|
||||
filter.talkgroupNum = {
|
||||
@@ -161,7 +141,7 @@ function build_filter(filter_type, code, start_time, direction, shortName, numRe
|
||||
|
||||
if (filter_type == "unit") {
|
||||
if (code) {
|
||||
var codeArray = code.split(',').map(function(item) {
|
||||
var codeArray = code.split(',').map(function (item) {
|
||||
return parseInt(item, 10);
|
||||
});
|
||||
filter.srcListv = {
|
||||
@@ -180,7 +160,7 @@ function build_filter(filter_type, code, start_time, direction, shortName, numRe
|
||||
}
|
||||
|
||||
|
||||
exports.get_card = function(req, res) {
|
||||
exports.get_card = async function (req, res) {
|
||||
var objectId = req.params.id;
|
||||
try {
|
||||
var o_id = ObjectID.createFromHexString(objectId);
|
||||
@@ -193,28 +173,24 @@ exports.get_card = function(req, res) {
|
||||
}));
|
||||
return;
|
||||
}
|
||||
db.get().collection('calls', function(err, transCollection) {
|
||||
transCollection.findOne({
|
||||
'_id': o_id
|
||||
},
|
||||
function(err, item) {
|
||||
//console.log(util.inspect(item));
|
||||
if (item) {
|
||||
var time = new Date(item.time);
|
||||
var timeString = time.toLocaleTimeString("en-US");
|
||||
var dateString = time.toDateString();
|
||||
res.render('card', {
|
||||
item: item,
|
||||
channel: channels[item.talkgroupNum],
|
||||
time: timeString,
|
||||
date: dateString
|
||||
});
|
||||
} else {
|
||||
console.warn("Error - /card/:id Could not find Item " + err);
|
||||
res.send(404, 'Sorry, we cannot find that!');
|
||||
}
|
||||
});
|
||||
});
|
||||
const transCollection = db.get().collection('calls');
|
||||
const item = await transCollection.findOne({ '_id': o_id });
|
||||
|
||||
//console.log(util.inspect(item));
|
||||
if (item) {
|
||||
var time = new Date(item.time);
|
||||
var timeString = time.toLocaleTimeString("en-US");
|
||||
var dateString = time.toDateString();
|
||||
res.render('card', {
|
||||
item: item,
|
||||
channel: channels[item.talkgroupNum],
|
||||
time: timeString,
|
||||
date: dateString
|
||||
});
|
||||
} else {
|
||||
console.warn("Error - /card/:id Could not find Item " + err);
|
||||
res.send(404, 'Sorry, we cannot find that!');
|
||||
}
|
||||
}
|
||||
|
||||
function package_call(item) {
|
||||
@@ -240,7 +216,7 @@ function package_call(item) {
|
||||
return call;
|
||||
}
|
||||
|
||||
exports.remove_star = function(req, res, next) {
|
||||
exports.remove_star = async function (req, res, next) {
|
||||
var objectId = req.params.id;
|
||||
try {
|
||||
var o_id = ObjectID.createFromHexString(objectId);
|
||||
@@ -254,28 +230,26 @@ exports.remove_star = function(req, res, next) {
|
||||
}));
|
||||
return;
|
||||
}
|
||||
Call.findOneAndUpdate({ _id: objectId }, { $inc: { star: -1 } }, {new: true },function(err, item) {
|
||||
if (err) {
|
||||
res.status(500);
|
||||
res.send(JSON.stringify({
|
||||
success: false,
|
||||
message: err,
|
||||
"_id": objectId
|
||||
}));
|
||||
} else {
|
||||
var call = package_call(item);
|
||||
req.call = call;
|
||||
res.send(JSON.stringify({
|
||||
success: true,
|
||||
call: call
|
||||
}));
|
||||
next();
|
||||
}
|
||||
})
|
||||
const item = await Call.findOneAndUpdate({ _id: objectId }, { $inc: { star: -1 } }, { new: true });
|
||||
if (!item) {
|
||||
res.status(500);
|
||||
res.send(JSON.stringify({
|
||||
success: false,
|
||||
"_id": objectId
|
||||
}));
|
||||
} else {
|
||||
var call = package_call(item);
|
||||
req.call = call;
|
||||
res.send(JSON.stringify({
|
||||
success: true,
|
||||
call: call
|
||||
}));
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
exports.add_star = function(req, res, next) {
|
||||
exports.add_star = async function (req, res, next) {
|
||||
var objectId = req.params.id;
|
||||
try {
|
||||
var o_id = ObjectID.createFromHexString(objectId);
|
||||
@@ -289,28 +263,27 @@ exports.add_star = function(req, res, next) {
|
||||
}));
|
||||
return;
|
||||
}
|
||||
Call.findOneAndUpdate({ _id: objectId }, { $inc: { star: 1 } }, {new: true },function(err, item) {
|
||||
if (err) {
|
||||
res.status(500);
|
||||
res.send(JSON.stringify({
|
||||
success: false,
|
||||
message: err,
|
||||
"_id": objectId
|
||||
}));
|
||||
} else {
|
||||
var call = package_call(item);
|
||||
req.call = call;
|
||||
res.send(JSON.stringify({
|
||||
success: true,
|
||||
call: call
|
||||
}));
|
||||
next();
|
||||
}
|
||||
})
|
||||
const item = await Call.findOneAndUpdate({ _id: objectId }, { $inc: { star: 1 } }, { new: true });
|
||||
if (!item) {
|
||||
res.status(500);
|
||||
res.send(JSON.stringify({
|
||||
success: false,
|
||||
"_id": objectId
|
||||
}));
|
||||
} else {
|
||||
var call = package_call(item);
|
||||
req.call = call;
|
||||
res.send(JSON.stringify({
|
||||
success: true,
|
||||
call: call
|
||||
}));
|
||||
next();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
exports.get_call = function(req, res) {
|
||||
exports.get_call = async function (req, res) {
|
||||
var objectId = req.params.id;
|
||||
try {
|
||||
var o_id = ObjectID.createFromHexString(objectId);
|
||||
@@ -324,37 +297,34 @@ exports.get_call = function(req, res) {
|
||||
}));
|
||||
return;
|
||||
}
|
||||
db.get().collection('calls', function(err, transCollection) {
|
||||
transCollection.findOne({
|
||||
'_id': o_id
|
||||
},
|
||||
function(err, item) {
|
||||
if (item) {
|
||||
var call = package_call(item);
|
||||
res.contentType('json');
|
||||
res.send(JSON.stringify({
|
||||
success: true,
|
||||
call: call})
|
||||
);
|
||||
const transCollection = db.get().collection('calls');
|
||||
const item = await transCollection.findOne({ '_id': o_id });
|
||||
|
||||
if (item) {
|
||||
var call = package_call(item);
|
||||
res.contentType('json');
|
||||
res.send(JSON.stringify({
|
||||
success: true,
|
||||
call: call
|
||||
})
|
||||
);
|
||||
|
||||
} else {
|
||||
console.warn("[" + req.params.shortName + "] Error - /:shortName/call/:id Could not find item " + err + " ID: " + objectId);
|
||||
res.status(404);
|
||||
res.send(JSON.stringify({
|
||||
success: false,
|
||||
"_id": objectId
|
||||
}));
|
||||
}
|
||||
|
||||
} else {
|
||||
console.warn("[" + req.params.shortName + "] Error - /:shortName/call/:id Could not find item " + err + " ID: " + objectId);
|
||||
res.status(404);
|
||||
res.send(JSON.stringify({
|
||||
success: false,
|
||||
error: err,
|
||||
"_id": objectId
|
||||
}));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
exports.get_latest_calls = function(req, res) {
|
||||
exports.get_latest_calls = function (req, res) {
|
||||
var filter_code = req.query["filter-code"];
|
||||
var filter_type = req.query["filter-type"];
|
||||
var starred = req.query["filter-starred"] === 'true'?true:false;
|
||||
var starred = req.query["filter-starred"] === 'true' ? true : false;
|
||||
var short_name = req.params.shortName.toLowerCase();
|
||||
//console.log("[" + short_name + "] Latest - Call Get Filter code: " + filter_code + " Filter Type: " + filter_type );
|
||||
|
||||
@@ -362,10 +332,10 @@ exports.get_latest_calls = function(req, res) {
|
||||
}
|
||||
|
||||
|
||||
exports.get_next_calls = function(req, res) {
|
||||
exports.get_next_calls = function (req, res) {
|
||||
var filter_code = req.query["filter-code"];
|
||||
var filter_type = req.query["filter-type"];
|
||||
var starred = req.query["filter-starred"] === 'true'?true:false;
|
||||
var starred = req.query["filter-starred"] === 'true' ? true : false;
|
||||
var start_time = parseInt(req.query["time"]);
|
||||
var short_name = req.params.shortName.toLowerCase();
|
||||
//console.log("[" + short_name + "] Next Calls - time: " + start_time + " Filter code: " + filter_code + " Filter Type: " + filter_type);
|
||||
@@ -373,10 +343,10 @@ exports.get_next_calls = function(req, res) {
|
||||
build_filter(filter_type, filter_code, start_time, 'newer', short_name, 1, starred, res);
|
||||
}
|
||||
|
||||
exports.get_newer_calls = function(req, res) {
|
||||
exports.get_newer_calls = function (req, res) {
|
||||
var filter_code = req.query["filter-code"];
|
||||
var filter_type = req.query["filter-type"];
|
||||
var starred = req.query["filter-starred"] === 'true'?true:false;
|
||||
var starred = req.query["filter-starred"] === 'true' ? true : false;
|
||||
var start_time = parseInt(req.query["time"]);
|
||||
var short_name = req.params.shortName.toLowerCase();
|
||||
//console.log("[" + short_name + "] Newer Calls - time: " + start_time + " Filter code: " + filter_code + " Filter Type: " + filter_type );
|
||||
@@ -384,10 +354,10 @@ exports.get_newer_calls = function(req, res) {
|
||||
build_filter(filter_type, filter_code, start_time, 'newer', short_name, defaultNumResults, starred, res);
|
||||
}
|
||||
|
||||
exports.get_older_calls = function(req, res) {
|
||||
exports.get_older_calls = function (req, res) {
|
||||
var filter_code = req.query["filter-code"];
|
||||
var filter_type = req.query["filter-type"];
|
||||
var starred = req.query["filter-starred"] === 'true'?true:false;
|
||||
var starred = req.query["filter-starred"] === 'true' ? true : false;
|
||||
var start_time = parseInt(req.query["time"]);
|
||||
var short_name = req.params.shortName.toLowerCase();
|
||||
//console.log("[" + short_name + "] Older Calls - time: " + start_time + " Filter code: " + filter_code + " Filter Type: " + filter_type);
|
||||
@@ -397,10 +367,10 @@ exports.get_older_calls = function(req, res) {
|
||||
|
||||
|
||||
//Delete this after I fix the iPhone app
|
||||
exports.get_iphone_calls = function(req, res) {
|
||||
exports.get_iphone_calls = function (req, res) {
|
||||
var filter_code = req.query["filter-code"];
|
||||
var filter_type = req.query["filter-type"];
|
||||
var starred = req.query["filter-starred"] === 'true'?true:false;
|
||||
var starred = req.query["filter-starred"] === 'true' ? true : false;
|
||||
var start_time = parseInt(req.params.time);
|
||||
var short_name = req.params.shortName.toLowerCase();
|
||||
//console.log("[" + short_name + "] iPhone Newer Calls - time: " + start_time + " Filter code: " + filter_code + " Filter Type: " + filter_type);
|
||||
@@ -408,10 +378,10 @@ exports.get_iphone_calls = function(req, res) {
|
||||
build_filter(filter_type, filter_code, start_time, 'older', short_name, defaultNumResults, starred, res);
|
||||
}
|
||||
|
||||
exports.get_calls = function(req, res) {
|
||||
exports.get_calls = function (req, res) {
|
||||
var filter_code = req.query["filter-code"];
|
||||
var filter_type = req.query["filter-type"];
|
||||
var starred = req.query["filter-starred"] === 'true'?true:false;
|
||||
var starred = req.query["filter-starred"] === 'true' ? true : false;
|
||||
var short_name = req.params.shortName.toLowerCase();
|
||||
//console.log("[" + short_name + "] Inital Calls - Call Get Filter code: " + filter_code + " Filter Type: " + filter_type);
|
||||
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
var db = require('../db');
|
||||
|
||||
exports.get_groups = function(req, res) {
|
||||
db.get().collection('groups', function(err, grp_coll) {
|
||||
grp_coll.find({
|
||||
'shortName': req.params.shortName.toLowerCase()
|
||||
}).toArray(function(err, grp_results) {
|
||||
res.contentType('json');
|
||||
res.send(JSON.stringify(grp_results));
|
||||
});
|
||||
});
|
||||
exports.get_groups = async function (req, res) {
|
||||
|
||||
const grp_coll = db.get().collection('groups');
|
||||
const cursor = grp_coll.find({ 'shortName': req.params.shortName.toLowerCase() });
|
||||
const grp_results = await cursor.toArray();
|
||||
|
||||
res.contentType('json');
|
||||
res.send(JSON.stringify(grp_results));
|
||||
}
|
||||
|
||||
@@ -2,18 +2,16 @@ var mongoose = require("mongoose");
|
||||
var User = require("../models/user");
|
||||
var System = require("../models/system");
|
||||
|
||||
exports.get_systems = function (req, res) {
|
||||
exports.get_systems = async function (req, res) {
|
||||
let fromDate = new Date(Date.now() - 60 * 60 * 24 * 30 * 1000);
|
||||
System.find({lastActive: {$gte: fromDate}}).populate('userId', "screenName").
|
||||
exec(function (err, results) {
|
||||
if (err) {
|
||||
console.error("Error - get_systems: " + err.message);
|
||||
const results = await System.find({lastActive: {$gte: fromDate}}).populate('userId', "screenName").catch( err => {
|
||||
console.error("Error - get_systems: " + err.message);
|
||||
res.json({
|
||||
success: false,
|
||||
message: err
|
||||
});
|
||||
return;
|
||||
}
|
||||
});
|
||||
var systemList = [];
|
||||
for (var result in results) {
|
||||
|
||||
@@ -47,6 +45,5 @@ exports.get_systems = function (req, res) {
|
||||
success: true,
|
||||
systems: systemList
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,18 +1,15 @@
|
||||
var db = require('../db');
|
||||
|
||||
exports.get_talkgroups = function(req, res) {
|
||||
db.get().collection('talkgroups', function(err, tg_coll) {
|
||||
tg_coll.find({
|
||||
'shortName': req.params.shortName.toLowerCase()
|
||||
}).toArray(function(err, tg_results) {
|
||||
var talkgroups = {};
|
||||
for (var tg in tg_results) {
|
||||
talkgroups[tg_results[tg].num] = tg_results[tg];
|
||||
}
|
||||
res.contentType('json');
|
||||
res.send(JSON.stringify({
|
||||
talkgroups: talkgroups
|
||||
}));
|
||||
});
|
||||
});
|
||||
exports.get_talkgroups = async function (req, res) {
|
||||
const tg_coll = db.get().collection('talkgroups');
|
||||
const cursor = tg_coll.find({ 'shortName': req.params.shortName.toLowerCase() })
|
||||
const tg_results = await cursor.toArray();
|
||||
var talkgroups = {};
|
||||
for (var tg in tg_results) {
|
||||
talkgroups[tg_results[tg].num] = tg_results[tg];
|
||||
}
|
||||
res.contentType('json');
|
||||
res.send(JSON.stringify({
|
||||
talkgroups: talkgroups
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -11,33 +11,24 @@ var state = {
|
||||
db: null,
|
||||
}
|
||||
|
||||
exports.connect = function(done) {
|
||||
exports.connect = async function (done) {
|
||||
if (state.db) return done()
|
||||
|
||||
var url = 'mongodb://' + host + ':' + port + '/scanner';
|
||||
|
||||
MongoClient.connect(url, function(err, db) {
|
||||
if (err) return done(err)
|
||||
if (dbUser) {
|
||||
db.authenticate(dbUser, dbPass, function (err, result) {
|
||||
//test.equal(true, result);
|
||||
state.db = db;
|
||||
done();
|
||||
});
|
||||
} else {
|
||||
state.db = db;
|
||||
done();
|
||||
}
|
||||
})
|
||||
const client = new MongoClient(url);
|
||||
await client.connect();
|
||||
state.db = client.db();
|
||||
done();
|
||||
}
|
||||
|
||||
exports.get = function() {
|
||||
exports.get = function () {
|
||||
return state.db
|
||||
}
|
||||
|
||||
exports.close = function(done) {
|
||||
exports.close = function (done) {
|
||||
if (state.db) {
|
||||
state.db.close(function(err, result) {
|
||||
state.db.close(function (err, result) {
|
||||
state.db = null
|
||||
state.mode = null
|
||||
done(err)
|
||||
|
||||
118
backend/index.js
118
backend/index.js
@@ -4,7 +4,6 @@ var passport = require("passport");
|
||||
var util = require("util");
|
||||
|
||||
var secrets = require("./config/secrets");
|
||||
var configurePassport = require("./config/passport");
|
||||
var configureExpress = require("./config/express");
|
||||
|
||||
var calls = require("./controllers/calls");
|
||||
@@ -21,7 +20,6 @@ var ObjectID = require('mongodb').ObjectID;
|
||||
var db = require('./db');
|
||||
|
||||
var System = require("./models/system");
|
||||
var Permission = require("./models/permission");
|
||||
var multer = require('multer');
|
||||
|
||||
// -------------------------------------------
|
||||
@@ -37,105 +35,34 @@ const io = require('socket.io')(server, {
|
||||
}
|
||||
});
|
||||
|
||||
//var Twitter = require('twitter-lite');
|
||||
|
||||
|
||||
//io.origins('*:*');
|
||||
/*
|
||||
io.origins((origin, callback) => {
|
||||
//if (origin !== 'https://foo.example.com') {
|
||||
// return callback('origin not allowed', false);
|
||||
//}
|
||||
callback(null, true);
|
||||
});*/
|
||||
|
||||
io.on('connection', function (socket) {
|
||||
//console.log('a user connected');
|
||||
console.log('a user connected');
|
||||
socket.on('disconnect', function () {
|
||||
//console.log('user disconnected');
|
||||
console.log('user disconnected');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// This section of code enable a Twitter client to tweet out messages when a new call comes in
|
||||
|
||||
/*
|
||||
const client = new Twitter({
|
||||
subdomain: "api", // "api" is the default (change for other subdomains)
|
||||
version: "1.1", // version "1.1" is the default (change for other subdomains)
|
||||
consumer_key: process.env.TWITTER_CONSUMER_KEY, // from Twitter.
|
||||
consumer_secret: process.env.TWITTER_CONSUMER_SECRET, // from Twitter.
|
||||
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY, // from your User (oauth_token)
|
||||
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET // from your User (oauth_token_secret)
|
||||
});
|
||||
|
||||
client
|
||||
.get("account/verify_credentials")
|
||||
.then(results => {
|
||||
console.log("Connected to Twitter");
|
||||
})
|
||||
.catch(console.error);
|
||||
*/
|
||||
|
||||
function getRole(req) {
|
||||
if (req.params.shortName && req.user) {
|
||||
var short_name = req.params.shortName.toLowerCase();
|
||||
var userId = req.user._id;
|
||||
Permission.findOne({ 'userId': userId, 'shortName': short_name }, function (err, permission) {
|
||||
if (err) {
|
||||
console.error('Error - getRole userId: ' + userId + ' shortName: ' + short_name + ' error: ' + err);
|
||||
return -1;
|
||||
}
|
||||
console.log('Found - getRole userId: ' + userId + ' shortName: ' + short_name + ' role: ' + permission.role);
|
||||
|
||||
return permission.role;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function isUser(req, res, next) {
|
||||
if (req.params.shortName) {
|
||||
var short_name = req.params.shortName.toLowerCase();
|
||||
System.findOne({ 'shortName': short_name }, function (err, system) {
|
||||
if (err) {
|
||||
console.error('Error - System not found: ' + short_name);
|
||||
res.status(505).send({
|
||||
success: false,
|
||||
message: "System Not Found"
|
||||
});
|
||||
}
|
||||
if (!system.private) return next();
|
||||
|
||||
// Looks like the system is private...
|
||||
|
||||
if (getRole(req) > 0) return next();
|
||||
res.status(401).send({
|
||||
success: false,
|
||||
message: "Insufficent Permission."
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
});*/
|
||||
|
||||
|
||||
// -------------------------------------------
|
||||
|
||||
configurePassport(app, passport)
|
||||
configureExpress(app, passport)
|
||||
|
||||
configureExpress(app)
|
||||
|
||||
// -------------------------------------------
|
||||
|
||||
|
||||
const connect = () => {
|
||||
mongoose.connect(secrets.db, (err, res) => {
|
||||
if (err) {
|
||||
console.log(`Error connecting to ${secrets.db}. ${err}`)
|
||||
} else {
|
||||
console.log(`Successfully connected to ${secrets.db}.`)
|
||||
}
|
||||
mongoose.connect(secrets.db).then((result) => { // Successfully connected
|
||||
console.log("connected to Mongo");
|
||||
})
|
||||
.catch((err) => {
|
||||
// Catch any potential error
|
||||
console.log(mongoose.version);
|
||||
console.log("Unable to connect to MongoDB. Error: " + err);
|
||||
});
|
||||
}
|
||||
connect()
|
||||
connect();
|
||||
|
||||
mongoose.connection.on("error", console.error)
|
||||
mongoose.connection.on("disconnected", connect)
|
||||
@@ -185,21 +112,6 @@ app.get('/:shortName/calls', calls.get_calls);
|
||||
/*------ UPLOADS ---------- upload.single('call'), uploads.upload,*/
|
||||
app.post('/:shortName/upload', upload.single('call'), uploads.upload, async function (req, res) {
|
||||
notify_clients(req.call);
|
||||
|
||||
// This section is for sending out tweets when new calls come in
|
||||
|
||||
/*
|
||||
if ((req.call.shortName == "dcfd") && (req.call.talkgroupNum == 101)) {
|
||||
var call_time = new Date(req.call.time);
|
||||
var call_link = "https://openmhz.com/system/dcfd?call-id=" + req.call._id + "&time=" + call_time.getTime();
|
||||
|
||||
var call_time_label = call_time.toLocaleDateString('en-US', { timeZone: "America/New_York" }) + " " + call_time.toLocaleTimeString('en-US', { timeZone: "America/New_York" })
|
||||
var status = req.call.len + " sec transmission\n" + call_time_label + "\n" + call_link;
|
||||
const response = await client.post('statuses/update', {
|
||||
status: status,
|
||||
});
|
||||
//console.log("Tweet Response: " + response);
|
||||
}*/
|
||||
});
|
||||
|
||||
/*------ SYSTEMS ----------*/
|
||||
@@ -431,8 +343,4 @@ var calcSched = schedule.scheduleJob(calcRule, function() {
|
||||
});
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = app;
|
||||
|
||||
3256
backend/package-lock.json
generated
3256
backend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -4,33 +4,33 @@
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"adm-zip": "^0.5.10",
|
||||
"aws-sdk": "^2.5",
|
||||
"axios": "0.18.0",
|
||||
"aws-sdk": "^2.1353",
|
||||
"axios": "1.3.5",
|
||||
"bcrypt-nodejs": "0.0.3",
|
||||
"body-parser": "1.18.3",
|
||||
"bson": "1.0.4",
|
||||
"connect-mongo": "2.0.1",
|
||||
"cookie-parser": "^1.4.0",
|
||||
"csv": "1.1.1",
|
||||
"body-parser": "1.20.2",
|
||||
"bson": "5.2.0",
|
||||
"connect-mongo": "5.0.0",
|
||||
"cookie-parser": "^1.4.6",
|
||||
"csv": "6.2.8",
|
||||
"csv-write-stream": "^2.0.0",
|
||||
"express": "4.16.4",
|
||||
"express-session": "1.15.6",
|
||||
"ffprobe": "^1.1.0",
|
||||
"ffprobe-static": "2.0.0",
|
||||
"find-remove": "1.0.1",
|
||||
"get-folder-size": "^1.0.0",
|
||||
"mkdirp": "^0.5.1",
|
||||
"mongodb": "2.2.36",
|
||||
"mongoose": "5.12.3",
|
||||
"express": "4.18.2",
|
||||
"express-session": "1.17.3",
|
||||
"ffprobe": "^1.1.2",
|
||||
"ffprobe-static": "3.1.0",
|
||||
"find-remove": "4.1.0",
|
||||
"get-folder-size": "^4.0.0",
|
||||
"mkdirp": "^2.1.6",
|
||||
"mongodb": "5.2.0",
|
||||
"mongoose": "7.0.3",
|
||||
"multer": "1.3.0",
|
||||
"node-ffprobe": "https://github.com/ListenerApproved/node-ffprobe",
|
||||
"node-schedule": "1.2.4",
|
||||
"passport": "0.4.0",
|
||||
"node-schedule": "2.1.1",
|
||||
"passport": "0.6.0",
|
||||
"passport-local": "1.0.0",
|
||||
"socket.io": "^4.0.0",
|
||||
"socket.io": "^4.6.1",
|
||||
"twitter-lite": "1.1.0",
|
||||
"websocket": "^1.0.24",
|
||||
"ws": "^3.1.0"
|
||||
"websocket": "^1.0.34",
|
||||
"ws": "^8.13.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha --reporter spec"
|
||||
|
||||
@@ -8,71 +8,46 @@ const timePeriod = 15; // in minutes
|
||||
var spots = (24*60) / timePeriod; // the number of spots needed to keep track of 24 hours of stats
|
||||
|
||||
|
||||
function updateActiveSystems() {
|
||||
async function updateActiveSystems() {
|
||||
|
||||
// Go through all of the Systems
|
||||
db.get().collection('systems', function(err, sysCollection) {
|
||||
const sysCollection = db.get().collection('systems');
|
||||
var cursor = sysCollection.find();
|
||||
cursor.each(function(err, item) {
|
||||
await cursor.forEach(function(item) {
|
||||
// go through all the systems
|
||||
|
||||
// While there are still Systems to work with
|
||||
if (item) {
|
||||
// if you have recieved some calls during that last period, make the system active
|
||||
if ((callTotals[item.shortName] != undefined) && (callTotals[item.shortName][0] > 0)) {
|
||||
const active = true;
|
||||
const callAvg = callTotals[item.shortName][0] / timePeriod;
|
||||
sysCollection.update({
|
||||
shortName: item.shortName
|
||||
}, {
|
||||
$set: {
|
||||
"active": active,
|
||||
"callAvg": callAvg,
|
||||
"lastActive": new Date()
|
||||
}
|
||||
}, {
|
||||
upsert: true
|
||||
}, function(err, objects) {
|
||||
if (err) {
|
||||
console.log("Shortname: " + item.shortName);
|
||||
console.warn(err.message);
|
||||
}
|
||||
});
|
||||
|
||||
const query = { shortName: item.shortName};
|
||||
const update = { $set: {"active": active,"callAvg": callAvg,"lastActive": new Date() }};
|
||||
const options = {upsert: true};
|
||||
|
||||
sysCollection.updateOne(query, update, options);
|
||||
|
||||
} else {
|
||||
sysCollection.update({
|
||||
shortName: item.shortName
|
||||
}, {
|
||||
$set: {
|
||||
"active": false,
|
||||
"callAvg": 0
|
||||
}
|
||||
}, {
|
||||
upsert: true
|
||||
}, function(err, objects) {
|
||||
if (err) {
|
||||
console.log("Shortname: " + item.shortName);
|
||||
console.warn(err.message);
|
||||
}
|
||||
});
|
||||
const query = { shortName: item.shortName};
|
||||
const update = { $set: {"active": false, "callAvg": 0}};
|
||||
const options = {upsert: true};
|
||||
|
||||
sysCollection.updateOne(query, update, options);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
exports.initStats = function() {
|
||||
exports.initStats = async function() {
|
||||
// get the System Stats collection
|
||||
db.get().createCollection("system_stats")
|
||||
db.get().collection('system_stats', function(err, statsCollection) {
|
||||
|
||||
db.get().createCollection("system_stats").catch((err)=>{console.log("System Stats collection is ready")});
|
||||
const statsCollection = db.get().collection('system_stats');
|
||||
|
||||
var cursor = statsCollection.find();
|
||||
|
||||
// go through each of the Systems in Stats
|
||||
cursor.each(function(err, item) {
|
||||
await cursor.forEach(function(item) {
|
||||
|
||||
// If you are not at the end...
|
||||
if (item) {
|
||||
//console.log("Loading stats for: " + item.shortName);
|
||||
|
||||
// Talkgroup Stats
|
||||
if (item.talkgroupStats != undefined) {
|
||||
talkgroupStats[item.shortName] = item.talkgroupStats;
|
||||
@@ -91,11 +66,7 @@ exports.initStats = function() {
|
||||
if (item.callTotals != undefined) {
|
||||
callTotals[item.shortName] = item.callTotals;
|
||||
}
|
||||
} else {
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// keeps track of the number of calls that get uploaded with an audio file
|
||||
@@ -168,8 +139,8 @@ exports.addCall = function(call) {
|
||||
|
||||
|
||||
// This gets called when a Time Period is up
|
||||
exports.shiftStats = function() {
|
||||
db.get().collection('system_stats', function(err, statsCollection) {
|
||||
exports.shiftStats = async function() {
|
||||
const statsCollection = db.get().collection('system_stats');
|
||||
|
||||
// for all the systems in Error Stats
|
||||
for (var shortName in uploadErrors) {
|
||||
@@ -234,21 +205,11 @@ exports.shiftStats = function() {
|
||||
}
|
||||
}
|
||||
|
||||
statsCollection.update({
|
||||
shortName: shortName
|
||||
}, {
|
||||
$set: {
|
||||
"decodeErrorsFreq": decodeErrorsFreq[shortName],
|
||||
}
|
||||
}, {
|
||||
upsert: true
|
||||
}, function(err, objects) {
|
||||
if (err){
|
||||
console.log("Shortname: " + shortName);
|
||||
console.log("decodeErrorsFreq: " + util.inspect(decodeErrorsFreq[shortName]));
|
||||
console.warn(err.message);
|
||||
}
|
||||
});
|
||||
const query = { shortName: item.shortName};
|
||||
const update = { $set: { "decodeErrorsFreq": decodeErrorsFreq[shortName]}};
|
||||
const options = {upsert: true};
|
||||
|
||||
sysCollection.updateOne(query, update, options);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -310,27 +271,15 @@ exports.shiftStats = function() {
|
||||
}
|
||||
callTotals[shortName][0] = callTotal;
|
||||
|
||||
statsCollection.update({
|
||||
shortName: shortName
|
||||
}, {
|
||||
$set: {
|
||||
"talkgroupStats": talkgroupStats[shortName],
|
||||
"callTotals": callTotals[shortName]
|
||||
}
|
||||
}, {
|
||||
upsert: true
|
||||
}, function(err, objects) {
|
||||
if (err){
|
||||
console.log("Shortname: " + shortName);
|
||||
console.log("TgStats: " + util.inspect(talkgroupStats[shortName]));
|
||||
console.log("Call Totals: " + util.inspect(callTotals[shortName]));
|
||||
console.warn(err.message);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
const query = { shortName: item.shortName};
|
||||
const update = { $set: { "talkgroupStats": talkgroupStats[shortName], "callTotals": callTotals[shortName] }};
|
||||
const options = {upsert: true};
|
||||
|
||||
sysCollection.updateOne(query, update, options);
|
||||
}
|
||||
}
|
||||
});
|
||||
updateActiveSystems();
|
||||
}
|
||||
exports.callTotals = function(shortName) {
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
source test.env
|
||||
echo "Docker Compose Command: " $@
|
||||
docker-compose -f docker-compose.yml -f test-compose.yml $@
|
||||
docker compose -f docker-compose.yml -f test-compose.yml $@
|
||||
|
||||
Reference in New Issue
Block a user