Compare commits

...

15 Commits

Author SHA1 Message Date
d72f3641d7 fix bug compare 2020-02-13 09:41:39 +07:00
ef8b843d81 check other with absolute 2020-02-13 09:33:22 +07:00
ded7fb097f fix console.log 2020-02-13 09:08:57 +07:00
54f9313a8b fix duplicate 2020-02-13 09:02:28 +07:00
e7cfe25ceb fix 2020-02-11 11:03:25 +07:00
5417531e83 update socket 2020-02-07 15:56:40 +07:00
6aefd96212 fix bug 2020-02-07 10:02:28 +07:00
1715d3173b update max false 2020-02-07 09:28:17 +07:00
7a402301c8 fix compare plate 2020-02-06 11:35:49 +07:00
51b76e56f2 update maxCompare 2020-02-06 09:45:50 +07:00
b56d26d5c1 compare with 10 plate 2020-02-06 09:38:30 +07:00
ae07cb58d7 update compare plate 7 2020-02-05 09:41:09 +07:00
7bf9cf9f25 fix levenshtein 2020-02-04 15:02:00 +07:00
8cbfd02008 don't save logs if loss track 2020-02-03 15:19:51 +07:00
6cd7d22a2b update full business 2020-02-03 14:28:56 +07:00
4 changed files with 345 additions and 71 deletions

View File

@ -1,81 +1,352 @@
'use strict'; 'use strict';
const util = require('util');
var mysql = require('mysql'); var mysql = require('mysql');
const random = require('random'); var fs = require('fs');
var fs = require("fs"); const config = {
var check;
var con = mysql.createConnection({
host: "localhost", host: "localhost",
user: "root", user: "root",
password: "", password: "",
database: "intops" database: "intops"
}); };
var currentIn = [];
var currentInOther = [];
var currentOut = [];
var currentOutOther = [];
var maxCompare = 5;
var maxFalse = 0;
var falseIn = 0;
var falseOut = 0;
function makeDb(config) {
const connection = mysql.createConnection(config);
return {
query(sql, args) {
return util.promisify(connection.query)
.call(connection, sql, args);
},
close() {
return util.promisify(connection.end).call(connection);
}
};
}
// console.log(sql_levenshtein_cmd); function levenshtein(a, b) {
con.connect(function (err) { if (a.length === 0)
if (err) throw err; return b.length;
console.log("Connected!!!") 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];
}
function isSavePlate(type, timestamp, plate) {
if (type === "in") {
for (var i = 0; i < currentIn.length; i++) {
if ((timestamp - currentIn[i].time) < 600) {
if (plate === currentIn[i].plate) {
return false;
} /*else if (levenshtein(plate, currentIn[i].plate) < 2) {
return false;
}*/
}
}
}
if (type === "out") {
for (var i = 0; i < currentOut.length; i++) {
if ((timestamp - currentOut[i].time) < 600) {
if (plate === currentOut[i].plate) {
return false;
} /*else if (levenshtein(plate, currentOut[i].plate) < 2) {
return false;
}*/
}
}
}
return true;
}
function isSavePlateOther(type, timestamp, plate) {
if (type === "in") {
for (var i = 0; i < currentInOther.length; i++) {
if ((timestamp - currentInOther[i].time) < 600) {
if (plate === currentInOther[i].plate) {
return false;
} else if (levenshtein(plate, currentInOther[i].plate) < 2) {
return false;
}
}
}
for (var i = 0; i < currentIn.length; i++) {
if ((timestamp - currentIn[i].time) < 600) {
if (plate === currentIn[i].plate) {
return false;
} else if (levenshtein(plate, currentIn[i].plate) < 3) {
return false;
}
}
}
}
if (type === "out") {
for (var i = 0; i < currentOutOther.length; i++) {
if ((timestamp - currentOutOther[i].time) < 600) {
if (plate === currentOutOther[i].plate) {
return false;
} else if (levenshtein(plate, currentOutOther[i].plate) < 2) {
return false;
} else if (levenshtein(plate, currentOut[i].plate) < 3) {
return false;
}
}
}
for (var i = 0; i < currentOut.length; i++) {
if ((timestamp - currentOut[i].time) < 600) {
if (plate === currentOut[i].plate) {
return false;
} else if (levenshtein(plate, currentOut[i].plate) < 3) {
return false;
}
}
}
}
return true;
}
function consoleLogPlate(type, reqPlate, currentPlate, currentTimestamp, saveType) {
if (type === "in") {
var listsIn = [];
for (var i = 0; i < currentIn.length; i++) {
listsIn.push(currentIn[i].plate);
}
console.log("req: ", reqPlate, " ", currentPlate, " ", type, " ", saveType, " ", listsIn);
if (currentIn.length >= maxCompare) {
currentIn.shift();
}
currentIn.push({"plate": currentPlate, "time": currentTimestamp});
}
if (type === "out") {
var listsOut = [];
for (var i = 0; i < currentOut.length; i++) {
listsOut.push(currentOut[i].plate);
}
console.log("req: ", reqPlate, " ", currentPlate, " ", type, " ", saveType, " ", listsOut);
if (currentOut.length >= maxCompare) {
currentOut.shift();
}
currentOut.push({"plate": currentPlate, "time": currentTimestamp});
}
}
function consoleLogPlateOther(type, reqPlate, currentPlate, currentTimestamp, saveType) {
if (type === "in") {
var listsIn = [];
for (var i = 0; i < currentInOther.length; i++) {
listsIn.push(currentInOther[i].plate);
}
console.log("req: ", reqPlate, " ", currentPlate, " ", type, " ", saveType, " ", listsIn);
if (currentInOther.length >= maxCompare) {
currentInOther.shift();
}
currentInOther.push({"plate": currentPlate, "time": currentTimestamp});
}
if (type === "out") {
var listsOut = [];
for (var i = 0; i < currentOutOther.length; i++) {
listsOut.push(currentOutOther[i].plate);
}
console.log("req: ", reqPlate, " ", currentPlate, " ", type, " ", saveType, " ", listsOut);
if (currentOutOther.length >= maxCompare) {
currentOutOther.shift();
}
currentOutOther.push({"plate": currentPlate, "time": currentTimestamp});
}
}
const db = makeDb(config);
exports.SaveLogs = async function (req, res) { exports.SaveLogs = async function (req, res) {
var sql_cmd = "SELECT * FROM vehicle WHERE plate=\"" + req.body.plate + "\"";
var sql_query_cmd = "select * from vehicle where levenshtein('" + req.body.plate + "',`plate`) BETWEEN 0 AND 2";
console.log("req: ", req.body.plate);
con.connect(function (err) {
// if (err)
// throw err;
if (check != req.body.plate) {
con.query(sql_query_cmd, function (err, result, fields) {
console.log("levenshtein");
console.log(result);
var result_vehicle_id;
var sql_insert_logs_cmd, sql_insert_logs_unknow_cmd;
//if levenshtein not found plate var io = req.app.get('socketio');
if (result == "") {
console.log("Nothing to show");
//timestamp in seconds
var today = new Date();
var currentTimeInInt = Math.round(today.getTime() / 1000);
sql_insert_logs_unknow_cmd = "INSERT INTO logs_unknow(`id`,`plate`,`plate_image_in`,`frame_image_in`,`time_in`,`plate_image_out`,`frame_image_out`,`time_out`,`seal_no`,`note`,`factory`) VALUES (" + random.int(1, 1000) + ",'" + req.body.plate + "','1','1'," + currentTimeInInt + ",'1','1',11,'1','1','1')";
con.query(sql_insert_logs_unknow_cmd, function (err, result, fields) {
if (err)
throw err;
});
console.log("Log unknown done");
}
//if levenshtein found plate var today = new Date();
else { var currentDate = today.getDate();
console.log("Log to logs table"); var currentMonth = today.getMonth() + 1;
//get vehicle id in vehicle table var currentYear = today.getFullYear();
con.query("SELECT id FROM vehicle where `plate`='" + result[0].plate + "'", function (err, result_vehicle_id) { var rootDir = "/mnt/d/xampp/htdocs/AIParking_Intops_Server/web/data/uploads/";
if (err)
throw err; if (!fs.existsSync(rootDir + currentYear))
console.log("result_vehicle_id"); fs.mkdirSync(rootDir + currentYear);
console.log(result_vehicle_id[0].id); if (!fs.existsSync(rootDir + currentYear + "/" + currentMonth))
var today = new Date(); fs.mkdirSync(rootDir + currentYear + "/" + currentMonth);
var currentTimeInInt = Math.round(today.getTime() / 1000); if (!fs.existsSync(rootDir + currentYear + "/" + currentMonth + "/" + currentDate))
sql_insert_logs_cmd = "INSERT INTO logs(`id`,`vehicle_id`,`plate_image_in`,`frame_image_in`,`time_in`,`plate_image_out`,`frame_image_out`,`time_out`,`seal_no`,`note`,`factory`) VALUES (" + random.int(1, 1000) + "," + result_vehicle_id[0].id + ",'1','1'," + currentTimeInInt + ",'1','1',11,'1','1','1')"; fs.mkdirSync(rootDir + currentYear + "/" + currentMonth + "/" + currentDate);
con.query(sql_insert_logs_cmd, function (err, result, fields) { if (!fs.existsSync(rootDir + currentYear + "/" + currentMonth + "/" + currentDate + "/" + req.body.type))
if (err) fs.mkdirSync(rootDir + currentYear + "/" + currentMonth + "/" + currentDate + "/" + req.body.type);
throw err; if (!fs.existsSync(rootDir + currentYear + "/" + currentMonth + "/" + currentDate + "/" + req.body.type + "/plate"))
console.log("Log done"); fs.mkdirSync(rootDir + currentYear + "/" + currentMonth + "/" + currentDate + "/" + req.body.type + "/plate");
}); 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 vehicleInfo = {"status": false};
var currentPlate = "";
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;
await fs.writeFileSync(rootDir + plateSaved, req.body.plate_image, 'base64');
await fs.writeFileSync(rootDir + frameSaved, req.body.frame_image, 'base64');
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]};
currentPlate = query_plate_raw[0].plate;
if (req.body.type == "in" && isSavePlate(req.body.type, currentTimestamp, currentPlate)) {
var newLogs = await db.query("INSERT INTO logs(`vehicle_id`,`plate_image_in`,`frame_image_in`,`time_in`) VALUES ('" + query_plate_raw[0].id + "','" + plateSaved + "','" + frameSaved + "'," + currentTimestamp + ")");
io.emit('logs', {
id: newLogs.insertId,
vehicleInfo: query_plate_raw[0],
type: "in",
image: plateSaved,
time: currentTimestamp
}); });
const base64Image = req.body.plate_image; consoleLogPlate(req.body.type, req.body.plate, currentPlate, currentTimestamp, "absolute");
const imageBuffer = new Buffer(base64Image, "base64");
fs.writeFileSync("image.jpg", imageBuffer);
console.log("wrote image");
check = req.body.plate;
} }
else { if (req.body.type == "out" && isSavePlate(req.body.type, currentTimestamp, currentPlate)) {
console.log("conflic plate"); var checkIn = await db.query("SELECT * FROM logs WHERE vehicle_id=" + query_plate_raw[0].id + " AND time_out=0 ORDER BY time_in DESC LIMIT 0,1");
if (checkIn.length > 0) {
await db.query("UPDATE logs SET `plate_image_out`='" + plateSaved + "',`frame_image_out`='" + frameSaved + "',`time_out`=" + currentTimestamp + " WHERE id=" + checkIn[0].id);
io.emit('logs', {
id: checkIn[0].id,
vehicleInfo: query_plate_raw[0],
type: "out",
image: plateSaved,
time: currentTimestamp,
logs: checkIn[0]
});
} else {
var newLogs = await db.query("INSERT INTO logs(`vehicle_id`,`plate_image_out`,`frame_image_out`,`time_out`) VALUES ('" + query_plate_raw[0].id + "','" + plateSaved + "','" + frameSaved + "'," + currentTimestamp + ")");
io.emit('logs', {
id: newLogs.insertId,
vehicleInfo: query_plate_raw[0],
type: "out",
image: plateSaved,
time: currentTimestamp,
logs: false
});
}
consoleLogPlate(req.body.type, req.body.plate, currentPlate, currentTimestamp, "absolute");
} }
}); } else {
var query_plate_levenshtein = await db.query("SELECT * FROM `vehicle` WHERE LENGTH(`plate`)=" + req.body.plate.length + " AND levenshtein('" + req.body.plate + "', `plate`) BETWEEN 0 AND 1 LIMIT 0,1");
if (query_plate_levenshtein.length > 0) {
vehicleInfo = {"status": true, "data": query_plate_levenshtein[0]};
currentPlate = query_plate_levenshtein[0].plate;
if (req.body.type == "in" && isSavePlate(req.body.type, currentTimestamp, currentPlate)) {
var newLogs = await db.query("INSERT INTO logs(`vehicle_id`,`plate_image_in`,`frame_image_in`,`time_in`) VALUES ('" + query_plate_levenshtein[0].id + "','" + plateSaved + "','" + frameSaved + "'," + currentTimestamp + ")");
io.emit('logs', {
id: newLogs.insertId,
vehicleInfo: query_plate_levenshtein[0],
type: "in",
image: plateSaved,
time: currentTimestamp
});
consoleLogPlate(req.body.type, req.body.plate, currentPlate, currentTimestamp, "levenshtein");
}
if (req.body.type == "out" && isSavePlate(req.body.type, currentTimestamp, currentPlate)) {
var checkIn = await db.query("SELECT * FROM logs WHERE vehicle_id=" + query_plate_levenshtein[0].id + " AND time_out=0 ORDER BY time_in DESC LIMIT 0,1");
if (checkIn.length > 0) {
await db.query("UPDATE logs SET `plate_image_out`='" + plateSaved + "',`frame_image_out`='" + frameSaved + "',`time_out`=" + currentTimestamp + " WHERE id=" + checkIn[0].id);
io.emit('logs', {
id: checkIn[0].id,
vehicleInfo: query_plate_levenshtein[0],
type: "out",
image: plateSaved,
time: currentTimestamp,
logs: checkIn[0]
});
} else {
var newLogs = await db.query("INSERT INTO logs(`vehicle_id`,`plate_image_out`,`frame_image_out`,`time_out`) VALUES ('" + query_plate_levenshtein[0].id + "','" + plateSaved + "','" + frameSaved + "'," + currentTimestamp + ")");
io.emit('logs', {
id: newLogs.insertId,
vehicleInfo: query_plate_levenshtein[0],
type: "out",
image: plateSaved,
time: currentTimestamp,
logs: false
});
}
consoleLogPlate(req.body.type, req.body.plate, currentPlate, currentTimestamp, "levenshtein");
}
res.send({ "status": "Done!" }); } else {
}; currentPlate = req.body.plate;
if (req.body.type == "in" && isSavePlateOther(req.body.type, currentTimestamp, currentPlate)) {
if (falseIn < maxFalse) {
vehicleInfo = {"status": false};
falseIn++;
console.log("status: results in not found");
} else {
falseIn = 0;
vehicleInfo = {"status": true};
await db.query("INSERT INTO logs_unknow(`plate`,`plate_image_in`,`frame_image_in`,`time_in`) VALUES ('" + req.body.plate + "','" + plateSaved + "','" + frameSaved + "'," + currentTimestamp + ")");
consoleLogPlateOther(req.body.type, req.body.plate, currentPlate, currentTimestamp, "other");
}
}
if (req.body.type == "out" && isSavePlateOther(req.body.type, currentTimestamp, currentPlate)) {
if (falseOut < maxFalse) {
vehicleInfo = {"status": false};
falseOut++;
console.log("status: results out not found");
} else {
falseIn = 0;
vehicleInfo = {"status": true};
var checkIn_raw = await db.query("SELECT * FROM logs_unknow WHERE plate='" + req.body.plate + "' AND time_out=0 ORDER BY time_in DESC LIMIT 0,1");
if (checkIn_raw.length > 0) {
await db.query("UPDATE logs_unknow SET `plate_image_out`='" + plateSaved + "',`frame_image_out`='" + frameSaved + "',`time_out`=" + currentTimestamp + " WHERE id=" + checkIn_raw[0].id);
} else {
var checkIn_levenshtein = await db.query("SELECT * FROM `logs_unknow` WHERE levenshtein('" + req.body.plate + "', `plate`) BETWEEN 0 AND 1 AND time_out=0 ORDER BY time_in DESC LIMIT 0,1");
if (checkIn_levenshtein.length > 0) {
await db.query("UPDATE logs_unknow SET `plate_image_out`='" + plateSaved + "',`frame_image_out`='" + frameSaved + "',`time_out`=" + currentTimestamp + " WHERE id=" + checkIn_levenshtein[0].id);
} else {
await db.query("INSERT INTO logs_unknow(`plate`,`plate_image_out`,`frame_image_out`,`time_out`) VALUES ('" + req.body.plate + "','" + plateSaved + "','" + frameSaved + "'," + currentTimestamp + ")");
}
}
consoleLogPlateOther(req.body.type, req.body.plate, currentPlate, currentTimestamp, "other");
}
}
}
}
res.send(vehicleInfo);
};

View File

@ -3,5 +3,5 @@
module.exports = function (app) { module.exports = function (app) {
var Api = require('../controllers/ApiController'); var Api = require('../controllers/ApiController');
app.route('/logs').post(Api.SaveLogs); app.route('/logs').post(Api.SaveLogs);
}; };

View File

@ -8,11 +8,7 @@
}, },
"dependencies": { "dependencies": {
"cookie-parser": "^1.4.3", "cookie-parser": "^1.4.3",
"cors": "^2.8.5", "express": "^4.13.3",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mysql": "^2.18.1",
"random": "^2.1.1",
"socket.io": "^2.1.1" "socket.io": "^2.1.1"
}, },
"engines": { "engines": {

View File

@ -6,7 +6,6 @@ var express = require('express'),
port = 4004, port = 4004,
bodyParser = require('body-parser'), bodyParser = require('body-parser'),
jsonwebtoken = require("jsonwebtoken"); jsonwebtoken = require("jsonwebtoken");
app.use(bodyParser.urlencoded({extended: true})); app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json({limit: '50mb'})); app.use(bodyParser.json({limit: '50mb'}));
@ -34,6 +33,14 @@ app.use(function (req, res) {
const server = app.listen(port); const server = app.listen(port);
const io = require('socket.io').listen(server);
app.set('socketio', io);
io.sockets.on('connection', function (socket) {
socket.on('logs', function (msg) {
socket.broadcast.emit(msg); // Send message to everyone BUT sender
});
});
console.log('AIParking API Server Started On Port: ' + port); console.log('AIParking API Server Started On Port: ' + port);
module.exports = app; module.exports = app;