97 lines
3.4 KiB
C#
97 lines
3.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using log4net;
|
|
|
|
namespace TBF.Rig.Network.Camera.KeyenceIV3G120
|
|
{
|
|
public class CameraUtils
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(KeyenceIV3G120.CameraUtils));
|
|
|
|
public static string GetLastCreationTimeCandidate(string[] picCandidates)
|
|
{
|
|
var creationTimes = picCandidates.Select(path => new FileInfo(path).CreationTime).ToArray();
|
|
|
|
// Find the index of the latest creation time
|
|
var latestIndex = Array.IndexOf(creationTimes, creationTimes.Max());
|
|
|
|
// Get the path of the file with the latest creation time
|
|
return picCandidates[latestIndex];
|
|
}
|
|
|
|
public static void WaitForImageCompleteOnDisk(string picturePath)
|
|
{
|
|
//TODO finish
|
|
//we start this part in moment we found some file in disk
|
|
//wait for finish write image on disk - FTP is slow
|
|
if (File.Exists(picturePath))
|
|
{
|
|
try
|
|
{
|
|
for (int iIteration = 0;iIteration < 10;iIteration++) // wait max 2 sec - otherwise there may be a problem
|
|
{
|
|
Thread.Sleep(200);
|
|
try
|
|
{
|
|
using (FileStream fs = File.Open(picturePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
catch (IOException ex)
|
|
{
|
|
// Handle the case when the file is not ready or cannot be accessed
|
|
log.Error("No accessing rihts! We will wait next iteration!");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
log.Error("Error to wait for writing file to disk. Detail: " + e.StackTrace);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public void CallTrigger()
|
|
{
|
|
//TODO call initialisation signal - valve open
|
|
}
|
|
|
|
public static void ClearDir(string path)
|
|
{
|
|
log.Debug($"ClearDir({path}) called!");
|
|
string[] filesToDelete = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly);
|
|
ClearDir(filesToDelete);
|
|
}
|
|
|
|
public static void ClearDir(string[] files)
|
|
{
|
|
foreach (string file in files)
|
|
{
|
|
if (File.Exists(file))
|
|
{
|
|
try
|
|
{
|
|
// Check if user has write permission for the file
|
|
if ((File.GetAttributes(file) & FileAttributes.ReadOnly) == 0)
|
|
{
|
|
File.Delete(file);
|
|
log.Debug($"Deleted: {file}");
|
|
}
|
|
else
|
|
{
|
|
log.Debug($"Insufficient permissions to delete: {file}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Debug($"Error deleting {file}: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |