PythonExecutor.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. 
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Serialization;
  4. using NUnit.Framework;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Windows.Controls;
  15. namespace AIRS
  16. {
  17. /// <summary>
  18. /// Python脚本执行类
  19. /// lbf 2020/05/20 14:43
  20. /// </summary>
  21. public class PythonExecutor :ScriptExecutor
  22. {
  23. /// <summary>
  24. /// IBM组织识别、标准评级py脚本文件
  25. /// </summary>
  26. public string IbmAiPy { get; set; }
  27. private readonly string ReturnDataPrefix = "AI_result";//"{\"content\":";
  28. private readonly string ParameterErrorPrefix = "AI_error:";
  29. private readonly string ProgressPrefix = "AI_progress:";
  30. private readonly int ARGUMENTS_MAX_LENGTH = 1024 * 32;//命令行参数最大长度
  31. /// <summary>
  32. /// python执行器,返回数据更新界面控件
  33. /// </summary>
  34. /// <param name="context">界面UI环境</param>
  35. public PythonExecutor(SynchronizationContext context):base(context,"python")
  36. {
  37. this.IbmAiPy = @"D:\vi\vi_server\ibm_ai.py ";
  38. }
  39. /// <summary>
  40. /// python执行器,返回数据更新界面控件
  41. /// </summary>
  42. /// <param name="context">界面UI环境</param>
  43. /// <param name="pyFilePath">要执行的python脚本文件全路径</param>
  44. public PythonExecutor(SynchronizationContext context,string pyFilePath) : base(context, "python")
  45. {
  46. this.IbmAiPy = pyFilePath;
  47. }
  48. /// <summary>
  49. /// 停止后台脚本运行
  50. /// </summary>
  51. public void stop()
  52. {
  53. lock(this.IbmAiPy)
  54. {
  55. string stop_file = Path.Combine(Path.GetDirectoryName(this.IbmAiPy), "stop.ai");
  56. if (!File.Exists(stop_file))
  57. {
  58. File.Create(stop_file);
  59. }
  60. }
  61. }
  62. /// <summary>
  63. /// 执行python文件脚本
  64. /// </summary>
  65. /// <param name="control">回调界面控件</param>
  66. /// <param name="pyPath">要执行的python脚本文件全路径</param>
  67. /// <param name="parameters">参数</param>
  68. public void executePy(Control control, params string[] parameters)
  69. {
  70. StringBuilder stringBuilder = new StringBuilder(256);
  71. stringBuilder.Append(this.IbmAiPy);
  72. if (parameters != null)
  73. {
  74. foreach (var parameter in parameters)
  75. {
  76. stringBuilder.Append(" \"");
  77. stringBuilder.Append(parameter.Replace("'", "\\\"").Replace("\"", "\\\""));
  78. stringBuilder.Append("\" ");
  79. }
  80. }
  81. execute(control, stringBuilder.ToString());
  82. }
  83. /// <summary>
  84. /// 标准评级python接口调用,需要挂载ReceivedControlData接收返回值
  85. /// </summary>
  86. /// <param name="control">回调显示控件</param>
  87. /// <param name="catagory">评级钢种ID</param>
  88. /// <param name="items">检测项目ID</param>
  89. /// <param name="parameters">其他参数</param>
  90. /// <param name="jpgFileList">等检测项目图像文件全名列表</param>
  91. public void ibmBzpj(Control control,string catagory,string items,object detectParams, List<string> jpgFileList)
  92. {
  93. Assert.IsNotNull(jpgFileList);
  94. Assert.IsNotEmpty(jpgFileList);
  95. Hashtable json = new Hashtable();
  96. json.Add("func", "bzpj");
  97. json.Add("catagory", catagory);
  98. json.Add("items", items);
  99. json.Add("path", Path.GetDirectoryName(jpgFileList[0]).Replace("\\", "/"));
  100. json.Add("images", string.Join(";", jpgFileList.Select(s => Path.GetFileName(s))));
  101. string arguments = JsonConvert.SerializeObject(json).Replace("\"", "\\\"");
  102. Assert.Less(Encoding.UTF8.GetBytes(arguments).Length, this.ARGUMENTS_MAX_LENGTH);
  103. string commandString;
  104. string vp = null;
  105. if (detectParams != null)
  106. {
  107. if (detectParams.GetType() == typeof(string))
  108. {
  109. vp = detectParams.ToString().Replace("'", "\\\"");
  110. }
  111. else
  112. {
  113. vp = JsonConvert.SerializeObject(detectParams).Replace("\"", "\\\"");
  114. }
  115. }
  116. if (!string.IsNullOrEmpty(vp))
  117. {
  118. commandString = string.Format("{0} \"{1}\" \"{2}\"", this.IbmAiPy, arguments, vp);
  119. }
  120. else
  121. {
  122. commandString = string.Format("{0} \"{1}\"", this.IbmAiPy, arguments);
  123. }
  124. System.Diagnostics.Debug.WriteLine(commandString);
  125. execute(control, commandString);
  126. }
  127. /// <summary>
  128. /// IBM组织识别python接口调用,需要挂载ReceivedControlData接收返回值
  129. /// </summary>
  130. /// <param name="control">回调显示控件</param>
  131. /// <param name="detectParams">其他参数</param>
  132. /// <param name="jpgFileList">等检测项目图像文件全名列表</param>
  133. public void ibmZzsb(Control control, object detectParams, List<string> jpgFileList)
  134. {
  135. Assert.IsNotNull(jpgFileList);
  136. Assert.IsNotEmpty(jpgFileList);
  137. Hashtable json = new Hashtable();
  138. json.Add("catagory", "-1");
  139. json.Add("status", "-1");
  140. json.Add("magnification", "-1");
  141. json.Add("func", "zzsb");
  142. json.Add("path", Path.GetDirectoryName(jpgFileList[0]).Replace("\\", "/"));
  143. json.Add("images", string.Join(";", jpgFileList.Select(s => Path.GetFileName(s))));
  144. string arguments = JsonConvert.SerializeObject(json).Replace("\"", "\\\"");
  145. Assert.Less(Encoding.UTF8.GetBytes(arguments).Length,this.ARGUMENTS_MAX_LENGTH);
  146. string commandString;
  147. if (detectParams != null && !string.IsNullOrEmpty(detectParams.ToString()))
  148. {
  149. commandString = string.Format("{0} \"{1}\" \"{2}\"", this.IbmAiPy, arguments,detectParams.ToString().Replace("'", "\\\""));
  150. }
  151. else
  152. {
  153. commandString = string.Format("{0} \"{1}\"", this.IbmAiPy, arguments);
  154. }
  155. execute(control, commandString);
  156. }
  157. /// <summary>
  158. /// 返回数据开头信息确认是什么类型数据,分为返回结果、一般信息、参数错误信息、执行的脚本命令
  159. /// </summary>
  160. /// <param name="control"></param>
  161. /// <param name="e"></param>
  162. /// <returns></returns>
  163. override
  164. protected ControlData produceControlData(Control control, DataReceivedEventArgs e)
  165. {
  166. ControlData controlData = new ControlData { Control = control };
  167. //不同类型返回数据
  168. if (e.Data.Contains(this.ReturnDataPrefix))
  169. {
  170. controlData.DataString = e.Data.Substring(e.Data.IndexOf(this.ReturnDataPrefix));
  171. controlData.CallbackDataType= CallbackDataType.ReturnData;
  172. int contentPos = controlData.DataString.IndexOf(',');
  173. int indexPos = controlData.DataString.IndexOf(':');
  174. if (contentPos >= 0)
  175. {
  176. controlData.Index = int.Parse(controlData.DataString.Substring(indexPos + 1, contentPos - indexPos - 1));
  177. controlData.DataString = controlData.DataString.Substring(contentPos + 1);
  178. }
  179. else
  180. {
  181. controlData.DataString = controlData.DataString.Substring(indexPos + 1);
  182. }
  183. }
  184. else if (e.Data.Contains(this.ParameterErrorPrefix))
  185. {
  186. controlData.DataString = e.Data.Substring(e.Data.IndexOf(this.ParameterErrorPrefix));
  187. controlData.CallbackDataType = CallbackDataType.ParameterError;
  188. }
  189. else if (e.Data.Contains(this.ProgressPrefix))
  190. {
  191. controlData.DataString = e.Data.Substring(e.Data.IndexOf(this.ProgressPrefix));
  192. controlData.CallbackDataType = CallbackDataType.Progress;
  193. }
  194. else
  195. {
  196. controlData.DataString = e.Data;
  197. controlData.CallbackDataType = CallbackDataType.Info;
  198. }
  199. return controlData;
  200. }
  201. }
  202. }