|
|
@@ -8,6 +8,7 @@ using YSAI.Unility;
|
|
|
using static YSAI.DaqManage.DaqManageData;
|
|
|
using YSAI.Core.@enum;
|
|
|
using Microsoft.Extensions.FileSystemGlobbing;
|
|
|
+using System.IO;
|
|
|
|
|
|
namespace YSAI.DaqManage
|
|
|
{
|
|
|
@@ -168,13 +169,10 @@ namespace YSAI.DaqManage
|
|
|
Search();
|
|
|
|
|
|
|
|
|
-
|
|
|
//文件夹监控
|
|
|
watcherLibFolder = new FileSystemWatcher(basics.LibFolder);
|
|
|
- //监控的配置
|
|
|
- watcherLibFolder.Filter = basics.DllWatcherFormat;
|
|
|
// 设置监控类型
|
|
|
- watcherLibFolder.NotifyFilter =
|
|
|
+ watcherLibFolder.NotifyFilter =
|
|
|
NotifyFilters.FileName | //文件的名称
|
|
|
NotifyFilters.DirectoryName | //目录名
|
|
|
NotifyFilters.Attributes | //文件或文件夹的属性
|
|
|
@@ -190,14 +188,13 @@ namespace YSAI.DaqManage
|
|
|
//启动监听
|
|
|
watcherLibFolder.EnableRaisingEvents = true;
|
|
|
|
|
|
-
|
|
|
|
|
|
//文件夹监视
|
|
|
watcherLibConfigFolder = new FileSystemWatcher(basics.LibConfigFolder);
|
|
|
//监控的配置
|
|
|
watcherLibConfigFolder.Filter = basics.ConfigWatcherFormat;
|
|
|
// 设置监控类型
|
|
|
- watcherLibConfigFolder.NotifyFilter =
|
|
|
+ watcherLibFolder.NotifyFilter =
|
|
|
NotifyFilters.FileName | //文件的名称
|
|
|
NotifyFilters.DirectoryName | //目录名
|
|
|
NotifyFilters.Attributes | //文件或文件夹的属性
|
|
|
@@ -220,77 +217,228 @@ namespace YSAI.DaqManage
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
/// <summary>
|
|
|
- /// 当文件夹中删除文件
|
|
|
+ /// 数据队列
|
|
|
/// </summary>
|
|
|
- /// <param name="sender"></param>
|
|
|
- /// <param name="e"></param>
|
|
|
- /// <param name="Type">【0:库文件】【1:配置文件】</param>
|
|
|
- private void Watcher_Deleted(object sender, FileSystemEventArgs e, int Type)
|
|
|
+ private ConcurrentQueue<WatcherData> WatcherQueue;
|
|
|
+ /// <summary>
|
|
|
+ /// 监控开关
|
|
|
+ /// </summary>
|
|
|
+ private CancellationTokenSource WatcherToken;
|
|
|
+ /// <summary>
|
|
|
+ /// 文件监控数据
|
|
|
+ /// </summary>
|
|
|
+ private class WatcherData
|
|
|
{
|
|
|
- //程序集ISn
|
|
|
- string TypeIocSN = string.Empty;
|
|
|
- switch (Type)
|
|
|
+ public FileSystemEventArgs e { get; set; }
|
|
|
+ public int Type { get; set; }
|
|
|
+ public WatcherType WType { get; set; }
|
|
|
+ public enum WatcherType
|
|
|
{
|
|
|
- case 0:
|
|
|
- //程序集ISn
|
|
|
- TypeIocSN = e.Name.Replace(".dll", string.Empty);
|
|
|
+ Deleted,
|
|
|
+ Created
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- foreach (var item in InstanceIoc)
|
|
|
- {
|
|
|
- if (item.Key.Contains(TypeIocSN))
|
|
|
- {
|
|
|
- InstanceIoc[item.Key].Dispose();
|
|
|
- if (InstanceIoc.Remove(item.Key, out _))
|
|
|
- {
|
|
|
- OnEventHandler(this, new EventResult(true, $"{e.Name} 移除配置实例 {item.Key} 成功"));
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- OnEventHandler(this, new EventResult(false, $"{e.Name} 移除配置实例 {item.Key} 失败"));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ /// <summary>
|
|
|
+ /// 监控任务
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public Task WatcherTask()
|
|
|
+ {
|
|
|
+ return Task.Factory.StartNew(() => {
|
|
|
+ while (!WatcherToken.IsCancellationRequested)
|
|
|
+ {
|
|
|
+ //队列数据
|
|
|
+ WatcherData? watcherData;
|
|
|
|
|
|
- foreach (var item in TypeIoc)
|
|
|
- {
|
|
|
- if (item.Key.Contains(TypeIocSN))
|
|
|
- {
|
|
|
- if (TypeIoc.Remove(item.Key, out _))
|
|
|
- {
|
|
|
- OnEventHandler(this, new EventResult(true, $"{e.Name} 移除程序集 {item.Key} 成功"));
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- OnEventHandler(this, new EventResult(false, $"{e.Name} 移除程序集 {item.Key} 失败"));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- case 1:
|
|
|
- OnEventHandler(this, new EventResult(true, $"{e.Name} 文件被删除,移除对应配置实例"));
|
|
|
- //实例的ISn
|
|
|
- string InstanceIocSN = e.Name.Replace(basics.ConfigReplaceFormat, string.Empty);
|
|
|
- //程序集ISn
|
|
|
- TypeIocSN = InstanceIocSN.Replace($".{InstanceIocSN.Split('.')[InstanceIocSN.Split('.').Length - 1]}", string.Empty);
|
|
|
- if (InstanceIoc.ContainsKey(InstanceIocSN))
|
|
|
+ while (WatcherQueue.TryDequeue(out watcherData))
|
|
|
{
|
|
|
- InstanceIoc[InstanceIocSN].Dispose();
|
|
|
- if (InstanceIoc.Remove(InstanceIocSN, out _))
|
|
|
- {
|
|
|
- OnEventHandler(this, new EventResult(true, $"{e.Name} 移除配置实例成功"));
|
|
|
- }
|
|
|
- else
|
|
|
+ Thread.Sleep(1000);
|
|
|
+
|
|
|
+ FileSystemEventArgs e = watcherData.e;
|
|
|
+ int Type = watcherData.Type;
|
|
|
+ //程序集ISn
|
|
|
+ string TypeIocSN = string.Empty;
|
|
|
+ switch (watcherData.WType)
|
|
|
{
|
|
|
- OnEventHandler(this, new EventResult(false, $"{e.Name} 移除配置实例失败"));
|
|
|
+ case WatcherData.WatcherType.Deleted:
|
|
|
+
|
|
|
+ switch (Type)
|
|
|
+ {
|
|
|
+ case 0:
|
|
|
+ //这是文件夹
|
|
|
+ if (Directory.Exists(e.FullPath))
|
|
|
+ {
|
|
|
+ //检索里面的文件
|
|
|
+ List<string> libs = Directory.GetFiles(e.FullPath, basics.DllWatcherFormat, SearchOption.AllDirectories).ToList();
|
|
|
+ //检索文件
|
|
|
+ foreach (var lib in libs)
|
|
|
+ {
|
|
|
+ //文件名称
|
|
|
+ string fileName = FileTool.GetFileName(lib);
|
|
|
+
|
|
|
+ //程序集ISn
|
|
|
+ TypeIocSN = fileName.Replace(".dll", string.Empty);
|
|
|
+
|
|
|
+ //检索实例容器
|
|
|
+ foreach (var item in InstanceIoc)
|
|
|
+ {
|
|
|
+ if (item.Key.Contains(TypeIocSN))
|
|
|
+ {
|
|
|
+ InstanceIoc[item.Key].Dispose();
|
|
|
+ if (InstanceIoc.Remove(item.Key, out _))
|
|
|
+ {
|
|
|
+ OnEventHandler(this, new EventResult(true, $"{e.Name} 移除配置实例 {item.Key} 成功"));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ OnEventHandler(this, new EventResult(false, $"{e.Name} 移除配置实例 {item.Key} 失败"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //检索库类型容器
|
|
|
+ foreach (var item in TypeIoc)
|
|
|
+ {
|
|
|
+ if (item.Key.Contains(TypeIocSN))
|
|
|
+ {
|
|
|
+ if (TypeIoc.Remove(item.Key, out _))
|
|
|
+ {
|
|
|
+ OnEventHandler(this, new EventResult(true, $"{e.Name} 移除程序集 {item.Key} 成功"));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ OnEventHandler(this, new EventResult(false, $"{e.Name} 移除程序集 {item.Key} 失败"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ OnEventHandler(this, new EventResult(true, $"{e.Name} 文件被删除,移除对应配置实例"));
|
|
|
+ //实例的ISn
|
|
|
+ string InstanceIocSN = e.Name.Replace(basics.ConfigReplaceFormat, string.Empty);
|
|
|
+ //程序集ISn
|
|
|
+ TypeIocSN = InstanceIocSN.Replace($".{InstanceIocSN.Split('.')[InstanceIocSN.Split('.').Length - 1]}", string.Empty);
|
|
|
+ if (InstanceIoc.ContainsKey(InstanceIocSN))
|
|
|
+ {
|
|
|
+ InstanceIoc[InstanceIocSN].Dispose();
|
|
|
+ if (InstanceIoc.Remove(InstanceIocSN, out _))
|
|
|
+ {
|
|
|
+ OnEventHandler(this, new EventResult(true, $"{e.Name} 移除配置实例成功"));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ OnEventHandler(this, new EventResult(false, $"{e.Name} 移除配置实例失败"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ OnEventHandler(this, new EventResult(false, $"{e.Name} 移除配置实例失败 {TypeIocSN} 实例不存在"));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+ case WatcherData.WatcherType.Created:
|
|
|
+
|
|
|
+
|
|
|
+ switch (Type)
|
|
|
+ {
|
|
|
+ case 0:
|
|
|
+ //这是文件夹
|
|
|
+ if (Directory.Exists(e.FullPath))
|
|
|
+ {
|
|
|
+ //检索里面的文件
|
|
|
+ Search(e.FullPath);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ OnEventHandler(this, new EventResult(true, $"{e.Name} 文件新增,新增对应配置实例"));
|
|
|
+ #region 新增对应配置实例
|
|
|
+ int until = 5;
|
|
|
+ int i = 0;
|
|
|
+ bool success = false;
|
|
|
+ while (!success && i < until)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ string path = String.Format(e.FullPath);
|
|
|
+ string? filename = Path.GetFileName(path);
|
|
|
+ using (Stream fs = System.IO.File.OpenRead(@path))
|
|
|
+ {
|
|
|
+ StreamReader srdPreview = new StreamReader(fs);
|
|
|
+ String temp = string.Empty;
|
|
|
+ while (srdPreview.Peek() > -1)
|
|
|
+ {
|
|
|
+ String input = srdPreview.ReadLine();
|
|
|
+ temp += input;
|
|
|
+ }
|
|
|
+ srdPreview.Close();
|
|
|
+ srdPreview.Dispose();
|
|
|
+
|
|
|
+ //实例的ISn
|
|
|
+ string InstanceIocSN = e.Name.Replace(basics.ConfigReplaceFormat, string.Empty);
|
|
|
+ //程序集ISn
|
|
|
+ TypeIocSN = InstanceIocSN.Replace($".{InstanceIocSN.Split('.')[InstanceIocSN.Split('.').Length - 1]}", string.Empty);
|
|
|
+ //判断是否存在此程序集ISn
|
|
|
+ if (TypeIoc.ContainsKey(TypeIocSN))
|
|
|
+ {
|
|
|
+ if (!InstanceIoc.ContainsKey(InstanceIocSN))
|
|
|
+ {
|
|
|
+ ConfigCreateInstance(TypeIoc[TypeIocSN], temp);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ OnEventHandler(this, new EventResult(false, $" {e.Name} 此配置实例已存在"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ OnEventHandler(this, new EventResult(false, $" {e.Name} 新增对应配置创建实例失败 {TypeIocSN} 程序集不存在"));
|
|
|
+ }
|
|
|
+ fs.Close();
|
|
|
+ fs.Dispose();
|
|
|
+ }
|
|
|
+ success = true;
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ i++;
|
|
|
+ Thread.Sleep(TimeSpan.FromSeconds(1));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- OnEventHandler(this, new EventResult(false, $"{e.Name} 移除配置实例失败 {TypeIocSN} 实例不存在"));
|
|
|
- }
|
|
|
- break;
|
|
|
+ Thread.Sleep(1000);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 当文件夹中删除文件
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="sender"></param>
|
|
|
+ /// <param name="e"></param>
|
|
|
+ /// <param name="Type">【0:库文件】【1:配置文件】</param>
|
|
|
+ private void Watcher_Deleted(object sender, FileSystemEventArgs e, int Type)
|
|
|
+ {
|
|
|
+ if (WatcherToken == null && WatcherQueue == null)
|
|
|
+ {
|
|
|
+ WatcherQueue = new ConcurrentQueue<WatcherData>();
|
|
|
+ WatcherToken = new CancellationTokenSource();
|
|
|
+ WatcherTask();
|
|
|
}
|
|
|
+
|
|
|
+ WatcherQueue.Enqueue(new WatcherData { e = e, Type = Type, WType = WatcherData.WatcherType.Deleted });
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -301,80 +449,29 @@ namespace YSAI.DaqManage
|
|
|
/// <param name="Type">【0:库文件】【1:配置文件】</param>
|
|
|
private void Watcher_Created(object sender, FileSystemEventArgs e, int Type)
|
|
|
{
|
|
|
- switch (Type)
|
|
|
+ if (WatcherToken == null && WatcherQueue == null)
|
|
|
{
|
|
|
- case 0:
|
|
|
- SearchType(e.FullPath);
|
|
|
- break;
|
|
|
- case 1:
|
|
|
- OnEventHandler(this, new EventResult(true, $"{e.Name} 文件新增,新增对应配置实例"));
|
|
|
- #region 新增对应配置实例
|
|
|
- int until = 5;
|
|
|
- int i = 0;
|
|
|
- bool success = false;
|
|
|
- while (!success && i < until)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- string path = String.Format(e.FullPath);
|
|
|
- string? filename = Path.GetFileName(path);
|
|
|
- using (Stream fs = System.IO.File.OpenRead(@path))
|
|
|
- {
|
|
|
- StreamReader srdPreview = new StreamReader(fs);
|
|
|
- String temp = string.Empty;
|
|
|
- while (srdPreview.Peek() > -1)
|
|
|
- {
|
|
|
- String input = srdPreview.ReadLine();
|
|
|
- temp += input;
|
|
|
- }
|
|
|
- srdPreview.Close();
|
|
|
- srdPreview.Dispose();
|
|
|
-
|
|
|
- //实例的ISn
|
|
|
- string InstanceIocSN = e.Name.Replace(basics.ConfigReplaceFormat, string.Empty);
|
|
|
- //程序集ISn
|
|
|
- string TypeIocSN = InstanceIocSN.Replace($".{InstanceIocSN.Split('.')[InstanceIocSN.Split('.').Length - 1]}", string.Empty);
|
|
|
- //判断是否存在此程序集ISn
|
|
|
- if (TypeIoc.ContainsKey(TypeIocSN))
|
|
|
- {
|
|
|
- if (!InstanceIoc.ContainsKey(InstanceIocSN))
|
|
|
- {
|
|
|
- ConfigCreateInstance(TypeIoc[TypeIocSN], temp);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- OnEventHandler(this, new EventResult(false, $" {e.Name} 此配置实例已存在"));
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- OnEventHandler(this, new EventResult(false, $" {e.Name} 新增对应配置创建实例失败 {TypeIocSN} 程序集不存在"));
|
|
|
- }
|
|
|
- fs.Close();
|
|
|
- fs.Dispose();
|
|
|
- }
|
|
|
- success = true;
|
|
|
- }
|
|
|
- catch
|
|
|
- {
|
|
|
- i++;
|
|
|
- Thread.Sleep(TimeSpan.FromSeconds(1));
|
|
|
- }
|
|
|
- }
|
|
|
- #endregion
|
|
|
- break;
|
|
|
+ WatcherQueue = new ConcurrentQueue<WatcherData>();
|
|
|
+ WatcherToken = new CancellationTokenSource();
|
|
|
+ WatcherTask();
|
|
|
}
|
|
|
+
|
|
|
+ WatcherQueue.Enqueue(new WatcherData { e = e, Type = Type, WType = WatcherData.WatcherType.Created });
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 检索文件并创建实例
|
|
|
/// </summary>
|
|
|
- private void Search()
|
|
|
+ private void Search(string? path = null)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
+ if (string.IsNullOrWhiteSpace(path))
|
|
|
+ {
|
|
|
+ path = basics.LibFolder;
|
|
|
+ }
|
|
|
//库
|
|
|
- List<string> libs = Directory.GetFiles(basics.LibFolder, "YSAI.*.dll", SearchOption.AllDirectories).ToList();
|
|
|
+ List<string> libs = Directory.GetFiles(path, basics.DllWatcherFormat, SearchOption.AllDirectories).ToList();
|
|
|
//循环文件,添加程序集
|
|
|
foreach (var lib in libs)
|
|
|
{
|
|
|
@@ -1034,11 +1131,11 @@ namespace YSAI.DaqManage
|
|
|
return Write(pairs, ISn);
|
|
|
}
|
|
|
/// <summary>
|
|
|
- /// 设置库文件
|
|
|
+ /// 设置库文件ZIP
|
|
|
/// </summary>
|
|
|
/// <param name="FormFiles">文件集合</param>
|
|
|
/// <returns>统一出参</returns>
|
|
|
- public OperateResult SettingLib(List<IFormFile> FormFiles)
|
|
|
+ public OperateResult SettingLibZip(List<IFormFile> FormFiles)
|
|
|
{
|
|
|
Depart("SettingLib");
|
|
|
try
|