|
|
|
@ -15,6 +15,7 @@ using System.Security.Cryptography.Xml;
|
|
|
|
|
using System.ServiceModel.Channels;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Timers;
|
|
|
|
|
using TouchSocket.Core;
|
|
|
|
|
|
|
|
|
|
namespace SlnMesnac.Business
|
|
|
|
@ -38,6 +39,8 @@ namespace SlnMesnac.Business
|
|
|
|
|
|
|
|
|
|
// 存放照片路径
|
|
|
|
|
public static string PicturePath = System.Environment.CurrentDirectory + "/picture/";
|
|
|
|
|
// 存放日志路径
|
|
|
|
|
public static string LogPath = System.Environment.CurrentDirectory + "/Logs/";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 存储海康相机识别结果
|
|
|
|
@ -73,6 +76,10 @@ namespace SlnMesnac.Business
|
|
|
|
|
plc = _plcPool.GetPlcByKey("plc");
|
|
|
|
|
lightHelper.InstanceSerialPort();
|
|
|
|
|
|
|
|
|
|
//定时清除日志及照片
|
|
|
|
|
CleanOldLogs(PicturePath);
|
|
|
|
|
CleanOldLogs(LogPath);
|
|
|
|
|
InitClearTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ReceiveCameraResult(string result)
|
|
|
|
@ -448,5 +455,64 @@ namespace SlnMesnac.Business
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 定时清除照片及日志
|
|
|
|
|
private void InitClearTimer()
|
|
|
|
|
{
|
|
|
|
|
System.Timers.Timer timer = new System.Timers.Timer
|
|
|
|
|
{
|
|
|
|
|
Interval = TimeSpan.FromDays(1).TotalMilliseconds, // 每天执行一次
|
|
|
|
|
AutoReset = true
|
|
|
|
|
};
|
|
|
|
|
timer.Elapsed += OnTimedEvent;
|
|
|
|
|
timer.Start();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnTimedEvent(object source, ElapsedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
CleanOldLogs(PicturePath);
|
|
|
|
|
CleanOldLogs(LogPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void CleanOldLogs(string Path)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int saveDays = int.Parse(config.PictureSaveTime);
|
|
|
|
|
var directoryInfo = new DirectoryInfo(Path);
|
|
|
|
|
if (!directoryInfo.Exists)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"日志文件夹 {Path} 不存在.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cutoffDate = DateTime.Now.AddDays(-saveDays);
|
|
|
|
|
|
|
|
|
|
var directories = directoryInfo.GetDirectories();
|
|
|
|
|
foreach (var directory in directories)
|
|
|
|
|
{
|
|
|
|
|
// 解析文件夹名称
|
|
|
|
|
if (DateTime.TryParseExact(directory.Name, "yyyy-MM-dd", null, System.Globalization.DateTimeStyles.None, out DateTime folderDate))
|
|
|
|
|
{
|
|
|
|
|
if (folderDate < cutoffDate)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"删除过期日志文件夹: {directory.FullName}");
|
|
|
|
|
directory.Delete(true); // 删除文件夹及其内容
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"文件夹名称不符合日期格式: {directory.Name}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"清理日志时发生错误: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|