ExecuteCommand.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using SHJX.Service.Common.ReadXML;
  2. using System.Windows.Threading;
  3. using SHJX.Service.Control.Extends;
  4. namespace SHJX.Service.Control.Execute
  5. {
  6. public class ExecuteCommand
  7. {
  8. private static readonly ILogger logger = LogFactory.BuildLogger(typeof(ExecuteCommand));
  9. private static IDataManager _dataManager;
  10. private static ReadConfigUtil _config;
  11. private static readonly object recode_lock = new();
  12. private static IEventAggregator _ea;
  13. public static List<ShowStatusValueArgs> statusValue = new();
  14. private static string ShowStatusBefore = "";
  15. public ExecuteCommand(IEventAggregator ea, IDataManager dataManager, TaskExtend taskExtend, ReadConfigUtil config)
  16. {
  17. _ea = ea;
  18. _dataManager = dataManager;
  19. _config = config;
  20. }
  21. /// <summary>
  22. /// 寄存器操作
  23. /// </summary>
  24. /// <param name="typeName">类型</param>
  25. /// <param name="time">时间</param>
  26. /// <returns></returns>
  27. public static bool RegisterExecute(string typeName, double time)
  28. {
  29. if (time <= 0)
  30. {
  31. return true;
  32. }
  33. bool res = typeName.RegisterOpen();
  34. Thread.Sleep(Convert.ToInt32(Math.Round(time * 1000)));
  35. return res && typeName.RegisterClose();
  36. }
  37. /// <summary>
  38. /// 电机返回原点
  39. /// </summary>
  40. /// <param name="typeName"></param>
  41. /// <returns></returns>
  42. public static bool MotorGoBackExecute(string typeName)
  43. {
  44. try
  45. {
  46. bool res = false;
  47. if (typeName.In(EquipmentNames.AxisX, EquipmentNames.AxisY)) //这段代码是xy移动距离大于1000前要看z是否回到原点
  48. {
  49. char[] judgeRes = EquipmentNames.AxisZ.MotorRead();
  50. if (judgeRes is null or { Length: 0 })
  51. {
  52. return false;
  53. }
  54. if (!judgeRes[3 - 1].Equals('0'))
  55. {
  56. EquipmentNames.AxisZ.MotorGoBack();
  57. }
  58. if (typeName == EquipmentNames.AxisX)
  59. {
  60. MotorSpeed speed = _dataManager.Query<MotorSpeed>().Where(it => it.SystemName.Equals(EquipmentNames.AxisX)).First();
  61. res = speed.SystemName.WriteSpeed(6);
  62. }
  63. if (typeName == EquipmentNames.AxisY)
  64. {
  65. MotorSpeed speed = _dataManager.Query<MotorSpeed>().Where(it => it.SystemName.Equals(EquipmentNames.AxisY)).First();
  66. res = speed.SystemName.WriteSpeed(6);
  67. }
  68. }
  69. bool writeResponse = typeName.MotorGoBack();
  70. if (!writeResponse)
  71. {
  72. return false;
  73. }
  74. MotorMoveStatus moveStatus = _dataManager.Query<MotorMoveStatus>().Where(it => it.MotorName.Equals(typeName)).First();
  75. if (moveStatus is null)
  76. {
  77. return writeResponse;
  78. }
  79. moveStatus.CurrentPosition = 0;
  80. writeResponse = writeResponse && _dataManager.Update(moveStatus) > 0;
  81. logger.LogDebug($"下发{typeName}返回原点{(writeResponse ? "成功" : "失败")}");
  82. if (typeName == EquipmentNames.AxisX)
  83. {
  84. MotorSpeed speed = _dataManager.Query<MotorSpeed>().Where(it => it.SystemName.Equals(EquipmentNames.AxisX)).First();
  85. res = speed.SystemName.WriteSpeed(speed.Speed);
  86. }
  87. if (typeName == EquipmentNames.AxisY)
  88. {
  89. MotorSpeed speed = _dataManager.Query<MotorSpeed>().Where(it => it.SystemName.Equals(EquipmentNames.AxisY)).First();
  90. res = speed.SystemName.WriteSpeed(speed.Speed);
  91. }
  92. return writeResponse;
  93. }
  94. catch (Exception ex)
  95. {
  96. logger.LogError(ex.ToString());
  97. return false;
  98. }
  99. }
  100. /// <summary>
  101. /// 电机移动
  102. /// </summary>
  103. /// <param name="typeName">类型名称</param>
  104. /// <param name="task">任务</param>
  105. public static bool MotorMoveExecute(string typeName, EquipmentTask task)
  106. {
  107. try
  108. {
  109. string movePoint = string.Empty;
  110. movePoint = task.Target switch
  111. {
  112. "From" => task.From,
  113. "To" => task.To,
  114. _ => throw new ArgumentNullException(task.Target),
  115. };
  116. EquipmentArea area = _dataManager.Query<EquipmentArea>().Where(item => item.PointName.Equals(movePoint)).First();
  117. if (area is null)
  118. {
  119. return false;
  120. }
  121. MotorMoveStatus moveStatus = _dataManager.Query<MotorMoveStatus>().Where(item => item.MotorName.Equals(typeName)).First();
  122. if (moveStatus is null)
  123. {
  124. return false;
  125. }
  126. MotorConvert motorConvert = _dataManager.Query<MotorConvert>().Where(item => item.MotorName.Equals(typeName)).First();
  127. if (motorConvert is null)
  128. {
  129. return false;
  130. }
  131. #region 移动距离
  132. char[] readRes = EquipmentNames.Tongs.MotorRead(); //这里需要读取
  133. if (typeName == EquipmentNames.AxisX|| typeName == EquipmentNames.AxisY)//X,Y的话要看z在不在原点
  134. {
  135. if (!readRes[2].Equals('0'))
  136. {
  137. logger.LogDebug($"X,Y运动前Z轴回原点命令发送");
  138. ExecuteCommand.MotorGoBackExecute(EquipmentNames.AxisZ);
  139. }
  140. }
  141. bool judgeRes = false;
  142. if (typeName == EquipmentNames.AxisZ)//抓着杯的时候 抓手不需要太向下
  143. {
  144. judgeRes = readRes[4].Equals('0');
  145. }
  146. double? localPos = typeName switch
  147. {
  148. EquipmentNames.AxisX => area?.LocationX - moveStatus?.CurrentPosition,
  149. EquipmentNames.AxisY => area?.LocationY - moveStatus?.CurrentPosition,
  150. EquipmentNames.AxisZ => judgeRes ? area?.LocationZF : area?.LocationZ,
  151. _ => throw new ArgumentNullException(typeName),
  152. };
  153. double? distance = typeName switch
  154. {
  155. EquipmentNames.AxisX or EquipmentNames.AxisY or EquipmentNames.AxisZ => -1 * localPos * motorConvert?.ConvertRatio,
  156. _ => throw new ArgumentNullException(typeName)
  157. };
  158. #endregion
  159. bool writeResponse = typeName.MotorMove(Convert.ToInt32(Math.Round(distance ?? 0)));
  160. logger.LogDebug($"{task.Source} ==> 下发{typeName}{(writeResponse ? "成功" : "失败")}");
  161. if (!writeResponse)
  162. {
  163. return false;
  164. }
  165. moveStatus.CurrentPosition += localPos ?? 0;
  166. return _dataManager.Update(moveStatus) > 0;
  167. }
  168. catch (Exception)
  169. {
  170. throw;
  171. }
  172. }
  173. public static bool ShowStatus(EquipmentTask task)
  174. {
  175. try
  176. {
  177. if (task is null||task.SerialKey==0)
  178. {
  179. return false;
  180. }
  181. lock (recode_lock)
  182. {
  183. ShowStatusValueArgs status = new ShowStatusValueArgs();
  184. if (ShowStatusBefore == task.RouteStep.ToString())
  185. {
  186. return true;
  187. }
  188. ShowStatusBefore = task.RouteStep.ToString();
  189. string step = task.RouteStep.ToString() switch
  190. {
  191. "AddLiquid" => "加液",
  192. "AddLiquidGoback" => "加液返回",
  193. "Dissolve" => "消解",
  194. "DissolveGoback" => "消解结束",
  195. "Titration" => "1号滴定",
  196. "Titration2" => "2号滴定",
  197. "TitrationGoback" => "1号滴定返回",
  198. "Titration2Goback" => "2号滴定返回",
  199. };
  200. status.CurrentContent = task.Source + ":" + step + "-" + "从(" + task.From + ")到(" + task.To + ")";
  201. status.Name = task.Source.ToString();
  202. _ea.GetEvent<UpdateStausEvent>().Publish(status);
  203. return true;
  204. }
  205. }
  206. catch
  207. {
  208. return false;
  209. }
  210. }
  211. public static bool ShowStatusAction(EquipmentTask task, string value)
  212. {
  213. try
  214. {
  215. if (task is null)
  216. {
  217. return false;
  218. }
  219. lock (recode_lock)
  220. {
  221. ShowStatusValueArgs status = new ShowStatusValueArgs();
  222. if (ShowStatusBefore == value)
  223. {
  224. return true;
  225. }
  226. ShowStatusBefore = value;
  227. string step = value switch
  228. {
  229. "Sodium2Hydroxide" => "氢氧化钠",
  230. "Titration1Vitriol" => "滴定硫酸1",
  231. "Titration2Vitriol" => "滴定硫酸2",
  232. "AddLiquidVitriol" => "加液硫酸",
  233. "Titration1PotassiumPermanganate" => "高锰酸钾1",
  234. "AddLiquidPotassiumPermanganate" => "加液高锰酸钾2",
  235. "Sodium1Oxalate" => "草酸钠1",
  236. "Sodium2Oxalate" => "草酸钠2",
  237. };
  238. status.CurrentContent = task.Source + ":加入" + "(" + step + ")溶液";
  239. if (task.Source is null)
  240. {
  241. status.Name = "清洗";
  242. }
  243. else
  244. status.Name = task.Source.ToString();
  245. _ea.GetEvent<UpdateStausEvent>().Publish(status);
  246. return true;
  247. }
  248. }
  249. catch
  250. {
  251. return false;
  252. }
  253. }
  254. public static bool ShowStatusOpenClose(EquipmentTask task, string value)
  255. {
  256. try
  257. {
  258. if (task is null)
  259. {
  260. return false;
  261. }
  262. lock (recode_lock)
  263. {
  264. ShowStatusValueArgs status = new ShowStatusValueArgs();
  265. if (ShowStatusBefore == value)
  266. {
  267. return true;
  268. }
  269. status.CurrentContent = task.Source + ":" + value;
  270. if (task.Source is null)
  271. {
  272. status.Name = "清洗";
  273. }
  274. else
  275. status.Name = task.Source.ToString();
  276. _ea.GetEvent<UpdateStausEvent>().Publish(status);
  277. return true;
  278. }
  279. }
  280. catch
  281. {
  282. return false;
  283. }
  284. }
  285. }
  286. }