예제 #1
0
        private bool ParseData(string msg, string gprs, uint modemId)
        {
            //           InvokeMessage("协议。。。 ", "进入函数7");
            try
            {
                string rawData = msg;
                string sid;
                try
                {
                    sid = Manager.XmlStationData.Instance.GetStationByGprsID(gprs);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("查找站点id失败,赋值为9999" + e.ToString());
                    sid = "9999";
                }
                if (msg.Contains("$"))
                {
                    string data = string.Empty;
                    if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                    {
                        return(false);
                    }
                    msg = data;

                    CReportStruct report     = new CReportStruct();
                    CDownConf     downReport = new CDownConf();
                    string        protocol   = Manager.XmlStationData.Instance.GetProtocolBySId(sid);

                    if (protocol == "YNXY")
                    {
                        Up   = new Data.YNXY.UpParser();
                        Down = new Data.YNXY.DownParser();
                    }
                    else if (protocol == "ZYJBX")
                    {
                        Up   = new Data.ZYJBX.UpParser();
                        Down = new Data.ZYJBX.DownParser();
                    }


                    #region 1K
                    //批量传输解析
                    if (msg.Contains("1K"))
                    {
                        var station = FindStationBySID(sid);
                        if (station == null)
                        {
                            throw new Exception("批量传输,站点匹配错误");
                        }
                        CBatchStruct batch = new CBatchStruct();
                        InvokeMessage(String.Format("{0,-10}   ", "批量传输") + msg, "接收");

                        if (Down.Parse_Flash(msg, EChannelType.GPRS, out batch))
                        {
                            if (this.BatchDataReceived != null)
                            {
                                this.BatchDataReceived.Invoke(null, new BatchEventArgs()
                                {
                                    Value = batch, RawData = msg
                                });
                            }
                        }
                    }
                    #endregion

                    #region 1G
                    if (msg.Contains("1G"))
                    {
                        //if (msg.Contains("RG"))
                        //{
                        //    var lists = msg.Split('$');
                        //    foreach (var segMsg in lists)
                        //    {
                        //        if (segMsg.Length < 5)
                        //        {
                        //            continue;
                        //        }
                        //        string plusMsg = "$" + segMsg;
                        //        string rtype = "人工报";
                        //        InvokeMessage("gprs号码:  " + gprs + "   " + String.Format("{0,-10}   ", rtype) + plusMsg, "接收");
                        //        string package = Up.Parse_1(plusMsg, out report);
                        //        if (package != "")
                        //        {
                        //            report.ChannelType = EChannelType.GPRS;
                        //            report.ListenPort = this.GetListenPort().ToString();
                        //            report.flagId = gprs;
                        //            SendTru(modemId, package);
                        //            //TODO 重新定义事件
                        //            if (this.UpDataReceived != null)
                        //            {
                        //                this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = plusMsg });
                        //            }
                        //        }
                        //        else
                        //        {
                        //            InvokeMessage(" " + rawData, "接收");
                        //        }
                        //    }
                        //}
                        //else
                        if (msg.Contains("1G21") || msg.Contains("1G22") || msg.Contains("1G25"))   //   定时报,加报
                        {
                            if (msg.Contains("1G29"))
                            {
                                var station = FindStationBySID(sid);
                                if (station == null)
                                {
                                    throw new Exception("批量传输,站点匹配错误");
                                }
                                CBatchStruct batch = new CBatchStruct();
                                InvokeMessage(String.Format("{0,-10}   ", "批量传输") + msg, "接收");
                                if (Down.Parse_Batch(msg, out batch))
                                {
                                    if (this.BatchDataReceived != null)
                                    {
                                        this.BatchDataReceived.Invoke(null, new BatchEventArgs()
                                        {
                                            Value = batch, RawData = msg
                                        });
                                    }
                                }
                            }
                            else
                            {
                                var lists = msg.Split('$');
                                foreach (var segMsg in lists)
                                {
                                    if (segMsg.Length < 5)
                                    {
                                        continue;
                                    }
                                    string plusMsg = "$" + segMsg;
                                    if (Up.Parse(plusMsg, out report))
                                    {
                                        report.ChannelType = EChannelType.GPRS;
                                        report.ListenPort  = this.GetListenPort().ToString();
                                        report.flagId      = gprs;
                                        string rtype = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                        InvokeMessage("gprs号码:  " + gprs + "   " + String.Format("{0,-10}   ", rtype) + plusMsg, "接收");
                                        //TODO 重新定义事件
                                        if (this.UpDataReceived != null)
                                        {
                                            this.UpDataReceived.Invoke(null, new UpEventArgs()
                                            {
                                                Value = report, RawData = plusMsg
                                            });
                                        }
                                    }
                                    else
                                    {
                                        InvokeMessage(" " + rawData, "接收");
                                    }
                                }
                            }
                        }
                        else
                        {
                            Down.Parse(rawData, out downReport);
                            if (downReport != null)
                            {
                                InvokeMessage(String.Format("{0,-10}   ", "下行指令读取参数") + rawData, "接收");
                                if (this.DownDataReceived != null)
                                {
                                    this.DownDataReceived.Invoke(null, new DownEventArgs()
                                    {
                                        Value = downReport, RawData = rawData
                                    });
                                }
                            }
                        }
                        #endregion
                    }
                }
                else if (msg.Contains("BEG"))
                {
                    CSDStruct sd = new CSDStruct();
                    if (Down.Parse_SD(msg, sid, out sd))
                    {
                        InvokeMessage(String.Format("{0,-10}   ", "批量SD传输") + msg, "接收");

                        if (this.BatchSDDataReceived != null)
                        {
                            this.BatchSDDataReceived.Invoke(null, new BatchSDEventArgs()
                            {
                                Value = sd, RawData = msg
                            });
                        }
                    }
                }
                else if (msg.Contains("#"))
                {
                    CEntitySoilData soil       = new CEntitySoilData();
                    CReportStruct   soilReport = new CReportStruct();
                    if (Soil.Parse(rawData, out soil, out soilReport))
                    {
                        soil.ChannelType = EChannelType.GPRS;

                        if (null != this.SoilDataReceived)
                        {
                            this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil));
                        }

                        if (null != soilReport && null != this.UpDataReceived)
                        {
                            soilReport.ChannelType = EChannelType.GPRS;
                            soilReport.ListenPort  = this.GetListenPort().ToString();
                            this.UpDataReceived(null, new UpEventArgs()
                            {
                                RawData = rawData, Value = soilReport
                            });
                        }
                    }
                }
                else
                {
                    return(false);
                }
                return(true);
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("GPRS 数据解析出错 !" + msg + "\r\n" + exp.Message);
            }
            return(false);
        }
        private void DealData()
        {
            while (true)
            {
                m_semaphoreData.WaitOne(); //阻塞当前线程,知道被其它线程唤醒
                // 获取对data内存缓存的访问权
                m_mutexListDatas.WaitOne();
                List <HDModemDataStruct> dataListTmp = m_listDatas;
                m_listDatas = new List <HDModemDataStruct>(); //开辟一快新的缓存区
                m_mutexListDatas.ReleaseMutex();
                for (int i = 0; i < dataListTmp.Count; ++i)
                {
                    try
                    {
                        HDModemDataStruct dat      = dataListTmp[i];
                        string            data     = System.Text.Encoding.Default.GetString(dat.m_data_buf).TrimEnd('\0');
                        string            recvData = data.Trim();

                        InvokeMessage(data, "原始数据");

                        string temp = data.Trim();

                        string gprs = System.Text.Encoding.Default.GetString(dat.m_modemId);
                        gprs = gprs.Replace("\0", "");
                        string sid = Manager.XmlStationData.Instance.GetStationByGprsID(gprs);

                        string result = null;
                        if (temp.Contains("TRU"))
                        {
                            Debug.WriteLine("接收数据TRU完成,停止计时器");
                            //m_timer.Stop();
                            InvokeMessage("TRU " + System.Text.Encoding.Default.GetString(dat.m_modemId), "接收");
                            if (this.ErrorReceived != null)
                            {
                                this.ErrorReceived.Invoke(null, new ReceiveErrorEventArgs()
                                {
                                    //   Msg = "TRU " + dat.m_modemId
                                    Msg = "TRU " + System.Text.Encoding.Default.GetString(dat.m_modemId)
                                });
                            }
                        }
                        if (temp.Contains("ATE0"))
                        {
                            Debug.WriteLine("接收数据ATE0完成,停止计时器");
                            //m_timer.Stop();
                            // InvokeMessage("ATE0", "接收");
                            if (this.ErrorReceived != null)
                            {
                                this.ErrorReceived.Invoke(null, new ReceiveErrorEventArgs()
                                {
                                    Msg = "ATE0"
                                });
                            }
                        }
                        if (temp.Contains("$"))
                        {
                            result = temp.Substring(temp.IndexOf("$"));
                            int length = int.Parse(result.Substring(11, 4));
                            //获取报文长度
                            if (length > MAX_BUFFER)
                            {
                                continue;
                            }

                            result = result.Substring(0, length);

                            if (!(result.StartsWith("$") && result.EndsWith("\r\n")))
                            {
                                InvokeMessage(result + "报文开始符结束符不合法", "接收");
                            }

                            String dataProtocol = Manager.XmlStationData.Instance.GetProtocolBySId(sid);

                            CReportStruct report     = new CReportStruct();
                            CDownConf     downReport = new CDownConf();
                            if (dataProtocol == "RG30")
                            {
                                Up   = new Data.RG30.UpParser();
                                Down = new Data.RG30.DownParser();
                            }

                            if (dataProtocol == "SM100H")
                            {
                                Up   = new Data.SM100H.UpParser();
                                Down = new Data.SM100H.DownParser();
                            }
                            //时差法
                            if (dataProtocol == "TDXY")
                            {
                                Up   = new Data.TDXY.UpParser();
                                Down = new Data.TDXY.DownParser();
                            }

                            //中游局协议
                            if (dataProtocol == "ZYJBX")
                            {
                                Up   = new Data.ZYJBX.UpParser();
                                Down = new Data.ZYJBX.DownParser();
                            }

                            //蒸发协议
                            if (dataProtocol == "ZFXY")
                            {
                                Up   = new Data.ZFXY.UpParse();
                                Down = new Data.ZFXY.DownParse();
                            }

                            if (dataProtocol == "EN2B")
                            {
                                Up   = new Data.EN2B.UpParser();
                                Down = new Data.EN2B.DownParser();
                            }
                            if (dataProtocol == "OBS")
                            {
                                Up   = new Protocol.Data.OBS.UpParser();
                                Down = new Protocol.Data.OBS.DownParser();
                            }
                            //云南协议
                            if (dataProtocol == "YNXY")
                            {
                                Up   = new Data.YNXY.UpParser();
                                Down = new Data.YNXY.DownParser();
                            }

                            //批量传输解析
                            if (temp.Contains("1K"))
                            {
                                var station = FindStationBySID(sid);
                                if (station == null)
                                {
                                    throw new Exception("批量传输,站点匹配错误");
                                }
                                CBatchStruct batch = new CBatchStruct();
                                InvokeMessage(String.Format("{0,-10}   ", "批量传输") + temp, "接收");

                                if (Down.Parse_Flash(result, EChannelType.GPRS, out batch))
                                {
                                    if (this.BatchDataReceived != null)
                                    {
                                        this.BatchDataReceived.Invoke(null, new BatchEventArgs()
                                        {
                                            Value = batch, RawData = temp
                                        });
                                    }
                                }
                                else if (Down.Parse_Batch(result, out batch))
                                {
                                    if (this.BatchDataReceived != null)
                                    {
                                        this.BatchDataReceived.Invoke(null, new BatchEventArgs()
                                        {
                                            Value = batch, RawData = temp
                                        });
                                    }
                                }
                            }
                            //+ 代表的是蒸发报文,需要特殊处理
                            //数据报文解析
                            if (result.Contains("1G21") || result.Contains("1G22") || result.Contains("1G23") ||
                                result.Contains("1G25") || result.Contains("1G29") || result.Contains("+"))
                            {
                                //回复TRU
                                InvokeMessage("TRU " + gprs, "发送");
                                byte[] bts = new byte[] { 84, 82, 85, 13, 10 };
                                this.sendHex(gprs.Trim(), bts, (uint)bts.Length, null);

                                //根据$将字符串进行分割

                                var lists = result.Split('$');
                                foreach (var msg in lists)
                                {
                                    if (msg.Length < 10)
                                    {
                                        continue;
                                    }
                                    string plusMsg = "$" + msg.TrimEnd();
                                    bool   ret     = Up.Parse(plusMsg, out report);
                                    if (ret && report != null)
                                    {
                                        report.ChannelType = EChannelType.GPRS;
                                        report.ListenPort  = this.GetListenPort().ToString();
                                        report.flagId      = gprs;
                                        string rtype = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                        InvokeMessage("gprs号码:  " + gprs + "   " + String.Format("{0,-10}   ", rtype) + plusMsg, "接收");
                                        //TODO 重新定义事件
                                        if (this.UpDataReceived != null)
                                        {
                                            this.UpDataReceived.Invoke(null, new UpEventArgs()
                                            {
                                                Value = report, RawData = plusMsg
                                            });
                                        }
                                    }
                                }
                            }
                            //其他报文
                            else
                            {
                                Down.Parse(result, out downReport);
                                if (downReport != null)
                                {
                                    InvokeMessage(String.Format("{0,-10}   ", "下行指令读取参数") + result, "接收");
                                    if (this.DownDataReceived != null)
                                    {
                                        this.DownDataReceived.Invoke(null, new DownEventArgs()
                                        {
                                            Value = downReport, RawData = result
                                        });
                                    }
                                }
                            }
                        }

                        if (temp.Contains("BEG"))
                        {
                            Data.ZYJBX.DownParser down1 = new Data.ZYJBX.DownParser();
                            CSDStruct             sd    = new CSDStruct();
                            string id = Manager.XmlStationData.Instance.GetStationByGprsID(gprs);
                            if (down1.Parse_SD(temp, id, out sd))
                            {
                                InvokeMessage(String.Format("{0,-10}   ", "批量SD传输") + temp, "接收");

                                if (this.BatchSDDataReceived != null)
                                {
                                    this.BatchSDDataReceived.Invoke(null, new BatchSDEventArgs()
                                    {
                                        Value = sd, RawData = temp
                                    });
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("" + e.Message);
                    }
                }
            }
        }