예제 #1
0
        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);
                        string            temp = data.Trim();

                        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("$"), temp.IndexOf("\0"));
                            string sid  = result.Substring(1, 4);
                            string type = result.Substring(5, 2);

                            //if (sid == "\0\0\0\0")
                            //{
                            //    continue;
                            //}

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

                            //        if (this.BatchDataReceived != null)
                            //            this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp });
                            //    }
                            //    if (Down.Parse_Batch(result, out batch))
                            //    {
                            //        InvokeMessage(String.Format("{0,-10}   ", "批量传输") + temp, "接收");

                            //        if (this.BatchDataReceived != null)
                            //            this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp });
                            //    }
                            //}

                            String dataProtocol = null;
                            if (HdProtocolMap.ContainsKey("HD-GPRS"))
                            {
                                dataProtocol = HdProtocolMap["HD-GPRS"];
                            }

                            switch (dataProtocol)
                            {
                            //case "LN":
                            //    break;
                            //case "SXDZ":
                            //    {
                            //        CReportStruct report = new CReportStruct();
                            //        CDownConf downReport = new CDownConf();
                            //        Data.SXDZ.UpParser Up1 = new Data.SXDZ.UpParser();
                            //        Data.SXDZ.DownParser down1 = new Data.SXDZ.DownParser();

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

                            //                if (this.BatchDataReceived != null)
                            //                    this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp });
                            //            }
                            //            //if (down1.Parse_Batch(result, out batch))
                            //            //{
                            //            //    InvokeMessage(String.Format("{0,-10}   ", "批量传输") + temp, "接收");

                            //            //    if (this.BatchDataReceived != null)
                            //            //        this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp });
                            //            //}
                            //        }

                            //        Up1.Parse(result, out report);
                            //        down1.Parse(result, out downReport);
                            //        if (report != null)
                            //        {
                            //            report.ChannelType = EChannelType.GPRS;
                            //            report.ListenPort = this.GetListenPort().ToString();
                            //            InvokeMessage(String.Format("{0,-10}   ", "") + result, "接收");
                            //            //TODO 重新定义事件
                            //            if (this.UpDataReceived != null)
                            //            {
                            //                this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = result });
                            //            }
                            //        }
                            //        if (downReport != null)
                            //        {
                            //            InvokeMessage(String.Format("{0,-10}   ", "下行指令读取参数") + result, "接收");
                            //            if (this.DownDataReceived != null)
                            //                this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downReport, RawData = result });
                            //        }
                            //        break;
                            //    }
                            //case "HJJBX":
                            //    //需要处理新老协议

                            //    //TODO 判断新老协议
                            //    bool isNew = false;

                            //    if (isNew)
                            //    {
                            //        List<CUpReport> reports = new List<CUpReport>();
                            //        Up = new Protocol.Data.HJJBX.UpParser();
                            //        Up.Parse_New(result, out reports);
                            //        foreach(var report2 in reports)
                            //        {
                            //            report2.ChannelType = EChannelType.GPRS;
                            //            report2.ListenPort = this.GetListenPort().ToString();
                            //        }
                            //        InvokeMessage(String.Format("{0,-10}   ","") + result, "接收");
                            //        //TODO 重新定义事件
                            //        if (this.UpDataReceived_new != null)
                            //        {
                            //            this.UpDataReceived_new.Invoke(null, new UpEventArgs_new() { Value = reports, RawData = result });
                            //        }

                            //    }
                            //    else
                            //    {
                            //        List<CUpReport> reports = new List<CUpReport>();
                            //        Up = new Protocol.Data.HJJBX.UpParser();
                            //        Up.Parse_Old(result, out reports);
                            //        foreach (var report1 in reports)
                            //        {
                            //            report1.ChannelType = EChannelType.GPRS;
                            //            report1.ListenPort = this.GetListenPort().ToString();
                            //        }
                            //        InvokeMessage(String.Format("{0,-10}   ", "") + result, "接收");
                            //        //测试代码
                            //        Dictionary<string, HDModemInfoStruct> data11 = new Dictionary<string, HDModemInfoStruct>();
                            //        this.getDTUList(out data11);

                            //        //TODO 重新定义事件
                            //        if (this.UpDataReceived_new != null)
                            //        {
                            //            this.UpDataReceived_new.Invoke(null, new UpEventArgs_new() { Value = reports, RawData = result });
                            //        }
                            //    }
                            //    break;
                            case "ZYJBX":
                            {
                                CReportStruct         report     = new CReportStruct();
                                CDownConf             downReport = new CDownConf();
                                Data.ZYJBX.UpParser   Up1        = new Data.ZYJBX.UpParser();
                                Data.ZYJBX.DownParser down1      = new Data.ZYJBX.DownParser();

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

                                    if (down1.Parse_Flash(result, EChannelType.GPRS, out batch))
                                    {
                                        if (this.BatchDataReceived != null)
                                        {
                                            this.BatchDataReceived.Invoke(null, new BatchEventArgs()
                                                {
                                                    Value = batch, RawData = temp
                                                });
                                        }
                                    }
                                    else if (down1.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("1G25"))
                                {
                                    var lists = result.Split('$');
                                    foreach (var msg in lists)
                                    {
                                        if (msg.Length < 5)
                                        {
                                            continue;
                                        }
                                        string plusMsg = "$" + msg;
                                        Up1.Parse(plusMsg, out report);
                                        if (report != null)
                                        {
                                            string gprs = System.Text.Encoding.Default.GetString(dat.m_modemId);
                                            gprs = gprs.Contains("\0")? gprs.Replace("\0", "") : gprs;
                                            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
                                                    });
                                            }
                                            InvokeMessage("TRU " + gprs, "发送");
                                            byte[] bts = new byte[] { 84, 82, 85, 13, 10 };
                                            this.sendHex(gprs.Trim(), bts, (uint)bts.Length, null);
                                        }
                                    }
                                }
                                else
                                {
                                    down1.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
                                                });
                                        }
                                    }
                                }

                                break;
                            }
                            //case "XYJBX":
                            //    {
                            //        CReportStruct report = new CReportStruct();
                            //        CDownConf downReport = new CDownConf();
                            //        Data.XYJBX.UpParser Up1 = new Data.XYJBX.UpParser();
                            //        Data.XYJBX.DownParser down1 = new Data.XYJBX.DownParser();

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

                            //            //    if (this.BatchDataReceived != null)
                            //            //        this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp });
                            //            //}
                            //            //if (down1.Parse_Batch(result, out batch))
                            //            //{
                            //            //    InvokeMessage(String.Format("{0,-10}   ", "批量传输") + temp, "接收");

                            //            //    if (this.BatchDataReceived != null)
                            //            //        this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp });
                            //            //}
                            //        }

                            //        Up1.Parse(result, out report);
                            //        down1.Parse(result, out downReport);
                            //        if (report != null)
                            //        {
                            //            report.ChannelType = EChannelType.GPRS;
                            //            report.ListenPort = this.GetListenPort().ToString();
                            //            InvokeMessage(String.Format("{0,-10}   ", "") + result, "接收");
                            //            //TODO 重新定义事件
                            //            if (this.UpDataReceived != null)
                            //            {
                            //                this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = result });
                            //            }
                            //        }
                            //        if (downReport != null)
                            //        {
                            //            InvokeMessage(String.Format("{0,-10}   ", "下行指令读取参数") + result, "接收");
                            //            if (this.DownDataReceived != null)
                            //                this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downReport, RawData = result });
                            //        }
                            //        break;
                            //    }
                            default:
                                break;
                            }
                        }
                        //List<CUpReport> reports = new List<CUpReport>();
                        //Up = new UpParser();
                        //Up.Parse_New(result, out reports);


                        //获取DTU列表的函数,DTU在线列表测试
                        //uint amount = this.getDTUAmount();
                        //HDModemInfoStruct data1= new HDModemInfoStruct();
                        //this.getDTUByPosition(0, out data1);
                        //Dictionary<string, HDModemInfoStruct> datas = new Dictionary<string, HDModemInfoStruct>();
                        //this.getDTUList(out datas);
                        if (temp.Contains("BEG"))
                        {
                            Data.ZYJBX.DownParser down1 = new Data.ZYJBX.DownParser();
                            CSDStruct             sd    = new CSDStruct();
                            string gprs = System.Text.Encoding.Default.GetString(dat.m_modemId);
                            gprs = gprs.Replace("\0", "");
                            //string gprs = dat.m_modemId.ToString();
                            string id = Manager.XmlStationDataSerializer.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);
                    }
                }
            }
        }
        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);
                    }
                }
            }
        }