Exemplo n.º 1
0
 public static void Import(CBatchStruct batch, bool isUpdate = false)
 {
     // 站点类型
     //     01为水位
     //     02为雨量
     //  若数据库中没有时间相同的记录,则更新
     //  若数据库中包含时间相同的记录,且isUpdate = true 判断是否更新
 }
        /// <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);
        }
Exemplo n.º 3
0
        private void BatchForData_EventHandler(object sender, BatchEventArgs e)
        {
            try
            {
                batchMsg = new BatchMsg()
                {
                    Msg = e.RawData
                };
                CBatchStruct info = e.Value;
                if (info == null)
                {
                    return;
                }
                BatchData batchInfo = new BatchData();
                batchInfo.StationId = info.StationID;
                if (info.StationType == EStationType.ERainFall)
                {
                    batchInfo.StationType = true;
                }
                else
                {
                    batchInfo.StationType = false;
                }
                this.GetBatchList.Add(batchInfo);

                foreach (var v in info.Datas)
                {
                    BatchData dataInfo = new BatchData();
                    dataInfo.Time = v.Time.ToString();
                    dataInfo.Data = v.Data;
                    this.GetBatchList.Add(dataInfo);
                }

                BatchData endInfo = new BatchData();
                endInfo.StationId = endInfo.StationId;
                endInfo.End       = true;
                this.GetBatchList.Add(endInfo);
            }
            catch (Exception exp)
            {
                Debug.WriteLine("" + exp.Message);
            }
        }
Exemplo n.º 4
0
        private void BatchForData_EventHandler(object sender, BatchEventArgs e)
        {
            try
            {
                CBatchStruct info = e.Value;
                if (info == null)
                {
                    return;
                }
                if (BatchList.Bdata.Count != 0 && info.StationID == BatchList.StationId)
                {
                    return;
                }
                BatchList.RawInfo = e.RawData;
                if (info.StationType == EStationType.ERainFall)
                {
                    BatchList.SType = BatchList.Types.stationType.RainStation;
                }
                else if (info.StationType == EStationType.ERiverWater)
                {
                    BatchList.SType = BatchList.Types.stationType.WaterStation;
                }

                foreach (var v in info.Datas)
                {
                    BatchData dataInfo = new BatchData();
                    dataInfo.BatchTime  = v.Time.ToString();
                    dataInfo.BatchValue = v.Data;
                    BatchList.Bdata.Add(dataInfo);
                }
            }
            catch (Exception exp)
            {
                Debug.WriteLine("" + exp.Message);
            }
        }
Exemplo n.º 5
0
        private void DealData()
        {
            while (true)
            {
                m_semaphoreData.WaitOne(); //阻塞当前线程,知道被其它线程唤醒
                // 获取对data内存缓存的访问权
                m_mutexListDatas.WaitOne();
                List <HDModemDataStruct> dataListTmp = m_listDatas;
                m_listDatas = new List <HDModemDataStruct>(); //开辟一快新的缓存区
                m_mutexListDatas.ReleaseMutex();
                for (int i = 0; i < dataListTmp.Count; ++i)
                {
                    try
                    {
                        HDModemDataStruct dat = dataListTmp[i];
                        //1获取gprs号码,并根据gprs号码获取站点id
                        //StringBuilder gprsId = new StringBuilder();
                        //foreach (byte b in dat.m_data_buf)
                        //{
                        //    gprsId.AppendFormat("{0:x2}", b);
                        //}
                        string gprs = System.Text.Encoding.Default.GetString(dat.m_modemId);
                        gprs = gprs.Replace("\0", "");
                        string sid = Manager.XmlStationData.Instance.GetStationByGprsID(gprs);

                        //1.2 获取ascii原始数据,获取报文头
                        string data = System.Text.Encoding.Default.GetString(dat.m_data_buf).TrimEnd('\0');
                        InvokeMessage(data, "原始数据");
                        data = data.Trim();


                        if (data.Contains("TRU"))
                        {
                            Debug.WriteLine("接收数据TRU完成,停止计时器");
                            //m_timer.Stop();
                            InvokeMessage("TRU " + System.Text.Encoding.Default.GetString(dat.m_modemId), "接收");
                            if (this.ErrorReceived != null)
                            {
                                this.ErrorReceived.Invoke(null, new ReceiveErrorEventArgs()
                                {
                                    //   Msg = "TRU " + dat.m_modemId
                                    Msg = "TRU " + System.Text.Encoding.Default.GetString(dat.m_modemId)
                                });
                            }
                        }
                        if (data.Contains("ATE0"))
                        {
                            Debug.WriteLine("接收数据ATE0完成,停止计时器");
                            //m_timer.Stop();
                            // InvokeMessage("ATE0", "接收");
                            if (this.ErrorReceived != null)
                            {
                                this.ErrorReceived.Invoke(null, new ReceiveErrorEventArgs()
                                {
                                    Msg = "ATE0"
                                });
                            }
                        }
                        string result = null;
                        if (data.Contains("$"))
                        {
                            result = data.Substring(data.IndexOf("$"), data.IndexOf("\0"));

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

                            CReportStruct report     = new CReportStruct();
                            CDownConf     downReport = new CDownConf();
                            if (dataProtocol == "ZYJBX")
                            {
                                Up   = new Data.ZYJBX.UpParser();
                                Down = new Data.ZYJBX.DownParser();
                            }
                            if (dataProtocol == "RG30")
                            {
                                //回复TRU,确认接收数据
                                InvokeMessage("TRU " + gprs, "发送");
                                byte[] bts = new byte[] { 84, 82, 85, 13, 10 };
                                this.sendHex(gprs.Trim(), bts, (uint)bts.Length, null);


                                Up   = new Data.RG30.UpParser();
                                Down = new Data.RG30.DownParser();
                                //2.1 如果是RG30,则需获取16进制字符串
                                StringBuilder adcpMsg = new StringBuilder();
                                foreach (byte b in dat.m_data_buf)
                                {
                                    adcpMsg.AppendFormat("{0:x2}", b);
                                }
                                string temp = adcpMsg.ToString().Trim();
                                if (temp.Length < 200)
                                {
                                    return;
                                }
                                InvokeMessage(temp, "原始数据");
                                //2.2 获取封装的头部信息
                                string head   = data.Substring(0, 57);
                                int    length = int.Parse(data.Substring(53, 4));

                                //2.3 根据头部信息获取数据类型  HADCP OR VADCP
                                string type = "";
                                if (head.Contains("HADCP"))
                                {
                                    type = "H";
                                }
                                else if (head.Contains("VADCP"))
                                {
                                    type = "V";
                                }
                                string hdt0 = "";
                                string vdt0 = "";
                                //2.4 根据头部信息截图DT0数据
                                if (type == "H")
                                {
                                    hdt0 = temp.Substring(57, length * 2);
                                    //hdt0 = temp.Substring(57);
                                    //写入DT0文件
                                    Write2File writeClass = new Write2File("hdt0");
                                    Thread     t          = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile));
                                    t.Start(hdt0 + "\r\n");

                                    //调用dll解析计算
                                    //TODO需要调试
                                    //ReadPD0FileTest();
                                }
                                else if (type == "V")
                                {
                                    vdt0 = temp.Substring(57, length * 2);
                                    Write2File writeClass = new Write2File("vdt0");
                                    Thread     t          = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile));
                                    t.Start(vdt0 + "\r\n");
                                }
                                return;
                            }
                            //批量传输解析
                            if (data.Contains("1K"))
                            {
                                var station = FindStationBySID(sid);
                                if (station == null)
                                {
                                    throw new Exception("批量传输,站点匹配错误");
                                }
                                CBatchStruct batch = new CBatchStruct();
                                InvokeMessage(String.Format("{0,-10}   ", "批量传输") + data, "接收");

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

                                //根据$将字符串进行分割
                                var lists = result.Split('$');
                                foreach (var msg in lists)
                                {
                                    if (msg.Length < 10)
                                    {
                                        continue;
                                    }
                                    string plusMsg = "$" + msg.TrimEnd();
                                    bool   ret     = Up.Parse(plusMsg, out report);
                                    if (ret && report != null)
                                    {
                                        report.ChannelType = EChannelType.GPRS;
                                        report.ListenPort  = this.GetListenPort().ToString();
                                        report.flagId      = gprs;
                                        string rtype = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                        InvokeMessage("gprs号码:  " + gprs + "   " + String.Format("{0,-10}   ", rtype) + plusMsg, "接收");
                                        //TODO 重新定义事件
                                        if (this.UpDataReceived != null)
                                        {
                                            this.UpDataReceived.Invoke(null, new UpEventArgs()
                                            {
                                                Value = report, RawData = plusMsg
                                            });
                                        }
                                    }
                                }
                            }
                            //其他报文
                            else
                            {
                                Down.Parse(result, out downReport);
                                if (downReport != null)
                                {
                                    InvokeMessage(String.Format("{0,-10}   ", "下行指令读取参数") + result, "接收");
                                    if (this.DownDataReceived != null)
                                    {
                                        this.DownDataReceived.Invoke(null, new DownEventArgs()
                                        {
                                            Value = downReport, RawData = result
                                        });
                                    }
                                }
                            }
                        }
                        #region 报文解析部分
                        //报文解析 直接调用
                        //if (temp.StartsWith("24") && (temp.Contains("7F7F") || temp.Contains("7f7f")) && (temp.Contains("2A2A") || temp.Contains("2a2a")))
                        ////if ((temp.Contains("7F7F") || temp.Contains("7f7f")) && (temp.Contains("2A2A") || temp.Contains("2a2a")))
                        //{
                        //    //1.获取头部信息
                        //    string headInfo = temp.Substring(0, 114);
                        //    //2.集合头信息
                        //    //2.1 计算集合头信息总长度
                        //    string setHeadInfoID = temp.Substring(114, 4);
                        //    string setHeadInfoMsgLength = temp.Substring(118, 4);
                        //    string setHeadInfoStay = temp.Substring(122, 2);
                        //    string setHeadInfoNum = temp.Substring(123, 2);
                        //    int setHeadInfoLength = 4 + 4 + 2 + 2 + 2 * (Convert.ToInt32(setHeadInfoNum,16));
                        //    string setHeadInof = temp.Substring(114, setHeadInfoLength);

                        //    //3.固定头信息
                        //    string fixHeadInfo = temp.Substring(114 + setHeadInfoLength, 118);
                        //    //3.1 获取层数信息
                        //    string speedLayersStr = fixHeadInfo.Substring(18, 2);
                        //    int speedLayers = Convert.ToInt32(speedLayersStr, 16);
                        //    //3.2 获取层厚
                        //    decimal thickness = Convert.ToInt32(fixHeadInfo.Substring(26, 2) + fixHeadInfo.Substring(24, 2), 16);
                        //    //3.3 获取盲区
                        //    decimal blindzone = Convert.ToInt32(fixHeadInfo.Substring(30, 2) + fixHeadInfo.Substring(28, 2), 16);



                        //    //4.可变头信息
                        //    string variableHeadInfo = temp.Substring(114 + 118 + setHeadInfoLength, 112);
                        //    //4.1获取数据时间
                        //    string year = "20" + Convert.ToInt32(variableHeadInfo.Substring(8, 2), 16).ToString();
                        //    string month = Convert.ToInt32(variableHeadInfo.Substring(10, 2), 16).ToString();
                        //    string day = Convert.ToInt32(variableHeadInfo.Substring(12, 2), 16).ToString();
                        //    string hour = Convert.ToInt32(variableHeadInfo.Substring(14, 2), 16).ToString();
                        //    string minute = Convert.ToInt32(variableHeadInfo.Substring(16, 2), 16).ToString();
                        //    string second = Convert.ToInt32(variableHeadInfo.Substring(18, 2), 16).ToString();
                        //    DateTime datatime = new DateTime(int.Parse(year), int.Parse(month), int.Parse(day), int.Parse(hour), int.Parse(minute),int.Parse(second));

                        //    //5.流速数据
                        //    string speedInfo = temp.Substring(114 + 118 + 112 + setHeadInfoLength, speedLayers * 16 + 4);



                        //    //6.相关系数
                        //    string coefficientInfo = temp.Substring(114 + 118 + 112 + setHeadInfoLength + speedLayers * 16 + 4, speedLayers * 8 + 4);

                        //    //7.回波强度
                        //    string echoIntensityInfo = temp.Substring(114 + 118 + 112 + setHeadInfoLength + speedLayers * 16 + 4 + speedLayers * 8 + 4, speedLayers * 8 + 4);

                        //    //5.1 6.1 7.1 解析获得每层的数据
                        //    List<CentityLayerSpeed> layerInfoList = new List<CentityLayerSpeed>();
                        //    int speedFlag = 4;
                        //    int cfctFlag = 4;
                        //    int echoFlag = 4;
                        //    for(int layer=0;i< speedLayers; layer++)
                        //    {
                        //        speedFlag = layer * 16 + 4;
                        //        cfctFlag = layer * 8 + 4;
                        //        cfctFlag = layer * 8 + 4;
                        //        CentityLayerSpeed layerInfo = new CentityLayerSpeed();
                        //        try
                        //        {
                        //            layerInfo.layers = layer + 1;
                        //            layerInfo.StationID = "6000";
                        //            layerInfo.datatime = datatime;
                        //            layerInfo.speed1 = Convert.ToInt32(speedInfo.Substring(speedFlag + 2, 2) + speedInfo.Substring(speedFlag, 2), 16);
                        //            layerInfo.speed2 = Convert.ToInt32(speedInfo.Substring(speedFlag + 6, 2) + speedInfo.Substring(speedFlag+4, 2), 16);
                        //            layerInfo.speed3 = Convert.ToInt32(speedInfo.Substring(speedFlag + 10, 2) + speedInfo.Substring(speedFlag+8, 2), 16);
                        //            layerInfo.speed4 = Convert.ToInt32(speedInfo.Substring(speedFlag + 14, 2) + speedInfo.Substring(speedFlag+12, 2), 16);
                        //            layerInfo.cfct1 = Convert.ToInt32(coefficientInfo.Substring(cfctFlag, 2), 16);
                        //            layerInfo.cfct2 = Convert.ToInt32(coefficientInfo.Substring(cfctFlag+2, 2), 16);
                        //            layerInfo.cfct3 = Convert.ToInt32(coefficientInfo.Substring(cfctFlag+4, 2), 16);
                        //            layerInfo.cfct4 = Convert.ToInt32(coefficientInfo.Substring(cfctFlag+6, 2), 16);
                        //            layerInfo.echo1 = Convert.ToInt32(echoIntensityInfo.Substring(echoFlag, 2), 16);
                        //            layerInfo.echo2 = Convert.ToInt32(echoIntensityInfo.Substring(echoFlag+2, 2), 16);
                        //            layerInfo.echo3 = Convert.ToInt32(echoIntensityInfo.Substring(echoFlag+4, 2), 16);
                        //            layerInfo.echo4 = Convert.ToInt32(echoIntensityInfo.Substring(echoFlag+6, 2), 16);
                        //            layerInfoList.Add(layerInfo);
                        //        }
                        //        catch(Exception e)
                        //        {

                        //        }
                        //    }

                        //    //8.比例因子
                        //    string scaleFactorInfo = temp.Substring(114 + 118 + 112 + setHeadInfoLength + speedLayers * 16 + 4 + speedLayers * 8 + 4 + speedLayers * 8 + 4, 22);
                        //    //8.1 相关参数
                        //    List<CEntityAdcpParam> adcpParamList = new List<CEntityAdcpParam>();
                        //    CEntityAdcpParam adcpParam = new CEntityAdcpParam();
                        //    adcpParam.StationID = "6000";
                        //    adcpParam.datatime = datatime;
                        //    adcpParam.layers = speedLayers;
                        //    adcpParam.thickness = thickness;
                        //    adcpParam.blindzone = blindzone;
                        //    try
                        //    {
                        //        adcpParam.cfct = Convert.ToInt32(scaleFactorInfo.Substring(4, 2), 16);
                        //        adcpParam.voltage = Convert.ToInt32(scaleFactorInfo.Substring(6, 2), 16);
                        //        adcpParam.height1 = Convert.ToInt32(scaleFactorInfo.Substring(8, 2), 16);
                        //        adcpParam.height2 = Convert.ToInt32(scaleFactorInfo.Substring(10, 2), 16);
                        //        adcpParam.height3 = Convert.ToInt32(scaleFactorInfo.Substring(12, 2), 16);
                        //        adcpParam.height4 = Convert.ToInt32(scaleFactorInfo.Substring(14, 2), 16);
                        //        adcpParam.v1 = Convert.ToInt32(scaleFactorInfo.Substring(16, 2), 16);
                        //        adcpParam.v2 = Convert.ToInt32(scaleFactorInfo.Substring(18, 2), 16);
                        //        adcpParam.echo = Convert.ToInt32(scaleFactorInfo.Substring(20, 2), 16);
                        //        adcpParamList.Add(adcpParam);
                        //    }catch(Exception e)
                        //    {

                        //    }
                        #endregion



                        //调用计算dll
                        //TODO


                        //11.将数据写入返回数据结构
                        //CReportStruct report = new CReportStruct();
                        //report.Stationid = "6000";
                        //report.Type = "01";
                        //report.ReportType = EMessageType.EAdditional;
                        //report.StationType = EStationType.EHydrology;
                        //report.ChannelType = EChannelType.GPRS;
                        //report.ListenPort = this.GetListenPort().ToString();
                        //report.flagId = gprs;
                        //string rtype = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                        //List<CReportData> datas = new List<CReportData>();
                        //foreach(CentityLayerSpeed layerInfo in layerInfoList)
                        //{
                        //    CReportData data = new CReportData();
                        //    data.Time = layerInfo.datatime;
                        //    data.layer = layerInfo.layers;
                        //    data.speed1 = layerInfo.speed1;
                        //    data.speed2 = layerInfo.speed2;
                        //    data.speed3 = layerInfo.speed3;
                        //    data.speed4 = layerInfo.speed4;
                        //    data.cfct1 = layerInfo.cfct1;
                        //    data.cfct2 = layerInfo.cfct2;
                        //    data.cfct3 = layerInfo.cfct3;
                        //    data.cfct4 = layerInfo.cfct4;
                        //    data.echo1 = layerInfo.echo1;
                        //    data.echo2 = layerInfo.echo2;
                        //    data.echo3 = layerInfo.echo3;
                        //    data.echo4 = layerInfo.echo4;
                        //    datas.Add(data);
                        //}

                        //foreach(CEntityAdcpParam param in adcpParamList)
                        //{
                        //    CReportData data = new CReportData();
                        //    data.Time = param.datatime;
                        //    data.layers = param.layers;
                        //    data.thickness = param.thickness;
                        //    data.blindzone = param.blindzone;
                        //    data.cfct   = param.cfct;
                        //    data.voltage = param.voltage;
                        //    data.height1 = param.height1;
                        //    data.height2 = param.height2;
                        //    data.height3 = param.height3;
                        //    data.height4 = param.height4;
                        //    data.v1 = param.v1;
                        //    data.v2 = param.v2;
                        //    data.echo = param.echo;
                        //    datas.Add(data);
                        //}

                        //if (this.UpDataReceived != null)
                        //{
                        //    this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = temp });
                        //}
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("" + e.Message);
                    }
                }
            }
        }
Exemplo n.º 6
0
        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);
            }
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
 public bool Parse_Batch(string msg, out CBatchStruct batch)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 11
0
 //  数据批量主板下行解析
 public bool Parse_Batch(String msg, out CBatchStruct batch)
 {
     batch = null;
     return(false);
 }
Exemplo n.º 12
0
 //  批量数据flash下行解析
 public bool Parse_Flash(String msg, EChannelType ctype, out CBatchStruct batch)
 {
     batch = null;
     return(false);
 }
Exemplo n.º 13
0
        private void DealData()
        {
            while (true)
            {
                m_semaphoreData.WaitOne(); //阻塞当前线程,知道被其它线程唤醒
                // 获取对data内存缓存的访问权
                m_mutexListDatas.WaitOne();
                List <HDModemDataStruct> dataListTmp = m_listDatas;
                m_listDatas = new List <HDModemDataStruct>(); //开辟一快新的缓存区
                m_mutexListDatas.ReleaseMutex();
                for (int i = 0; i < dataListTmp.Count; ++i)
                {
                    try
                    {
                        HDModemDataStruct dat  = dataListTmp[i];
                        string            data = System.Text.Encoding.Default.GetString(dat.m_data_buf);
                        string            temp = data.Trim();

                        string result = null;
                        if (temp.Contains("TRU"))
                        {
                            Debug.WriteLine("接收数据TRU完成,停止计时器");
                            //m_timer.Stop();
                            InvokeMessage("TRU " + System.Text.Encoding.Default.GetString(dat.m_modemId), "接收");
                            if (this.ErrorReceived != null)
                            {
                                this.ErrorReceived.Invoke(null, new ReceiveErrorEventArgs()
                                {
                                    //   Msg = "TRU " + dat.m_modemId
                                    Msg = "TRU " + System.Text.Encoding.Default.GetString(dat.m_modemId)
                                });
                            }
                        }
                        if (temp.Contains("ATE0"))
                        {
                            Debug.WriteLine("接收数据ATE0完成,停止计时器");
                            //m_timer.Stop();
                            // InvokeMessage("ATE0", "接收");
                            if (this.ErrorReceived != null)
                            {
                                this.ErrorReceived.Invoke(null, new ReceiveErrorEventArgs()
                                {
                                    Msg = "ATE0"
                                });
                            }
                        }
                        if (temp.Contains("$"))
                        {
                            result = temp.Substring(temp.IndexOf("$"), temp.IndexOf("\0"));
                            string sid  = result.Substring(1, 4);
                            string type = result.Substring(5, 2);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                                    if (down1.Parse_Flash(result, EChannelType.GPRS, out batch))
                                    {
                                        if (this.BatchDataReceived != null)
                                        {
                                            this.BatchDataReceived.Invoke(null, new BatchEventArgs()
                                                {
                                                    Value = batch, RawData = temp
                                                });
                                        }
                                    }
                                    else if (down1.Parse_Batch(result, out batch))
                                    {
                                        if (this.BatchDataReceived != null)
                                        {
                                            this.BatchDataReceived.Invoke(null, new BatchEventArgs()
                                                {
                                                    Value = batch, RawData = temp
                                                });
                                        }
                                    }
                                }

                                if (result.Contains("1G21") || result.Contains("1G22") || result.Contains("1G25"))
                                {
                                    var lists = result.Split('$');
                                    foreach (var msg in lists)
                                    {
                                        if (msg.Length < 5)
                                        {
                                            continue;
                                        }
                                        string plusMsg = "$" + msg;
                                        Up1.Parse(plusMsg, out report);
                                        if (report != null)
                                        {
                                            string gprs = System.Text.Encoding.Default.GetString(dat.m_modemId);
                                            gprs = gprs.Contains("\0")? gprs.Replace("\0", "") : gprs;
                                            report.ChannelType = EChannelType.GPRS;
                                            report.ListenPort  = this.GetListenPort().ToString();
                                            report.flagId      = gprs;
                                            string rtype = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报";
                                            InvokeMessage("gprs号码:  " + gprs + "   " + String.Format("{0,-10}   ", rtype) + plusMsg, "接收");
                                            //TODO 重新定义事件
                                            if (this.UpDataReceived != null)
                                            {
                                                this.UpDataReceived.Invoke(null, new UpEventArgs()
                                                    {
                                                        Value = report, RawData = plusMsg
                                                    });
                                            }
                                            InvokeMessage("TRU " + gprs, "发送");
                                            byte[] bts = new byte[] { 84, 82, 85, 13, 10 };
                                            this.sendHex(gprs.Trim(), bts, (uint)bts.Length, null);
                                        }
                                    }
                                }
                                else
                                {
                                    down1.Parse(result, out downReport);
                                    if (downReport != null)
                                    {
                                        InvokeMessage(String.Format("{0,-10}   ", "下行指令读取参数") + result, "接收");
                                        if (this.DownDataReceived != null)
                                        {
                                            this.DownDataReceived.Invoke(null, new DownEventArgs()
                                                {
                                                    Value = downReport, RawData = result
                                                });
                                        }
                                    }
                                }

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

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

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

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

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


                        //获取DTU列表的函数,DTU在线列表测试
                        //uint amount = this.getDTUAmount();
                        //HDModemInfoStruct data1= new HDModemInfoStruct();
                        //this.getDTUByPosition(0, out data1);
                        //Dictionary<string, HDModemInfoStruct> datas = new Dictionary<string, HDModemInfoStruct>();
                        //this.getDTUList(out datas);
                        if (temp.Contains("BEG"))
                        {
                            Data.ZYJBX.DownParser down1 = new Data.ZYJBX.DownParser();
                            CSDStruct             sd    = new CSDStruct();
                            string gprs = System.Text.Encoding.Default.GetString(dat.m_modemId);
                            gprs = gprs.Replace("\0", "");
                            //string gprs = dat.m_modemId.ToString();
                            string id = Manager.XmlStationDataSerializer.Instance.GetStationByGprsID(gprs);
                            if (down1.Parse_SD(temp, id, out sd))
                            {
                                InvokeMessage(String.Format("{0,-10}   ", "批量SD传输") + temp, "接收");

                                if (this.BatchSDDataReceived != null)
                                {
                                    this.BatchSDDataReceived.Invoke(null, new BatchSDEventArgs()
                                    {
                                        Value = sd, RawData = temp
                                    });
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("" + e.Message);
                    }
                }
            }
        }
Exemplo n.º 14
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);
        }
Exemplo n.º 15
0
        private void DealData()
        {
            while (true)
            {
                m_semaphoreData.WaitOne(); //阻塞当前线程,知道被其它线程唤醒
                // 获取对data内存缓存的访问权
                m_mutexListDatas.WaitOne();
                List <HDModemDataStruct> dataListTmp = m_listDatas;
                m_listDatas = new List <HDModemDataStruct>(); //开辟一快新的缓存区
                m_mutexListDatas.ReleaseMutex();
                for (int i = 0; i < dataListTmp.Count; ++i)
                {
                    try
                    {
                        HDModemDataStruct dat      = dataListTmp[i];
                        string            data     = System.Text.Encoding.Default.GetString(dat.m_data_buf).TrimEnd('\0');
                        string            recvData = data.Trim();

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

                        string temp = data.Trim();

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

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

                            result = result.Substring(0, length);

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

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

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

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

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

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

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

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

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

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

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

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

                                if (this.BatchSDDataReceived != null)
                                {
                                    this.BatchSDDataReceived.Invoke(null, new BatchSDEventArgs()
                                    {
                                        Value = sd, RawData = temp
                                    });
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("" + e.Message);
                    }
                }
            }
        }
Exemplo n.º 16
0
        public void Import(CBatchStruct batch, DateTime DStartTime, DateTime DEndTime, bool isUpdate = false)
        {
            DateTime?     lastTime             = null;
            ERTDDataState tmpRTDWaterDataState = ERTDDataState.ENormal;
            ////gm  1024 $60091K02031610220800084505230800084505
            //string rawString = rawData;
            //string startTime = rawString.Substring(11, 10);
            //string endTime = rawString.Substring(27, 6);
            //string strYear = 20 + startTime.Substring(0, 2);
            //string strMonth = startTime.Substring(2, 2);
            //string strStartDay = startTime.Substring(4, 2);
            //string strStartHour = startTime.Substring(6, 2);
            //string strStartMinute = startTime.Substring(8, 2);
            //string strEndDay = endTime.Substring(0, 2);
            //string strEndHour = endTime.Substring(2, 2);
            //string strEndMinute = endTime.Substring(4, 2);

            ////DateTime DStartTime = new DateTime(int.Parse(strYear), int.Parse(strMonth), int.Parse(strStartDay), int.Parse(strStartHour), int.Parse(strStartMinute), 0);
            ////DateTime DEndTime = new DateTime(int.Parse(strYear), int.Parse(strMonth), int.Parse(strEndDay), int.Parse(strEndHour), int.Parse(strEndMinute), 0);

            List <CTimeAndData> datas = batch.Datas;
            string         stationid  = batch.StationID;
            EStationType   type       = batch.StationType;
            CEntityStation station    = m_proxyStation.QueryById(stationid);

            //  if (type == EStationType.ERainFall)
            if (type == EStationType.ERiverWater)
            // if (true)
            {
                for (int i = 0; i < datas.Count; i++)
                {
                    if (lastTime == datas[i].Time)
                    {
                        lastTime = datas[i].Time;
                        continue;
                    }
                    lastTime = datas[i].Time;
                    int                status          = 1;
                    DateTime           dt              = datas[i].Time;
                    string             data            = datas[i].Data.ToString();
                    int                RawtotalRain    = int.Parse(data.Substring(0, 4));
                    Nullable <Decimal> RawrainAccuracy = decimal.Parse((data.Substring(4, 2)));
                    Nullable <Decimal> rainAccuracy    = RawrainAccuracy / 10;
                    Nullable <Decimal> totalRain       = RawtotalRain * rainAccuracy;
                    CEntityRain        LastSharpMes    = new CEntityRain();
                    CEntityRain        LastDayMes      = new CEntityRain();
                    //如果是第一条数据,则通过数据库获取上一条数据
                    if (i == 0)
                    {
                        Nullable <Decimal>      lastTotalRain = null, lastDayTotalRain = null, lastSharpClockTotalRain = null;
                        Nullable <DateTime>     lastDayTime        = null;
                        Nullable <DateTime>     lastClockSharpTime = null;
                        Nullable <DateTime>     lastDataTime       = null;
                        Nullable <Decimal>      lastPeriodRain     = null;
                        Nullable <EChannelType> lastChannelType    = null;
                        Nullable <EMessageType> lastMessageType    = null;

                        LastSharpMes = m_proxyRain.GetLastSharpRain(stationid, dt);
                        LastDayMes   = m_proxyRain.GetLastDayRain(stationid, dt);

                        lastTotalRain           = m_proxyRain.GetLastRain(stationid, dt).TotalRain;
                        lastSharpClockTotalRain = LastSharpMes.TotalRain;
                        lastDayTotalRain        = LastDayMes.TotalRain;

                        lastClockSharpTime = LastSharpMes.TimeCollect;
                        lastDayTime        = LastDayMes.TimeCollect;

                        //改动

                        // 查询成功

                        station.LastTotalRain    = lastTotalRain;
                        station.LastDayTotalRain = lastDayTotalRain;
                        station.LastPeriodRain   = lastPeriodRain;
                        if (lastDataTime != null && lastDataTime.HasValue)
                        {
                            station.LastDataTime = lastDataTime;
                        }
                        if (lastClockSharpTime != null && lastClockSharpTime.HasValue)
                        {
                            station.LastClockSharpTime = lastClockSharpTime;
                        }
                        if (lastDayTime != null && lastDayTime.HasValue)
                        {
                            station.LastDayTime = lastDayTime;
                        }
                        if (lastChannelType != null && lastChannelType.HasValue)
                        {
                            station.LastChannelType = lastChannelType;
                        }
                        if (lastMessageType != null && lastMessageType.HasValue)
                        {
                            station.LastMessageType = lastMessageType;
                        }

                        //int year = dt.Year;
                        //int month = dt.Month;
                        //int day = dt.Day;
                        //DateTime tmp1 = new DateTime(year, month, day, 8, 0, 0);
                        //DateTime tmp2 = tmp1.Subtract(new TimeSpan(24, 0, 0));
                        //station.LastDayTime = tmp2;
                        //lastSharpClockTotalRain = m_proxyRain.GetLastClockSharpTotalRain(stationid, dt);
                        //lastDayTotalRain = m_proxyRain.GetLastDayTotalRain(stationid, tmp2);
                        station.LastTotalRain = lastTotalRain;

                        station.LastClockSharpTotalRain = lastSharpClockTotalRain;
                        station.LastClockSharpTime      = lastClockSharpTime;

                        station.LastDayTotalRain = lastDayTotalRain;
                        station.LastDayTime      = lastDayTime;
                    }
                    CEntityRain rain = new CEntityRain();
                    rain.StationID   = stationid;
                    rain.TimeCollect = dt;
                    rain.TotalRain   = totalRain;
                    // rain.DifferneceRain = CalDifferenceRain(rainAccuracy, RawtotalRain, station.LastTotalRain, station.DRainChange, ref status);

                    if ((dt.Minute + dt.Second) == 0)
                    {
                        //rain.PeriodRain = CalPeriodRain(rainAccuracy, RawtotalRain, dt, station.LastClockSharpTotalRain);
                        rain.PeriodRain = CalPeriodRain_2(rainAccuracy, RawtotalRain, dt, station.LastClockSharpTotalRain, station.LastClockSharpTime, station.LastTotalRain);
                        station.LastClockSharpTotalRain = totalRain;
                        station.LastClockSharpTime      = dt;
                    }
                    if (dt.Hour == 8)
                    {
                        //rain.DayRain = CalDayRain(rainAccuracy, RawtotalRain, dt, station.LastDayTime, station.LastDayTotalRain);
                        rain.DayRain             = CalDayRain_2(rainAccuracy, RawtotalRain, dt, station.LastDayTotalRain, station.LastDayTime);
                        station.LastDayTotalRain = totalRain;
                        station.LastDayTime      = dt;
                    }
                    rain.DifferneceRain   = CalDifferenceRain_1(rainAccuracy, RawtotalRain, station.LastTotalRain, station.DRainChange, ref status);
                    station.LastTotalRain = totalRain;
                    // 待检测
                    rain.ChannelType = EChannelType.GPRS;
                    rain.MessageType = EMessageType.Batch;

                    rain.TimeRecieved = DateTime.Now;
                    if (status == 1)
                    {
                        rain.BState = 1;
                    }
                    else
                    {
                        rain.BState = 0;
                    }

                    rainResults.Add(rain);
                }
                List <CEntityRain> listInsert   = new List <CEntityRain>();
                List <CEntityRain> listUpdate   = new List <CEntityRain>();
                List <DateTime>    listDateTime = m_proxyRain.getExistsTime(stationid, DStartTime, DEndTime);
                for (int i = 0; i < rainResults.Count; i++)
                {
                    if (listDateTime.Contains(rainResults[i].TimeCollect))
                    {
                        listUpdate.Add(rainResults[i]);
                    }
                    else
                    {
                        listInsert.Add(rainResults[i]);
                    }
                }
                m_proxyRain.AddNewRows(listInsert); //写入数据库
                m_proxyRain.UpdateRows(listUpdate); //更新

                //m_proxyRain.AddOrUpdate(rainResults);
            }
            //操作water表
            //   if (type == EStationType.ERiverWater)
            if (type == EStationType.ERainFall)
            {
                for (int i = 0; i < datas.Count; i++)
                {
                    decimal  data = 0;
                    DateTime dt   = datas[i].Time;
                    try
                    {
                        string strData = datas[i].Data.Trim();
                        //decimal data = int.Parse(datas[i].Data) / 100;
                        data = decimal.Parse(strData) / 100;
                    }
#pragma warning disable CS0168 // 声明了变量“e”,但从未使用过
                    catch (Exception e)
#pragma warning restore CS0168 // 声明了变量“e”,但从未使用过
                    {
                        break;
                    }
                    //操作water表
                    CEntityWater water = new CEntityWater();
                    water.StationID    = station.StationID;
                    water.TimeCollect  = dt;
                    water.TimeRecieved = DateTime.Now;
                    if (station.DWaterBase.HasValue)
                    {
                        // 减去水位基值
                        water.WaterStage = data + station.DWaterBase.Value;
                    }
                    else
                    {
                        water.WaterStage = data;
                    }
                    water.WaterFlow = CDBDataMgr.GetInstance().GetWaterFlowByWaterStageAndStation(stationid, data);
                    //此处 waterflow需要计算的
                    water.ChannelType = EChannelType.GPRS;
                    water.MessageType = EMessageType.Batch;
                    AssertWaterData(water, ref tmpRTDWaterDataState);
                    //if (tmpRTDWaterDataState == ERTDDataState.ENormal)
                    //{
                    //    water.state = 1;
                    //}
                    //if (tmpRTDWaterDataState == ERTDDataState.EError)
                    //{
                    //    water.state = 0;
                    //}
                    //if (tmpRTDWaterDataState == ERTDDataState.EWarning)
                    //{
                    //    water.state = 2;
                    //}

                    waterResults.Add(water);
                }
                List <CEntityWater> listInsert   = new List <CEntityWater>();
                List <CEntityWater> listUpdate   = new List <CEntityWater>();
                List <DateTime>     listDateTime = m_proxyWater.getExistsTime(stationid, DStartTime, DEndTime);
                for (int i = 0; i < waterResults.Count; i++)
                {
                    if (listDateTime.Contains(waterResults[i].TimeCollect))
                    {
                        listUpdate.Add(waterResults[i]);
                    }
                    else
                    {
                        listInsert.Add(waterResults[i]);
                    }
                }
                //
                m_proxyWater.AddNewRows(listInsert); //写入数据库
                m_proxyWater.UpdateRows(listUpdate); //更新数据库

                //m_proxyWater.AddOrUpdate(waterResults);
            }
        }
Exemplo n.º 17
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);
        }
Exemplo n.º 18
0
 public bool Parse_Flash(string msg, EChannelType ctype, out CBatchStruct batch)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 19
0
        private bool ParseData(string msg, string gprs, uint modemId)
        {
            //           InvokeMessage("协议。。。 ", "进入函数7");
            try
            {
                string rawData = msg;
                string sid;
                try
                {
                    sid = Manager.XmlStationData.Instance.GetStationByGprsID(gprs);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("查找站点id失败,赋值为9999" + e.ToString());
                    sid = "9999";
                }
                if (msg.Contains("$"))
                {
                    string data = string.Empty;
                    if (!ProtocolHelpers.DeleteSpecialChar(msg, out data))
                    {
                        return(false);
                    }
                    msg = data;

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

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


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

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

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

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

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

                        if (null != soilReport && null != this.UpDataReceived)
                        {
                            soilReport.ChannelType = EChannelType.GPRS;
                            soilReport.ListenPort  = this.GetListenPort().ToString();
                            this.UpDataReceived(null, new UpEventArgs()
                            {
                                RawData = rawData, Value = soilReport
                            });
                        }
                    }
                }
                else
                {
                    return(false);
                }
                return(true);
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("GPRS 数据解析出错 !" + msg + "\r\n" + exp.Message);
            }
            return(false);
        }
Exemplo n.º 20
0
        //  数据批量主板下行解析
        public bool Parse_Batch(String msg, out CBatchStruct batch)
        {
            batch = new CBatchStruct();
            UpParser      up     = new UpParser();
            CReportStruct report = new CReportStruct();

            try
            {
                if (up.Parse(msg.Substring(msg.IndexOf("1G29") + 5), out report))
                {
                    batch.StationID   = report.Stationid;
                    batch.StationType = report.StationType;
                    List <CTimeAndData> datas  = new List <CTimeAndData>();
                    List <CTimeAndData> rains  = new List <CTimeAndData>();
                    List <CTimeAndData> waters = new List <CTimeAndData>();
                    if (report.StationType == EStationType.ERainFall)
                    {
                        foreach (var d in report.Datas)
                        {
                            CTimeAndData rain = new CTimeAndData();
                            rain.Time = d.Time;
                            if (d.Rain != -1)
                            {
                                int rainData = (int)(d.Rain);
                                rain.Data = string.Format("{0:D4}", rainData);
                                datas.Add(rain);
                            }
                        }
                    }
                    else if (report.StationType == EStationType.ERiverWater)
                    {
                        foreach (var d in report.Datas)
                        {
                            CTimeAndData water = new CTimeAndData();
                            water.Time = d.Time;
                            if (d.Water != -20000 && d.Water != -200)
                            {
                                int waterData = (int)(d.Water * 100);
                                water.Data = string.Format("{0:D6}", waterData);
                                datas.Add(water);
                            }
                        }
                    }
                    else if (report.StationType == EStationType.EHydrology)
                    {
                        foreach (var d in report.Datas)
                        {
                            CTimeAndData rain = new CTimeAndData();
                            rain.Time = d.Time;
                            CTimeAndData water = new CTimeAndData();
                            water.Time = d.Time;
                            if (d.Water != -20000 && d.Water != -200)
                            {
                                int waterData = (int)(d.Water * 100);
                                water.Data = string.Format("{0:D6}", waterData);
                                waters.Add(water);
                            }
                            if (d.Rain != -1)
                            {
                                int rainData = (int)(d.Rain);
                                rain.Data = string.Format("{0:D4}", rainData);
                                rains.Add(rain);
                            }
                        }
                    }
                    batch.Datas  = datas;
                    batch.Rains  = rains;
                    batch.Waters = waters;
                    return(true);
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("批量主板异常:" + e);
            }
            return(false);
        }
Exemplo n.º 21
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);
        }