PythonExecutor.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.Forms;
  15. namespace AiControlRequest
  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 (!System.IO.File.Exists(stop_file))
  57. {
  58. System.IO.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. execute(control, commandString);
  125. }
  126. /// <summary>
  127. /// IBM组织识别python接口调用,需要挂载ReceivedControlData接收返回值
  128. /// </summary>
  129. /// <param name="control">回调显示控件</param>
  130. /// <param name="detectParams">其他参数</param>
  131. /// <param name="jpgFileList">等检测项目图像文件全名列表</param>
  132. public void ibmZzsb(Control control, object detectParams, List<string> jpgFileList)
  133. {
  134. Assert.IsNotNull(jpgFileList);
  135. Assert.IsNotEmpty(jpgFileList);
  136. Hashtable json = new Hashtable();
  137. json.Add("catagory", "-1");
  138. json.Add("status", "-1");
  139. json.Add("magnification", "-1");
  140. json.Add("func", "zzsb");
  141. json.Add("path", Path.GetDirectoryName(jpgFileList[0]).Replace("\\", "/"));
  142. json.Add("images", string.Join(";", jpgFileList.Select(s => Path.GetFileName(s))));
  143. string arguments = JsonConvert.SerializeObject(json).Replace("\"", "\\\"");
  144. Assert.Less(Encoding.UTF8.GetBytes(arguments).Length,this.ARGUMENTS_MAX_LENGTH);
  145. string commandString;
  146. if (detectParams != null && !string.IsNullOrEmpty(detectParams.ToString()))
  147. {
  148. commandString = string.Format("{0} \"{1}\" \"{2}\"", this.IbmAiPy, arguments,detectParams.ToString().Replace("'", "\\\""));
  149. }
  150. else
  151. {
  152. commandString = string.Format("{0} \"{1}\"", this.IbmAiPy, arguments);
  153. }
  154. execute(control, commandString);
  155. }
  156. /// <summary>
  157. /// 返回数据开头信息确认是什么类型数据,分为返回结果、一般信息、参数错误信息、执行的脚本命令
  158. /// </summary>
  159. /// <param name="control"></param>
  160. /// <param name="e"></param>
  161. /// <returns></returns>
  162. override
  163. protected ControlData produceControlData(Control control, DataReceivedEventArgs e)
  164. {
  165. ControlData controlData = new ControlData { Control = control };
  166. //不同类型返回数据
  167. if (e.Data.Contains(this.ReturnDataPrefix))
  168. {
  169. controlData.DataString = e.Data.Substring(e.Data.IndexOf(this.ReturnDataPrefix));
  170. controlData.CallbackDataType= CallbackDataType.ReturnData;
  171. int contentPos = controlData.DataString.IndexOf(',');
  172. int indexPos = controlData.DataString.IndexOf(':');
  173. if (contentPos >= 0)
  174. {
  175. controlData.Index = int.Parse(controlData.DataString.Substring(indexPos + 1, contentPos - indexPos - 1));
  176. controlData.DataString = controlData.DataString.Substring(contentPos + 1);
  177. }
  178. else
  179. {
  180. controlData.DataString = controlData.DataString.Substring(indexPos + 1);
  181. }
  182. }
  183. else if (e.Data.Contains(this.ParameterErrorPrefix))
  184. {
  185. controlData.DataString = e.Data.Substring(e.Data.IndexOf(this.ParameterErrorPrefix));
  186. controlData.CallbackDataType = CallbackDataType.ParameterError;
  187. }
  188. else if (e.Data.Contains(this.ProgressPrefix))
  189. {
  190. controlData.DataString = e.Data.Substring(e.Data.IndexOf(this.ProgressPrefix));
  191. controlData.CallbackDataType = CallbackDataType.Progress;
  192. }
  193. else
  194. {
  195. controlData.DataString = e.Data;
  196. controlData.CallbackDataType = CallbackDataType.Info;
  197. }
  198. return controlData;
  199. }
  200. }
  201. }