/// <summary>
        /// 遥测站回应
        /// $00011K01yymmddhhmm000Dhhmm000D#13
        /// </summary>
        public bool Parse(String msg, out CBatchStruct batch)
        {
            batch = new CBatchStruct();
            try
            {
                string data = string.Empty;
                if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                {
                    return(false);
                }

                //  解析站点ID , 4位     0001
                batch.StationID = data.Substring(0, 4);

                //  解析命令指令 ,2位     1K
                batch.Cmd = data.Substring(4, 2);

                //  解析站点类型, 2位      01
                batch.StationType = ProtocolHelpers.ProtoStr2StationType(data.Substring(6, 2));

                int year  = Int32.Parse("20" + data.Substring(8, 2));   //  年       yy
                int month = Int32.Parse(data.Substring(10, 2));         //  月       mm
                int day   = Int32.Parse(data.Substring(12, 2));         //  日       dd

                //  截取小时,分钟和数据段
                //  hhmm000Dhhmm000D
                var    datas     = new List <CTimeAndData>();
                int    segLength = 10;
                string dataSegs  = data.Substring(14);
                while (dataSegs.Length >= segLength)
                {
                    int hour   = Int32.Parse(dataSegs.Substring(0, 2)); //  小时   hh
                    int minute = Int32.Parse(dataSegs.Substring(2, 2)); //  分钟   mm

                    String hexStr = dataSegs.Substring(4, 4);           //  数据   000D,水位值要16转10,后2位没用
                                                                        // int value = Int32.Parse(hexStr, System.Globalization.NumberStyles.HexNumber);
                    string value = "22";
                    datas.Add(new CTimeAndData()
                    {
                        Time = new DateTime(year, month, day, hour, minute, 0),
                        Data = value
                    });

                    //  取剩下的小时,分钟和数据段
                    dataSegs = dataSegs.Substring(segLength);
                }
                batch.Datas = datas;
                return(true);
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("数据:" + msg);
                System.Diagnostics.Debug.WriteLine("U盘批量传输解析数据不完整!" + exp.Message);
            }
            return(false);
        }
Beispiel #2
0
        public bool Parse_2(String msg, out CReportStruct report)
        {
            //
            //6013 $60131G23 030200 012345 4 1 123
            report = null;
            try
            {
                string data = string.Empty;
                if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                {
                    return(false);
                }

                //  解析站点ID
                String stationID = data.Substring(0, 4);
                //  解析通信类别
                String type = data.Substring(6, 2);
                if (type != "23")
                {
                    return(false);
                }
                //  解析报文类别
                //EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(data.Substring(8, 2));
                //  解析接收时间
                DateTime recvTime       = DateTime.Now;
                string   day            = data.Substring(8, 2);
                string   hour           = data.Substring(10, 2);
                string   minute         = data.Substring(12, 2);
                DateTime collectTine    = new DateTime(recvTime.Year, recvTime.Month, int.Parse(day), int.Parse(hour), int.Parse(minute), 0);
                string   water          = data.Substring(14, 6);
                string   WaterPotential = data.Substring(20, 1);
                string   acc            = data.Substring(21, 1);
                string   num            = data.Substring(22, 3);
                string   method         = data.Substring(25, 1);
                //  获取数据段,不包含站号、类别、报类、站类信息
                report = new CReportStruct()
                {
                    RecvTime  = recvTime,
                    Stationid = stationID
                };
                return(true);
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("数据:" + msg);
                System.Diagnostics.Debug.WriteLine("上行指令解析数据不完整!" + exp.Message);
            }
            return(false);
        }
Beispiel #3
0
        public bool Parse(String msg, out CReportStruct report)
        {
            //6013 $60131G2201161111040003046112271367
            report = null;
            try
            {
                string data = string.Empty;
                if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                {
                    return(false);
                }

                //  解析站点ID
                String stationID = data.Substring(0, 4);
                //  解析通信类别
                String type = data.Substring(4, 2);
                //  解析报文类别
                EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(data.Substring(6, 2));

                //  解析站点类别
                //EStationType stationType = EStationType.EHydrology;
                //String stationTypeStr = data.Substring(8, 2);
                //switch (stationTypeStr)
                //{
                //    case "01":  //  雨量
                //        stationType = EStationType.ERainFall;
                //        break;
                //    case "02":  //  水位
                //    case "12":
                //        stationType = EStationType.ERiverWater;
                //        break;
                //    case "03":  //  水文
                //    case "13":
                //        stationType = EStationType.EHydrology;
                //        break;
                //}
                EStationType stationType = ProtocolHelpers.ProtoStr2StationType(data.Substring(8, 2));

                //  解析接收时间
                DateTime recvTime = DateTime.Now;

                //  获取数据段,不包含站号、类别、报类、站类信息
                var lists = data.Substring(10).Split(CSpecialChars.BALNK_CHAR);
                var datas = GetData(lists, stationType);

                report = new CReportStruct()
                {
                    Stationid   = stationID,
                    Type        = type,
                    ReportType  = reportType,
                    StationType = stationType,
                    RecvTime    = recvTime,
                    Datas       = datas
                };
                return(true);
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("数据:" + msg);
                System.Diagnostics.Debug.WriteLine("上行指令解析数据不完整!" + exp.Message);
            }
            return(false);
        }
Beispiel #4
0
        public bool Parse_New(string msg, out List <CUpReport> upReport)
        {
            upReport = new List <CUpReport>();
            CUpReport report  = null;
            var       msgSegs = msg.Split(CSpecialChars.BALNK_CHAR);

            foreach (var msgItem in msgSegs)
            {
                try
                {
                    string data = string.Empty;
                    if (!ProtocolHelpers.DeleteSpecialChar(msgItem, out data))
                    {
                        return(false);
                    }

                    //  解析站点ID
                    String stationID = data.Substring(0, 4);
                    //  解析通信类别
                    String type = data.Substring(4, 2);
                    //  解析报文类别
                    EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(data.Substring(6, 2));
                    //  解析站点类别
                    EStationType stationType = ProtocolHelpers.ProtoStr2StationType(data.Substring(8, 2));
                    //  解析包序号
                    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: Int32.Parse(data.Substring(22, 2)),       //分
                        second: 0
                        );

                    //  解析数据
                    CUpData upData = new CUpData();

                    string item = data.Substring(24);

                    while (item.Length > 2)
                    {
                        try
                        {
                            //  数据分为两部分
                            //  2 Byte 指令  +  剩下的为数据,数据的长度>= 1
                            //  解析指令类型param
                            ENewParam param = NewTypeMaps.NewParamMap.FindKey(item.Substring(0, 2));
                            //  如果接收到的数据段长度大于2,表示对应的字段有值
                            //  默认为String.Empty
                            string info   = string.Empty;
                            int    length = Int32.Parse(NewTypeMaps.NewParamLengthMap.FindValue(param));
                            info = item.Substring(2, length);
                            item = item.Substring(2 + length);
                            if (String.IsNullOrEmpty(info) && ProtocolHelpers.isLegalCharacters(info))
                            {
                                continue;
                            }

                            switch (param)
                            {
                            case ENewParam.Rain:
                                upData.Rain = Decimal.Parse(info); break;

                            case ENewParam.Water1:
                                upData.Water1 = Decimal.Parse(info) * (Decimal)0.01; break;

                            case ENewParam.Water2:
                                upData.Water2 = Decimal.Parse(info) * (Decimal)0.01; break;

                            case ENewParam.Clock:
                                int year   = Int32.Parse("20" + info.Substring(0, 2));
                                int month  = Int32.Parse(info.Substring(2, 2));
                                int day    = Int32.Parse(info.Substring(4, 2));
                                int hour   = Int32.Parse(info.Substring(6, 2));
                                int minute = Int32.Parse(info.Substring(8, 2));
                                int second = Int32.Parse(info.Substring(10, 2));
                                upData.Time = new DateTime(year, month, day, hour, minute, second);
                                break;

                            case ENewParam.Voltage:
                                upData.Voltage = (Decimal.Parse(info) * (Decimal)0.01); break;

                            case ENewParam.WaterInterval:
                                int WaterInterval = Int32.Parse(info); break;

                            case ENewParam.RainInterval:
                                int RainInterval = Int32.Parse(info); break;

                            case ENewParam.ETimedNum:
                                int ETimedNum = Int32.Parse(info); break;

                            case ENewParam.ESaveNum:
                                int ESaveNum = Int32.Parse(info); break;

                            case ENewParam.WaterBase1:
                                Decimal WaterBase1 = Decimal.Parse(info) * (Decimal)0.01; break;

                            case ENewParam.WaterBase2:
                                Decimal WaterBase2 = Decimal.Parse(info) * (Decimal)0.01; break;

                            case ENewParam.MannualWater:
                                DateTime MannualWaterTime = new DateTime(
                                    year: Int32.Parse(info.Substring(0, 2)),
                                    month: Int32.Parse(info.Substring(2, 2)),
                                    day: Int32.Parse(info.Substring(4, 2)),
                                    hour: Int32.Parse(data.Substring(6, 2)),
                                    minute: Int32.Parse(data.Substring(8, 2)),
                                    second: 0
                                    );
                                Decimal MannualWater = Decimal.Parse(info.Substring(10, 6)) * (Decimal)0.01;
                                upData.WaterList.Add(MannualWaterTime, MannualWater);
                                break;

                            case ENewParam.WaterAddRange:
                                Decimal WaterAddRange = Decimal.Parse(info); break;

                            case ENewParam.RainAddRange:
                                Decimal RainAddRange = Decimal.Parse(info); break;

                            case ENewParam.DestPhoneNum1:
                                string DestPhoneNum1 = info; break;

                            case ENewParam.DestPhoneNum2:
                                string DestPhoneNum2 = info; break;

                            case ENewParam.TerminalNum:
                                string TerminalNum = info; break;

                            case ENewParam.RespWave:
                                string RespWave = info; break;

                            case ENewParam.WavePost:
                                int WavePost = Int32.Parse(info); break;

                            case ENewParam.WorkStatus:
                                EWorkStatus WorkStatus = OldTypeMaps.WorkStatus4ProtoMap.FindKey(info); break;

                            case ENewParam.StandbyChannel:
                                ChannelType MainChannel = OldTypeMaps.ChannelType4ProtoMap.FindKey(info);
                                System.Diagnostics.Debug.Assert(MainChannel != ChannelType.None, "主用信道不能为NONE");
                                ChannelType ViceChannel = OldTypeMaps.ChannelType4ProtoMap.FindKey(info.Substring(2, 2));
                                break;

                            case ENewParam.GSMSignal:
                                Decimal GSMSignal = Decimal.Parse(info); break;

                            case ENewParam.MannualFlow:
                                DateTime MannualFlowTime = new DateTime(
                                    year: Int32.Parse(info.Substring(0, 2)),
                                    month: Int32.Parse(info.Substring(2, 2)),
                                    day: Int32.Parse(info.Substring(4, 2)),
                                    hour: Int32.Parse(data.Substring(6, 2)),
                                    minute: Int32.Parse(data.Substring(8, 2)),
                                    second: 0
                                    );
                                upData.Time      = MannualFlowTime;
                                upData.WaterFlow = Decimal.Parse(info.Substring(10, 6)) * (Decimal)0.01;
                                break;

                            case ENewParam.GSMElec:
                                EGSMElec GSMElec = OldTypeMaps.GSMElec4ProtoMap.FindKey(info); break;

                            case ENewParam.WaterTemp:
                                upData.WaterTemp = Decimal.Parse(info) * (Decimal)0.1; break;

                            case ENewParam.FourWaterTemp:
                                var      waterTempList = new Dictionary <DateTime, Decimal>();
                                DateTime WaterTempTime = recvTime;
                                for (int i = 1; i <= 4; i++)
                                {
                                    Decimal WaterTemp = Decimal.Parse(info.Substring(0, 4)) * (Decimal)0.1;
                                    WaterTempTime = recvTime.AddHours(-6 * (i - 1));
                                    info          = info.Substring(4);
                                    waterTempList.Add(WaterTempTime, WaterTemp);
                                }
                                upData.TempList = waterTempList;
                                break;

                            case ENewParam.DataPackNum:
                                int DataPackNum = Int32.Parse(info); break;

                            default: break;
                            }
                        }
                        catch (Exception e)
                        {
                            System.Diagnostics.Debug.WriteLine("数据:" + msg);
                            System.Diagnostics.Debug.WriteLine("下行指令解析数据不完整!" + e.Message);
                            break;
                        }
                        //填充基本数据
                        report = new CUpReport()
                        {
                            Stationid   = stationID,
                            Type        = type,
                            ReportType  = reportType,
                            StationType = stationType,
                            RecvTime    = recvTime,
                            PackageNum  = packageNum,
                            Data        = upData
                        };
                        upReport.Add(report);
                    }
                }
                catch (Exception exp)
                {
                    System.Diagnostics.Debug.WriteLine(exp.Message);
                    continue;
                }
            }
            return(true);
        }
Beispiel #5
0
        public bool Parse_Old(string msg, out List <CUpReport> upReport)
        {
            upReport = new List <CUpReport>();
            var msgSegs = msg.Split(CSpecialChars.BALNK_CHAR);

            foreach (var msgItem in msgSegs)
            {
                try
                {
                    CUpReport report = null;
                    string    data   = string.Empty;
                    if (!ProtocolHelpers.DeleteSpecialChar(msgItem, out data))
                    {
                        return(false);
                    }

                    //  解析站点ID
                    String StationID = data.Substring(0, 4);
                    //  解析通信类别
                    String type = data.Substring(4, 2);
                    //  解析报文类别
                    EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(data.Substring(6, 2));
                    //  解析站点类别
                    EStationType stationType = ProtocolHelpers.ProtoStr2StationType(data.Substring(8, 2));
                    // 解析接收时间
                    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
                        );
                    //  解析包序号
                    string packageNum = data.Substring(20, 2);
                    //  解析数据
                    string  item   = data.Substring(22);
                    CUpData upData = new CUpData();

                    while (item.Length > 2)
                    {
                        try
                        {
                            //  数据分为两部分
                            //  2 Byte 指令  +  剩下的为数据,数据的长度>= 1
                            //  解析指令类型param
                            EOldParam param = OldTypeMaps.OldParamMap.FindKey(item.Substring(0, 2));
                            //  如果接收到的数据段长度大于2,表示对应的字段有值
                            //  默认为String.Empty
                            string info   = string.Empty;
                            int    length = Int32.Parse(OldTypeMaps.OldParamLengthMap.FindValue(param));
                            info = item.Substring(2);
                            if (String.IsNullOrEmpty(info) && ProtocolHelpers.isLegalCharacters(info))
                            {
                                continue;
                            }

                            switch (param)
                            {
                            case EOldParam.Rain:
                                upData.Rain = Decimal.Parse(info.Substring(0, length)); break;

                            case EOldParam.Water:
                                upData.Water1 = Decimal.Parse(info.Substring(0, length)) * (Decimal)0.01; break;

                            case EOldParam.WaterTimed:
                                //  将水位定时报中的数据储存到list中
                                var waterList = new Dictionary <DateTime, Decimal>();
                                int roundNum  = Int32.Parse(info.Substring(0, 2));
                                var watersegs = info;
                                if (roundNum == 01 || roundNum == 02)
                                {
                                    roundNum  = roundNum == 01 ? 12 : 24;
                                    length    = roundNum == 24 ? 96 : 48;
                                    watersegs = info.Substring(2);
                                }
                                else
                                {
                                    roundNum = 12;
                                }
                                recvTime = recvTime.AddMinutes(-5 * roundNum);

                                for (int i = 1; i <= roundNum; i++)
                                {
                                    DateTime WaterTime  = recvTime.AddMinutes(5);
                                    Decimal  TimedWater = Decimal.Parse(watersegs.Substring(0, 4)) * (Decimal)0.01;
                                    waterList.Add(WaterTime, TimedWater);
                                    watersegs = watersegs.Substring(4);
                                }
                                upData.WaterList = waterList;
                                break;

                            case EOldParam.HalfVoltage:
                                //  将电压值存入list中
                                var      halfVoltageList = new Dictionary <DateTime, Decimal>();
                                DateTime HalfVoltageTime = recvTime;
                                for (int i = 1; i <= 12; i++)
                                {
                                    Decimal Voltage = Decimal.Parse(info.Substring(0, 4)) * (Decimal)0.01;
                                    HalfVoltageTime = recvTime.AddHours(-1 * (i - 1));
                                    halfVoltageList.Add(HalfVoltageTime, Voltage);
                                    info = info.Substring(4);
                                }
                                upData.VoltageList = halfVoltageList;
                                break;

                            case EOldParam.Voltage:
                                upData.Voltage = (Decimal.Parse(info.Substring(0, length)) * (Decimal)0.01); break;

                            case EOldParam.AllVoltage:
                                //  将电压值存入list中
                                var      allVoltageList = new Dictionary <DateTime, Decimal>();
                                DateTime AllVoltageTime = recvTime;
                                for (int i = 1; i <= 24; i++)
                                {
                                    Decimal Voltage = Decimal.Parse(info.Substring(0, 4)) * (Decimal)0.01;
                                    AllVoltageTime = recvTime.AddHours(-1 * (i - 1));
                                    allVoltageList.Add(AllVoltageTime, Voltage);
                                    info = info.Substring(4);
                                }
                                upData.VoltageList = allVoltageList;
                                break;

                            case EOldParam.Interval:
                                int Interval = Int32.Parse(info.Substring(0, length)); break;

                            case EOldParam.ESaveNum:
                                int ESaveNum = Int32.Parse(info.Substring(0, length)); break;

                            case EOldParam.ETimedNum:
                                int ETimedNum = Int32.Parse(info.Substring(0, length)); break;

                            case EOldParam.WaterBase:
                                decimal WaterBase = Decimal.Parse(info.Substring(0, length)) * (Decimal)0.01; break;

                            case EOldParam.MannualWater:
                                DateTime MannualWaterTime = new DateTime(
                                    year: Int32.Parse(info.Substring(0, 2)),
                                    month: Int32.Parse(info.Substring(2, 2)),
                                    day: Int32.Parse(info.Substring(4, 2)),
                                    hour: Int32.Parse(data.Substring(6, 2)),
                                    minute: Int32.Parse(data.Substring(8, 2)),
                                    second: 0
                                    );
                                recvTime = MannualWaterTime;
                                Decimal MannualWater = Decimal.Parse(info.Substring(10, 6)) * (Decimal)0.01;
                                upData.WaterList.Add(MannualWaterTime, MannualWater);
                                break;

                            case EOldParam.WaterAddRange:
                                Decimal WaterAddRange = Decimal.Parse(info.Substring(0, length)); break;

                            case EOldParam.RainAddRange:
                                Decimal RainAddRange = Decimal.Parse(info.Substring(0, length)); break;

                            case EOldParam.DestPhoneNum:
                                string DestPhoneNum = info.Substring(0, length); break;

                            case EOldParam.TerminalNum:
                                string TerminalNum = info.Substring(0, length); break;

                            case EOldParam.RespWave:
                                string RespWave = info.Substring(0, length); break;

                            case EOldParam.WavePost:
                                int WavePost = Int32.Parse(info.Substring(0, length)); break;

                            case EOldParam.WorkStatus:
                                EWorkStatus WorkStatus = OldTypeMaps.WorkStatus4ProtoMap.FindKey(info.Substring(0, length)); break;

                            case EOldParam.StandbyChannel:
                                ChannelType MainChannel = OldTypeMaps.ChannelType4ProtoMap.FindKey(info.Substring(0, 2));
                                System.Diagnostics.Debug.Assert(MainChannel != ChannelType.None, "主用信道不能为NONE");
                                ChannelType ViceChannel = OldTypeMaps.ChannelType4ProtoMap.FindKey(info.Substring(2, 2));
                                break;

                            case EOldParam.GSMSignal:
                                Decimal GSMSignal = Decimal.Parse(info.Substring(0, length)); break;

                            case EOldParam.MannualFlow:
                                DateTime MannualFlowTime = new DateTime(
                                    year: Int32.Parse(info.Substring(0, 2)),
                                    month: Int32.Parse(info.Substring(2, 2)),
                                    day: Int32.Parse(info.Substring(4, 2)),
                                    hour: Int32.Parse(data.Substring(6, 2)),
                                    minute: Int32.Parse(data.Substring(8, 2)),
                                    second: 0
                                    );
                                recvTime         = MannualFlowTime;
                                upData.WaterFlow = Decimal.Parse(info.Substring(10, 6)) * (Decimal)0.01;
                                break;

                            case EOldParam.GSMElec:
                                EGSMElec GSMElec = OldTypeMaps.GSMElec4ProtoMap.FindKey(info.Substring(0, length)); break;

                            case EOldParam.WaterTemp:
                                upData.WaterTemp = Decimal.Parse(info.Substring(0, length)) * (Decimal)0.1; break;

                            case EOldParam.DataPackNum:
                                int dataPackNum = Int32.Parse(info.Substring(0, length)); break;

                            default: break;
                            }
                            item = item.Substring(2 + length);
                        }
                        catch (Exception e)
                        {
                            System.Diagnostics.Debug.WriteLine("数据:" + msg);
                            System.Diagnostics.Debug.WriteLine("下行指令解析数据不完整!" + e.Message);
                            break;
                        }

                        //填充基本数据
                        report = new CUpReport()
                        {
                            Stationid   = StationID,
                            Type        = type,
                            ReportType  = reportType,
                            StationType = stationType,
                            RecvTime    = recvTime,
                            PackageNum  = packageNum,
                            Data        = upData
                        };
                        upReport.Add(report);
                    }
                }
                catch (Exception exp)
                {
                    System.Diagnostics.Debug.WriteLine(exp.Message);
                    continue;
                }
            }
            return(true);
        }
        private void Parser_3(object str)
        {
            try
            {
                string data = str as string;
                /* 删除 '\r\n' 字符串 */
                while (data.StartsWith("\r\n"))
                {
                    data = data.Substring(2);
                }

                /*
                 * 解析数据,获取CGSMStruct
                 */
                var gsm = new CGSMStruct();
                if (!GsmHelper.Parse_3(data, out gsm))
                {
                    return;
                }
                /*  如果解析成功,触发GSM数据接收完成事件  */
                //string rawdata = "";
                //string rawdata1 = gsm.Message;
                //if (!rawdata1.Contains('$'))
                //{
                //    rawdata = "$" + rawdata1;
                //}

                if (data.EndsWith("+"))
                {
                    data = data.Substring(0, data.Length - 1);
                }
                InvokeMessage(data, "接收");

                string msg = string.Empty;
                if (!ProtocolHelpers.DeleteSpecialChar(gsm.Message, out msg))
                {
                    return;
                }
                msg = gsm.Message;
                string rawdata = msg;
                if (msg.Contains('$'))
                {
                    msg = msg.Substring(1);
                }
                //if (msg.Contains('9'))
                //{
                //    msg = msg.Substring(1);
                //}


                if (!msg.ToUpper().Contains("TRU"))
                {
                    string sid  = msg.Substring(0, 4);
                    string type = msg.Substring(4, 2);

                    /*
                     *  上行指令信息,
                     *  或者读取参数返回的信息
                     */
                    #region 1G
                    if (type == "1G")
                    {
                        string reportType = msg.Substring(6, 2);
                        /*  定时报,加报 */
                        if (reportType == "21" || reportType == "22")
                        {
                            //  YAC设备的墒情协议:
                            string stationType = msg.Substring(8, 2);
                            switch (stationType)
                            {
                            //  站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站
                            case "04":
                            case "05":
                            case "06":
                            case "07":
                                //case "17":
                                //    {
                                //        CEntitySoilData soil = new CEntitySoilData();
                                //        CReportStruct soilReport = new CReportStruct();
                                //        Soil = new Protocol.Data.Lib.SoilParser();
                                //        if (Soil.Parse(rawdata, out soil, out soilReport))
                                //        {
                                //            soil.ChannelType = EChannelType.GSM;

                                //            if (null != this.SoilDataReceived)
                                //                this.SoilDataReceived(null, new CEventSingleArgs<CEntitySoilData>(soil));
                                //            //1111gm
                                //            string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                //            //InvokeMessage(String.Format("{0,-10}   ", temp) + rawdata, "接收");

                                //            if (null != soilReport && null != this.UpDataReceived)
                                //           // if (null != soilReport)
                                //             {
                                //                soilReport.ChannelType = EChannelType.GSM;
                                //                soilReport.ListenPort = "COM" + this.ListenPort.PortName;
                                //                this.UpDataReceived(null, new UpEventArgs() { RawData = rawdata, Value = soilReport });
                                //            }
                                //        }
                                //        else
                                //        {
                                //            //string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                //            InvokeMessage(" " + rawdata, "接收");
                                //        }
                                //    }
                                break;

                            //  站类为01,02,03,12,13时,不是墒情站
                            case "01":
                            case "02":
                            case "03":
                            case "12":
                            case "13":
                            {
                                CReportStruct report = new CReportStruct();
                                if (Up.Parse(msg, out report))         /* 解析成功 */
                                {
                                    report.ChannelType = EChannelType.GSM;
                                    report.ListenPort  = "COM" + this.ListenPort.PortName;
                                    report.flagId      = gsm.PhoneNumber;
                                    if (this.UpDataReceived != null)
                                    {
                                        this.UpDataReceived.Invoke(null, new UpEventArgs()
                                            {
                                                Value = report, RawData = data
                                            });
                                    }
                                }
                            }
                            break;

                            default:
                                break;
                            }
                        }
                        else if (reportType == "11")    //  人工水位
                        {
                        }
                        else if (reportType == "23")    //  人工流量
                        {
                        }
                        else if (reportType == "25")
                        {
                        }
                        else /* 下行指令 */
                        {
                            CDownConf downconf = new CDownConf();
                            if (Down.Parse(msg, out downconf)) /* 解析成功 */
                            {
                                if (this.DownDataReceived != null)
                                {
                                    this.DownDataReceived.Invoke(null, new DownEventArgs()
                                    {
                                        Value = downconf, RawData = data
                                    });
                                }
                            }
                        }
                    }

                    #endregion

                    #region 1K
                    if (type == "1K") /* 批量传输 */
                    {
                        var station = FindStationBySID(sid);
                        if (station == null)
                        {
                            throw new Exception("批量传输,站点传输类型匹配错误");
                        }
                        //  EStationBatchType batchType = station.BatchTranType;

                        //if (batchType == EStationBatchType.EFlash)          /* Flash传输 */
                        //{
                        CBatchStruct batch = new CBatchStruct();
                        if (FlashBatch.Parse(gsm.Message, out batch)) /* 解析成功 */
                        {
                            if (this.BatchDataReceived != null)
                            {
                                this.BatchDataReceived.Invoke(null, new BatchEventArgs()
                                {
                                    Value = batch, RawData = data
                                });
                            }
                        }
                        //}
                        //else if (batchType == EStationBatchType.EUPan)  /* U盘传输 */
                        //{
                        //    CBatchStruct batch = new CBatchStruct();
                        //    if (UBatch.Parse(gsm.Message, out batch))   /* 解析成功 */
                        //    {
                        //        if (this.BatchDataReceived != null)
                        //            this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = data });
                        //    }
                        //}
                        else
                        {
                            throw new Exception("批量传输,站点传输类型匹配错误");
                        }
                    }
                    #endregion

                    #region 1S
                    if (type == "1S") /* 远地下行指令,设置参数 */
                    {
                        CDownConf downconf = new CDownConf();
                        if (Down.Parse(gsm.Message, out downconf))/* 解析成功 */
                        {
                            if (this.DownDataReceived != null)
                            {
                                this.DownDataReceived.Invoke(null, new DownEventArgs()
                                {
                                    Value = downconf, RawData = data
                                });
                            }
                        }
                    }
                    #endregion
                }
                else
                {
                    if (this.ErrorReceived != null)
                    {
                        this.ErrorReceived.Invoke(null, new ReceiveErrorEventArgs()
                        {
                            Msg = data
                        });
                    }
                }
            }
            catch (Exception exp)
            {
                Debug.WriteLine(exp.Message);
            }
        }
        public bool Parse(String msg, out CReportStruct report)
        {
            //$30011G2512010030yymmddhhmm1297001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100;
            report = null;
            try
            {
                string data = string.Empty;
                if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                {
                    return(false);
                }

                //  解析站号
                String StationId = data.Substring(0, 4);
                //  解析通信类别
                String type = data.Substring(4, 2);
                //定义报文类别
                String reportTypeString = data.Substring(6, 2);
                //定义站点类别
                String stationTypeString = data.Substring(8, 2);
                //  根据报文类别进行区别处理
                switch (reportTypeString)
                {
                case "25":
                {
                    //  解析报文类别
                    reportTypeString = "22";
                    EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString);
                    //  解析站点类别
                    EStationType stationType = ProtocolHelpers.ProtoStr2StationType(stationTypeString);
                    //解析间隔分钟
                    int intervalMin = Int32.Parse(data.Substring(10, 2));
                    //解析数据个数
                    int dataNum = Int32.Parse(data.Substring(12, 4));
                    //解析接收时间
                    DateTime recvTime = new DateTime(
                        year: Int32.Parse("20" + data.Substring(16, 2)),          //年
                        month: Int32.Parse(data.Substring(18, 2)),                //月
                        day: Int32.Parse(data.Substring(20, 2)),                  //日
                        hour: Int32.Parse(data.Substring(22, 2)),                 //时
                        minute: Int32.Parse(data.Substring(24, 2)),               //分
                        second: 0
                        );
                    //解析电压 2(整数位)+ 2(小数位) 单位 V
                    Decimal Voltage = Decimal.Parse(data.Substring(26, 4)) * (Decimal)0.01;
                    //获取数据段,不包括间隔分钟、数据个数、电压等
                    var lists = data.Substring(30).Split(CSpecialChars.BALNK_CHAR);
                    var datas = GetProData(intervalMin, dataNum, lists, recvTime, Voltage);

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

                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                                        //秒
                        );
                    //把包序号写入缓存
                    CEntityPackage package = new CEntityPackage()
                    {
                        StrStationID = StationId,
                        PackageNum   = PackageNum,
                        time         = recvTime
                    };
                    if (cEntityPackage.ContainsKey(StationId))
                    {
                        cEntityPackage[StationId] = package;
                    }
                    else
                    {
                        cEntityPackage.Add(StationId, package);
                    }
                    //解析电压 2(整数位)+ 2(小数位) 单位 V
                    Decimal Voltage = Decimal.Parse(data.Substring(22, 4)) * (Decimal)0.01;
                    //获取数据段,不包括间隔分钟、数据个数、电压等
                    var lists = data.Substring(26).Split(CSpecialChars.BALNK_CHAR);
                    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 = EMessageType.EMannual;
                        //  解析站点类别
                        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);

                        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;

                case "29":
                {
                    string doubleMsg = data.Substring(8);
                    Parse(doubleMsg, out report);
                } break;
                }
                return(true);
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("数据:" + msg);
                System.Diagnostics.Debug.WriteLine("数据协议解析不完整" + exp.Message);
            }
            return(false);
        }
Beispiel #8
0
        //  批量数据flash下行解析
        public bool Parse_Flash(String msg, out CBatchStruct batch)
        {
            batch = new CBatchStruct();
            try
            {
                string data = string.Empty;
                if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                {
                    return(false);
                }

                //  解析站点ID , 4位     0001
                batch.StationID = data.Substring(0, 4);
                //  解析命令指令 ,2位     1K
                batch.Cmd = data.Substring(4, 2);
                //  解析站点类型, 2位     01
                batch.StationType = ProtocolHelpers.ProtoStr2StationType(data.Substring(6, 2));
                //  解析传输类型, 2位     02/03
                batch.TransType = ProtocolMaps.TransMap.FindKey(data.Substring(8, 2));

                //创建返回的数据对象
                var datas = new List <CTimeAndData>();
                //ByDay ByHour区分
                //02 按天传   03按小时  04暂时不需要处理
                switch (batch.TransType)
                {
                //按天传 02
                case ETrans.ByDay:
                {
                    int dateYear  = Int32.Parse("20" + data.Substring(10, 2));  // 年
                    int dateMonth = Int32.Parse(data.Substring(12, 2));         // 月

                    //数据处理
                    var lists = data.Substring(14).Split(CSpecialChars.BALNK_CHAR);
                    foreach (var item in lists)
                    {
                        DateTime recvTime = new DateTime(
                            year: dateYear,                            //年
                            month: dateMonth,                          //月
                            day: Int32.Parse(item.Substring(0, 2)),    //日
                            hour: Int32.Parse(item.Substring(2, 2)),   //时
                            minute: Int32.Parse(item.Substring(4, 2)), //分
                            second: 0
                            );
                        string datasegs = item.Substring(6, 6);
                        datas.Add(new CTimeAndData
                            {
                                Time = recvTime,
                                Data = datasegs
                            });
                    }
                } break;

                //按小时传 03
                case ETrans.ByHour:
                {
                    //判断是GSM还是GPRS传送方式
                    //GSM:yymmddhh  GPRS:yymm
                    string difference = data.Substring(10, 5).Replace(" ", "");

                    if (difference.Length <= 4)
                    {
                        //GPRS类型
                        int dateYear  = Int32.Parse("20" + data.Substring(10, 2));     // 年
                        int dateMonth = Int32.Parse(data.Substring(12, 2));            // 月

                        //数据处理
                        var lists = data.Substring(14).Split(CSpecialChars.BALNK_CHAR);
                        foreach (var item in lists)
                        {
                            DateTime recvTime = new DateTime(
                                year: dateYear,                            //年
                                month: dateMonth,                          //月
                                day: Int32.Parse(item.Substring(0, 2)),    //日
                                hour: Int32.Parse(item.Substring(2, 2)),   //时
                                minute: Int32.Parse(item.Substring(4, 2)), //分
                                second: 0
                                );
                            string datasegs = item.Substring(6, 6);
                            datas.Add(new CTimeAndData
                                {
                                    Time = recvTime,
                                    Data = datasegs
                                });
                        }
                    }
                    else
                    {
                        //GSM类型
                        int dateYear  = Int32.Parse("20" + data.Substring(10, 2));  // 年
                        int dateMonth = Int32.Parse(data.Substring(12, 2));         // 月
                        int dateDay   = Int32.Parse(data.Substring(14, 2));         // 日
                        int dateHour  = Int32.Parse(data.Substring(16, 2));         // 时

                        //数据处理
                        var lists = data.Substring(18).Split(CSpecialChars.BALNK_CHAR);
                        foreach (var item in lists)
                        {
                            DateTime recvTime = new DateTime(
                                year: dateYear,                            //年
                                month: dateMonth,                          //月
                                day: dateDay,                              //日
                                hour: dateHour,                            //时
                                minute: Int32.Parse(item.Substring(0, 2)), //分
                                second: 0
                                );
                            string datasegs = item.Substring(2, 6);
                            datas.Add(new CTimeAndData
                                {
                                    Time = recvTime,
                                    Data = datasegs
                                });
                        }
                    }
                } break;
                }
                batch.Datas = datas;
                return(true);
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("数据:" + msg);
                System.Diagnostics.Debug.WriteLine("批量数据解析不完整" + exp.Message);
            }
            return(false);
        }
Beispiel #9
0
        private bool ParseData(string msg, string gprs)
        {
            //           InvokeMessage("协议。。。 ", "进入函数7");
            try
            {
                string rawData = msg;

                if (msg.Contains("$"))
                {
                    string data = string.Empty;
                    if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                    {
                        return(false);
                    }
                    msg = data;

                    string sid  = msg.Substring(0, 4);
                    string type = msg.Substring(4, 2);

                    #region 1G
                    if (type == "1G")
                    {
                        string reportType = msg.Substring(6, 2);
                        if (reportType == "21" || reportType == "22")   //   定时报,加报
                        {
                            //  YAC设备的墒情协议:
                            string stationType = msg.Substring(8, 2);
                            switch (stationType)
                            {
                            //  站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站
                            case "04":
                            case "05":
                            case "06":
                            case "07":
                            case "17":
                            {
                                //CEntitySoilData soilStruct = new CEntitySoilData();
                                //if (Soil.Parse(msg, out soilStruct))
                                //{
                                //    soilStruct.ChannelType = EChannelType.GPRS;
                                //    //soilStruct.ListenPort = this.GetListenPort().ToString();

                                //    //string temp = soilStruct.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                //    //InvokeMessage(String.Format("{0,-10}   ", temp) + rawData, "接收");

                                //    //  抛出YAC设备墒情事件
                                //    if (null != this.SoilDataReceived)
                                //        this.SoilDataReceived.Invoke(null, new YACSoilEventArg()
                                //        {
                                //            RawData = rawData,
                                //            Value = soilStruct
                                //        });
                                //}

                                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));
                                    }
                                    //1111gm
                                    string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                    InvokeMessage("  gprs号码:  " + gprs + String.Format("  {0,-10}   ", temp) + rawData, "接收");

                                    if (null != soilReport && null != this.UpDataReceived)
                                    {
                                        soilReport.ChannelType = EChannelType.GPRS;
                                        soilReport.ListenPort  = this.GetListenPort().ToString();
                                        soilReport.flagId      = gprs;
                                        this.UpDataReceived(null, new UpEventArgs()
                                            {
                                                RawData = rawData, Value = soilReport
                                            });
                                    }
                                }
                                else
                                {
                                    //string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                    InvokeMessage("  gprs号码:  " + gprs + "  " + rawData, "接收");
                                }
                            }
                            break;

                            //  站类为01,02,03,12,13时,不是墒情站
                            case "01":
                            case "02":
                            case "03":
                            case "12":
                            case "13":
                            {
                                CReportStruct report = new CReportStruct();
                                if (Up.Parse(msg, out report))
                                {
                                    report.ChannelType = EChannelType.GPRS;
                                    report.ListenPort  = this.GetListenPort().ToString();
                                    report.flagId      = gprs;

                                    string temp = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                    InvokeMessage("  gprs号码:  " + gprs + String.Format("  {0,-10}   ", temp) + rawData, "接收");

                                    if (this.UpDataReceived != null)
                                    {
                                        this.UpDataReceived.Invoke(null, new UpEventArgs()
                                            {
                                                Value = report, RawData = rawData
                                            });
                                    }
                                    //   InvokeMessage("12333", "接收");
                                }
                                else
                                {
                                    //string temp = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                    InvokeMessage("  gprs号码:  " + gprs + "  可疑数据 " + rawData, "接收");
                                }
                            }
                            break;

                            case "11":
                            {
                                CReportStruct report = new CReportStruct();
                                //CReportArtificalWater report = new CReportArtificalWater();
                                if (Up.Parse_1(msg, out report))
                                {
                                    report.ChannelType = EChannelType.GPRS;
                                    report.ListenPort  = this.GetListenPort().ToString();
                                    report.ReportType  = EMessageType.Batch;
                                    string temptype = "人工水位";
                                    InvokeMessage(String.Format("{0,-10}   ", temptype) + rawData, "接收");
                                    //if (this.UpDataReceived != null)
                                    this.UpDataReceived.Invoke(null, new UpEventArgs()
                                        {
                                            Value = report, RawData = rawData
                                        });
                                }
                            }
                            break;

                            default:
                                break;
                            }
                        }
                        else if (reportType == "23")    //  人工流量
                        {
                            CReportStruct report = new CReportStruct();
                            if (Up.Parse_2(msg, out report))
                            {
                                report.ChannelType = EChannelType.GPRS;
                                report.ListenPort  = this.GetListenPort().ToString();
                                report.ReportType  = EMessageType.Batch;
                                string temptype = "人工流量";
                                InvokeMessage(String.Format("{0,-10}   ", temptype) + rawData, "接收");
                                this.UpDataReceived.Invoke(null, new UpEventArgs()
                                {
                                    Value = report, RawData = rawData
                                });
                            }
                        }
                        else if (reportType == "32")    //  人工报送水位
                        {
                            CReportStruct report = new CReportStruct();
                            report.ChannelType = EChannelType.GPRS;
                            report.ListenPort  = this.GetListenPort().ToString();
                            string temptype = "人工报送水位";
                            InvokeMessage(String.Format("{0,-10}   ", temptype) + rawData, "接收");
                            WriteToFileClass writeClass = new WriteToFileClass("RGwater");
                            //Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile));
                            //t.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n");
                            string a1 = rawData.Substring(9, 1);
                            string a2 = rawData.Substring(10, 1);
                            if (a1 == "P" || a1 == "H" || a1 == "K" || a1 == "Z" || a1 == "D" || a1 == "T" || a1 == "M" || a1 == "G" || a1 == "Y" || a1 == "F" || a1 == "R")
                            {
                                if (a2 == "A")
                                {
                                    if (rawData.Contains("ST"))
                                    {
                                        WriteToFileClass writeClass1 = new WriteToFileClass("sharewater");
                                        Thread           t1          = new Thread(new ParameterizedThreadStart(writeClass1.WriteInfoToFile));
                                        t1.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n");
                                    }
                                }
                            }
                        }
                        else if (reportType == "53")    // 人工报送时段雨量 日雨量 旬雨量 水库水位 蓄水量 入库流量 出库流量
                        {
                            CReportStruct report = new CReportStruct();
                            report.ChannelType = EChannelType.GPRS;
                            report.ListenPort  = this.GetListenPort().ToString();
                            string temptype = "人工报送雨量";
                            InvokeMessage(String.Format("{0,-10}   ", temptype) + rawData, "接收");
                            WriteToFileClass writeClass = new WriteToFileClass("RGRain");
                            Thread           t          = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile));
                            t.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n");
                        }
                        else if (reportType == "25")
                        {
                            //CEntitySoilData readSoilStruct = new CEntitySoilData();
                            //if (Soil.Parse(msg, out readSoilStruct))
                            //{
                            //    readSoilStruct.ChannelType = EChannelType.GPRS;
                            //    //readSoilStruct.ListenPort = this.GetListenPort().ToString();

                            //    //string temp = readSoilStruct.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                            //    //InvokeMessage(String.Format("{0,-10}   ", temp) + rawData, "接收");
                            //    //  抛出读墒情事件
                            //    if (null != this.SoilDataReceived)
                            //        this.SoilDataReceived.Invoke(null, new YACSoilEventArg()
                            //        {
                            //            RawData = rawData,
                            //            Value = readSoilStruct
                            //        });
                            //}

                            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();
                                    soilReport.flagId      = gprs;
                                    this.UpDataReceived(null, new UpEventArgs()
                                    {
                                        RawData = rawData, Value = soilReport
                                    });
                                }
                            }
                        }
                        else //  下行指令
                        {
                            CDownConf downconf = new CDownConf();
                            if (Down.Parse(msg, out downconf))
                            {
                                InvokeMessage(String.Format("{0,-10}   ", "下行指令读取参数") + rawData, "接收");
                                if (this.DownDataReceived != null)
                                {
                                    this.DownDataReceived.Invoke(null, new DownEventArgs()
                                    {
                                        Value = downconf, RawData = rawData
                                    });
                                }
                            }
                        }
                    }
                    #endregion

                    #region 1K
                    if (type == "1K")
                    {
                        var station = FindStationBySID(sid);
                        if (station == null)
                        {
                            throw new Exception("批量传输,站点匹配错误");
                        }
                        //EStationBatchType batchType = station.BatchTranType;

                        //if (batchType == EStationBatchType.EFlash)
                        //{
                        //    CBatchStruct batch = new CBatchStruct();
                        //    if (FlashBatch.Parse(msg, out batch))
                        //    {
                        //        InvokeMessage(String.Format("{0,-10}   ", "Flash批量传输") + rawData, "接收");

                        //        if (this.BatchDataReceived != null)
                        //            this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = rawData });
                        //    }
                        //}
                        //else if (batchType == EStationBatchType.EUPan)
                        //{
                        //    CBatchStruct batch = new CBatchStruct();
                        //    if (UBatch.Parse(msg, out batch))
                        //    {
                        //        InvokeMessage(String.Format("{0,-10}   ", "U盘批量传输") + rawData, "接收");

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

                        CBatchStruct batch = new CBatchStruct();
                        if (FlashBatch.Parse(msg, out batch))
                        {
                            InvokeMessage(String.Format("{0,-10}   ", "批量传输") + rawData, "接收");

                            if (this.BatchDataReceived != null)
                            {
                                this.BatchDataReceived.Invoke(null, new BatchEventArgs()
                                {
                                    Value = batch, RawData = rawData
                                });
                            }
                        }
                    }
                    #endregion

                    #region 1S
                    if (type == "1S")
                    {
                        CDownConf downconf = new CDownConf();
                        if (Down.Parse(msg, out downconf))
                        {
                            InvokeMessage(String.Format("{0,-10}   ", "下行指令设置参数") + rawData, "接收");

                            if (this.DownDataReceived != null)
                            {
                                this.DownDataReceived.Invoke(null, new DownEventArgs()
                                {
                                    Value = downconf, RawData = rawData
                                });
                            }
                        }
                    }
                    #endregion
                }
                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();
                            soilReport.flagId      = gprs;
                            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 bool ParseData(string msg)
        {
            //           InvokeMessage("协议。。。 ", "进入函数7");
            try
            {
                string rawData = msg;

                if (msg.Contains("$"))
                {
                    string data = string.Empty;
                    if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                    {
                        return(false);
                    }
                    msg = data;

                    string sid  = msg.Substring(0, 4);
                    string type = msg.Substring(4, 2);

                    #region 1G
                    if (type == "1G")
                    {
                        //   InvokeMessage("1: " + DateTime.Now, "函数");
                        string reportType = msg.Substring(6, 2);
                        if (reportType == "21" || reportType == "22")   //   定时报,加报
                        {
                            //  YAC设备的墒情协议:
                            string stationType = msg.Substring(8, 2);
                            switch (stationType)
                            {
                            //  站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站
                            case "04":
                            case "05":
                            case "06":
                            case "07":
                            case "17":
                            {
                                //CEntitySoilData soilStruct = new CEntitySoilData();
                                //if (Soil.Parse(msg, out soilStruct))
                                //{
                                //    soilStruct.ChannelType = EChannelType.GPRS;
                                //    //soilStruct.ListenPort = this.GetListenPort().ToString();

                                //    //string temp = soilStruct.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                //    //InvokeMessage(String.Format("{0,-10}   ", temp) + rawData, "接收");

                                //    //  抛出YAC设备墒情事件
                                //    if (null != this.SoilDataReceived)
                                //        this.SoilDataReceived.Invoke(null, new YACSoilEventArg()
                                //        {
                                //            RawData = rawData,
                                //            Value = soilStruct
                                //        });
                                //}

                                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));
                                    }
                                    //1111gm
                                    string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                    InvokeMessage(String.Format("{0,-10}   ", temp) + rawData, "接收");

                                    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
                                {
                                    //string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                    InvokeMessage(" " + rawData, "接收");
                                }
                            }
                            break;

                            //  站类为01,02,03,12,13时,不是墒情站
                            case "01":
                            case "02":
                            case "03":
                            case "12":
                            case "13":
                            {
                                CReportStruct report = new CReportStruct();
                                if (Up.Parse(msg, out report))
                                {
                                    report.ChannelType = EChannelType.GPRS;
                                    report.ListenPort  = this.GetListenPort().ToString();

                                    string temp = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                    InvokeMessage(String.Format("{0,-10}   ", temp) + rawData, "接收");
                                    //by LH 10.05
                                    //读取数据
                                    //ModemDataStruct dat = new ModemDataStruct();
                                    //while (this.GetNextData(out dat))
                                    //{
                                    //uint dtuID = 0;

                                    //var gprs = FindGprsByUserid(userID);
                                    //if (gprs.FindByID(userID, out dtuID))
                                    //{
                                    //    //query = gprs.Down.BuildQuery(stationID, cmds, EChannelType.GPRS);
                                    //    gprs.SendHex(dtuID, bts);
                                    //}

                                    //InvokeMessage("TRU,modemId: " + dat.m_modemId, "发送");
                                    //    InvokeMessage("bts: " + bts, "发送");
                                    //    //InvokeMessage("bts.length: " + (ushort)bts.Length, "发送");
                                    //    // 发送回执

                                    //InvokeMessage("3: " + DateTime.Now, "函数");
                                    // }
                                    //DTUdll.Instance.SendHex(m, bts, (ushort)bts.Length);

                                    //byte[] bts = new byte[] { 84, 82, 85, 13, 10 };
                                    //uint m = 1610637336;
                                    //InvokeMessage("2: " + DateTime.Now, "函数");
                                    //DTUdll.Instance.SendHex(m, bts, (ushort)bts.Length);
                                    //   report.Datas[0].Voltge = Decimal.Parse("111111") * (Decimal)0.01;
                                    //  InvokeMessage(report.Datas[0].Voltge.ToString(), "接收");
                                    if (this.UpDataReceived != null)
                                    {
                                        this.UpDataReceived.Invoke(null, new UpEventArgs()
                                            {
                                                Value = report, RawData = rawData
                                            });
                                    }
                                    //   InvokeMessage("12333", "接收");
                                }
                                else
                                {
                                    //string temp = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                    InvokeMessage(" " + rawData, "接收");
                                }
                            }
                            break;

                            case "11":
                            {
                                CReportStruct report = new CReportStruct();
                                //CReportArtificalWater report = new CReportArtificalWater();
                                if (Up.Parse_1(msg, out report))
                                {
                                    report.ChannelType = EChannelType.GPRS;
                                    report.ListenPort  = this.GetListenPort().ToString();
                                    report.ReportType  = EMessageType.Batch;
                                    string temptype = "人工水位";
                                    InvokeMessage(String.Format("{0,-10}   ", temptype) + rawData, "接收");
                                    //if (this.UpDataReceived != null)
                                    this.UpDataReceived.Invoke(null, new UpEventArgs()
                                        {
                                            Value = report, RawData = rawData
                                        });
                                }
                            }
                            break;

                            default:
                                break;
                            }
                        }
                        else if (reportType == "23")    //  人工流量
                        {
                            CReportStruct report = new CReportStruct();
                            if (Up.Parse_2(msg, out report))
                            {
                                report.ChannelType = EChannelType.GPRS;
                                report.ListenPort  = this.GetListenPort().ToString();
                                report.ReportType  = EMessageType.Batch;
                                string temptype = "人工流量";
                                InvokeMessage(String.Format("{0,-10}   ", temptype) + rawData, "接收");
                                this.UpDataReceived.Invoke(null, new UpEventArgs()
                                {
                                    Value = report, RawData = rawData
                                });
                            }
                        }
                        else if (reportType == "32")    //  人工报送水位
                        {
                            CReportStruct report = new CReportStruct();
                            report.ChannelType = EChannelType.GPRS;
                            report.ListenPort  = this.GetListenPort().ToString();
                            string temptype = "人工报送水位";
                            InvokeMessage(String.Format("{0,-10}   ", temptype) + rawData, "接收");
                            WriteToFileClass writeClass = new WriteToFileClass("RGwater");
                            Thread           t          = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile));
                            t.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n");
                            string a1 = rawData.Substring(9, 1);
                            string a2 = rawData.Substring(10, 1);
                            if (a1 == "P" || a1 == "H" || a1 == "K" || a1 == "Z" || a1 == "D" || a1 == "T" || a1 == "M" || a1 == "G" || a1 == "Y" || a1 == "F" || a1 == "R")
                            {
                                if (a2 == "A")
                                {
                                    if (rawData.Contains("ST"))
                                    {
                                        WriteToFileClass writeClass1 = new WriteToFileClass("sharewater");
                                        Thread           t1          = new Thread(new ParameterizedThreadStart(writeClass1.WriteInfoToFile));
                                        t1.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n");
                                    }
                                }
                            }
                        }
                        else if (reportType == "53")    // 人工报送时段雨量 日雨量 旬雨量 水库水位 蓄水量 入库流量 出库流量
                        {
                            CReportStruct report = new CReportStruct();
                            report.ChannelType = EChannelType.GPRS;
                            report.ListenPort  = this.GetListenPort().ToString();
                            string temptype = "人工报送雨量";
                            InvokeMessage(String.Format("{0,-10}   ", temptype) + rawData, "接收");
                            WriteToFileClass writeClass = new WriteToFileClass("RGRain");
                            Thread           t          = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile));
                            t.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n");
                        }
                        else if (reportType == "25")
                        {
                            //CEntitySoilData readSoilStruct = new CEntitySoilData();
                            //if (Soil.Parse(msg, out readSoilStruct))
                            //{
                            //    readSoilStruct.ChannelType = EChannelType.GPRS;
                            //    //readSoilStruct.ListenPort = this.GetListenPort().ToString();

                            //    //string temp = readSoilStruct.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                            //    //InvokeMessage(String.Format("{0,-10}   ", temp) + rawData, "接收");
                            //    //  抛出读墒情事件
                            //    if (null != this.SoilDataReceived)
                            //        this.SoilDataReceived.Invoke(null, new YACSoilEventArg()
                            //        {
                            //            RawData = rawData,
                            //            Value = readSoilStruct
                            //        });
                            //}

                            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 //  下行指令
                        {
                            CDownConf downconf = new CDownConf();
                            if (Down.Parse(msg, out downconf))
                            {
                                InvokeMessage(String.Format("{0,-10}   ", "下行指令读取参数") + rawData, "接收");
                                if (this.DownDataReceived != null)
                                {
                                    this.DownDataReceived.Invoke(null, new DownEventArgs()
                                    {
                                        Value = downconf, RawData = rawData
                                    });
                                }
                            }
                        }
                    }
                    #endregion

                    #region 1K
                    if (type == "1K")
                    {
                        var station = FindStationBySID(sid);
                        if (station == null)
                        {
                            throw new Exception("批量传输,站点匹配错误");
                        }
                        //EStationBatchType batchType = station.BatchTranType;

                        //if (batchType == EStationBatchType.EFlash)
                        //{
                        //    CBatchStruct batch = new CBatchStruct();
                        //    if (FlashBatch.Parse(msg, out batch))
                        //    {
                        //        InvokeMessage(String.Format("{0,-10}   ", "Flash批量传输") + rawData, "接收");

                        //        if (this.BatchDataReceived != null)
                        //            this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = rawData });
                        //    }
                        //}
                        //else if (batchType == EStationBatchType.EUPan)
                        //{
                        //    CBatchStruct batch = new CBatchStruct();
                        //    if (UBatch.Parse(msg, out batch))
                        //    {
                        //        InvokeMessage(String.Format("{0,-10}   ", "U盘批量传输") + rawData, "接收");

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

                        CBatchStruct batch = new CBatchStruct();
                        if (FlashBatch.Parse(msg, out batch))
                        {
                            InvokeMessage(String.Format("{0,-10}   ", "批量传输") + rawData, "接收");

                            if (this.BatchDataReceived != null)
                            {
                                this.BatchDataReceived.Invoke(null, new BatchEventArgs()
                                {
                                    Value = batch, RawData = rawData
                                });
                            }
                        }
                    }
                    #endregion

                    #region 1S
                    if (type == "1S")
                    {
                        CDownConf downconf = new CDownConf();
                        if (Down.Parse(msg, out downconf))
                        {
                            InvokeMessage(String.Format("{0,-10}   ", "下行指令设置参数") + rawData, "接收");

                            if (this.DownDataReceived != null)
                            {
                                this.DownDataReceived.Invoke(null, new DownEventArgs()
                                {
                                    Value = downconf, RawData = rawData
                                });
                            }
                        }
                    }
                    #endregion
                }
                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 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);
        }
Beispiel #12
0
        //批量传输Flash解析
        public bool Parse_Flash(String msg, out CBatchStruct batch)
        {
            batch = new CBatchStruct();
            try
            {
                string data = string.Empty;
                if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                {
                    return(false);
                }
                Debug.WriteLine(msg);
                //  解析站点ID , 4位     0001
                batch.StationID = data.Substring(0, 4);
                //  解析命令指令 ,2位     1K
                batch.Cmd = data.Substring(4, 2);
                //  解析站点类型, 2位     01
                batch.StationType = ProtocolHelpers.ProtoStr2StationType(data.Substring(6, 2));

                //  解析传输类型, 2位      03
                batch.TransType = ProtocolMaps.TransMap.FindKey(data.Substring(8, 2));

                var datas = new List <CTimeAndData>();
                //  按小时传
                int    year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
                int    segLength = 0;
                string dataSegs  = string.Empty;
                switch (batch.TransType)
                {
                case ETrans.ByHour:
                    year  = Int32.Parse("20" + data.Substring(10, 2)); //  年       yy
                    month = Int32.Parse(data.Substring(12, 2));        //  月       mm

                    //  截取日、小时、分钟和数据段 ,以\r结尾
                    //  ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456#13
                    dataSegs  = data.Substring(14);
                    segLength = 12;
                    while (dataSegs.Length >= segLength)
                    {
                        //  读取前8位数据
                        day    = Int32.Parse(dataSegs.Substring(0, 2));    //  日       dd
                        hour   = Int32.Parse(dataSegs.Substring(2, 2));    //  小时      hh
                        minute = Int32.Parse(dataSegs.Substring(4, 2));    //  分钟   mm
                        string value = dataSegs.Substring(6, 6);
                        datas.Add(new CTimeAndData()
                        {
                            Time = new DateTime(year, month, day, hour, minute, 0),
                            Data = value
                        });
                        //  取剩下的分钟和数据段
                        dataSegs = dataSegs.Substring(segLength);
                    }
                    break;

                case ETrans.ByDay:                                     //  按天传
                    year  = Int32.Parse("20" + data.Substring(10, 2)); //  年     yy
                    month = Int32.Parse(data.Substring(12, 2));        //  月     mm
                    DateTime dt = new DateTime();

                    //day = Int32.Parse(data.Substring(14, 2));        //  日     dd
                    //hour = Int32.Parse(data.Substring(16, 2));       //  小时   hh
                    //DateTime dt = new DateTime(year, month, day, hour, 0, second);
                    ////  截取分钟和数据段
                    ////  mm123456mm123456mm123456mm123456mm123456mm123456mm123456mm123456mm123456mm123456mm123456mm123456#13
                    dataSegs  = data.Substring(14);
                    segLength = 12;
                    // int previousMinute = 0;
                    //bool isFirst = true;
                    while (dataSegs.Length >= segLength)
                    {
                        day    = Int32.Parse(dataSegs.Substring(0, 2));     //  日     dd
                        hour   = Int32.Parse(dataSegs.Substring(2, 2));     //  小时   hh
                        minute = Int32.Parse(dataSegs.Substring(4, 2));     //  分钟   mm
                        second = 0;
                        //  第一段数据
                        //if (isFirst)
                        //{
                        //    dt = new DateTime(year, month, day, hour, minute, second);
                        //}

                        //  不是第一段数据
                        dt = new DateTime(year, month, day, hour, minute, second);
                        //if (!isFirst)
                        //{
                        //    if (previousMinute >= minute)
                        //    {
                        //        dt = dt.AddHours(1);
                        //        //double addhour = 1;
                        //        //dt.AddHours(addhour);
                        //    }
                        //    dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, minute, dt.Second);
                        //}

                        //Int32 value = Int32.Parse(dataSegs.Substring(2, 6));//  数据   123456
                        string value = dataSegs.Substring(6, 6);
                        datas.Add(new CTimeAndData()
                        {
                            Time = dt,
                            Data = value
                        });

                        Debug.WriteLine(string.Format("{0:D2}  {1:D6}  ------ {2}   {3:D6}", minute, value, dt.ToString("yyyyMMdd HH:mm:ss"), value));

                        //  取剩下的日,小时,分钟和数据段
                        dataSegs = dataSegs.Substring(segLength);
                        //previousMinute = minute;
                        //isFirst = false;
                    }
                    break;
                }
                batch.Datas = datas;
                return(true);
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("数据:" + msg);
                System.Diagnostics.Debug.WriteLine("Flash批量传输解析数据不完整!" + exp.Message);
            }
            return(false);
        }
        /// <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);
            }
        }
        /// <summary>
        /// 解析上行报文数据
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="report"></param>
        /// <returns></returns>
        public bool Parse(string msg, out CReportStruct report)
        {
            ///$12345678+140331080400+1257+0251+0078+990+PP
            report = null;
            if (!msg.Contains("+"))
            {
                return(false);
            }
            try
            {
                string data = string.Empty;
                //去除$
                if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                {
                    return(false);
                }
                string[] dataList = data.Split('+');

                DateTime           dataTime;
                string             evpType    = string.Empty;
                string             stationId  = string.Empty;
                List <CReportData> datas      = new List <CReportData>();
                CReportData        reportData = new CReportData();
                if (dataList.Length != 6 && dataList.Length != 7)
                {
                    return(false);
                }

                for (int i = 0; i < dataList.Length; i++)
                {
                    if (i == 0)
                    {
                        if (dataList[i].Length == 8)
                        {
                            stationId = dataList[i];
                            continue;
                        }
                        else
                        {
                            System.Diagnostics.Debug.Write("数据:" + msg);
                            System.Diagnostics.Debug.Write("站号格式不对");
                            return(false);
                        }
                    }
                    if (i == 1)
                    {
                        if (dataList[i].Length == 14)
                        {
                            dataTime = new DateTime(
                                year: Int32.Parse(dataList[i].Substring(0, 4)),
                                month: Int32.Parse(dataList[i].Substring(4, 2)),
                                day: Int32.Parse(dataList[i].Substring(6, 2)),
                                hour: Int32.Parse(dataList[i].Substring(8, 2)),
                                minute: Int32.Parse(dataList[i].Substring(10, 2)),
                                second: Int32.Parse(dataList[i].Substring(12, 2))
                                );
                            reportData.Time = dataTime;
                            continue;
                        }
                        else
                        {
                            System.Diagnostics.Debug.Write("数据:" + msg);
                            System.Diagnostics.Debug.Write("时间格式不对");
                            return(false);
                        }
                    }
                    else if (i == 2)
                    {
                        reportData.Voltge = Decimal.Parse(dataList[i]) * (Decimal)0.01;
                        continue;
                    }
                    else if (i == 3)
                    {
                        reportData.Rain = Decimal.Parse(dataList[i]) * (Decimal)0.01;
                        continue;
                    }
                    else if (i == 4)
                    {
                        reportData.Evp = Decimal.Parse(dataList[i]) * (Decimal)0.01;
                        continue;
                    }
                    else if (i == 5)
                    {
                        reportData.Temperature = Decimal.Parse(dataList[i]) * (Decimal)0.01;
                        continue;
                    }
                    else if (i == 6)
                    {
                        reportData.EvpType = dataList[i];
                        continue;
                    }
                }
                datas.Add(reportData);
                report = new CReportStruct()
                {
                    Stationid   = stationId,
                    Type        = string.Empty,
                    ReportType  = EMessageType.Evp,
                    StationType = EStationType.EHydrology,
                    RecvTime    = DateTime.Now,
                    Datas       = datas
                };
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.Write("数据:" + msg);
                System.Diagnostics.Debug.Write("格式不对");
                return(false);
            }
            return(true);
        }
Beispiel #15
0
        //  批量数据UBatch下行解析
        public bool Parse(String msg, out CBatchStruct batch)
        {
            batch = new CBatchStruct();
            try
            {
                string data = string.Empty;
                if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                {
                    return(false);
                }

                //  解析站点ID , 4位     0001
                batch.StationID = data.Substring(0, 4);
                //  解析命令指令 ,2位     1K
                batch.Cmd = data.Substring(4, 2);
                //  解析站点类型, 2位     01
                batch.StationType = ProtocolHelpers.ProtoStr2StationType(data.Substring(6, 2));
                int DataYear  = Int32.Parse(data.Substring(8, 2));
                int DataMonth = Int32.Parse(data.Substring(10, 2));
                int DataDay   = Int32.Parse(data.Substring(12, 2));
                //创建返回的数据对象
                var datas = new List <CTimeAndData>();
                var lists = data.Substring(14).Split(CSpecialChars.BALNK_CHAR);

                foreach (var item in lists)
                {
                    string itemData = item;
                    while (itemData.Length > 0)
                    {
                        int DataHour   = Int32.Parse(itemData.Substring(0, 2));
                        int DataMinute = Int32.Parse(itemData.Substring(2, 2));
                        //  解析出数据时间
                        DateTime recvTime = new DateTime(
                            year: DataYear,
                            month: DataMonth,
                            day: DataDay,
                            hour: DataHour,
                            minute: DataMinute,
                            second: 0
                            );
                        //  解析出16进制的数据
                        string itemDatas = itemData.Substring(4, 4);
                        //  将16进制数据转化为10进制
                        itemDatas = int.Parse(itemDatas, System.Globalization.NumberStyles.AllowHexSpecifier).ToString();

                        //  创建CTimeAndData对象
                        CTimeAndData result = new CTimeAndData();
                        result.Time = recvTime;
                        result.Data = itemDatas;
                        //  将对像填充至list中
                        datas.Add(result);

                        //构成循环
                        itemData = itemData.Substring(10);
                    }
                }

                batch.Datas = datas;
                return(true);
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("数据:" + msg);
                System.Diagnostics.Debug.WriteLine("批量数据解析不完整" + exp.Message);
            }
            return(false);
        }
Beispiel #16
0
        //  数据下行解析
        public bool Parse(string msg, out CDownConf downConf)
        {
            downConf = new CDownConf();
            int mgb = 0;

            try
            {
                string data = string.Empty;
                if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                {
                    return(false);
                }



                //  解析站点ID
                String stationID = data.Substring(0, 4);
                //  解析通信类别
                String type = data.Substring(4, 2);
                data = data.Substring(6);
                var segs = data.Split(CSpecialChars.BALNK_CHAR);
                foreach (var item in segs)
                {
                    try
                    {
                        //  数据分为两部分
                        //  2 Byte 指令  +  剩下的为数据,数据的长度>= 2
                        //  解析指令类型param
                        EDownParam param = ProtocolMaps.DownParamMap.FindKey(item.Substring(0, 2) == "13" ? "12" : item.Substring(0, 2));
                        //  如果接收到的数据段长度大于2,表示对应的字段有值
                        //  默认为String.Empty
                        string info = string.Empty;
                        if (item.Length > 2)
                        {
                            int length = Int32.Parse(ProtocolMaps.DownParamLengthMap.FindValue(param));
                            //  如果接收到的数据段的长度不等于规定长度,或者规定长度为-1(即长度不定)
                            //        截取剩下的所有字符串
                            //  否则,截取规定长度的字符串
                            info = (item.Length - 2 != length || length == -1) ? item.Substring(2) : item.Substring(2, length);
                        }
                        if (String.IsNullOrEmpty(info))
                        {
                            continue;
                        }

                        switch (param)
                        {
                        case EDownParam.Clock:
                            int year   = Int32.Parse("20" + info.Substring(0, 2));
                            int month  = Int32.Parse(info.Substring(2, 2));
                            int day    = Int32.Parse(info.Substring(4, 2));
                            int hour   = Int32.Parse(info.Substring(6, 2));
                            int minute = Int32.Parse(info.Substring(8, 2));
                            int second = Int32.Parse(info.Substring(10, 2));
                            downConf.Clock = new DateTime(year, month, day, hour, minute, second);
                            break;

                        case EDownParam.NormalState: downConf.NormalState = ProtocolMaps.NormalState4ProtoMap.FindKey(info); break;

                        case EDownParam.Voltage: downConf.Voltage = (Decimal.Parse(info) * (Decimal)0.01); break;

                        case EDownParam.StationCmdID: downConf.StationCmdID = info; break;

                        case EDownParam.TimeChoice: downConf.TimeChoice = ProtocolMaps.TimeChoice4ProtoMap.FindKey(info == "01" ? info : "02"); break;

                        case EDownParam.TimePeriod: downConf.TimePeriod = ProtocolMaps.TimePeriodMap.FindKey(info); break;

                        case EDownParam.WorkStatus: downConf.WorkStatus = ProtocolMaps.WorkStatus4ProtoMap.FindKey(info); break;

                        case EDownParam.VersionNum: downConf.VersionNum = info; break;

                        case EDownParam.StandbyChannel:
                            downConf.MainChannel = ProtocolMaps.ChannelType4ProtoMap.FindKey(info.Substring(0, 2));
                            System.Diagnostics.Debug.Assert(downConf.MainChannel != EChannelType.None, "主用信道不能为NONE");
                            downConf.ViceChannel = ProtocolMaps.ChannelType4ProtoMap.FindKey(info.Substring(2, 2));
                            break;

                        case EDownParam.TeleNum: downConf.TeleNum = info; break;

                        case EDownParam.RingsNum: downConf.RingsNum = Decimal.Parse(info); break;

                        case EDownParam.DestPhoneNum: downConf.DestPhoneNum = info; break;

                        case EDownParam.TerminalNum: downConf.TerminalNum = info; break;

                        case EDownParam.GSMElec: downConf.GSMElec = ProtocolMaps.GSMElec4ProtoMap.FindKey(info); break;

                        case EDownParam.RespBeam: downConf.RespBeam = info; break;

                        case EDownParam.AvegTime: downConf.AvegTime = Decimal.Parse(info); break;

                        case EDownParam.RainPlusReportedValue: downConf.RainPlusReportedValue = Decimal.Parse(info); break;

                        case EDownParam.KC: downConf.KC = info; break;

                        case EDownParam.Rain: downConf.Rain = Decimal.Parse(info); break;

                        //  单位为米    case EDownParam.Water: downConf.Water = (Decimal.Parse(info) * (Decimal)0.01); break;
                        //  默认单位为厘米
                        case EDownParam.storeWater: downConf.storeWater = Decimal.Parse(info); break;

                        case EDownParam.realWater: downConf.realWater = Decimal.Parse(info); break;

                        case EDownParam.WaterBase: downConf.WaterBase = Decimal.Parse(info); break;

                        case EDownParam.WaterPlusReportedValue: downConf.WaterPlusReportedValue = Decimal.Parse(info); break;

                        case EDownParam.SelectCollectionParagraphs: downConf.SelectCollectionParagraphs = ProtocolMaps.SelectCollectionParagraphs4ProtoMap.FindKey(info); break;

                        case EDownParam.StationType:
                            var stype = ProtocolMaps.StationType4ProtoMap.FindKey(info);
                            downConf.StationType = stype; break;

                        case EDownParam.UserName:
                            downConf.UserName = info;
                            break;

                        case EDownParam.StationName:
                            downConf.StationName = info;
                            break;

                        case EDownParam.SensorType:
                            downConf.SensorType = ProtocolMaps.SensorType4ProtoMap.FindKey(info);
                            break;

                        case EDownParam.FlashClear:
                            downConf.FlashClear = info;
                            break;

                        default:
                            mgb--;
                            break;
                        }
                        mgb++;
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine("数据:" + msg);
                        System.Diagnostics.Debug.WriteLine("下行指令解析数据不完整!" + e.Message);
                    }
                }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine(exp.Message);
            }
            if (mgb == 0)
            {
                downConf = null;
                return(false);
            }
            return(true);
        }
Beispiel #17
0
        //  批量数据flash下行解析
        public bool Parse_Flash(String msg, EChannelType ctype, out CBatchStruct batch)
        {
            batch = new CBatchStruct();
            try
            {
                string data = string.Empty;
                if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                {
                    return(false);
                }

                //  解析站点ID , 4位     0001
                batch.StationID = data.Substring(0, 4);
                //  解析命令指令 ,2位     1K
                batch.Cmd = data.Substring(4, 2);
                //  解析站点类型, 2位     01
                batch.StationType = ProtocolHelpers.ProtoStr2StationType_1(data.Substring(6, 2));
                //  解析传输类型, 2位     02/03
                batch.TransType = ProtocolMaps.TransMap.FindKey(data.Substring(8, 2));

                //创建返回的数据对象
                var datas = new List <CTimeAndData>();
                //ByDay ByHour区分
                //02 按天传   03按小时  04按一天整点
                switch (batch.TransType)
                {
                //按天传 02
                case ETrans.ByDay:
                {
                    int dateYear  = Int32.Parse("20" + data.Substring(10, 2));  // 年
                    int dateMonth = Int32.Parse(data.Substring(12, 2));         // 月

                    //数据处理
                    data = data.Substring(14);
                    while (data.Length >= 12)
                    {
                        DateTime recvTime = new DateTime(
                            year: dateYear,                            //年
                            month: dateMonth,                          //月
                            day: Int32.Parse(data.Substring(0, 2)),    //日
                            hour: Int32.Parse(data.Substring(2, 2)),   //时
                            minute: Int32.Parse(data.Substring(4, 2)), //分
                            second: 0
                            );
                        string datasegs = data.Substring(6, 6);
                        datas.Add(new CTimeAndData
                            {
                                Time = recvTime,
                                Data = datasegs
                            });
                        data = data.Substring(12);
                    }
                }
                break;

                //按小时传 03
                case ETrans.ByHour:
                {
                    //判断是GSM还是GPRS传送方式
                    //GSM:yymmddhh  GPRS:yymm
                    if (ctype == EChannelType.GPRS)
                    {
                        //GPRS类型
                        int dateYear  = Int32.Parse("20" + data.Substring(10, 2));     // 年
                        int dateMonth = Int32.Parse(data.Substring(12, 2));            // 月

                        //数据处理
                        data = data.Substring(14);
                        while (data.Length >= 12)
                        {
                            DateTime recvTime = new DateTime(
                                year: dateYear,                            //年
                                month: dateMonth,                          //月
                                day: Int32.Parse(data.Substring(0, 2)),    //日
                                hour: Int32.Parse(data.Substring(2, 2)),   //时
                                minute: Int32.Parse(data.Substring(4, 2)), //分
                                second: 0
                                );
                            string datasegs = data.Substring(6, 6);
                            datas.Add(new CTimeAndData
                                {
                                    Time = recvTime,
                                    Data = datasegs
                                });
                            data = data.Substring(12);
                        }
                    }
                    else if (ctype == EChannelType.GSM)
                    {
                        //GSM类型
                        int dateYear  = Int32.Parse("20" + data.Substring(10, 2));  // 年
                        int dateMonth = Int32.Parse(data.Substring(12, 2));         // 月
                        int dateDay   = Int32.Parse(data.Substring(14, 2));         // 日
                        int dateHour  = Int32.Parse(data.Substring(16, 2));         // 时

                        //数据处理
                        data = data.Substring(14);
                        while (data.Length >= 12)
                        {
                            DateTime recvTime = new DateTime(
                                year: dateYear,                            //年
                                month: dateMonth,                          //月
                                day: dateDay,                              //日
                                hour: dateHour,                            //时
                                minute: Int32.Parse(data.Substring(0, 2)), //分
                                second: 0
                                );
                            string datasegs = data.Substring(2, 6);
                            datas.Add(new CTimeAndData
                                {
                                    Time = recvTime,
                                    Data = datasegs
                                });
                            data = data.Substring(8);
                        }
                    }
                }
                break;

                //按一天整点传 04
                case ETrans.ByDayHour:
                {
                    int dateYear  = Int32.Parse("20" + data.Substring(10, 2)); // 年
                    int dateMonth = Int32.Parse(data.Substring(12, 2));        // 月
                    int dateDay   = Int32.Parse(data.Substring(14, 2));        // 日

                    //数据处理
                    data = data.Substring(16);
                    while (data.Length >= 8)
                    {
                        DateTime recvTime = new DateTime(
                            year: dateYear,                          //年
                            month: dateMonth,                        //月
                            day: dateDay,                            //日
                            hour: Int32.Parse(data.Substring(0, 2)), //时
                            minute: 0,                               //分
                            second: 0
                            );
                        string datasegs = data.Substring(2, 6);
                        datas.Add(new CTimeAndData
                            {
                                Time = recvTime,
                                Data = datasegs
                            });
                        data = data.Substring(8);
                    }
                }
                break;
                }
                batch.Datas = datas;
                return(true);
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("数据:" + msg);
                System.Diagnostics.Debug.WriteLine("批量数据解析不完整" + exp.Message);
            }
            return(false);
        }
Beispiel #18
0
        /// <summary>
        /// 非卫星报文数据解析过程
        /// </summary>
        /// <param name="msg">原始报文数据</param>
        /// <param name="report">报文最终解析出的结果数据结构</param>
        /// <returns>是否解析成功</returns>
        public bool Parse(String msg, out CReportStruct report)
        {
            //$30151G22010201120326001297065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239
            report = null;
            try
            {
                if (msg == "")
                {
                    return(false);
                }
                string data = string.Empty;
                //去除起始符'$'
                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);

                EMessageType       reportType;
                EStationType       stationType;
                string             packageNum;
                DateTime           recvTime;
                Decimal            Voltage;
                string             lists;
                List <CReportData> datas;
                ///0201120326001297065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239
                //站类区别处理
                switch (reportTypeString)
                {
                //定时报新增48段次定时报类
                case "25":
                    //获取报类
                    reportTypeString = "22";
                    reportType       = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString);
                    //获取站类
                    stationType = ProtocolHelpers.ProtoStr2StationType(stationTypeString);
                    //包序号暂不处理
                    packageNum = data.Substring(10, 4);
                    //接收时间
                    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: Int32.Parse(data.Substring(22, 2)),
                        second: 0
                        );
                    //电压值:1297=12.97V
                    Voltage = Decimal.Parse(data.Substring(24, 4)) * (Decimal)0.01;
                    //数据段
                    lists  = data.Substring(28).Replace(" ", "");
                    datas  = GetData_1(lists, recvTime, Voltage, stationType);
                    report = new CReportStruct()
                    {
                        Stationid   = StationId,
                        Type        = type,
                        ReportType  = reportType,
                        StationType = stationType,
                        RecvTime    = DateTime.Now,
                        Datas       = datas
                    };
                    break;

                //定时报
                case "22":
                    //获取报类
                    reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString);
                    //获取站类
                    stationType = ProtocolHelpers.ProtoStr2StationType(stationTypeString);
                    //包序号
                    packageNum = data.Substring(10, 4);
                    //接收时间
                    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
                        );
                    //把包序号写入缓存
                    CEntityPackage package = new CEntityPackage()
                    {
                        StrStationID = StationId,
                        PackageNum   = packageNum,
                        time         = recvTime
                    };
                    if (cEntityPackage.ContainsKey(StationId))
                    {
                        cEntityPackage[StationId] = package;
                    }
                    else
                    {
                        cEntityPackage.Add(StationId, package);
                    }
                    //电压值:1297=12.97V
                    Voltage = Decimal.Parse(data.Substring(22, 4)) * (Decimal)0.01;
                    //数据段
                    lists  = data.Substring(26).Replace(" ", "");
                    datas  = GetData(lists, recvTime, Voltage, stationType);
                    report = new CReportStruct()
                    {
                        Stationid   = StationId,
                        Type        = type,
                        ReportType  = reportType,
                        StationType = stationType,
                        RecvTime    = DateTime.Now,
                        Datas       = datas
                    };
                    break;

                //报讯系统加报
                case "21":
                {
                    if (data.Substring(8, 2) != "11")
                    {
                        //  解析报文类别
                        reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString);
                        //  解析站点类别
                        stationType = ProtocolHelpers.ProtoStr2StationType(stationTypeString);
                        //  解析接收时间
                        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 dataLists = data.Substring(20).Split(CSpecialChars.BALNK_CHAR);
                        datas = GetAddData(dataLists, recvTime, stationType);

                        report = new CReportStruct()
                        {
                            Stationid   = StationId,
                            Type        = type,
                            ReportType  = reportType,
                            StationType = stationType,
                            RecvTime    = DateTime.Now,
                            Datas       = datas
                        };
                    }
                    break;
                }
                }
                return(true);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("数据:" + msg);
                System.Diagnostics.Debug.WriteLine("中游局协议解析不完整" + e.Message);
                //return false;
            }
            return(false);
        }