add-添加日志及照片定时删除功能

master
liuwf 4 months ago
parent 8ccd59e5a3
commit 17eafa3366

@ -15,6 +15,7 @@ using System.Security.Cryptography.Xml;
using System.ServiceModel.Channels; using System.ServiceModel.Channels;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Timers;
using TouchSocket.Core; using TouchSocket.Core;
namespace SlnMesnac.Business namespace SlnMesnac.Business
@ -38,6 +39,8 @@ namespace SlnMesnac.Business
// 存放照片路径 // 存放照片路径
public static string PicturePath = System.Environment.CurrentDirectory + "/picture/"; public static string PicturePath = System.Environment.CurrentDirectory + "/picture/";
// 存放日志路径
public static string LogPath = System.Environment.CurrentDirectory + "/Logs/";
/// <summary> /// <summary>
/// 存储海康相机识别结果 /// 存储海康相机识别结果
@ -73,6 +76,10 @@ namespace SlnMesnac.Business
plc = _plcPool.GetPlcByKey("plc"); plc = _plcPool.GetPlcByKey("plc");
lightHelper.InstanceSerialPort(); lightHelper.InstanceSerialPort();
//定时清除日志及照片
CleanOldLogs(PicturePath);
CleanOldLogs(LogPath);
InitClearTimer();
} }
private void ReceiveCameraResult(string result) private void ReceiveCameraResult(string result)
@ -448,5 +455,64 @@ namespace SlnMesnac.Business
#endregion #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
} }
} }

@ -125,5 +125,15 @@ namespace SlnMesnac.Config
get { return iniHelper.IniReadValue("system", "Ch2"); } get { return iniHelper.IniReadValue("system", "Ch2"); }
set { iniHelper.IniWriteValue("system", "Ch2", value); } set { iniHelper.IniWriteValue("system", "Ch2", value); }
} }
/// <summary>
/// 照片及日志保存时间
/// </summary>
public string PictureSaveTime
{
get { return iniHelper.IniReadValue("system", "PictureSaveTime"); }
set { iniHelper.IniWriteValue("system", "PictureSaveTime", value); }
}
} }
} }

@ -233,7 +233,7 @@ namespace SlnMesnac.WPF.ViewModel
#endregion #endregion
#region 根据条码号准确查询或者根据型号加上时间段模糊查询 #region 根据条码号准确查询或者根据型号加上时间段与结果模糊查询
private async void QueryByCode() private async void QueryByCode()
{ {
@ -252,6 +252,15 @@ namespace SlnMesnac.WPF.ViewModel
list = await logoIdentifyService.QueryAllByTime(BeginDate, EndDate); list = await logoIdentifyService.QueryAllByTime(BeginDate, EndDate);
//过滤型号 //过滤型号
list = list.Where(x => x.ProductCode.Contains(QueryCode) || x.MaterialName.Contains(QueryCode) || x.MaterialType.Contains(QueryCode)).ToList(); list = list.Where(x => x.ProductCode.Contains(QueryCode) || x.MaterialName.Contains(QueryCode) || x.MaterialType.Contains(QueryCode)).ToList();
if (SelectedOption.Content.ToString() == "OK")
{
list = list.Where(x => x.Result == 1).ToList();
}
else if (SelectedOption.Content.ToString() == "NG")
{
list = list.Where(x => x.Result == 0).ToList();
}
} }
if (list != null && list.Count > 0) if (list != null && list.Count > 0)

Loading…
Cancel
Save