don't save logs if loss track

This commit is contained in:
dongpd 2020-02-03 15:19:51 +07:00
parent 6cd7d22a2b
commit 8cbfd02008

View File

@ -22,8 +22,46 @@ function makeDb(config) {
};
}
function levenshtein(a, b) {
if (a.length === 0)
return b.length;
if (b.length === 0)
return a.length;
var matrix = [];
// increment along the first column of each row
var i;
for (i = 0; i <= b.length; i++) {
matrix[i] = [i];
}
// increment each column in the first row
var j;
for (j = 0; j <= a.length; j++) {
matrix[0][j] = j;
}
// Fill in the rest of the matrix
for (i = 1; i <= b.length; i++) {
for (j = 1; j <= a.length; j++) {
if (b.charAt(i - 1) == a.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
Math.min(matrix[i][j - 1] + 1, // insertion
matrix[i - 1][j] + 1)); // deletion
}
}
}
return matrix[b.length][a.length];
}
var current = {"plate": "", "time": 0, "type": "in"};
exports.SaveLogs = async function (req, res) {
console.log("req: ", req.body.plate);
console.log("req: ", req.body.plate, " current: ", current);
var today = new Date();
var currentDate = today.getDate();
@ -44,6 +82,19 @@ exports.SaveLogs = async function (req, res) {
if (!fs.existsSync(rootDir + currentYear + "/" + currentMonth + "/" + currentDate + "/" + req.body.type + "/frame"))
fs.mkdirSync(rootDir + currentYear + "/" + currentMonth + "/" + currentDate + "/" + req.body.type + "/frame");
var currentTimestamp = Math.round(today.getTime() / 1000);
var canSaveLogs = true;
var vehicleInfo = {"status": false};
if (req.body.type === current.type && (currentTimestamp - current.time) < 600) {
if (req.body.plate === current.plate) {
canSaveLogs = false;
}
// else if (levenshtein(req.body.type, current.type) < 3) {
// canSaveLogs = false;
// }
}
if (canSaveLogs) {
current = {"plate": req.body.plate, "time": currentTimestamp, "type": req.body.type};
var fileName = req.body.plate + "_" + currentTimestamp + ".png";
var plateSaved = currentYear + "/" + currentMonth + "/" + currentDate + "/" + req.body.type + "/plate/" + fileName;
var frameSaved = currentYear + "/" + currentMonth + "/" + currentDate + "/" + req.body.type + "/frame/" + fileName;
@ -52,7 +103,6 @@ exports.SaveLogs = async function (req, res) {
await fs.writeFileSync(rootDir + frameSaved, req.body.frame_image, 'base64');
const db = makeDb(config);
var vehicleInfo = {"status": false};
var query_plate_raw = await db.query("SELECT * FROM vehicle WHERE plate='" + req.body.plate + "'");
if (query_plate_raw.length > 0) {
vehicleInfo = {"status": true, "data": query_plate_raw[0]};
@ -101,5 +151,6 @@ exports.SaveLogs = async function (req, res) {
}
}
}
}
res.send(vehicleInfo);
};