AutoIVCDiagnosisService.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. using AI.Common;
  2. using AI.Common.Log;
  3. using AIDiagnosis.Common.Enums;
  4. using AIDiagnosis.Common.Models;
  5. using AutoIVCCalculationLib;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Threading;
  11. using Vinno.AI.AutoIVCDiagnosisSDK.Interfaces;
  12. using Vinno.AI.AutoIVCDiagnosisSDK.Models;
  13. using Vinno.AI.AutoIVCDiagnosisService.Tools;
  14. using Vinno.AI.CommonSDK.Enums;
  15. using Vinno.AI.CommonSDK.Models;
  16. using Vinno.AI.CommonSDK.Tools;
  17. using Vinno.AI.Service.Common.Interfaces;
  18. using Vinno.AI.Service.Common.Models;
  19. using Vinno.AI.Service.Common.Tools;
  20. using AutoIVCDiagnosis = AutoIVCDiagnosisSDK.AutoIVCDiagnosis;
  21. namespace Vinno.AI.AutoIVCDiagnosisService
  22. {
  23. public class AutoIVCDiagnosisService : IAutoIVCDiagnosisService, IDisposable, IEngine
  24. {
  25. private readonly ConcurrentStack<byte[]> _singleImageCache;
  26. private readonly string _modelFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks");
  27. private PipeServer _singleImagePipeServer;
  28. private AutoIVCDiagnosis _autoIVCDiagnosis;
  29. private ImageProvider _imageProvider;
  30. private bool _disposed;
  31. public static List<FunctionInfo> AllFunctions { get; }
  32. static AutoIVCDiagnosisService()
  33. {
  34. AllFunctions = AutoIVCDiagnosis.AllFunctions;
  35. }
  36. public static void Init(FunctionInfo enableFunction, FunctionInfo currentUsedFunction)
  37. {
  38. AutoIVCDiagnosis.Init(enableFunction, currentUsedFunction);
  39. }
  40. public AutoIVCDiagnosisService()
  41. {
  42. try
  43. {
  44. _singleImageCache = new ConcurrentStack<byte[]>();
  45. _singleImagePipeServer = new PipeServer(AIDiagnosisSystemConsts.PipeForAutoIVCDiagnosisSingleImage);
  46. _singleImagePipeServer.LogMsgThrow += OnLogMsgThrow;
  47. _singleImagePipeServer.DataReceived += OnDataReceived;
  48. _singleImagePipeServer.StartAndReceive();
  49. _imageProvider = new ImageProvider(AIEnumType.AutoIVCDiagnosis);
  50. }
  51. catch (Exception ex)
  52. {
  53. Logger.Error($"AutoIVCDiagnosisService-Constructor error:{ex}");
  54. }
  55. }
  56. public void Initialize(AutoIVCDiagnosisParameter autoIVCParameter, bool hasImageProvider)
  57. {
  58. try
  59. {
  60. CloseDiagnosis();
  61. var cpuNumber = autoIVCParameter.CPUNumber;
  62. var isIncludeContour = autoIVCParameter.IsIncludeCountour;
  63. var detectMode = (EnumDetectMode)autoIVCParameter.DetectMode;
  64. var detectTps = autoIVCParameter.DetectTps;
  65. var intervalTime = autoIVCParameter.IntervalTime;
  66. var cmPerPixel = autoIVCParameter.CmPerPixel;
  67. _autoIVCDiagnosis = new AutoIVCDiagnosis(Setting.Instance.IsSaveDetectImage, Setting.Instance.IsSaveAllImage, Logger.LogDir, hasImageProvider ? _imageProvider : null, cpuNumber, _modelFolder, isIncludeContour, cmPerPixel, detectMode, detectTps, intervalTime);
  68. _autoIVCDiagnosis.StartEvaluation += OnStartEvaluation;
  69. _autoIVCDiagnosis.FinishEvaluation += OnFinishEvaluation;
  70. _autoIVCDiagnosis.NotifyError += OnNotifyError;
  71. _autoIVCDiagnosis.NotifyLog += OnNotifyLog;
  72. }
  73. catch (Exception ex)
  74. {
  75. Logger.Error($"AutoIVCDiagnosisService-Initialize error:{ex}");
  76. }
  77. }
  78. /// <summary>
  79. /// 切换引擎
  80. /// </summary>
  81. /// <param name="functionInfo"></param>
  82. public void SwitchEngines(FunctionInfo functionInfo)
  83. {
  84. try
  85. {
  86. AutoIVCDiagnosis.SwitchAIEngines(functionInfo);
  87. if (_autoIVCDiagnosis != null)
  88. {
  89. _autoIVCDiagnosis.SwitchAIEngines2(functionInfo);
  90. }
  91. }
  92. catch (Exception ex)
  93. {
  94. Logger.Error($"AutoIVCDiagnosisService-SwitchEngines error:{ex}");
  95. }
  96. }
  97. public void Start()
  98. {
  99. try
  100. {
  101. if (_autoIVCDiagnosis != null)
  102. {
  103. _autoIVCDiagnosis.Start();
  104. }
  105. }
  106. catch (Exception ex)
  107. {
  108. Logger.Error($"AutoIVCDiagnosisService-Start error:{ex}");
  109. }
  110. }
  111. public void Stop()
  112. {
  113. try
  114. {
  115. if (_autoIVCDiagnosis != null)
  116. {
  117. _autoIVCDiagnosis.Stop();
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. Logger.Error($"AutoIVCDiagnosisService-Stop error:{ex}");
  123. }
  124. }
  125. private void OnDataReceived(object sender, byte[] e)
  126. {
  127. try
  128. {
  129. _singleImageCache.Push(e);
  130. }
  131. catch (Exception ex)
  132. {
  133. Logger.Error($"AutoIVCDiagnosisService-OnDataReceived error:{ex}");
  134. }
  135. }
  136. public void SetDetectTps(int detectTps)
  137. {
  138. try
  139. {
  140. if (_autoIVCDiagnosis != null)
  141. {
  142. _autoIVCDiagnosis.DetectTps = detectTps;
  143. }
  144. }
  145. catch (Exception ex)
  146. {
  147. Logger.Error($"AutoIVCDiagnosisService-SetDetectTps error:{ex}");
  148. }
  149. }
  150. public void SetIntervalTime(int intervalTime)
  151. {
  152. try
  153. {
  154. if (_autoIVCDiagnosis != null)
  155. {
  156. _autoIVCDiagnosis.IntervalTime = intervalTime;
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. Logger.Error($"AutoIVCDiagnosisService-SetIntervalTime error:{ex}");
  162. }
  163. }
  164. public void SetDetectMode(AIEnumDetectMode detectMode)
  165. {
  166. try
  167. {
  168. if (_autoIVCDiagnosis != null)
  169. {
  170. _autoIVCDiagnosis.DetectMode = (EnumDetectMode)detectMode;
  171. }
  172. }
  173. catch (Exception ex)
  174. {
  175. Logger.Error($"AutoIVCDiagnosisService-SetDetectMode error:{ex}");
  176. }
  177. }
  178. /// <summary>
  179. /// 设置一个像素代表的实际物理距离是多少cm
  180. /// </summary>
  181. /// <param name="cmPerPixel"></param>
  182. public void SetCmPerPixel(float cmPerPixel)
  183. {
  184. try
  185. {
  186. if (_autoIVCDiagnosis != null)
  187. {
  188. _autoIVCDiagnosis.CmPerPixel = cmPerPixel;
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. Logger.Error($"AutoIVCDiagnosisService-SetCmPerPixel error:{ex}");
  194. }
  195. }
  196. public string StartAutoIVCCalculation(double ivcCurveTotalTime, float cmPerPixel, double intervalTime)
  197. {
  198. try
  199. {
  200. return _autoIVCDiagnosis?.StartAutoIVCCalculation(ivcCurveTotalTime, cmPerPixel, intervalTime);
  201. }
  202. catch (Exception ex)
  203. {
  204. Logger.Error($"AutoIVCDiagnosisService-StartAutoIVCCalculation error:{ex}");
  205. return null;
  206. }
  207. }
  208. /// <summary>
  209. /// 检测单张Byte Image
  210. /// </summary>
  211. /// <param name="realFrameTime">图像输入的真实时间戳</param>
  212. /// <returns></returns>
  213. public AIIVCCurveInfos DetectOneByteImage(string calcultationId, double realFrameTime)
  214. {
  215. try
  216. {
  217. byte[] byteImage = null;
  218. int retryCount = 0;
  219. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  220. {
  221. Thread.Sleep(10);
  222. retryCount++;
  223. }
  224. if (byteImage == null)
  225. {
  226. Logger.Error($"AutoIVCDiagnosisService-DetectOneByteImage Get Image Cache Fail");
  227. return null;
  228. }
  229. var ivcCurveInfos = _autoIVCDiagnosis?.DetectOneImage(calcultationId, byteImage, realFrameTime);
  230. return AIConvertHelper.ConvertIVCCurveInfosToAIIVCCurveInfos(ivcCurveInfos);
  231. }
  232. catch (Exception ex)
  233. {
  234. Logger.Error($"AutoIVCDiagnosisService-DetectOneByteImage error:{ex}");
  235. return null;
  236. }
  237. }
  238. public AIIVCCurveInfos DetectOneRawImage(string calcultationId, int height, int width, AIEnumColorType colorType, double realFrameTime)
  239. {
  240. try
  241. {
  242. byte[] byteImage = null;
  243. int retryCount = 0;
  244. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  245. {
  246. Thread.Sleep(10);
  247. retryCount++;
  248. }
  249. if (byteImage == null)
  250. {
  251. Logger.Error($"AutoIVCDiagnosisService-DetectOneRawImage Get Image Cache Fail");
  252. return null;
  253. }
  254. var rawImage = new RawImage(byteImage, width, height, (EnumColorType)colorType);
  255. var ivcCurveInfos = _autoIVCDiagnosis?.DetectOneImage(calcultationId, rawImage, realFrameTime);
  256. return AIConvertHelper.ConvertIVCCurveInfosToAIIVCCurveInfos(ivcCurveInfos);
  257. }
  258. catch (Exception ex)
  259. {
  260. Logger.Error($"AutoIVCDiagnosisService-DetectOneRawImage error:{ex}");
  261. return null;
  262. }
  263. }
  264. public void DetectOneByteImageAsync(string calculationId, double realFrameTime)
  265. {
  266. try
  267. {
  268. byte[] byteImage = null;
  269. int retryCount = 0;
  270. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  271. {
  272. Thread.Sleep(10);
  273. retryCount++;
  274. }
  275. if (byteImage == null)
  276. {
  277. Logger.Error($"AutoIVCDiagnosisService-DetectOneByteImageAsync Get Image Cache Fail");
  278. return;
  279. }
  280. _autoIVCDiagnosis?.DetectOneImageAsync(calculationId, byteImage, realFrameTime);
  281. }
  282. catch (Exception ex)
  283. {
  284. Logger.Error($"AutoIVCDiagnosisService-DetectOneByteImageAsync error:{ex}");
  285. }
  286. }
  287. public void DetectOneRawImageAsync(string calculationId, int height, int width, AIEnumColorType colorType, double realFrameTime)
  288. {
  289. try
  290. {
  291. byte[] byteImage = null;
  292. int retryCount = 0;
  293. while (!_singleImageCache.TryPop(out byteImage) && retryCount < 50)
  294. {
  295. Thread.Sleep(10);
  296. retryCount++;
  297. }
  298. if (byteImage == null)
  299. {
  300. Logger.Error($"AutoIVCDiagnosisService-DetectOneRawImageAsync Get Image Cache Fail");
  301. return;
  302. }
  303. var rawImage = new RawImage(byteImage, width, height, (EnumColorType)colorType);
  304. _autoIVCDiagnosis?.DetectOneImageAsync(calculationId, rawImage, realFrameTime);
  305. }
  306. catch (Exception ex)
  307. {
  308. Logger.Error($"AutoIVCDiagnosisService-DetectOneRawImageAsync error:{ex}");
  309. }
  310. }
  311. public AIIVCCurveInfos StopAutoIVCCalculation(string calculationId)
  312. {
  313. try
  314. {
  315. var ivcCurveInfos = _autoIVCDiagnosis?.StopAutoIVCCalculation(calculationId);
  316. return AIConvertHelper.ConvertIVCCurveInfosToAIIVCCurveInfos(ivcCurveInfos);
  317. }
  318. catch (Exception ex)
  319. {
  320. Logger.Error($"AutoIVCDiagnosisService-StopAutoIVCCalculation error:{ex}");
  321. return null;
  322. }
  323. }
  324. /// <summary>
  325. /// Send Raw Image Data For Pipe
  326. /// </summary>
  327. /// <param name="height"></param>
  328. /// <param name="width"></param>
  329. /// <param name="colorType"></param>
  330. public void SendRawImageData(int height, int width, AIEnumColorType colorType)
  331. {
  332. try
  333. {
  334. _imageProvider?.ReceiveRawImageData(height, width, (EnumColorType)colorType);
  335. }
  336. catch (Exception ex)
  337. {
  338. Logger.Error($"AutoIVCDiagnosisService-SendRawImageData error:{ex}");
  339. }
  340. }
  341. /// <summary>
  342. /// Send Byte Image Data For Pipe
  343. /// </summary>
  344. public void SendByteImageData()
  345. {
  346. try
  347. {
  348. _imageProvider?.ReceiveByteImageData();
  349. }
  350. catch (Exception ex)
  351. {
  352. Logger.Error($"AutoIVCDiagnosisService-SendByteImageData error:{ex}");
  353. }
  354. }
  355. public void Close()
  356. {
  357. try
  358. {
  359. Logger.Info($"AutoIVCDiagnosisService-Close Invoke");
  360. CloseDiagnosis();
  361. }
  362. catch (Exception ex)
  363. {
  364. Logger.Error($"AutoIVCDiagnosisService-Close error:{ex}");
  365. }
  366. }
  367. private void CloseDiagnosis()
  368. {
  369. if (_autoIVCDiagnosis != null)
  370. {
  371. _autoIVCDiagnosis.StartEvaluation -= OnStartEvaluation;
  372. _autoIVCDiagnosis.FinishEvaluation -= OnFinishEvaluation;
  373. _autoIVCDiagnosis.NotifyError -= OnNotifyError;
  374. _autoIVCDiagnosis.NotifyLog -= OnNotifyLog;
  375. _autoIVCDiagnosis.Close();
  376. _autoIVCDiagnosis = null;
  377. }
  378. }
  379. public void Dispose()
  380. {
  381. try
  382. {
  383. if (!_disposed)
  384. {
  385. Logger.Info($"AutoIVCDiagnosisService-Start Dispose");
  386. CloseDiagnosis();
  387. if (_singleImagePipeServer != null)
  388. {
  389. _singleImagePipeServer.DataReceived -= OnDataReceived;
  390. _singleImagePipeServer.Dispose();
  391. _singleImagePipeServer.LogMsgThrow -= OnLogMsgThrow;
  392. _singleImagePipeServer = null;
  393. }
  394. _imageProvider.Dispose();
  395. _imageProvider = null;
  396. _disposed = true;
  397. Logger.Info($"AutoIVCDiagnosisService Dispose End");
  398. }
  399. }
  400. catch (Exception ex)
  401. {
  402. Logger.Error($"AutoIVCDiagnosisService-Dispose error:{ex}");
  403. }
  404. }
  405. private void OnNotifyLog(object sender, LogEventArgs e)
  406. {
  407. if (e == null)
  408. {
  409. return;
  410. }
  411. switch (e.LogType)
  412. {
  413. case EnumLogType.InfoLog:
  414. Logger.Info($"AutoIVCDiagnosisService OnNotifyLog:{e.Msg}");
  415. break;
  416. case EnumLogType.WarnLog:
  417. Logger.Warning($"AutoIVCDiagnosisService OnNotifyLog:{e.Msg}");
  418. break;
  419. case EnumLogType.ErrorLog:
  420. case EnumLogType.FatalLog:
  421. default:
  422. Logger.Error($"AutoIVCDiagnosisService OnNotifyLog:{e.Msg}");
  423. break;
  424. }
  425. var logEventArgs = AICommonServiceConvertHelper.ConvertLogEventArgsToAILogEventArgs(e);
  426. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.AutoIVCDiagnosisNotifyLogRaised, Params = logEventArgs });
  427. }
  428. private void OnNotifyError(object sender, ErrorEventArgs e)
  429. {
  430. Logger.Error($"AutoIVCDiagnosisService OnNotifyError:{e.GetException()}");
  431. var logEventArgs = AICommonServiceConvertHelper.ConvertErrorEventArgsToAILogEventArgs(e);
  432. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.AutoIVCDiagnosisNotifyLogRaised, Params = logEventArgs });
  433. }
  434. private void OnLogMsgThrow(object sender, AILogEventArgs e)
  435. {
  436. if (e == null)
  437. {
  438. return;
  439. }
  440. switch (e.LogType)
  441. {
  442. case AIEnumLogType.ErrorLog:
  443. case AIEnumLogType.FatalLog:
  444. Logger.Error(e.Msg);
  445. break;
  446. case AIEnumLogType.WarnLog:
  447. Logger.Warning(e.Msg);
  448. break;
  449. case AIEnumLogType.InfoLog:
  450. default:
  451. Logger.Info(e.Msg);
  452. break;
  453. }
  454. }
  455. private void OnStartEvaluation(object sender, EventArgs e)
  456. {
  457. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.AutoIVCDiagnosisStartEvaluationRaised, Params = e });
  458. }
  459. private void OnFinishEvaluation(object sender, IVCCurveInfos e)
  460. {
  461. var aiIVCCurveInfos = AIConvertHelper.ConvertIVCCurveInfosToAIIVCCurveInfos(e);
  462. NotificationSender.SendNotification(new AINotificationArgs() { NotificationType = AIEnumNotificationType.AutoIVCDiagnosisFinishEvaluationRaised, Params = aiIVCCurveInfos });
  463. }
  464. }
  465. }