From b56d26d5c1f59424e55f93ef93e65389812b6cf8 Mon Sep 17 00:00:00 2001 From: dongpd Date: Thu, 6 Feb 2020 09:38:30 +0700 Subject: [PATCH] compare with 10 plate --- api/controllers/ApiController.js | 54 ++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/api/controllers/ApiController.js b/api/controllers/ApiController.js index 26a12a8..82357cd 100644 --- a/api/controllers/ApiController.js +++ b/api/controllers/ApiController.js @@ -59,8 +59,8 @@ function levenshtein(a, b) { } const db = makeDb(config); -var currentIn = {"plate": "", "time": 0}; -var currentOut = {"plate": "", "time": 0}; +var currentIn = []; +var currentOut = []; exports.SaveLogs = async function (req, res) { @@ -87,32 +87,54 @@ exports.SaveLogs = async function (req, res) { var canSaveLogs = true; var vehicleInfo = {"status": false}; - if (req.body.type === "in" && (currentTimestamp - currentIn.time) < 600) { - if (req.body.plate === currentIn.plate) { - canSaveLogs = false; - } else if (levenshtein(req.body.plate, currentIn.plate) < 2) { - canSaveLogs = false; + if (req.body.type === "in") { + for (var i = 0; i < currentIn.length; i++) { + if ((currentTimestamp - currentIn[i].time) < 600) { + if (req.body.plate === currentIn[i].plate) { + canSaveLogs = false; + } else if (levenshtein(req.body.plate, currentIn[i].plate) < 3) { + canSaveLogs = false; + } + } } } - if (req.body.type === "out" && (currentTimestamp - currentOut.time) < 600) { - if (req.body.plate === currentOut.plate) { - canSaveLogs = false; - } else if (levenshtein(req.body.plate, currentOut.plate) < 2) { - canSaveLogs = false; + if (req.body.type === "out") { + for (var i = 0; i < currentOut.length; i++) { + if ((currentTimestamp - currentOut[i].time) < 600) { + if (req.body.plate === currentOut[i].plate) { + canSaveLogs = false; + } else if (levenshtein(req.body.plate, currentOut[i].plate) < 3) { + canSaveLogs = false; + } + } } } if (req.body.type == "in") { - console.log("req: ", req.body.plate, "\t type: ", req.body.type, " \t compare: ", currentIn.plate, "\t save: ", canSaveLogs); + var lists = []; + for (var i = 0; i < currentIn.length; i++) { + lists.push(currentIn[i].plate); + } + console.log("req: ", req.body.plate, " ", req.body.type, " ", lists, " ", canSaveLogs); } if (req.body.type == "out") { - console.log("req: ", req.body.plate, "\t type: ", req.body.type, " \t compare: ", currentOut.plate, "\t save: ", canSaveLogs); + var lists = []; + for (var i = 0; i < currentOut.length; i++) { + lists.push(currentOut[i].plate); + } + console.log("req: ", req.body.plate, " ", req.body.type, " ", lists, " ", canSaveLogs); } if (canSaveLogs) { if (req.body.type === "in") { - currentIn = {"plate": req.body.plate, "time": currentTimestamp}; + if (currentIn.length >= 10) { + currentIn.shift(); + } + currentIn.push({"plate": req.body.plate, "time": currentTimestamp}); } if (req.body.type === "out") { - currentOut = {"plate": req.body.plate, "time": currentTimestamp}; + if (currentOut.length >= 10) { + currentOut.shift(); + } + currentOut.push({"plate": req.body.plate, "time": currentTimestamp}); } var query_plate_raw = await db.query("SELECT * FROM vehicle WHERE plate='" + req.body.plate + "'");