StubFunc_Common.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Globalization;
  6. using System.Threading.Tasks;
  7. using UESTC.Auto.SCPIManager;
  8. using Uestc.Auto6.Dso.Core;
  9. using System.Reflection;
  10. using System.ComponentModel;
  11. using System.Text.RegularExpressions;
  12. using Uestc.Auto6.Dso.Core.PassFail;
  13. using Uestc.Auto6.Dso.ComModel;
  14. namespace Uestc.Auto6.Dso.Scpi
  15. {
  16. partial class StubFunc
  17. {
  18. #region P层对象
  19. public static DsoPrsnt Presenter { set; get; }
  20. #endregion P层对象
  21. /// <summary>
  22. /// 返回值自动科学计数法 开关
  23. /// </summary>
  24. #region 公有基础方法
  25. #region 字符串编码
  26. public static string encodingBytes(byte[] bytes)
  27. {
  28. return Encoding.UTF8.GetString(bytes);
  29. }
  30. public static List<string> ParamListToStrList(List<byte[]> inputBytesList)
  31. {
  32. if (inputBytesList == null)
  33. {
  34. return null;
  35. }
  36. List<string> returnValue = new List<string>();
  37. foreach (byte[] bytes in inputBytesList)
  38. returnValue.Add(encodingBytes(bytes));
  39. return returnValue;
  40. }
  41. public static byte[] decodeStr(string str)
  42. {
  43. return Encoding.UTF8.GetBytes(str);
  44. }
  45. #endregion 字符串编码
  46. #region 命令缩写
  47. private static string shortCMD(string str)
  48. {
  49. int index = findLowerChar(str);
  50. int numIndex = findNumChar(str);
  51. if (index < 0)
  52. {
  53. return str;
  54. }
  55. else if (numIndex < index)
  56. {
  57. return str.Substring(0, index);
  58. }
  59. else
  60. {
  61. return str.Substring(0, index) + str.Substring(numIndex);
  62. }
  63. }
  64. private static int findNumChar(string str)
  65. {
  66. var lowerChar = str.FirstOrDefault(cr => cr >= '0' && cr <= '9');
  67. return str.IndexOf(lowerChar);
  68. }
  69. private static int findLowerChar(string str)
  70. {
  71. var lowerChar = str.FirstOrDefault(cr => cr >= 'a');
  72. return str.IndexOf(lowerChar);
  73. }
  74. #endregion 命令缩写
  75. #region 将一个对象转换为指定类型
  76. /// <summary>
  77. /// 将一个对象转换为指定类型
  78. /// </summary>
  79. /// <param name="obj">待转换的对象</param>
  80. /// <param name="type">目标类型</param>
  81. /// <returns>转换后的对象</returns>
  82. private static object ConvertObject(string obj, Type type, List<string> paramList)
  83. {
  84. if (type == null || obj == null) return null;
  85. if (type.IsEnum)
  86. {
  87. if (paramList == null)
  88. return null;
  89. //列表计较,并得到相应的index
  90. int indexOflist = 0;
  91. int foundIndex = -1;
  92. foreach (string paramName in paramList)
  93. {
  94. if ((paramName == obj) || obj == shortCMD(paramName))
  95. {
  96. foundIndex = indexOflist;
  97. break;
  98. }
  99. indexOflist++;
  100. }
  101. if (foundIndex == -1)
  102. return null;
  103. //设置枚举值,按顺序来设置,避免Int值的不连续性
  104. int enumIndex = 0;
  105. int foundEnumIndex = -1;
  106. foreach (int enumValue in Enum.GetValues(type))
  107. {
  108. if (enumIndex == foundIndex)
  109. {
  110. foundEnumIndex = enumValue;
  111. break;
  112. }
  113. enumIndex++;
  114. }
  115. if (foundEnumIndex >= 0)
  116. return foundEnumIndex;
  117. return null;
  118. }
  119. else if (type.IsValueType)
  120. {
  121. if (type != typeof(Boolean))
  122. {
  123. if (type == typeof(Int32))
  124. {
  125. if (Int32.TryParse(obj, out Int32 v))
  126. return v;
  127. }
  128. else if (type == typeof(double))
  129. {
  130. if (obj.Contains('e') || (obj.Contains('E')))
  131. {
  132. if (double.TryParse(obj, NumberStyles.AllowExponent, null, out double v))
  133. return v;
  134. }
  135. else if (double.TryParse(obj, out double v))
  136. return v;
  137. }
  138. else if (type == typeof(UInt32))
  139. {
  140. if (UInt32.TryParse(obj, out UInt32 v))
  141. return v;
  142. }
  143. else if (type == typeof(Int16))
  144. {
  145. if (Int16.TryParse(obj, out Int16 v))
  146. return v;
  147. }
  148. else if (type == typeof(UInt16))
  149. {
  150. if (UInt16.TryParse(obj, out UInt16 v))
  151. return v;
  152. }
  153. else if (type == typeof(long))
  154. {
  155. if (long.TryParse(obj, out long v))
  156. return v;
  157. }
  158. else if (type == typeof(float))
  159. {
  160. if (float.TryParse(obj, out float v))
  161. return v;
  162. }
  163. return null;
  164. }
  165. //Boolean
  166. if (paramList == null)
  167. return false;
  168. if (paramList.Count != 2)
  169. return false;
  170. if ((paramList[0] == obj) || obj == shortCMD(paramList[0]))
  171. return false;
  172. if ((paramList[1] == obj) || obj == shortCMD(paramList[1]))
  173. return true;
  174. return null;
  175. }
  176. else if (type == typeof(String))
  177. return obj;
  178. return null;
  179. }
  180. #endregion 将一个对象转换为指定类型
  181. public static bool TryGetPropertyInfoByUsingDeclareTablePrsntObject(SCPICommandProcessFuncParam analyResult, out PropertyInfo propertyInfo)
  182. {
  183. propertyInfo = null;
  184. if ((analyResult.Tag is null) || (analyResult.Tag is not ScpiTagObj))
  185. return false;
  186. ScpiTagObj scpiTagObj = (ScpiTagObj)analyResult.Tag;
  187. if (scpiTagObj.PrsntObj is null || scpiTagObj.PropertyName is null || scpiTagObj.PropertyName.Trim() == "")
  188. return false;
  189. propertyInfo = scpiTagObj.PrsntObj.GetType().GetProperty(scpiTagObj.PropertyName);
  190. if (propertyInfo == null)
  191. return false;
  192. if (propertyInfo.GetType() == typeof(Enum))
  193. {
  194. if (scpiTagObj.ParamList == null)
  195. return false;
  196. }
  197. return true;
  198. }
  199. public static bool TryGetPropertyInfo(object obj, string propertyName, out PropertyInfo propertyInfo)
  200. {
  201. propertyInfo = null;
  202. if (obj != null)
  203. propertyInfo = obj.GetType().GetProperty(propertyName);
  204. return propertyInfo != null;
  205. }
  206. public static bool TrySetPropertyValue(object nodeClassPrsntObject, PropertyInfo propertyInfo, string valueString, List<string> paramList = null, long multiply = 1)
  207. {
  208. if (propertyInfo != null)
  209. {
  210. if (multiply != 0 && double.TryParse(valueString, out double value))
  211. {
  212. valueString = (value * multiply).ToString();
  213. }
  214. object setValue = ConvertObject(valueString, propertyInfo.PropertyType, paramList);
  215. if (setValue != null)
  216. {
  217. propertyInfo.SetValue(nodeClassPrsntObject, setValue);
  218. }
  219. else
  220. return false;
  221. }
  222. return propertyInfo != null;
  223. }
  224. public static bool TryGetPropertyValue(object nodeClassPrsntObject, PropertyInfo propertyInfo, out string outputString, List<string> paramList = null, long intOrDoubleMultiplier = 1)
  225. {
  226. outputString = null;
  227. if (nodeClassPrsntObject == null)
  228. return false;
  229. if (propertyInfo == null)
  230. return false;
  231. object value = propertyInfo.GetValue(nodeClassPrsntObject);
  232. if (value is Enum && paramList != null)
  233. {
  234. //需要解决ParamList定义的连续顺序与枚举Int值不连续的问题
  235. var enumIndex = (Int32)value;
  236. var okParamListIndex = 0;
  237. bool bFound = false;
  238. foreach (int enumValue in Enum.GetValues(value.GetType()))
  239. {
  240. if (enumValue == enumIndex)
  241. {
  242. bFound = true;
  243. break;
  244. }
  245. okParamListIndex++;
  246. }
  247. if (bFound && okParamListIndex < paramList.Count)
  248. outputString = paramList[okParamListIndex];
  249. }
  250. else if ((value is Boolean) && paramList != null && paramList.Count == 2)
  251. {
  252. outputString = paramList[((bool)value) == false ? 0 : 1];
  253. }
  254. else if (intOrDoubleMultiplier != 0
  255. && double.TryParse(value.ToString(), out double getValue))
  256. {
  257. outputString = (getValue / intOrDoubleMultiplier).ToString();
  258. }
  259. else if (value is string)
  260. {
  261. outputString = value.ToString();
  262. }
  263. return (outputString != null);
  264. }
  265. public static bool TryGetPropertyValue(object classPrsntObject, string propertyName, out string outputString, List<string> paramList = null, long intOrDoubleMultiplier = 1)
  266. {
  267. object searchPrsnt = classPrsntObject == null ? Presenter : classPrsntObject;
  268. PropertyInfo propertyInfo = searchPrsnt.GetType().GetProperty(propertyName);
  269. return TryGetPropertyValue(searchPrsnt, propertyInfo, out outputString, paramList, intOrDoubleMultiplier);
  270. }
  271. #endregion
  272. #region 通用可直接使用的Query和SettingStubFunc函数
  273. public static bool scpiQuy_CommonByUsingDeclareTable(SCPICommandProcessFuncParam analyResult, ref SCPISendMessage sendMessage)
  274. {
  275. if (TryGetPropertyInfoByUsingDeclareTablePrsntObject(analyResult, out PropertyInfo propertyInfo))
  276. {
  277. ScpiTagObj scpiTagObj = (ScpiTagObj)analyResult.Tag;
  278. if (TryGetPropertyValue(scpiTagObj.PrsntObj, propertyInfo, out string outputString, scpiTagObj.ParamList, scpiTagObj.IntOrDoubleMultiplier))
  279. {
  280. sendMessage.SendData = decodeStr(outputString);
  281. return true;
  282. }
  283. }
  284. return false;
  285. }
  286. public static bool scpiSet_CommonByUsingDeclareTable(SCPICommandProcessFuncParam analyResult)
  287. {
  288. if (!scpiSet_ParamCheck(analyResult, out string param))
  289. {
  290. return false;
  291. }
  292. if (TryGetPropertyInfoByUsingDeclareTablePrsntObject(analyResult, out PropertyInfo propertyInfo))
  293. {
  294. ScpiTagObj scpiTagObj = (ScpiTagObj)analyResult.Tag;
  295. if (TrySetPropertyValue(scpiTagObj.PrsntObj, propertyInfo, param, scpiTagObj.ParamList, scpiTagObj.IntOrDoubleMultiplier))
  296. return true;
  297. }
  298. return false;
  299. }
  300. public static bool scpiQuy_CommonByUsingDeclareTableDivide10_6(SCPICommandProcessFuncParam analyResult, ref SCPISendMessage sendMessage)
  301. {
  302. if (TryGetPropertyInfoByUsingDeclareTablePrsntObject(analyResult, out PropertyInfo propertyInfo))
  303. {
  304. ScpiTagObj scpiTagObj = (ScpiTagObj)analyResult.Tag;
  305. if (TryGetPropertyValue(scpiTagObj.PrsntObj, propertyInfo, out string outputString, scpiTagObj.ParamList, 1000_000))
  306. {
  307. sendMessage.SendData = decodeStr(outputString);
  308. return true;
  309. }
  310. }
  311. return false;
  312. }
  313. #region 调试信息
  314. private static void PrintDebug(List<Byte> inputMessage, List<byte> outputMessage)
  315. {
  316. //todo
  317. }
  318. private static List<byte> ConvertInputData(SCPICommandProcessFuncParam analyResult, int MaxLength = 100)
  319. {
  320. StringBuilder outputStrBuilder = new StringBuilder();
  321. outputStrBuilder.Append(">> " + analyResult.CmdPath + " ");
  322. bool bFirst = true;
  323. if (analyResult.Params != null)
  324. {
  325. foreach (byte[] param in analyResult.Params)
  326. {
  327. if (param != null)
  328. {
  329. StringBuilder sb = new StringBuilder();
  330. foreach (byte b in param)
  331. sb.Append((char)b);
  332. if (!bFirst)
  333. {
  334. outputStrBuilder.Append("," + sb.ToString());
  335. }
  336. else
  337. {
  338. outputStrBuilder.Append(sb.ToString());
  339. bFirst = false;
  340. }
  341. }
  342. }
  343. }
  344. string outputStr = outputStrBuilder.ToString();
  345. if (outputStr.Length > MaxLength)
  346. outputStr = outputStr.Substring(0, MaxLength);
  347. return new List<byte>(decodeStr(outputStr));
  348. }
  349. private static List<byte> ConvertOutputData(string resultStr, int MaxLength = 100)
  350. {
  351. List<byte> result = new List<byte>();
  352. if (resultStr == "")
  353. return result;
  354. if (resultStr.Length > MaxLength)
  355. resultStr = resultStr.Substring(0, MaxLength);
  356. result.AddRange(decodeStr(resultStr));
  357. return result;
  358. }
  359. private static List<byte> ConvertOutputData(byte[] resultBytes, bool bIsBinFormat = false, int MaxLength = 100)
  360. {
  361. List<byte> result = new List<byte>();
  362. if (resultBytes == null)
  363. return result;
  364. if (bIsBinFormat)
  365. result.AddRange(decodeStr("#9" + resultBytes.Length.ToString().PadLeft(9, '0')));
  366. result.AddRange(resultBytes);
  367. if (resultBytes.Length > MaxLength)
  368. result.RemoveRange(MaxLength, resultBytes.Length - MaxLength);
  369. return result;
  370. }
  371. private static byte[] ConvertBinDataFromScpiData(byte[] origin)
  372. {
  373. if (origin[0] != 0x23)
  374. {
  375. return origin;
  376. }
  377. int lengthMarkLen = origin[1] - '0';
  378. StringBuilder lenStr = new StringBuilder("");
  379. lenStr.Append(Encoding.ASCII.GetChars(origin, 2, lengthMarkLen));
  380. int dataLength = int.Parse(lenStr.ToString());
  381. byte[] resultData = new byte[dataLength];
  382. Array.Copy(origin, lengthMarkLen + 2, resultData, 0, dataLength);
  383. return resultData;
  384. }
  385. #endregion 调试信息
  386. #endregion 基础方法
  387. #region 参数检查
  388. /// <summary>
  389. /// 参数检查
  390. /// </summary>
  391. /// <param name="analyResult">传入对象</param>
  392. /// <returns></returns>
  393. private static bool scpiSet_ParamCheck(SCPICommandProcessFuncParam analyResult)
  394. {
  395. return scpiSet_ParamCheck(analyResult, out string paraStr);
  396. }
  397. /// <summary>
  398. /// 参数检查
  399. /// </summary>
  400. /// <param name="analyResult">传入对象</param>
  401. /// <param name="paraStr">返回参数</param>
  402. /// <returns></returns>
  403. private static bool scpiSet_ParamCheck(SCPICommandProcessFuncParam analyResult, out string paraStr)
  404. {
  405. paraStr = "";
  406. if (analyResult == null || analyResult.Params == null || analyResult.Params.Count == 0
  407. || string.IsNullOrWhiteSpace(encodingBytes(analyResult.Params[0])))
  408. {
  409. //todo msg - 参数为空
  410. return false;
  411. }
  412. paraStr = encodingBytes(analyResult.Params[0]).Trim();
  413. return true;
  414. }
  415. #endregion 参数检查
  416. }
  417. }