update socket

This commit is contained in:
dongpd 2020-02-07 15:56:40 +07:00
parent 6aefd96212
commit 5417531e83
2 changed files with 61 additions and 6 deletions

View File

@ -62,12 +62,14 @@ const db = makeDb(config);
var currentIn = [];
var currentOut = [];
var maxCompare = 5;
var maxFalse = 3;
var maxFalse = 0;
var falseIn = 0;
var falseOut = 0;
exports.SaveLogs = async function (req, res) {
var io = req.app.get('socketio');
var today = new Date();
var currentDate = today.getDate();
var currentMonth = today.getMonth() + 1;
@ -126,14 +128,37 @@ exports.SaveLogs = async function (req, res) {
if (query_plate_raw.length > 0) {
vehicleInfo = {"status": true, "data": query_plate_raw[0]};
if (req.body.type == "in") {
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 + ")");
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
});
}
if (req.body.type == "out") {
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 {
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 + ")");
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
});
}
}
} else {
@ -141,14 +166,37 @@ exports.SaveLogs = async function (req, res) {
if (query_plate_levenshtein.length > 0) {
vehicleInfo = {"status": true, "data": query_plate_levenshtein[0]};
if (req.body.type == "in") {
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 + ")");
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
});
}
if (req.body.type == "out") {
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 {
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 + ")");
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
});
}
}
} else {

View File

@ -6,7 +6,6 @@ var express = require('express'),
port = 4004,
bodyParser = require('body-parser'),
jsonwebtoken = require("jsonwebtoken");
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json({limit: '50mb'}));
@ -34,6 +33,14 @@ app.use(function (req, res) {
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);
module.exports = app;