tbf/TBF/Rig/Network/Camera/KeyenceIV3G120/GrabImagesOp.cs

135 lines
5.4 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Common;
using log4net;
namespace TBF.Rig.Network.Camera.KeyenceIV3G120
{
public class GrabImagesOp : IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(GrabImagesOp));
public override string ToString() { return string.Format("GrabImageOp()"); }
private readonly Camera camera;
readonly string[] imgFileNames;
private readonly string path;
private readonly DateTime time;
bool grabImageDone;
bool grabPassed;
bool grabFailed;
public GrabImagesOp(Camera camera, string[] imgFileNames, string path, DateTime time)
{
this.camera = camera;
this.imgFileNames = imgFileNames;
this.path = path;
this.time = time;
}
public void Start()
{
grabImageDone = false;
if ((imgFileNames != null) && (imgFileNames.Length > 0) && File.Exists(imgFileNames[0]))
{
try
{
///
/// An image specified, (1) delete previous image, (2) check if this is a simulation
/// If Exist Image on destination fileName path, delete this file
///
log.DebugFormat("Deleting destination image if exist, path: {0}", imgFileNames[0]);
File.Delete(imgFileNames[0]);
/* if (camera.CameraCfg.DebugLevel == DebugMode.Simulate ||
camera.CameraCfg.DebugLevel == DebugMode.DetectedOff)
{
///
/// Camera is in simulation mode => create a simulated image
///
string sampleImagePath = string.Format("{1}\\Pictures\\sample{0}.bmp",
imgFileNames[0].Contains("-end.bmp") ? "-end" : "", Program.ExecutableDir);
File.Copy( sampleImagePath, imgFileNames[0]);
log.DebugFormat("Camera is in simulation mode - sample image:{0}, to file: {1} ", sampleImagePath, imgFileNames[0]);
}*/
}
catch (Exception e)
{
log.Error("Error deleting image / copy sample.bmp: " + e.Message);
}
}
}
public Event Run()
{
if ((imgFileNames == null) || (imgFileNames.Length < 1) || (imgFileNames[0] == null))
{
///
/// No images to be grabbed => Done
///
return Event.GrabPassed;
}
/* else if (camera.CameraCfg.DebugLevel == DebugMode.Simulate ||
camera.CameraCfg.DebugLevel == DebugMode.DetectedOff)
{
///
/// Camera is in simulation mode => create a simulated image and complete
///
return Event.GrabPassed;
}*/
else if (!grabImageDone)
{
string pathDir = $"{Camera.FtpCameraSource}{Camera.PrefixCameraFolder}{camera.CameraIdx}\\";
string searchPattern = $"^.*{Camera.PrefixCameraName}{camera.CameraIdx}_[\\w]{{0,5}}[\\d]{{6}}_[\\d]{{6}}\\.(?i)bmp$";
string[] picCandidates = Directory.GetFiles(pathDir, "*.bmp", SearchOption.TopDirectoryOnly);
picCandidates = picCandidates.Where(pic => Regex.IsMatch(pic, searchPattern)).ToArray();
if (picCandidates != null && picCandidates.Length > 0)
{
string newCandidate = CameraUtils.GetLastCreationTimeCandidate(picCandidates);
log.Debug($"Run() in Cycle, newCandidate: {newCandidate}");
try
{
CameraUtils.WaitForImageCompleteOnDisk(newCandidate);
log.DebugFormat("Run() TRY newCandidate move to fileName:{0}", imgFileNames[0]);
File.Move(newCandidate, imgFileNames[0]);
log.DebugFormat("Run() newCandidate moved completly into new fileName:{0}", imgFileNames[0]);
grabImageDone = true;
grabPassed = true;
CameraUtils.ClearDir(pathDir);
return Event.GrabPassed;
}
catch (Exception ex)
{
//TODO BUMI add beck Event.Error
log.Error("Error (Event.Error) loading image: " + ex.Message);
grabFailed = true;
return Event.GrabPassed; //Event.Error -- FAKE
}
}
return Event.CameraBusy;
}
else if (grabImageDone)
{
return Event.GrabPassed;
}
return Event.CameraBusy;
}
public void Stop()
{
if (camera.CameraCfg.DebugLevel == DebugMode.Simulate) return;
if (grabImageDone)
{
string pathDir = $"{Camera.FtpCameraSource}{Camera.PrefixCameraFolder}{camera.CameraIdx}\\";
CameraUtils.ClearDir(pathDir);
}
}
}
}