package art.servers.gost.access.controller;
|
|
import art.library.interop.serialization.Serialization;
|
import art.library.model.devices.DeviceStatus;
|
import art.library.model.devices.gost.access.AccessEnforcement;
|
import art.library.model.devices.gost.access.AccessEnforcementInformation;
|
import art.library.model.devices.gost.access.types.AccessEnforcement_Detection;
|
import art.library.model.devices.gost.access.types.AccessEnforcement_Detection_Image;
|
import art.library.model.devices.gost.access.types.AccessEnforcement_Detection_State;
|
import art.library.model.devices.gost.access.types.AccessEnforcement_Detection_State_Discarded;
|
import art.library.model.devices.gost.access.types.AccessEnforcement_Detection_State_Vehicle;
|
import art.servers.gost.access.Shared;
|
import art.servers.gost.access.configuration.Configuration;
|
import art.servers.gost.access.configuration.ConfigurationDetail;
|
import art.servers.gost.access.types.DatabasePoolConnection;
|
import art.servers.gost.access.utils.OSD;
|
import java.io.BufferedReader;
|
import java.io.ByteArrayOutputStream;
|
import java.io.File;
|
import java.io.StringReader;
|
import java.nio.file.Files;
|
import java.nio.file.StandardCopyOption;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import net.lingala.zip4j.ZipFile;
|
import net.lingala.zip4j.io.inputstream.ZipInputStream;
|
import net.lingala.zip4j.model.FileHeader;
|
|
|
public class Controller_ACCESS_LectorVision_Oviedo extends Controller_ACCESS
|
{
|
private ConfigurationDetail configuration = null;
|
|
|
|
public Controller_ACCESS_LectorVision_Oviedo(AccessEnforcement access, DatabasePoolConnection database)
|
{
|
super(access, database);
|
this.configuration = ((Configuration)Shared.configuration).detail;
|
this.setName(Shared.getMessage("Controller access") + " " + access.information.name);
|
}
|
|
|
public AccessEnforcement getAccessEnforcement()
|
{
|
return (AccessEnforcement)getDevice();
|
}
|
|
|
|
public void run()
|
{
|
Shared.traceInformation(getName(),Shared.getMessage("Starting"));
|
|
initialise();
|
|
// Status
|
{
|
AccessEnforcement access = getAccessEnforcement();
|
AccessEnforcement clon = Serialization.clone(access);
|
clon.getDeviceStatus().status = DeviceStatus.STATUS_ONLINE;
|
clon.getDeviceAlarms().clear();
|
Shared.model.updateDevice(access, clon);
|
}
|
|
while ((isInterrupted() == false) && (exit == false))
|
{
|
long lastTimestampUpdate = System.currentTimeMillis();
|
|
try
|
{
|
if (Shared.isServerEnabled() == true)
|
{
|
update();
|
}
|
}
|
catch (Exception exception)
|
{
|
}
|
|
AccessEnforcement red = getAccessEnforcement();
|
long timetowait = (red.getDeviceInformation().polling * 1000) - (System.currentTimeMillis() - lastTimestampUpdate);
|
timetowait = Math.min(timetowait, red.getDeviceInformation().polling * 1000);
|
|
if (timetowait > 0)
|
{
|
try
|
{
|
sleep(timetowait);
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
if (Shared.model.existsDevice(getAccessEnforcement().getIdentifier()) == false)
|
{
|
Shared.traceInformation(getName(),Shared.getMessage("Device no longer exists"));
|
exit = true;
|
}
|
}
|
|
|
Shared.traceInformation(getName(),Shared.getMessage("Finishing"));
|
}
|
|
|
|
|
|
|
|
// <editor-fold defaultstate="collapsed" desc="Download and update detections">
|
|
|
private void update()
|
{
|
AccessEnforcement access = getAccessEnforcement();
|
AccessEnforcementInformation information = access.getDeviceInformation();
|
|
int totalDetections = 0;
|
int processedDetections = 0;
|
|
try
|
{
|
// Read files
|
|
File rootFolder = new File(information.oviedo.detectionsFolder);
|
|
if (rootFolder.exists() == true)
|
{
|
File files[] = rootFolder.listFiles();
|
|
// Wait all files to finish copy
|
|
try
|
{
|
sleep(15000);
|
}
|
catch (Exception exception)
|
{
|
}
|
|
for (File file : files)
|
{
|
totalDetections = totalDetections + 1;
|
|
if ((file.getName().endsWith("zip") == true) && (file.getName().indexOf(information.oviedo.identifier) == 0))
|
{
|
if (update(file) == true)
|
{
|
file.delete();
|
processedDetections = processedDetections + 1;
|
}
|
}
|
}
|
}
|
}
|
catch (Exception exception)
|
{
|
Shared.traceError(getName(),Shared.getMessage("Update"), exception);
|
}
|
|
if (totalDetections > 0)
|
{
|
String message = " " + processedDetections + " " + Shared.getMessage("of") + " " + totalDetections;
|
Shared.traceInformation(getName(),Shared.getMessage("Import"), message);
|
}
|
|
}
|
|
|
|
private boolean update(File file)
|
{
|
AccessEnforcement access = getAccessEnforcement();
|
AccessEnforcementInformation information = access.getDeviceInformation();
|
|
try
|
{
|
ZipFile zipFile = new ZipFile(file, null);
|
|
AccessEnforcement_Detection detection = new AccessEnforcement_Detection();
|
detection.states = new ArrayList<>();
|
detection.device = information;
|
|
for (FileHeader fileHeader : zipFile.getFileHeaders())
|
{
|
byte filedata[] = null;
|
|
String filename = fileHeader.getFileName();
|
{
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
ZipInputStream zis = zipFile.getInputStream(fileHeader);
|
int length = 0; byte[] buffer = new byte[8192];
|
while ((length = zis.read(buffer)) != -1) bos.write(buffer, 0, length);
|
zis.close();
|
bos.close();
|
filedata = bos.toByteArray();
|
}
|
|
if (filename.endsWith("txt") == true)
|
{
|
readInformation(detection, file, new String(filedata));
|
}
|
else if (filename.endsWith("_001.jpg") == true)
|
{
|
if (detection.images == null) detection.images = new ArrayList<AccessEnforcement_Detection_Image>();
|
AccessEnforcement_Detection_Image image = new AccessEnforcement_Detection_Image();
|
image.data = filedata;
|
image.format = AccessEnforcement_Detection_Image.FORMAT_JPG;
|
image.name = AccessEnforcement_Detection_Image.NAME_PLATE;
|
OSD.osd(detection, image, access.getDeviceInformation().getOSD(image.name));
|
detection.images.add(image);
|
}
|
else if (filename.endsWith("_002.jpg") == true)
|
{
|
if (detection.images == null) detection.images = new ArrayList<AccessEnforcement_Detection_Image>();
|
AccessEnforcement_Detection_Image image = new AccessEnforcement_Detection_Image();
|
image.data = filedata;
|
image.format = AccessEnforcement_Detection_Image.FORMAT_JPG;
|
image.name = AccessEnforcement_Detection_Image.NAME_PLATE_CLIP;
|
OSD.osd(detection, image, access.getDeviceInformation().getOSD(image.name));
|
detection.images.add(image);
|
}
|
else if (filename.endsWith("_003.jpg") == true)
|
{
|
if (detection.images == null) detection.images = new ArrayList<AccessEnforcement_Detection_Image>();
|
AccessEnforcement_Detection_Image image = new AccessEnforcement_Detection_Image();
|
image.data = filedata;
|
image.format = AccessEnforcement_Detection_Image.FORMAT_JPG;
|
image.name = AccessEnforcement_Detection_Image.NAME_OVERVIEW;
|
OSD.osd(detection, image, access.getDeviceInformation().getOSD(image.name));
|
detection.images.add(image);
|
}
|
}
|
|
AccessEnforcement_Detection_State detectionState = detection.getLastState();
|
|
if ((detection.images != null) && (detection.images.size() == information.oviedo.numberImages))
|
{
|
SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage("dd/MM/yyyy HH:mm:ss.SSS"));
|
|
if (Shared.controllerDetections.hasPermission(detection, detectionState.vehicle.plate ) != null)
|
{
|
detectionState.state = AccessEnforcement_Detection_State.STATE_DISCARDED;
|
detectionState.discarded = new AccessEnforcement_Detection_State_Discarded();
|
detectionState.discarded.code = AccessEnforcement_Detection_State_Discarded.DISCARDED_ALLOWED;
|
detectionState.discarded.timestamp = System.currentTimeMillis();
|
detectionState.discarded.user = Shared.getApplicationName();
|
Shared.printcorrect(getName(),Shared.getMessage("Successful (allowed), plate") + " = " + detectionState.vehicle.plate + ", " + Shared.getMessage("date") + " = " + formato1.format(detection.timestamp));
|
}
|
else
|
{
|
detectionState.state = AccessEnforcement_Detection_State.STATE_REVISION_PENDING;
|
detectionState.timestamp = System.currentTimeMillis();
|
Shared.printcorrect(getName(),Shared.getMessage("Successful (revision pending), plate") + " = " + detectionState.vehicle.plate + ", " + Shared.getMessage("date") + " = " + formato1.format(detection.timestamp));
|
}
|
}
|
else
|
{
|
SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage("dd/MM/yyyy HH:mm:ss.SSS"));
|
detectionState.state = AccessEnforcement_Detection_State.STATE_DISCARDED;
|
detectionState.discarded = new AccessEnforcement_Detection_State_Discarded();
|
detectionState.discarded.code = AccessEnforcement_Detection_State_Discarded.DISCARDED_MISSING_PICTURES;
|
detectionState.discarded.timestamp = System.currentTimeMillis();
|
detectionState.discarded.description = detectionState.discarded.getDescription(detectionState.discarded.code);
|
// Shared.printerr(getName(),Shared.getMessage("Missing images, plate") + " = " + detectionState.vehicle.plate + ", " + Shared.getMessage("date") + " = " + formato1.format(detection.timestamp));
|
}
|
|
if (updateDatabaseDetection(information, detection) == 0)
|
{
|
SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage("dd/MM/yyyy HH:mm:ss.SSS"));
|
Shared.printwarning(getName(),Shared.getMessage("Already imported, plate") + " = " + detectionState.vehicle.plate + ", " + Shared.getMessage("date") + " = " + formato1.format(detection.timestamp));
|
}
|
|
return true;
|
|
}
|
catch (Exception exception)
|
{
|
try
|
{
|
File fileerror = new File(information.storage.errorFolder + "/" + file.getName());
|
fileerror.getParentFile().mkdirs();
|
Files.move(file.toPath(), fileerror.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
}
|
catch (Exception e)
|
{
|
}
|
|
Shared.traceError(getName(),Shared.getMessage("Update"), file.getAbsolutePath(), exception);
|
}
|
|
return false;
|
}
|
|
|
|
|
private void readInformation(AccessEnforcement_Detection detection, File file, String data) throws Exception
|
{
|
BufferedReader reader = null;
|
|
try
|
{
|
String datetime = null;
|
String plate = null;
|
String serial = null;
|
String confidence = null;
|
|
reader = new BufferedReader(new StringReader(data));
|
String line;
|
while((line = reader.readLine()) != null)
|
{
|
if (line.indexOf("@03") == 0)
|
{
|
datetime = line.substring(4,line.length());
|
}
|
else if (line.indexOf("@04") == 0)
|
{
|
datetime = datetime + " " + line.substring(4,line.length());
|
}
|
else if (line.indexOf("@06") == 0)
|
{
|
serial = line.substring(4,line.length());
|
}
|
else if (line.indexOf("@08") == 0)
|
{
|
plate = line.substring(4,line.length());
|
}
|
else if (line.indexOf("@102") == 0)
|
{
|
confidence = line.substring(5,line.length());
|
}
|
}
|
|
detection.timestamp = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse(datetime).getTime();
|
AccessEnforcement_Detection_State detectionState = new AccessEnforcement_Detection_State();
|
detectionState.vehicle = new AccessEnforcement_Detection_State_Vehicle();
|
detectionState.vehicle.plate = plate.toUpperCase();
|
detectionState.vehicle.confidence = Float.parseFloat(confidence);
|
detection.states.add(detectionState);
|
|
AccessEnforcementInformation information = getAccessEnforcement().getDeviceInformation();
|
|
if (serial.equalsIgnoreCase(information.serial) == false)
|
{
|
throw new Exception(Shared.getMessage("Serial number does not match"));
|
}
|
|
}
|
finally
|
{
|
if (reader != null) try { reader.close(); } catch (Exception exception) {};
|
}
|
}
|
|
|
|
|
|
|
}
|