Exemplo n.º 1
0
        private static void FindFinalGS(ref List <ParsRowView> rowViewList, ref List <TemplateView> TvList, string msgCurrentFieldContent)
        {
            string[] gsGroup              = msgCurrentFieldContent.Split(XDCSplictorChar.GS);
            int      dataArrayIndex       = 0;
            string   tempDataValue        = gsGroup[dataArrayIndex];
            string   deviceID             = null;
            bool     isFoundGS            = false;
            bool     occurGS              = false;
            int      currentContextIndex  = 0;
            int      xmlIndex             = 0;
            bool     emptyAfterVT         = false;
            bool     isVTRepeat           = false;
            int      vtRepeatXMLnodeCount = 0;

            foreach (string gsDataItem in gsGroup)
            {
                DDCdeviceID.CheckDeviceID(gsDataItem, out deviceID);
                isFoundGS           = false;
                occurGS             = false;
                currentContextIndex = 0;
                string   TempgsDataItem = gsDataItem;
                string[] rsDataItemList = gsDataItem.Split(XDCSplictorChar.RS);
                foreach (string rsData in rsDataItemList)
                {
                    vtRepeatXMLnodeCount = 0;
                    isVTRepeat           = false;
                    string[] vtDataArray;
                    if (rsData.Contains(XDCSplictorChar.VT2))
                    {
                        vtDataArray = rsData.Split(XDCSplictorChar.VT2);
                    }
                    else
                    {
                        vtDataArray = rsData.Split(XDCSplictorChar.VT);
                    }
                    foreach (string vtDataItem in vtDataArray)
                    {
                        vtRepeatXMLnodeCount = 0;
                        deviceID             = "";
                        currentContextIndex  = 0;
                        DDCdeviceID.CheckDeviceID(vtDataItem, out deviceID);
                        for (int i = xmlIndex; i < TvList.Count; i++)
                        {
                            if (vtDataItem.Length > 0 && (currentContextIndex > vtDataItem.Length - 1) &&
                                (TvList[i].FieldName.StartsWith("GS") ||
                                 TvList[i].FieldName.StartsWith("RS") ||
                                 TvList[i].FieldName.StartsWith("VT")))
                            {
                                if (!TvList[i].FieldName.StartsWith("GS"))
                                {
                                }
                                if (isVTRepeat)
                                {
                                    xmlIndex = xmlIndex - vtRepeatXMLnodeCount - 1;
                                    //vtRepeatXMLnodeCount = 0;
                                    //isVTRepeat = false;
                                }
                                break;
                            }
                            xmlIndex++;
                            if (!TvList[i].FieldName.StartsWith("GS") && occurGS && !isFoundGS)
                            {
                                //如果之前遇到了GS开头的,但是又没找到匹配的,且,此时,又不是GS开头。则跳过这一段GS_x
                                continue;
                            }
                            if (TvList[i].FieldName.StartsWith("GS"))
                            {
                                //遇到GS开头,记录出现GS_
                                occurGS = true;
                                if (TvList[i].FieldName == "GS**")
                                {
                                    isVTRepeat = true;
                                    //如果匹配
                                    isFoundGS = true;
                                    //新建一行GS
                                    rowViewList.Add(new ParsRowView("GS", "", ""));
                                }
                                else if (TvList[i].FieldName == "GS" + deviceID)
                                {
                                    //如果匹配
                                    isFoundGS = true;
                                    //新建一行GS
                                    rowViewList.Add(new ParsRowView("GS", "", ""));
                                    //currentContextIndex += deviceID.Length;
                                }
                            }
                            else if (TvList[i].FieldName.StartsWith("RS"))
                            {
                                //遇到GS开头,记录出现GS_
                                occurGS = true;
                                if (TvList[i].FieldName == "RS" + deviceID)
                                {
                                    //如果匹配
                                    isFoundGS = true;
                                    //新建一行GS
                                    rowViewList.Add(new ParsRowView("RS", "", ""));
                                    //currentContextIndex += deviceID.Length;
                                }
                            }
                            else if (TvList[i].FieldName == "RS")
                            {
                                rowViewList.Add(new ParsRowView("RS", "", ""));
                            }
                            else if (TvList[i].FieldName.StartsWith("VT"))
                            {
                                if (TvList[i].FieldName == "VT**")
                                {
                                    //可以循环遍历
                                    isVTRepeat = true;
                                }
                                if (emptyAfterVT)
                                {
                                    xmlIndex--;
                                    emptyAfterVT = false;
                                    break;
                                }
                                rowViewList.Add(new ParsRowView("VT", "", ""));
                                if (string.IsNullOrEmpty(vtDataItem))
                                {
                                    emptyAfterVT = true;
                                }
                            }
                            else
                            {
                                if (isVTRepeat)
                                {
                                    vtRepeatXMLnodeCount++;
                                }
                                string tempText = "";
                                if (TvList[i].FieldSize > 0)
                                {
                                    try
                                    {
                                        tempText = vtDataItem.Substring(currentContextIndex, TvList[i].FieldSize);
                                    }
                                    catch
                                    {
                                        tempText = "";
                                    }
                                    finally
                                    {
                                        currentContextIndex += TvList[i].FieldSize;
                                    }
                                }
                                else
                                {
                                    try
                                    {
                                        tempText             = vtDataItem.Substring(currentContextIndex, vtDataItem.Length - currentContextIndex);
                                        currentContextIndex += vtDataItem.Length - currentContextIndex;
                                    }
                                    catch
                                    {
                                    }
                                }
                                string tempComment = "";
                                if (TvList[i].FieldValue != null)
                                {
                                    if (TvList[i].FieldValue.ContainsKey(tempText))
                                    {
                                        tempComment = TvList[i].FieldValue[tempText];
                                    }
                                    else if (TvList[i].FieldValue.ContainsKey("*"))
                                    {
                                        tempComment = TvList[i].FieldValue["*"];
                                    }
                                    else
                                    {
                                        tempComment = "";
                                    }
                                }
                                else
                                {
                                    tempComment = "";
                                }
                                rowViewList.Add(new ParsRowView(TvList[i].FieldName, tempText, tempComment));
                            }

                            if (isVTRepeat && i == TvList.Count - 1)
                            {
                                xmlIndex   = xmlIndex - vtRepeatXMLnodeCount - 1;
                                isVTRepeat = false;
                            }
                        }
                    }
                    if (isVTRepeat)
                    {
                        xmlIndex   = xmlIndex + vtRepeatXMLnodeCount + 1;
                        isVTRepeat = false;
                    }
                }
            }
        }
        /// <summary>
        /// 获取xml节点列表
        /// </summary>
        /// <returns></returns>
        public static List <TemplateView> GetXmlConfig(XDCMessage CurrentMessage)
        {
            string[] tempArrary = CurrentMessage.MsgASCIIStringFields;
            XmlNode  root       = null;
            string   attrID     = "";
            //1NDC|2DDC
            string attrProtocolType = "2";
            //1State
            //2Screen
            //3Fit
            //4Message
            string  attrDataType = "4";
            XmlNode cur          = null;
            string  commandCode  = "";

            string MessageIdentifier = "";
            int    sdIndex           = 3;

            root = XMLHelper.instance.XMLFiles["ProtocolTemplate"].XmlDoc.SelectSingleNode("ProtocolTemplate");
            switch (CurrentMessage.MsgType)
            {
            case MessageType.DataCommand:
            {
                #region 3x_DataCommand

                commandCode       = tempArrary[3].Substring(0, 1);
                MessageIdentifier = commandCode;
                attrID            = "3FSFSFS" + commandCode;
                cur = XDCUnity.GetNodeDetail(root, attrID, attrProtocolType, attrDataType);
                if (null == cur)
                {
                    //通常查询xml文件中ID=1的配置
                    attrID = tempArrary[0].Substring(0, 1);
                    cur    = XDCUnity.GetNodeDetail(root, attrID, attrProtocolType, attrDataType);
                }
                #endregion
            }
            break;

            case MessageType.TerminalCommand:
            {
                #region 1x_TerminalCommand

                commandCode = tempArrary[3].Substring(0, 1);
                //TerminalCommand消息中,MessageIdentifier的值等于commandCode,因为
                //TerminalCommand消息LUNO号后只有一个FS
                MessageIdentifier = commandCode;
                attrID            = "1FSFSFS" + commandCode;
                cur = XDCUnity.GetNodeDetail(root, attrID, attrProtocolType, attrDataType);
                if (null == cur)
                {
                    //通常查询xml文件中ID=1的配置
                    attrID = tempArrary[0].Substring(0, 1);
                    cur    = XDCUnity.GetNodeDetail(root, attrID, attrProtocolType, attrDataType);
                }
                #endregion
            }
            break;

            case MessageType.SolicitedMessage:
            {
                #region 22||23_SolicitedMessage
                attrID = tempArrary[0] + "FSFSFS";
                int fsCount = 3;
                try
                {
                    commandCode = tempArrary[sdIndex].Substring(0, 1);
                }
                catch
                {
                    commandCode = "";
                }
                if (tempArrary.Length > sdIndex + 1)
                {
                    MessageIdentifier = tempArrary[sdIndex + 1].Substring(0, 1);
                }
                attrID += commandCode;
                string tempXMLid = "";
                string miString  = "";
                if (commandCode == "8" ||
                    commandCode == "C" ||
                    commandCode == "F")
                {
                    miString = "FS" + MessageIdentifier;
                    fsCount++;
                }

                int maxFsCountRear = tempArrary.Length - fsCount - 1;
                //最多的FS,并逐个递减
                while (maxFsCountRear > -1)
                {
                    tempXMLid = attrID + miString + XDCUnity.GetFS(maxFsCountRear);
                    cur       = XDCUnity.GetNodeDetail(root, tempXMLid, attrProtocolType, attrDataType);
                    if (null != cur)
                    {
                        break;
                    }
                    maxFsCountRear--;
                }
                //如果还未空,去掉MessageIdentifier
                if (null == cur)
                {
                    tempXMLid = attrID;
                    cur       = XDCUnity.GetNodeDetail(root, tempXMLid, attrProtocolType, attrDataType);
                    if (null == cur)
                    {
                        tempXMLid = tempArrary[0];
                        cur       = XDCUnity.GetNodeDetail(root, tempXMLid, attrProtocolType, attrDataType);
                    }
                }
                #endregion
            }
            break;

            case MessageType.UnSolicitedMessage:
            {
                #region 12||11_UnSolicitedMessage
                try
                {
                    commandCode = tempArrary[3].Substring(0, 1);
                }
                catch
                {
                    commandCode = "";
                }
                MessageIdentifier = commandCode;
                attrID            = tempArrary[0] + "FSFSFS" + commandCode;
                if (tempArrary[0].Equals("11"))
                {
                    try
                    {
                        MessageIdentifier = tempArrary[13].Substring(0, 1);
                    }
                    catch
                    {
                        MessageIdentifier = "";
                    }
                }

                cur = XDCUnity.GetNodeDetail(root, attrID, attrProtocolType, attrDataType);
                if (null == cur)
                {
                    attrID = tempArrary[0];
                    cur    = XDCUnity.GetNodeDetail(root, attrID, attrProtocolType, attrDataType);
                }
                #endregion
            }
            break;

            case MessageType.TransactionReplyCommand:
            {
                #region 4_TransactionReplyCommand
                //扩张功能命令:‘
                attrID = "4";
                if (CurrentMessage.MsgASCIIString.Contains(XDCSplictorChar.FS + "'"))
                {
                    MessageIdentifier = "'";
                }
                cur = XDCUnity.GetNodeDetail(root, attrID, attrProtocolType, attrDataType);

                #endregion
            }
            break;

            case MessageType.UploadEJMessage:
            {
                #region 6x_UploadEJMessage
                try
                {
                    commandCode       = tempArrary[3].Substring(0, 1);
                    MessageIdentifier = commandCode;
                }
                catch
                {
                    commandCode       = "";
                    MessageIdentifier = commandCode;
                }
                attrID = tempArrary[0].Substring(0, tempArrary[0].Length);
                cur    = XDCUnity.GetNodeDetail(root, attrID, attrProtocolType, attrDataType);
                if (null == cur)
                {
                    attrID = tempArrary[0].Substring(0, 1);
                    cur    = XDCUnity.GetNodeDetail(root, attrID, attrProtocolType, attrDataType);
                }
                #endregion
            }
            break;

            case MessageType.ExitToHostMessages:
            {
            }
            break;

            case MessageType.HostToExitMessages:
            {
                #region 7_HostToExitMessages

                attrID = tempArrary[0].Substring(0, 1);
                cur    = XDCUnity.GetNodeDetail(root, attrID, attrProtocolType, attrDataType);

                #endregion
            }
            break;

            case MessageType.Unknow:
                break;

            default:
                break;
            }
            if (null == cur)
            {
                return(null);
            }
            bool alreadyFS_X           = true;
            List <TemplateView> TvList = new List <TemplateView>();
            foreach (XmlNode item in cur.ChildNodes)
            {
                TemplateView tv        = new TemplateView();
                XmlAttribute fieldName = item.Attributes["Name"];
                XmlAttribute fieldSize = item.Attributes["Size"];

                if (fieldName != null)
                {
                    tv.FieldName = fieldName.Value;
                }
                if ((tv.FieldName.StartsWith("FS") && tv.FieldName.Length > 2) ||
                    (tv.FieldName.StartsWith("GS") && tv.FieldName.Length > 2))
                {
                    if (tv.FieldName.Contains('|'))
                    {
                        string[] fsSpliterArray = tv.FieldName.Replace("FS", "").Split('|');
                        foreach (string fsSpliterStr in fsSpliterArray)
                        {
                            if (MessageIdentifier == fsSpliterStr)
                            {
                                tv.FieldName = "FS";
                                alreadyFS_X  = true;
                                break;
                            }
                            else
                            {
                                alreadyFS_X = false;
                            }
                        }
                        if (!alreadyFS_X)
                        {
                            continue;
                        }
                    }
                    else if (tv.FieldName.Equals("FS" + "*"))
                    {
                        //找到FS*
                        tv.FieldName = "FS*";
                        alreadyFS_X  = true;
                    }
                    //已FS开头,但是不等於FS/GS的需要越过两行
                    else if (!tv.FieldName.Equals("FS" + MessageIdentifier) &&
                             !tv.FieldName.Equals("GS" + MessageIdentifier))
                    {
                        string tempDeviceID = "";
                        if (DDCdeviceID.CheckDeviceID(tv.FieldName.Substring(2, tv.FieldName.Length - 2), out tempDeviceID))
                        {
                            tv.FieldName = tv.FieldName.Substring(0, 2);
                            alreadyFS_X  = true;
                        }
                        else
                        {
                            alreadyFS_X = false;
                            continue;
                        }
                    }
                    else
                    {
                        tv.FieldName = tv.FieldName.Substring(0, 2);
                        alreadyFS_X  = true;
                    }
                }
                else if (tv.FieldName.Equals("FS"))
                {
                    alreadyFS_X = true;
                }
                if (!alreadyFS_X)
                {
                    continue;
                }

                if (fieldSize != null)
                {
                    int  fSize;
                    bool isNumeric = int.TryParse(fieldSize.Value, out fSize);
                    if (isNumeric)
                    {
                        tv.FieldSize = fSize;
                    }
                    else
                    {
                        //非数字的情况,如*
                        tv.FieldSize = -1;
                    }
                }
                if (item.HasChildNodes)
                {
                    Dictionary <string, string> dic = new Dictionary <string, string>();
                    foreach (XmlNode valueItem in item.ChildNodes)
                    {
                        XmlAttribute attrComment   = valueItem.Attributes["Comment"];
                        XmlAttribute attrOperation = valueItem.Attributes["Operation"];
                        if (attrOperation != null)
                        {
                            //进行&运算
                            if (attrOperation.Value.StartsWith("&"))
                            {
                                //&运算
                                string ampValue = attrOperation.Value.Replace("&", "");
                                dic.Add(valueItem.InnerText + "&amp;" + ampValue, attrComment.Value);
                            }
                        }
                        else if (null != attrComment &&
                                 !dic.ContainsKey(valueItem.InnerText))
                        {
                            dic.Add(valueItem.InnerText, attrComment.Value);
                        }
                    }
                    tv.FieldValue = dic;
                }
                TvList.Add(tv);
            }


            return(TvList);
        }