Exemple #1
0
        public bool Parse_New_beidou(string msg, out List <CUpReport> upReport)
        {
            // 这里stationId和type暂时不能获取,写成固定值。
            string stationId = "9999";
            string type      = "1G";
            string appendMsg = "$" + stationId + type + ProtocolHelpers.dealBCD(msg) + CSpecialChars.ENTER_CHAR;

            return(Parse_New(msg, out upReport));
        }
        //  北斗信道数据解析
        public bool Parse_beidou(string sid, EMessageType type, string msg, out CReportStruct report)
        {
            // 这里stationId和type暂时不能获取,写成固定值。
            //string stationId = "9999";
            //string type = "1G";
            string appendMsg = "$" + sid + type + ProtocolHelpers.dealBCD(msg) + CSpecialChars.ENTER_CHAR;

            return(Parse(msg, out report));
        }
        // 处理COUT类型数据
        private void DealCOUT(string msg)
        //  private String DealCOUT(string msg)
        {
            string result = null;

            InvokeMessage("通信输出  " + msg, "接收");
            SendBackTTCAString = msg;
            if (msg.Contains("COUT"))
            {
                if (msg.Contains("COUT"))
                {
                    num = num + 1;
                    FileStream   fs = new FileStream("numbd.txt", FileMode.Create);
                    StreamWriter sw = new StreamWriter(fs);
                    //开始写入
                    sw.Write(num);
                    //清空缓冲区
                    sw.Flush();
                    //关闭流
                    sw.Close();
                    fs.Close();
                }
            }
            // InvokeString(msg);
            var cout = BeidouHelper.GetCOUTInfo(msg);

            if (cout == null)
            {
                return;
            }

            //  return result;
            //  发送COSS指令
            //  必须在1秒内发送给卫星终端,否则会重新发送“通信输出”
            var coss = new CCOSSStruct();

            coss.SuccessStatus = true;          //  终端接收到外设通信申请,并校验成功
            SendCOSS(coss);
            //  发送CACA指令
            //  每隔一分钟发送回执$CACA
            //  65秒左右发一条
            var caca = new CCACAStruct();

            caca.SenderID              = "1";                 //  发信方ID为1,表示本机ID,默认
            caca.RecvType              = cout.SenderType;     //  回执的收信方类型  ==  通信输出中的发信方类型
            caca.RecvAddr              = cout.SenderAddr;     //  回执的收信方地址  ==  通信输出中的发信方地址
            caca.Requirements          = "1";                 //  不保密
            caca.ReceiptMsgSequenceNum = cout.MsgSequenceNum; // 回执的报文顺序号  ==  通信输出中的报文顺序号
            caca.ReceiptContent        = "1";
            SendCACA(caca);

            //  解析通信输出中的内容
            string content = cout.MsgContent;

            //通信输出gm8  $60131G2201161111040003046112271367
            try
            {
                string           rawMsg     = content;
                string           sid        = null;
                string           dealMsg    = ProtocolHelpers.dealBCD(rawMsg);
                WriteToFileClass writeClass = new WriteToFileClass("ReceivedLog");
                Thread           t          = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile));
                t.Start("COUT Message: " + " " + dealMsg + "\r\n");
                string bdid = cout.SenderAddr;
                //InvokeMessage("gm3  " + content, "接收");
                //sid需要根据北斗卫星号获取
                try
                {
                    sid = Manager.XmlStationDataSerializer.Instance.GetStationByBDID(bdid);
                }
                catch (Exception e)
                {
                    sid = "5712";
                    Debug.WriteLine("获取站号失败" + e.Message);
                }
                EMessageType type       = ProtocolMaps.MessageTypeMap.FindKey(dealMsg.Substring(0, 2));
                string       reportType = dealMsg.Substring(0, 2);
                if (reportType == "21" || reportType == "22")   //   定时报,加报
                {
                    //  YAC设备的墒情协议:
                    string stationType = dealMsg.Substring(2, 2);
                    switch (stationType)
                    {
                    //  站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站
                    case "04":
                    case "05":
                    case "06":
                    case "07":
                    case "17":
                    {
                        //    var station = FindStationByBeidouID(cout.SenderAddr);
                        //    string currentMsg = rawMsg.Insert(0, "$" + station.StationID + "1G");
                        //    CEntitySoilData soil = new CEntitySoilData();
                        //    CReportStruct soilReport = new CReportStruct();
                        //    if (Soil.Parse(currentMsg, out soil, out soilReport))
                        //    {
                        //        soil.ChannelType = EChannelType.BeiDou;

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

                        //        if (null != soilReport && null != this.UpDataReceived)
                        //        {
                        //            soilReport.ChannelType = EChannelType.BeiDou;
                        //            soilReport.ListenPort = "COM" + this.Port.PortName;
                        //            this.UpDataReceived(null, new UpEventArgs() { RawData = rawMsg, Value = soilReport });
                        //        }
                        //    }

                        //}
                        //1111gm
                        string          newMsg     = dealMsg.Substring(1, dealMsg.Length - 1);
                        CEntitySoilData soil       = new CEntitySoilData();
                        CReportStruct   soilReport = new CReportStruct();
                        if (Soil.Parse(newMsg, out soil, out soilReport))
                        {
                            soilReport.ChannelType = EChannelType.BeiDou;
                            if (null != this.SoilDataReceived)
                            {
                                this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil));
                            }

                            if (null != soilReport && null != this.UpDataReceived)
                            {
                                soilReport.ChannelType = EChannelType.GPRS;
                                soilReport.ListenPort  = "COM" + this.Port.PortName;
                                soilReport.flagId      = bdid;
                                this.UpDataReceived(null, new UpEventArgs()
                                    {
                                        RawData = newMsg, Value = soilReport
                                    });
                            }
                        }
                    }
                    break;

                    //  站类为01,02,03,12,13时,不是墒情站
                    case "01":
                    case "02":
                    case "03":
                    case "12":
                    case "13":
                    {
                        //1111gm
                        //string newMsg = dealMsg.Substring(1, dealMsg.Length - 1);
                        //CReportStruct report = new CReportStruct();
                        //if (Up.Parse(newMsg, out report))
                        //{
                        //    //6013 $60131G2201161111040003046112271367
                        //    report.ChannelType = EChannelType.BeiDou;
                        //    report.ListenPort = "COM" + this.Port.PortName;
                        //    if (this.UpDataReceived != null)
                        //    {
                        //        this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = newMsg });
                        //    }
                        //}
                        CReportStruct report = new CReportStruct();
                        UpParser      Up1    = new UpParser();
                        if (Up1.Parse_beidou(sid, type, rawMsg, out report))
                        {
                            //InvokeMessage("gm6  " + rawMsg, "接收");
                            //$60131G2201161111040003046112271367
                            report.ChannelType = EChannelType.BeiDou;
                            report.ListenPort  = "COM" + this.Port.PortName;
                            report.flagId      = bdid;
                            if (this.UpDataReceived != null)
                            {
                                //InvokeMessage("gm7  " + rawMsg, "接收");
                                this.UpDataReceived.Invoke(null, new UpEventArgs()
                                    {
                                        Value = report, RawData = rawMsg
                                    });
                            }
                        }
                    }
                    break;

                    default:
                        break;
                    }
                }
                else if (reportType == "11")    //  人工水位
                {
                }
                else if (reportType == "23")    //  人工流量
                {
                }
                else if (reportType == "25")
                {
                    var    station    = FindStationByBeidouID(cout.SenderAddr);
                    string currentMsg = dealMsg.Insert(0, "$" + station.StationID + "1G");

                    CEntitySoilData soil       = new CEntitySoilData();
                    CReportStruct   soilReport = new CReportStruct();
                    if (Soil.Parse(currentMsg, out soil, out soilReport))
                    {
                        soil.ChannelType = EChannelType.BeiDou;

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

                        if (null != soilReport && null != this.UpDataReceived)
                        {
                            soilReport.ChannelType = EChannelType.BeiDou;
                            soilReport.ListenPort  = "COM" + this.Port.PortName;
                            soilReport.flagId      = bdid;
                            this.UpDataReceived(null, new UpEventArgs()
                            {
                                RawData = dealMsg, Value = soilReport
                            });
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("Beidou 数据解析出错 !" + content + "\r\n" + exp.Message);
            }
            //result = msg;
            //return result;
        }
        /// <summary>
        /// 对下游局报讯报文进行处理
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="report"></param>
        /// <returns></returns>
        public bool Parse(String msg, out CReportStruct report)
        {
            //$30151G22010201120326001297065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239
            report = null;
            try
            {
                string data = string.Empty;
                //卫星信道报
                if (msg.StartsWith("$"))
                {
                    data = ProtocolHelpers.dealBCD(data);
                }
                //去除起始符'$'
                if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                {
                    return(false);
                }
                //站号(4位)
                string StationId = data.Substring(0, 4);
                //类别(2位):1G
                string type = data.Substring(4, 2);
                //报类(2位):22-定时报
                string reportTypeString = data.Substring(6, 2);
                //站类(2位)
                string stationTypeString = data.Substring(8, 2);
                ///0201120326001297065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239
                //站类区别处理
                switch (reportTypeString)
                {
                //定时报
                case "22":
                {
                    //获取报类
                    EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString);
                    //获取站类
                    EStationType stationType = ProtocolHelpers.ProtoStr2StationType(stationTypeString);
                    //包序号暂不处理
                    string packageNum = data.Substring(10, 4);
                    //接收时间
                    DateTime recvTime = new DateTime(
                        year: Int32.Parse("20" + data.Substring(14, 2)),
                        month: Int32.Parse(data.Substring(16, 2)),
                        day: Int32.Parse(data.Substring(18, 2)),
                        hour: Int32.Parse(data.Substring(20, 2)),
                        minute: 0,
                        second: 0
                        );
                    //电压值:1297=12.97V
                    Decimal voltage = Decimal.Parse(data.Substring(22, 4)) * (Decimal)0.01;
                    //数据段
                    var lists = data.Substring(26).Replace(" ", "");
                    var datas = GetData(lists, recvTime, voltage, stationType);
                    report = new CReportStruct()
                    {
                        Stationid   = StationId,
                        Type        = type,
                        ReportType  = reportType,
                        StationType = stationType,
                        RecvTime    = recvTime,
                        Datas       = datas
                    };
                    break;
                }

                //报讯系统加报
                case "21":
                {
                    if (data.Substring(8, 2) != "11")
                    {
                        //  解析报文类别
                        EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString);
                        //  解析站点类别
                        EStationType stationType = ProtocolHelpers.ProtoStr2StationType(stationTypeString);
                        //  解析接收时间
                        DateTime recvTime = new DateTime(
                            year: Int32.Parse("20" + data.Substring(10, 2)), //年
                            month: Int32.Parse(data.Substring(12, 2)),       //月
                            day: Int32.Parse(data.Substring(14, 2)),         //日
                            hour: Int32.Parse(data.Substring(16, 2)),        //时
                            minute: Int32.Parse(data.Substring(18, 2)),      //分
                            second: 0                                        //秒
                            );

                        var lists = data.Substring(20).Split(CSpecialChars.BALNK_CHAR);
                        var datas = GetAddData(lists, recvTime, stationType);

                        report = new CReportStruct()
                        {
                            Stationid   = StationId,
                            Type        = type,
                            ReportType  = reportType,
                            StationType = stationType,
                            RecvTime    = recvTime,
                            Datas       = datas
                        };
                    }
                    else
                    {
                        //1G2111为人工水位

                        //  解析报文类别
                        EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString);
                        //  解析站点类别
                        EStationType stationType = EStationType.ERiverWater;
                        //  解析接收时间
                        DateTime recvTime = new DateTime(
                            year: DateTime.Now.Year,                    //年
                            month: DateTime.Now.Month,                  //月
                            day: DateTime.Now.Day,                      //日
                            hour: Int32.Parse(data.Substring(10, 2)),   //时
                            minute: Int32.Parse(data.Substring(12, 2)), //分
                            second: 0                                   //秒
                            );

                        var lists = data.Substring(14).Split(CSpecialChars.BALNK_CHAR);
                        var datas = GetMannualData(lists, recvTime);

                        //处理datas为空情况
                        if (datas.Count == 0)
                        {
                            return(false);
                        }

                        report = new CReportStruct()
                        {
                            Stationid   = StationId,
                            Type        = type,
                            ReportType  = reportType,
                            StationType = stationType,
                            RecvTime    = recvTime,
                            Datas       = datas
                        };
                    }
                    break;
                }

                //人工报
                case "23":
                {
                    //1G23为人工流量
                    //  解析报文类别
                    EMessageType reportType = EMessageType.EMannual;
                    //  解析站点类别
                    EStationType stationType = EStationType.ERiverWater;
                    //  解析接收时间
                    DateTime recvTime = new DateTime(
                        year: DateTime.Now.Year,                    //年
                        month: DateTime.Now.Month,                  //月
                        day: Int32.Parse(data.Substring(8, 2)),     //日
                        hour: Int32.Parse(data.Substring(10, 2)),   //时
                        minute: Int32.Parse(data.Substring(12, 2)), //分
                        second: 0                                   //秒
                        );

                    var lists = data.Substring(14).Split(CSpecialChars.BALNK_CHAR);
                    var datas = GetWaterData(lists, recvTime);

                    report = new CReportStruct()
                    {
                        Stationid   = StationId,
                        Type        = type,
                        ReportType  = reportType,
                        StationType = stationType,
                        RecvTime    = recvTime,
                        Datas       = datas
                    };
                    break;
                }
                }
                return(true);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("数据:" + msg);
                System.Diagnostics.Debug.WriteLine("数据协议解析不完整" + e.Message);
                return(false);
            }
        }
        // 处理通信输出 COUT类型数据
        public void DealCOUT(string msg)
        {
            InvokeMessage("通信输出  " + msg, "接收");
            string str = msg.Trim();

            if (str.Contains("COUT"))
            {
                if (str.Contains("COUT"))
                {
                    num = num + 1;
                    FileStream   fs = new FileStream("numbd.txt", FileMode.Create);
                    StreamWriter sw = new StreamWriter(fs);
                    //开始写入
                    sw.Write(num);
                    //清空缓冲区
                    sw.Flush();
                    //关闭流
                    sw.Close();
                    fs.Close();
                }
            }

            var cout = Beidou500Helper.GetCOUTInfo(msg);

            //InvokeMessage("gm1  " + "通过截取", "接收");
            if (cout == null)
            {
                //InvokeMessage("gm2  " + "通过截取", "接收");
                return;
            }

            //TODO
            //需要处理dealbcd码的走这里
            //  解析通信输出中的内容
            string           content = cout.MsgContent;
            UpParser         up      = new UpParser();
            List <CUpReport> reports = new List <CUpReport>();

            //TODO
            //添加调用代码

            try
            {
                string           rawMsg     = content;
                string           sid        = null;
                string           dealMsg    = ProtocolHelpers.dealBCD(rawMsg);
                WriteToFileClass writeClass = new WriteToFileClass("ReceivedLog");
                Thread           t          = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile));
                t.Start("COUT Message: " + " " + dealMsg + "\r\n");
                //InvokeMessage("gm3  " + content, "接收");
                //sid需要根据北斗卫星号获取
                try
                {
                    string bdid = cout.SenderAddr;
                    sid = Manager.XmlStationDataSerializer.Instance.GetStationByBDID(bdid);
                }
                catch (Exception e)
                {
                    sid = "5712";
                    Debug.WriteLine("获取站号失败" + e.Message);
                }
                EMessageType type       = ProtocolMaps.MessageTypeMap.FindKey(dealMsg.Substring(0, 2));
                string       reportType = dealMsg.Substring(0, 2);
                //InvokeMessage("gm4  " + reportType, "接收");
                if (reportType == "21" || reportType == "22")   //   定时报,加报
                {
                    //  YAC设备的墒情协议:
                    string stationType = dealMsg.Substring(2, 2);
                    //InvokeMessage("gm5  " + stationType, "接收");
                    switch (stationType)
                    {
                    //  站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站
                    case "04":
                    case "05":
                    case "06":
                    case "07":
                    case "17":
                    {
                        //var station = FindStationByBeidouID(cout.SenderAddr);
                        //string currentMsg = rawMsg.Insert(0, "$" + station.StationID + "1G");
                        //CEntitySoilData soil = new CEntitySoilData();
                        //CReportStruct soilReport = new CReportStruct();
                        //if (Soil.Parse(currentMsg, out soil, out soilReport))
                        //{
                        //    soil.ChannelType = EChannelType.BeiDou;

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

                        //    if (null != soilReport && null != this.UpDataReceived)
                        //    {
                        //        soilReport.ChannelType = EChannelType.BeiDou;
                        //        soilReport.ListenPort = "COM" + this.Port.PortName;
                        //        this.UpDataReceived(null, new UpEventArgs() { RawData = rawMsg, Value = soilReport });
                        //    }
                        //}
                        // string newMsg = rawMsg.Substring(1, rawMsg.Length - 1);
                        CEntitySoilData soil       = new CEntitySoilData();
                        CReportStruct   soilReport = new CReportStruct();
                        if (Soil.Parse(rawMsg, out soil, out soilReport))
                        {
                            soilReport.ChannelType = EChannelType.BeiDou;
                            if (null != this.SoilDataReceived)
                            {
                                this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil));
                            }

                            if (null != soilReport && null != this.UpDataReceived)
                            {
                                soilReport.ChannelType = EChannelType.GPRS;
                                soilReport.ListenPort  = "COM" + this.Port.PortName;
                                this.UpDataReceived(null, new UpEventArgs()
                                    {
                                        RawData = rawMsg, Value = soilReport
                                    });
                            }
                        }
                    }
                    break;

                    //  站类为01,02,03,12,13时,不是墒情站
                    case "01":
                    case "02":
                    case "03":
                    case "12":
                    case "13":
                    {
                        //var station = FindStationByBeidouID(cout.SenderAddr);
                        //rawMsg = rawMsg.Insert(0, station.StationID + "  ");

                        //CReportStruct report = new CReportStruct();
                        //if (Up.Parse(rawMsg, out report))
                        //{
                        //    report.ChannelType = EChannelType.BeiDou;
                        //    report.ListenPort = "COM" + this.Port.PortName;
                        //    if (this.UpDataReceived != null)
                        //        this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = msg });
                        //}
                        //string newMsg = rawMsg.Substring(1, rawMsg.Length - 1);
                        CReportStruct report = new CReportStruct();
                        UpParser      Up1    = new UpParser();
                        if (Up1.Parse_beidou(sid, type, rawMsg, out report))
                        {
                            //InvokeMessage("gm6  " + rawMsg, "接收");
                            //$60131G2201161111040003046112271367
                            report.ChannelType = EChannelType.BeiDou;
                            report.ListenPort  = "COM" + this.Port.PortName;
                            if (this.UpDataReceived != null)
                            {
                                //InvokeMessage("gm7  " + rawMsg, "接收");
                                this.UpDataReceived.Invoke(null, new UpEventArgs()
                                    {
                                        Value = report, RawData = rawMsg
                                    });
                            }
                        }
                    }
                    break;

                    default:
                        break;
                    }
                }
                else if (reportType == "11")    //  人工水位
                {
                }
                else if (reportType == "23")    //  人工流量
                {
                }
                else if (reportType == "25")
                {
                    var    station    = FindStationByBeidouID(cout.SenderAddr);
                    string currentMsg = rawMsg.Insert(0, "$" + station.StationID + "1G");

                    CEntitySoilData soil       = new CEntitySoilData();
                    CReportStruct   soilReport = new CReportStruct();
                    if (Soil.Parse(currentMsg, out soil, out soilReport))
                    {
                        soil.ChannelType = EChannelType.BeiDou;

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

                        if (null != soilReport && null != this.UpDataReceived)
                        {
                            soilReport.ChannelType = EChannelType.BeiDou;
                            soilReport.ListenPort  = "COM" + this.Port.PortName;
                            this.UpDataReceived(null, new UpEventArgs()
                            {
                                RawData = rawMsg, Value = soilReport
                            });
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("北斗卫星指挥机 数据解析出错 !" + content + "\r\n" + exp.Message);
            }
        }