WarningUpdateService.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using LogoForceTestApp.Modules.MainModule.Models;
  2. using Microsoft.Extensions.Options;
  3. using Prism.Events;
  4. using System;
  5. using System.Collections.Concurrent;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using Team.Utility;
  14. namespace LogoForceTestApp.Modules.MainModule.Services
  15. {
  16. public class WarningUpdateService : IWarningUpdateService
  17. {
  18. private readonly IEventAggregator _eventAggregator;
  19. private readonly IOptions<AppSettings> _appSettings;
  20. private readonly ConcurrentDictionary<int, DeviceErrorLog> _deviceErrorLogs = new();
  21. private readonly ushort[] _tempData;
  22. private readonly ConcurrentDictionary<int, Tuple<int,string>> _warningList = new();
  23. public WarningUpdateService(IEventAggregator eventAggregator, IOptions<AppSettings> appSettings)
  24. {
  25. _tempData = new ushort[100];
  26. _eventAggregator = eventAggregator;
  27. _appSettings = appSettings;
  28. var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ErrorCode.xlsx");
  29. if (File.Exists(filePath))
  30. {
  31. var dataTable = ExcelUtil.ExcelToTable(filePath);
  32. foreach (DataRow row in dataTable.Rows)
  33. {
  34. int id = int.Parse(row[0].ToString());
  35. int errorCode = int.Parse(row[1].ToString());
  36. _warningList.TryAdd(id, new Tuple<int,string>(errorCode, row[2].ToString()));
  37. }
  38. }
  39. else
  40. {
  41. var dataTable = new DataTable();
  42. dataTable.Columns.Add("报警编号");
  43. dataTable.Columns.Add("报警代码");
  44. dataTable.Columns.Add("报警信息");
  45. for (int i = 1; i <= 16 * 100; i++)
  46. {
  47. dataTable.Rows.Add(i.ToString(), 60020000 + i,$"未定义错误{i}");
  48. }
  49. foreach (DataRow row in dataTable.Rows)
  50. {
  51. int id = int.Parse(row[0].ToString());
  52. int errorCode = int.Parse(row[1].ToString());
  53. _warningList.TryAdd(id, new Tuple<int, string>(errorCode, row[2].ToString()));
  54. }
  55. ExcelUtil.TableToExcel(dataTable, filePath, true);
  56. }
  57. }
  58. public Tuple<int,string> GetErrorCode(int id)
  59. {
  60. if (!_warningList.ContainsKey(id))
  61. {
  62. return Tuple.Create(1, "error");
  63. }
  64. return _warningList[id];
  65. }
  66. public async Task UpdateWarningAsync(ushort[] data)
  67. {
  68. if (data is null)
  69. {
  70. throw new ArgumentNullException(nameof(data));
  71. }
  72. if (data.Length < 100)
  73. {
  74. throw new ArgumentException(nameof(data) + "长度不正确,长度数据应为100");
  75. }
  76. //var content = "X2,2022/9/2,D,TEAM,HSG,L1,M-04Fasten SANP to HSG(2x screws),001,H18-001,后门禁报警,9:51:05,9:58:04,419";
  77. for (int i = 0; i < 100; i++)
  78. {
  79. if (_tempData[i] != data[i])
  80. {
  81. var tempSingleValue = _tempData[i];
  82. var currentSingleValue = data[i];
  83. var tempArray = IntTo16BoolArray(tempSingleValue);
  84. var currentArray = IntTo16BoolArray(currentSingleValue);
  85. for (int j = 0; j < 16; j++)
  86. {
  87. var id = j + 1 + i * 16;
  88. if (!tempArray[j] && currentArray[j])
  89. {
  90. //error occured
  91. if (!_deviceErrorLogs.ContainsKey(id))
  92. {
  93. //_deviceErrorLogs.TryAdd(id, new DeviceErrorLog
  94. //{
  95. // Category = "X2",
  96. // Class = "D",
  97. // Vendor = _deviceParam.Vendor,
  98. // Date = DateTime.Today.ToString("yyyy-MM-dd"),
  99. // SegmentName = _deviceParam.SegmentName,
  100. // LineId = _deviceParam.LineName,
  101. // DeviceId = _deviceParam.DeviceId,
  102. // Jig = "H18-001",
  103. // StartTime = DateTime.Now,
  104. // WaringInfo = _warningList[id],
  105. // WorkStaionName = _deviceParam.WorkStaionName,
  106. //});
  107. _eventAggregator.GetEvent<WaringUpdateLog>().Publish(new Tuple<LogType, string>(LogType.Error, $"信号{id}" + _warningList[id]));
  108. }
  109. }
  110. else if (tempArray[j] && !currentArray[j])
  111. {
  112. if (_deviceErrorLogs.ContainsKey(id))
  113. {
  114. try
  115. {
  116. _deviceErrorLogs.TryRemove(id, out var deviceErrorLog);
  117. deviceErrorLog.EndTime = DateTime.Now;
  118. var start = deviceErrorLog.StartTime;
  119. var end = deviceErrorLog.EndTime;
  120. deviceErrorLog.Duration = (end - start).TotalSeconds;
  121. await WriteLogAsync(deviceErrorLog);
  122. _eventAggregator.GetEvent<WaringUpdateLog>().Publish(new Tuple<LogType, string>(LogType.Info, _warningList[id] + $"已经处理完毕,持续时间:{deviceErrorLog.Duration}s"));
  123. }
  124. catch (Exception)
  125. {
  126. }
  127. }
  128. }
  129. }
  130. _tempData[i] = data[i];
  131. }
  132. }
  133. }
  134. async Task WriteLogAsync(DeviceErrorLog deviceErrorLog)
  135. {
  136. //if (string.IsNullOrWhiteSpace(_deviceParam.WarningReocordPath))
  137. //{
  138. // _deviceParam.WarningReocordPath = "d:/Files";
  139. //}
  140. //var directoryName = _deviceParam.WarningReocordPath;
  141. //if (!Directory.Exists(directoryName))
  142. //{
  143. // Directory.CreateDirectory(directoryName);
  144. // Thread.Sleep(1);
  145. //}
  146. //var fileName = DateTime.Today.ToString("yyy_MM_dd") + "_报警日志" + ".csv";
  147. //var filePath = Path.Combine(directoryName, fileName);
  148. //bool writeHeader = !File.Exists(filePath);
  149. //var header = "机种,日期,班别,厂商,段别,线别,工站名称,机台编号,治具编号,报警信息,报警开始时间,报警结束时间,DT(s)";
  150. //using var streamWriter = new StreamWriter(filePath, true, Encoding.GetEncoding("gb2312"));
  151. //if (writeHeader)
  152. //{
  153. // await streamWriter.WriteLineAsync(header);
  154. //}
  155. //await streamWriter.WriteLineAsync(deviceErrorLog.ToString());
  156. //await streamWriter.FlushAsync();
  157. //streamWriter.Close();
  158. }
  159. /// <summary>
  160. /// 整型转16位布尔数组
  161. /// </summary>
  162. /// <param name="inputNum"></param>
  163. /// <returns></returns>
  164. private bool[] IntTo16BoolArray(ushort inputNum)
  165. {
  166. bool[] boolArray = new bool[16];
  167. for (int i = 0; i < boolArray.Length; i++)
  168. {
  169. if (inputNum % 2 == 1)
  170. boolArray[i] = true;
  171. else
  172. boolArray[i] = false;
  173. inputNum = (ushort)(inputNum / 2);
  174. }
  175. return boolArray;
  176. }
  177. }
  178. public class DeviceErrorLog
  179. {
  180. public string Category { get; set; }
  181. public string Date { get; set; }
  182. public string Class { get; set; }
  183. public string Vendor { get; set; }
  184. public string SegmentName { get; set; }
  185. public string LineId { get; set; }
  186. public string WorkStaionName { get; set; }
  187. public string DeviceId { get; set; }
  188. public string Jig { get; set; }
  189. public string WaringInfo { get; set; }
  190. public DateTime StartTime { get; set; }
  191. public DateTime EndTime { get; set; }
  192. public double Duration { get; set; }
  193. public override string ToString()
  194. {
  195. return $"{Category},{Date},{Class},{Vendor},{SegmentName},{LineId},{WorkStaionName},{DeviceId},{Jig},{WaringInfo}," +
  196. $"{StartTime:HH:mm:ss},{EndTime:HH:mm:ss},{Duration}";
  197. }
  198. }
  199. }