!提交代码
This commit is contained in:
73
货架标准上位机/Tool/Folder.cs
Normal file
73
货架标准上位机/Tool/Folder.cs
Normal file
@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace 货架标准上位机
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件夹操作
|
||||
/// </summary>
|
||||
public static class Folder
|
||||
{
|
||||
/// <summary>
|
||||
/// 打开目录
|
||||
/// </summary>
|
||||
/// <param name="folderPath">目录路径(比如:C:\Users\Administrator\)</param>
|
||||
public static void OpenFolder(string folderPath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(folderPath))
|
||||
return;
|
||||
|
||||
Process process = new Process();
|
||||
ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
|
||||
psi.Arguments = folderPath;
|
||||
process.StartInfo = psi;
|
||||
|
||||
try
|
||||
{
|
||||
process.Start();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
process?.Close();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打开目录且选中文件
|
||||
/// </summary>
|
||||
/// <param name="filePathAndName">文件的路径和名称(比如:C:\Users\Administrator\test.txt)</param>
|
||||
public static void OpenFolderAndSelectedFile(string filePathAndName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filePathAndName))
|
||||
return;
|
||||
|
||||
Process process = new Process();
|
||||
ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
|
||||
psi.Arguments = "/e,/select," + filePathAndName;
|
||||
process.StartInfo = psi;
|
||||
|
||||
//process.StartInfo.UseShellExecute = true;
|
||||
try
|
||||
{
|
||||
process.Start();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
finally
|
||||
{
|
||||
process?.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
31
货架标准上位机/Tool/GetBaseData.cs
Normal file
31
货架标准上位机/Tool/GetBaseData.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Documents;
|
||||
using WCS.Model;
|
||||
using WCS.Model.ApiModel.Home;
|
||||
using 货架标准上位机.Api;
|
||||
|
||||
namespace 货架标准上位机.Tool
|
||||
{
|
||||
public static class GetBaseData
|
||||
{
|
||||
public static List<ShelfTypeModel> GetShelfType()
|
||||
{
|
||||
var body = new RequestBase()
|
||||
{
|
||||
UserName = LocalStatic.CurrentUser,
|
||||
DeviceType = LocalFile.Config.DeviceType,
|
||||
};
|
||||
|
||||
var Result = ApiHelp.GetDataFromHttp<PageQueryResponse<ShelfTypeModel>>(LocalFile.Config.ApiIpHost + "home/getShelfTypes", body, "POST");
|
||||
if (Result != null && Result.Data != null && Result.Data.Lists.Count() > 0)
|
||||
{
|
||||
return Result.Data.Lists;
|
||||
}
|
||||
else { return new List<ShelfTypeModel>(); }
|
||||
}
|
||||
}
|
||||
}
|
38
货架标准上位机/Tool/JsonConverter.cs
Normal file
38
货架标准上位机/Tool/JsonConverter.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace 货架标准上位机
|
||||
{
|
||||
/// <summary>
|
||||
/// json时间转换器
|
||||
/// </summary>
|
||||
public class JsonDateTimeConverter : JsonConverter<DateTime?>
|
||||
{
|
||||
public override DateTime? ReadJson(JsonReader reader, Type objectType, DateTime? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
var dateTimeString = reader.Value?.ToString();
|
||||
if (!string.IsNullOrEmpty(dateTimeString))
|
||||
{
|
||||
try
|
||||
{
|
||||
return DateTime.ParseExact(dateTimeString, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
throw new JsonSerializationException("Invalid date time format.");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, DateTime? value, JsonSerializer serializer)
|
||||
{
|
||||
writer.WriteValue(value?.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
||||
}
|
||||
}
|
164
货架标准上位机/Tool/Logs.cs
Normal file
164
货架标准上位机/Tool/Logs.cs
Normal file
@ -0,0 +1,164 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WCS.Model;
|
||||
using WCS.Model.ApiModel;
|
||||
|
||||
namespace 货架标准上位机
|
||||
{
|
||||
/// <summary>
|
||||
/// 日志类型
|
||||
/// </summary>
|
||||
public enum LogsType
|
||||
{
|
||||
/// <summary>
|
||||
/// 信息
|
||||
/// </summary>
|
||||
Info,
|
||||
/// <summary>
|
||||
/// 警告
|
||||
/// </summary>
|
||||
Warning,
|
||||
/// <summary>
|
||||
/// 错误
|
||||
/// </summary>
|
||||
Err,
|
||||
/// <summary>
|
||||
/// 数据库错误
|
||||
/// </summary>
|
||||
DbErr,
|
||||
/// <summary>
|
||||
/// 扫码枪
|
||||
/// </summary>
|
||||
Scanner
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 日志
|
||||
/// </summary>
|
||||
public static class Logs
|
||||
{
|
||||
static object obj = new object();
|
||||
const string logExtension = ".log";
|
||||
/// <summary>
|
||||
/// 写入日志失败
|
||||
/// </summary>
|
||||
public static Action<IEnumerable<string>, DateTime, LogsType, Exception> WriteErr = null;
|
||||
|
||||
/// <summary>
|
||||
/// 写入日志
|
||||
/// </summary>
|
||||
/// <param name="content">内容</param>
|
||||
/// <param name="type">类型</param>
|
||||
/// <returns>是否写入成功</returns>
|
||||
public static void Write(IEnumerable<string> content, LogsType type = LogsType.Info, DateTime? dt = null)
|
||||
{
|
||||
if (content == null || !content.Any())
|
||||
return;
|
||||
|
||||
dt ??= DateTime.Now;
|
||||
string hms = dt.Value.ToString("HH:mm:ss.fff");
|
||||
List<string> lines = new List<string>(content.Count());
|
||||
|
||||
try
|
||||
{
|
||||
string path = Path.Combine(LocalFile.LogDir, type.ToString(), $"{(dt.Value.ToString("yyyyMMdd"))}{logExtension}");
|
||||
foreach (var item in content)
|
||||
lines.AddRange($"[{hms}]{item}".Split(new string[] { Environment.NewLine }, StringSplitOptions.None));
|
||||
|
||||
lock (obj)
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
||||
File.AppendAllLines(path, lines, Encoding.UTF8);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
WriteErr?.Invoke(content, dt.Value, type, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入日志
|
||||
/// </summary>
|
||||
/// <param name="content">内容对象</param>
|
||||
/// <param name="type">类型</param>
|
||||
/// <returns>是否写入成功</returns>
|
||||
public static void Write(string content, LogsType type = LogsType.Info, DateTime? dt = null)
|
||||
{
|
||||
Write(new string[] { content }, type, dt);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入日志
|
||||
/// </summary>
|
||||
/// <param name="content">内容对象</param>
|
||||
/// <param name="type">类型</param>
|
||||
/// <returns>是否写入成功</returns>
|
||||
public static void Write(object content, LogsType type = LogsType.Info, DateTime? dt = null)
|
||||
{
|
||||
Write(JsonConvert.SerializeObject(content), type, dt);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入日志
|
||||
/// </summary>
|
||||
/// <param name="content">内容</param>
|
||||
/// <param name="contentTitle">内容标题</param>
|
||||
/// <param name="type">类型</param>
|
||||
/// <returns>是否写入成功</returns>
|
||||
public static void Write(object content, string contentTitle, LogsType type = LogsType.Info, DateTime? dt = null)
|
||||
{
|
||||
Write($"{contentTitle} {JsonConvert.SerializeObject(content)}", type, dt);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入日志
|
||||
/// </summary>
|
||||
/// <param name="ex">错误</param>
|
||||
/// <returns>是否写入成功</returns>
|
||||
public static void Write(Exception ex, LogsType type = LogsType.Err, DateTime? dt = null)
|
||||
{
|
||||
Write(ex.ToString(), type, dt);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除日志
|
||||
/// </summary>
|
||||
/// <param name="time">保留时间</param>
|
||||
/// <returns>清理的大小(字节)</returns>
|
||||
public static long Clear(TimeSpan time)
|
||||
{
|
||||
long size = 0;
|
||||
var rs = EnumHelps.GetEnumList(typeof(LogsType));
|
||||
foreach (var item in rs)
|
||||
{
|
||||
try
|
||||
{
|
||||
var path = Path.Combine(LocalFile.LogDir, item);
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(path);
|
||||
if (!directoryInfo.Exists)
|
||||
continue;
|
||||
|
||||
var files = directoryInfo.GetFiles($"**{logExtension}", SearchOption.TopDirectoryOnly);
|
||||
var fileDel = files.Where(o => o.CreationTime < (DateTime.Now - time));
|
||||
foreach (var item2 in fileDel)
|
||||
{
|
||||
size += item2.Length;
|
||||
item2.Delete();
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
||||
}
|
||||
}
|
93
货架标准上位机/Tool/PrintTender.cs
Normal file
93
货架标准上位机/Tool/PrintTender.cs
Normal file
@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace 货架标准上位机.Tool
|
||||
{
|
||||
public static class PrintTender
|
||||
{
|
||||
public static BarTender.Application Inbtapp;
|
||||
static BarTender.Format InbtFormat;
|
||||
|
||||
//TO DO 打印机型号需要可配置
|
||||
static string PKNmae = "TSC TTP-244 Pro";
|
||||
|
||||
static object lockll = new object();
|
||||
static object lockPrint = new object();
|
||||
|
||||
//记录日志
|
||||
public static void WriteOneLine(string msg)//写到本地记录
|
||||
{
|
||||
lock (lockPrint)
|
||||
{
|
||||
try
|
||||
{
|
||||
Logs.Write($"{msg}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打印外箱条码
|
||||
/// PKGip 打印机地址
|
||||
/// PKNmae 打印机名字
|
||||
/// </summary>
|
||||
/// <param name="PKGip"></param>
|
||||
/// <param name="test"></param>
|
||||
public static bool PrintTag(PrintClass printtext)
|
||||
{
|
||||
lock (lockll)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Inbtapp == null)
|
||||
{
|
||||
Inbtapp = new BarTender.Application();
|
||||
}
|
||||
WriteOneLine($"标签开始打印");
|
||||
InbtFormat = Inbtapp.Formats.Open(System.Environment.CurrentDirectory + "\\Resources\\物料条码.btw", false, "");
|
||||
InbtFormat.PrintSetup.Printer = PKNmae;
|
||||
InbtFormat.PrintSetup.IdenticalCopiesOfLabel = 1; //设置同序列打印的份数
|
||||
InbtFormat.SetNamedSubStringValue("MatCode", printtext.MatCode);
|
||||
InbtFormat.SetNamedSubStringValue("MatName", printtext.MatName);
|
||||
InbtFormat.SetNamedSubStringValue("MatBatch", "批次:" + printtext.MatBatch);
|
||||
InbtFormat.SetNamedSubStringValue("MatQty", "数量:" + printtext.MatQty);
|
||||
InbtFormat.SetNamedSubStringValue("MatSn", printtext.MatSn);
|
||||
InbtFormat.SetNamedSubStringValue("MatSpec", "规格:" + printtext.MatSpec);
|
||||
InbtFormat.SetNamedSubStringValue("DateTime", DateTime.Now.ToString());
|
||||
|
||||
Thread.Sleep(20);
|
||||
int result = InbtFormat.PrintOut(true, false); //第二个false设置打印时是否跳出打印属性
|
||||
InbtFormat.Close(BarTender.BtSaveOptions.btSaveChanges); //退出时是否保存标签
|
||||
DateTime datenow = DateTime.Now;
|
||||
Thread.Sleep(200);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
WriteOneLine("打标签失败" + ex.ToString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class PrintClass
|
||||
{
|
||||
public string MatQty { get; set; }
|
||||
public string MatBatch { get; set; }
|
||||
public string MatCode { get; set; }
|
||||
public string MatName { get; set; }
|
||||
public string MatSn { get; set; }
|
||||
public string MatSpec { get; set; }
|
||||
}
|
||||
|
||||
}
|
124
货架标准上位机/Tool/RichTextBoxTool.cs
Normal file
124
货架标准上位机/Tool/RichTextBoxTool.cs
Normal file
@ -0,0 +1,124 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace 货架标准上位机
|
||||
{
|
||||
/// <summary>
|
||||
/// 富文本框扩展类
|
||||
/// </summary>
|
||||
public static class RichTextBoxTool
|
||||
{
|
||||
/// <summary>
|
||||
/// 清空内容
|
||||
/// </summary>
|
||||
public static void ClearText(this RichTextBox richTextBox)
|
||||
{
|
||||
try
|
||||
{
|
||||
richTextBox.Dispatcher.Invoke((Action)delegate
|
||||
{
|
||||
//清空文本
|
||||
richTextBox.Document.Blocks.Clear();
|
||||
//滚动到开头
|
||||
richTextBox.ScrollToVerticalOffset(0);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入文本
|
||||
/// </summary>
|
||||
/// <param name="richTextBox"></param>
|
||||
/// <param name="brush">颜色</param>
|
||||
/// <param name="txt">内容</param>
|
||||
private static void AppendText(this RichTextBox richTextBox, Brush brush, string txt)
|
||||
{
|
||||
try
|
||||
{
|
||||
richTextBox.Dispatcher.Invoke((Action)delegate
|
||||
{
|
||||
var p = new Paragraph(); //Paragraph 类似于 html 的 P 标签
|
||||
var r = new Run(txt); //Run 是一个 Inline 的标签
|
||||
p.Inlines.Add(r);
|
||||
p.LineHeight = 8;
|
||||
p.Foreground = brush;//设置字体颜色
|
||||
richTextBox.Document.Blocks.Add(p);
|
||||
if (richTextBox.Document.Blocks.Count > 1000)
|
||||
{
|
||||
richTextBox.Document.Blocks.Remove(richTextBox.Document.Blocks.FirstBlock);
|
||||
}
|
||||
//滚动到末尾
|
||||
richTextBox.ScrollToVerticalOffset(richTextBox.Document.Blocks.Count * 200);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入Title(棕色),为:######Title######
|
||||
/// </summary>
|
||||
public static void AppendTile(this RichTextBox richTextBox, string txt)
|
||||
{
|
||||
AppendText(richTextBox, Brushes.Brown, "#####" + txt + "#####");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入信息(蓝色)
|
||||
/// </summary>
|
||||
/// <param name="addTime">是否在开头追加时间信息,如:HH:mm:ss.fff=>txt</param>
|
||||
public static void AppendInfo(this RichTextBox richTextBox, string txt, bool addTime = false)
|
||||
{
|
||||
if (string.IsNullOrEmpty(txt))
|
||||
return;
|
||||
|
||||
if (addTime)
|
||||
AppendText(richTextBox, Brushes.Blue, $"{DateTime.Now.ToString("HH:mm:ss.fff")}=>{txt.Replace("\r", "\\r").Replace("\n", "\\n")}");
|
||||
else
|
||||
AppendText(richTextBox, Brushes.Blue, txt.Replace("\r", "\\r").Replace("\n", "\\n"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入信息(深绿色)
|
||||
/// </summary>
|
||||
/// <param name="addTime">是否在开头追加时间信息,如:HH:mm:ss.fff=>txt</param>
|
||||
public static void AppendResult(this RichTextBox richTextBox, string txt, bool addTime = false)
|
||||
{
|
||||
if (string.IsNullOrEmpty(txt))
|
||||
return;
|
||||
|
||||
if (addTime)
|
||||
AppendText(richTextBox, Brushes.Blue, $"{DateTime.Now.ToString("HH:mm:ss.fff")}=>{txt}");
|
||||
else
|
||||
AppendText(richTextBox, Brushes.ForestGreen, txt);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入错误(鲜红色)
|
||||
/// </summary>
|
||||
/// <param name="addTime">是否在开头追加时间信息,如:HH:mm:ss.fff=>txt</param>
|
||||
public static void AppendErr(this RichTextBox richTextBox, string txt, bool addTime = false)
|
||||
{
|
||||
if (string.IsNullOrEmpty(txt))
|
||||
return;
|
||||
|
||||
if (addTime)
|
||||
AppendText(richTextBox, Brushes.Blue, $"{DateTime.Now.ToString("HH:mm:ss.fff")}=>{txt}");
|
||||
else
|
||||
AppendText(richTextBox, Brushes.Tomato, txt);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
268
货架标准上位机/Tool/WarnInfoContainer.cs
Normal file
268
货架标准上位机/Tool/WarnInfoContainer.cs
Normal file
@ -0,0 +1,268 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace 货架标准上位机
|
||||
{
|
||||
/// <summary>
|
||||
/// 警告信息容器
|
||||
/// </summary>
|
||||
public class WarnInfoContainer
|
||||
{
|
||||
ConcurrentDictionary<WarnInfoItem, string> InfoAll = new ConcurrentDictionary<WarnInfoItem, string>();
|
||||
|
||||
/// <summary>
|
||||
/// 有新的日志加入或移除时,1,当前全部,2,加入的,3移除的
|
||||
/// </summary>
|
||||
public Action<IEnumerable<WarnInfoItem>, IEnumerable<WarnInfoItem>, IEnumerable<WarnInfoItem>> WarnInfoChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 添加或者更新信息
|
||||
/// </summary>
|
||||
public void AddOrUpdate(IEnumerable<WarnInfoItem> warnInfos)
|
||||
{
|
||||
if (warnInfos == null || !warnInfos.Any())
|
||||
return;
|
||||
|
||||
List<WarnInfoItem> adds = new List<WarnInfoItem>();
|
||||
List<WarnInfoItem> cles = new List<WarnInfoItem>();
|
||||
var info1 = warnInfos.Where(o => o.WarnType == WarnInfoType.WhileWarn).GroupBy(o => o.Source);
|
||||
var info2 = warnInfos.Where(o => o.WarnType == WarnInfoType.AlwayWarn);
|
||||
DateTime dt = DateTime.Now;
|
||||
|
||||
foreach (var addText in info1)
|
||||
{
|
||||
var yuan = InfoAll.Keys.Where(o => o.WarnType == WarnInfoType.WhileWarn && o.Source == addText.Key);
|
||||
var uni = yuan.Intersect(addText);
|
||||
var add = addText.Except(yuan).Except(uni);
|
||||
var cle = yuan.Except(addText).Except(uni);
|
||||
adds.AddRange(add);
|
||||
cles.AddRange(cle);
|
||||
|
||||
foreach (var item in cle)
|
||||
{
|
||||
item.TimeTo = item.TimeTo.HasValue ? item.TimeTo : dt;
|
||||
if (!InfoAll.TryRemove(item, out string val))
|
||||
InfoAll.TryRemove(item, out string val1);
|
||||
}
|
||||
foreach (var item in add)
|
||||
{
|
||||
item.Id = Guid.NewGuid().ToString("N");
|
||||
item.TimeGo = item.TimeGo == new DateTime() ? dt : item.TimeGo;
|
||||
if (!InfoAll.TryAdd(item, null))
|
||||
InfoAll.TryAdd(item, null);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var exc = info2.Except(InfoAll.Keys);
|
||||
foreach (var item in exc)
|
||||
{
|
||||
item.Id = Guid.NewGuid().ToString("N");
|
||||
item.TimeGo = item.TimeGo == new DateTime() ? dt : item.TimeGo;
|
||||
if (!InfoAll.TryAdd(item, null))
|
||||
InfoAll.TryAdd(item, null);
|
||||
adds.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
if (adds.Any() || cles.Any())
|
||||
WarnInfoChanged?.BeginInvoke(InfoAll.Keys.ToArray(), adds, cles, null, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新循环警告
|
||||
/// </summary>
|
||||
public void UpdateWhileWarn(IEnumerable<string> texts)
|
||||
{
|
||||
UpdateWhileWarn(string.Empty, texts);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新循环警告
|
||||
/// </summary>
|
||||
public void UpdateWhileWarn(string source, IEnumerable<string> texts)
|
||||
{
|
||||
DateTime dt = DateTime.Now;
|
||||
AddOrUpdate(texts.Select(o => new WarnInfoItem()
|
||||
{
|
||||
Source = source,
|
||||
Text = o,
|
||||
WarnType = WarnInfoType.WhileWarn,
|
||||
TimeGo = dt,
|
||||
TimeTo = null
|
||||
}));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加常驻警告
|
||||
/// </summary>
|
||||
public void AddAlwayWarn(string text)
|
||||
{
|
||||
AddAlwayWarn(string.Empty, new string[] { text });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加常驻警告
|
||||
/// </summary>
|
||||
public void AddAlwayWarn(IEnumerable<string> texts)
|
||||
{
|
||||
AddAlwayWarn(string.Empty, texts);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加常驻警告
|
||||
/// </summary>>
|
||||
public void AddAlwayWarn(string source, string text)
|
||||
{
|
||||
AddAlwayWarn(source, new string[] { text });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加常驻警告
|
||||
/// </summary>>
|
||||
public void AddAlwayWarn(string source, IEnumerable<string> texts)
|
||||
{
|
||||
DateTime dt = DateTime.Now;
|
||||
AddOrUpdate(texts.Select(o => new WarnInfoItem()
|
||||
{
|
||||
Source = source,
|
||||
Text = o,
|
||||
WarnType = WarnInfoType.AlwayWarn,
|
||||
TimeGo = dt,
|
||||
TimeTo = null
|
||||
}));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除警告信息
|
||||
/// </summary>
|
||||
public void RemoveAll()
|
||||
{
|
||||
RemoveAll(null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除警告信息
|
||||
/// </summary>
|
||||
public void RemoveAll(WarnInfoType errType)
|
||||
{
|
||||
RemoveAll(null, errType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除警告信息
|
||||
/// </summary>
|
||||
public void RemoveAll(string source, WarnInfoType? errType = null)
|
||||
{
|
||||
if (InfoAll.IsEmpty)
|
||||
return;
|
||||
|
||||
var clear = InfoAll.Keys
|
||||
.Where(o => (source == null ? true : o.Source == source) && (errType.HasValue ? o.WarnType == errType.Value : true));
|
||||
|
||||
if (clear == null || !clear.Any())
|
||||
return;
|
||||
|
||||
var dt = DateTime.Now;
|
||||
foreach (var item in clear)
|
||||
{
|
||||
item.TimeTo = item.TimeTo.HasValue ? item.TimeTo : dt;
|
||||
if (!InfoAll.TryRemove(item, out string val))
|
||||
InfoAll.TryRemove(item, out string val1);
|
||||
}
|
||||
|
||||
WarnInfoChanged?.BeginInvoke(InfoAll.Keys, new List<WarnInfoItem>(), clear, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 警告信息
|
||||
/// </summary>
|
||||
public class WarnInfoItem
|
||||
{
|
||||
/// <summary>
|
||||
/// 唯一编码
|
||||
/// </summary>
|
||||
public string Id { get; set; }
|
||||
/// <summary>
|
||||
/// 来源
|
||||
/// </summary>
|
||||
public string Source { get; set; }
|
||||
/// <summary>
|
||||
/// 文本信息
|
||||
/// </summary>
|
||||
public string Text { get; set; }
|
||||
/// <summary>
|
||||
/// 级别(提示、警告、错误、致命)
|
||||
/// </summary>
|
||||
public string Level { get; set; }
|
||||
/// <summary>
|
||||
/// 解决方案
|
||||
/// </summary>
|
||||
public string Solution { get; set; }
|
||||
/// <summary>
|
||||
/// 警告类型
|
||||
/// </summary>
|
||||
public WarnInfoType WarnType { get; set; }
|
||||
/// <summary>
|
||||
/// 开始时间
|
||||
/// </summary>
|
||||
public DateTime TimeGo { get; set; }
|
||||
/// <summary>
|
||||
/// 结束时间
|
||||
/// </summary>
|
||||
public DateTime? TimeTo { get; set; }
|
||||
/// <summary>
|
||||
/// 自定义信息
|
||||
/// </summary>
|
||||
public object Tag { get; set; }
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is WarnInfoItem b)
|
||||
return (Source == b.Source && Text == b.Text && Level == b.Level && Solution == b.Solution && WarnType == b.WarnType);
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return (Source + Text + Level + Solution + WarnType.ToString()).GetHashCode();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{(string.IsNullOrEmpty(Source) ? "" : $"[{Source}]")}{(string.IsNullOrEmpty(Level) ? "" : $"[{Level}]")}{Text}{(string.IsNullOrEmpty(Solution) ? "" : $"({Solution})")}{(TimeTo.HasValue ? $"({(TimeTo.Value - TimeGo).TotalSeconds.ToString("0.00")}s)" : "")}";
|
||||
}
|
||||
|
||||
public static bool operator ==(WarnInfoItem a, WarnInfoItem b)
|
||||
{
|
||||
return a.Equals(b);
|
||||
}
|
||||
|
||||
public static bool operator !=(WarnInfoItem a, WarnInfoItem b)
|
||||
{
|
||||
return !a.Equals(b);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 警告信息类型
|
||||
/// </summary>
|
||||
public enum WarnInfoType
|
||||
{
|
||||
/// <summary>
|
||||
/// 常驻错误。加入一次需要手动清除
|
||||
/// </summary>
|
||||
AlwayWarn = 10,
|
||||
/// <summary>
|
||||
/// 循环错误。每次需要把全部的错误参入进来
|
||||
/// </summary>
|
||||
WhileWarn = 20,
|
||||
}
|
||||
}
|
44
货架标准上位机/Tool/While.cs
Normal file
44
货架标准上位机/Tool/While.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace 货架标准上位机
|
||||
{
|
||||
/// <summary>
|
||||
/// 循环、暂停、继续、停止扩展
|
||||
/// </summary>
|
||||
public static class While
|
||||
{
|
||||
/// <summary>
|
||||
/// 带条件等待并检测停止、暂停
|
||||
/// </summary>
|
||||
/// <param name="func">不停的执行的任务,返回为true就跳出循环</param>
|
||||
public static async Task Wait(Func<bool> func, CancellationToken token = default, ManualResetEvent manualReset = default)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
manualReset?.WaitOne();
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
if (func.Invoke())
|
||||
break;
|
||||
|
||||
await Task.Delay(200, token);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待并检测停止、暂停
|
||||
/// </summary>
|
||||
public static void Wait(CancellationToken token = default, ManualResetEvent manualReset = default)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
manualReset?.WaitOne();
|
||||
token.ThrowIfCancellationRequested();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user