123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Globalization;
- using System.Threading.Tasks;
- using UESTC.Auto.SCPIManager;
- using Uestc.Auto6.Dso.Core;
- using System.Reflection;
- using System.ComponentModel;
- using System.Text.RegularExpressions;
- using Uestc.Auto6.Dso.Core.PassFail;
- using Uestc.Auto6.Dso.ComModel;
- namespace Uestc.Auto6.Dso.Scpi
- {
- partial class StubFunc
- {
- #region P层对象
- public static DsoPrsnt Presenter { set; get; }
- #endregion P层对象
- /// <summary>
- /// 返回值自动科学计数法 开关
- /// </summary>
- #region 公有基础方法
- #region 字符串编码
- public static string encodingBytes(byte[] bytes)
- {
- return Encoding.UTF8.GetString(bytes);
- }
- public static List<string> ParamListToStrList(List<byte[]> inputBytesList)
- {
- if (inputBytesList == null)
- {
- return null;
- }
- List<string> returnValue = new List<string>();
- foreach (byte[] bytes in inputBytesList)
- returnValue.Add(encodingBytes(bytes));
- return returnValue;
- }
- public static byte[] decodeStr(string str)
- {
- return Encoding.UTF8.GetBytes(str);
- }
- #endregion 字符串编码
- #region 命令缩写
- private static string shortCMD(string str)
- {
- int index = findLowerChar(str);
- int numIndex = findNumChar(str);
- if (index < 0)
- {
- return str;
- }
- else if (numIndex < index)
- {
- return str.Substring(0, index);
- }
- else
- {
- return str.Substring(0, index) + str.Substring(numIndex);
- }
- }
- private static int findNumChar(string str)
- {
- var lowerChar = str.FirstOrDefault(cr => cr >= '0' && cr <= '9');
- return str.IndexOf(lowerChar);
- }
- private static int findLowerChar(string str)
- {
- var lowerChar = str.FirstOrDefault(cr => cr >= 'a');
- return str.IndexOf(lowerChar);
- }
- #endregion 命令缩写
- #region 将一个对象转换为指定类型
- /// <summary>
- /// 将一个对象转换为指定类型
- /// </summary>
- /// <param name="obj">待转换的对象</param>
- /// <param name="type">目标类型</param>
- /// <returns>转换后的对象</returns>
- private static object ConvertObject(string obj, Type type, List<string> paramList)
- {
- if (type == null || obj == null) return null;
- if (type.IsEnum)
- {
- if (paramList == null)
- return null;
- //列表计较,并得到相应的index
- int indexOflist = 0;
- int foundIndex = -1;
- foreach (string paramName in paramList)
- {
- if ((paramName == obj) || obj == shortCMD(paramName))
- {
- foundIndex = indexOflist;
- break;
- }
- indexOflist++;
- }
- if (foundIndex == -1)
- return null;
- //设置枚举值,按顺序来设置,避免Int值的不连续性
- int enumIndex = 0;
- int foundEnumIndex = -1;
- foreach (int enumValue in Enum.GetValues(type))
- {
- if (enumIndex == foundIndex)
- {
- foundEnumIndex = enumValue;
- break;
- }
- enumIndex++;
- }
- if (foundEnumIndex >= 0)
- return foundEnumIndex;
- return null;
- }
- else if (type.IsValueType)
- {
- if (type != typeof(Boolean))
- {
- if (type == typeof(Int32))
- {
- if (Int32.TryParse(obj, out Int32 v))
- return v;
- }
- else if (type == typeof(double))
- {
- if (obj.Contains('e') || (obj.Contains('E')))
- {
- if (double.TryParse(obj, NumberStyles.AllowExponent, null, out double v))
- return v;
- }
- else if (double.TryParse(obj, out double v))
- return v;
- }
- else if (type == typeof(UInt32))
- {
- if (UInt32.TryParse(obj, out UInt32 v))
- return v;
- }
- else if (type == typeof(Int16))
- {
- if (Int16.TryParse(obj, out Int16 v))
- return v;
- }
- else if (type == typeof(UInt16))
- {
- if (UInt16.TryParse(obj, out UInt16 v))
- return v;
- }
- else if (type == typeof(long))
- {
- if (long.TryParse(obj, out long v))
- return v;
- }
- else if (type == typeof(float))
- {
- if (float.TryParse(obj, out float v))
- return v;
- }
- return null;
- }
- //Boolean
- if (paramList == null)
- return false;
- if (paramList.Count != 2)
- return false;
- if ((paramList[0] == obj) || obj == shortCMD(paramList[0]))
- return false;
- if ((paramList[1] == obj) || obj == shortCMD(paramList[1]))
- return true;
- return null;
- }
- else if (type == typeof(String))
- return obj;
- return null;
- }
- #endregion 将一个对象转换为指定类型
- public static bool TryGetPropertyInfoByUsingDeclareTablePrsntObject(SCPICommandProcessFuncParam analyResult, out PropertyInfo propertyInfo)
- {
- propertyInfo = null;
- if ((analyResult.Tag is null) || (analyResult.Tag is not ScpiTagObj))
- return false;
- ScpiTagObj scpiTagObj = (ScpiTagObj)analyResult.Tag;
- if (scpiTagObj.PrsntObj is null || scpiTagObj.PropertyName is null || scpiTagObj.PropertyName.Trim() == "")
- return false;
- propertyInfo = scpiTagObj.PrsntObj.GetType().GetProperty(scpiTagObj.PropertyName);
- if (propertyInfo == null)
- return false;
- if (propertyInfo.GetType() == typeof(Enum))
- {
- if (scpiTagObj.ParamList == null)
- return false;
- }
- return true;
- }
- public static bool TryGetPropertyInfo(object obj, string propertyName, out PropertyInfo propertyInfo)
- {
- propertyInfo = null;
- if (obj != null)
- propertyInfo = obj.GetType().GetProperty(propertyName);
- return propertyInfo != null;
- }
- public static bool TrySetPropertyValue(object nodeClassPrsntObject, PropertyInfo propertyInfo, string valueString, List<string> paramList = null, long multiply = 1)
- {
- if (propertyInfo != null)
- {
- if (multiply != 0 && double.TryParse(valueString, out double value))
- {
- valueString = (value * multiply).ToString();
- }
- object setValue = ConvertObject(valueString, propertyInfo.PropertyType, paramList);
- if (setValue != null)
- {
- propertyInfo.SetValue(nodeClassPrsntObject, setValue);
- }
- else
- return false;
- }
- return propertyInfo != null;
- }
- public static bool TryGetPropertyValue(object nodeClassPrsntObject, PropertyInfo propertyInfo, out string outputString, List<string> paramList = null, long intOrDoubleMultiplier = 1)
- {
- outputString = null;
- if (nodeClassPrsntObject == null)
- return false;
- if (propertyInfo == null)
- return false;
- object value = propertyInfo.GetValue(nodeClassPrsntObject);
- if (value is Enum && paramList != null)
- {
- //需要解决ParamList定义的连续顺序与枚举Int值不连续的问题
- var enumIndex = (Int32)value;
- var okParamListIndex = 0;
- bool bFound = false;
- foreach (int enumValue in Enum.GetValues(value.GetType()))
- {
- if (enumValue == enumIndex)
- {
- bFound = true;
- break;
- }
- okParamListIndex++;
- }
- if (bFound && okParamListIndex < paramList.Count)
- outputString = paramList[okParamListIndex];
- }
- else if ((value is Boolean) && paramList != null && paramList.Count == 2)
- {
- outputString = paramList[((bool)value) == false ? 0 : 1];
- }
- else if (intOrDoubleMultiplier != 0
- && double.TryParse(value.ToString(), out double getValue))
- {
- outputString = (getValue / intOrDoubleMultiplier).ToString();
- }
- else if (value is string)
- {
- outputString = value.ToString();
- }
- return (outputString != null);
- }
- public static bool TryGetPropertyValue(object classPrsntObject, string propertyName, out string outputString, List<string> paramList = null, long intOrDoubleMultiplier = 1)
- {
- object searchPrsnt = classPrsntObject == null ? Presenter : classPrsntObject;
- PropertyInfo propertyInfo = searchPrsnt.GetType().GetProperty(propertyName);
- return TryGetPropertyValue(searchPrsnt, propertyInfo, out outputString, paramList, intOrDoubleMultiplier);
- }
- #endregion
- #region 通用可直接使用的Query和SettingStubFunc函数
- public static bool scpiQuy_CommonByUsingDeclareTable(SCPICommandProcessFuncParam analyResult, ref SCPISendMessage sendMessage)
- {
- if (TryGetPropertyInfoByUsingDeclareTablePrsntObject(analyResult, out PropertyInfo propertyInfo))
- {
- ScpiTagObj scpiTagObj = (ScpiTagObj)analyResult.Tag;
- if (TryGetPropertyValue(scpiTagObj.PrsntObj, propertyInfo, out string outputString, scpiTagObj.ParamList, scpiTagObj.IntOrDoubleMultiplier))
- {
- sendMessage.SendData = decodeStr(outputString);
- return true;
- }
- }
- return false;
- }
- public static bool scpiSet_CommonByUsingDeclareTable(SCPICommandProcessFuncParam analyResult)
- {
- if (!scpiSet_ParamCheck(analyResult, out string param))
- {
- return false;
- }
- if (TryGetPropertyInfoByUsingDeclareTablePrsntObject(analyResult, out PropertyInfo propertyInfo))
- {
- ScpiTagObj scpiTagObj = (ScpiTagObj)analyResult.Tag;
- if (TrySetPropertyValue(scpiTagObj.PrsntObj, propertyInfo, param, scpiTagObj.ParamList, scpiTagObj.IntOrDoubleMultiplier))
- return true;
- }
- return false;
- }
- public static bool scpiQuy_CommonByUsingDeclareTableDivide10_6(SCPICommandProcessFuncParam analyResult, ref SCPISendMessage sendMessage)
- {
- if (TryGetPropertyInfoByUsingDeclareTablePrsntObject(analyResult, out PropertyInfo propertyInfo))
- {
- ScpiTagObj scpiTagObj = (ScpiTagObj)analyResult.Tag;
- if (TryGetPropertyValue(scpiTagObj.PrsntObj, propertyInfo, out string outputString, scpiTagObj.ParamList, 1000_000))
- {
- sendMessage.SendData = decodeStr(outputString);
- return true;
- }
- }
- return false;
- }
- #region 调试信息
- private static void PrintDebug(List<Byte> inputMessage, List<byte> outputMessage)
- {
- //todo
- }
- private static List<byte> ConvertInputData(SCPICommandProcessFuncParam analyResult, int MaxLength = 100)
- {
- StringBuilder outputStrBuilder = new StringBuilder();
- outputStrBuilder.Append(">> " + analyResult.CmdPath + " ");
- bool bFirst = true;
- if (analyResult.Params != null)
- {
- foreach (byte[] param in analyResult.Params)
- {
- if (param != null)
- {
- StringBuilder sb = new StringBuilder();
- foreach (byte b in param)
- sb.Append((char)b);
- if (!bFirst)
- {
- outputStrBuilder.Append("," + sb.ToString());
- }
- else
- {
- outputStrBuilder.Append(sb.ToString());
- bFirst = false;
- }
- }
- }
- }
- string outputStr = outputStrBuilder.ToString();
- if (outputStr.Length > MaxLength)
- outputStr = outputStr.Substring(0, MaxLength);
- return new List<byte>(decodeStr(outputStr));
- }
- private static List<byte> ConvertOutputData(string resultStr, int MaxLength = 100)
- {
- List<byte> result = new List<byte>();
- if (resultStr == "")
- return result;
- if (resultStr.Length > MaxLength)
- resultStr = resultStr.Substring(0, MaxLength);
- result.AddRange(decodeStr(resultStr));
- return result;
- }
- private static List<byte> ConvertOutputData(byte[] resultBytes, bool bIsBinFormat = false, int MaxLength = 100)
- {
- List<byte> result = new List<byte>();
- if (resultBytes == null)
- return result;
- if (bIsBinFormat)
- result.AddRange(decodeStr("#9" + resultBytes.Length.ToString().PadLeft(9, '0')));
- result.AddRange(resultBytes);
- if (resultBytes.Length > MaxLength)
- result.RemoveRange(MaxLength, resultBytes.Length - MaxLength);
- return result;
- }
- private static byte[] ConvertBinDataFromScpiData(byte[] origin)
- {
- if (origin[0] != 0x23)
- {
- return origin;
- }
- int lengthMarkLen = origin[1] - '0';
- StringBuilder lenStr = new StringBuilder("");
- lenStr.Append(Encoding.ASCII.GetChars(origin, 2, lengthMarkLen));
- int dataLength = int.Parse(lenStr.ToString());
- byte[] resultData = new byte[dataLength];
- Array.Copy(origin, lengthMarkLen + 2, resultData, 0, dataLength);
- return resultData;
- }
- #endregion 调试信息
- #endregion 基础方法
- #region 参数检查
- /// <summary>
- /// 参数检查
- /// </summary>
- /// <param name="analyResult">传入对象</param>
- /// <returns></returns>
- private static bool scpiSet_ParamCheck(SCPICommandProcessFuncParam analyResult)
- {
- return scpiSet_ParamCheck(analyResult, out string paraStr);
- }
- /// <summary>
- /// 参数检查
- /// </summary>
- /// <param name="analyResult">传入对象</param>
- /// <param name="paraStr">返回参数</param>
- /// <returns></returns>
- private static bool scpiSet_ParamCheck(SCPICommandProcessFuncParam analyResult, out string paraStr)
- {
- paraStr = "";
- if (analyResult == null || analyResult.Params == null || analyResult.Params.Count == 0
- || string.IsNullOrWhiteSpace(encodingBytes(analyResult.Params[0])))
- {
- //todo msg - 参数为空
- return false;
- }
- paraStr = encodingBytes(analyResult.Params[0]).Trim();
- return true;
- }
- #endregion 参数检查
- }
- }
|