duheng
2024-08-02 94ed0d75cd4aa090fa8421af6609066682028943
DAL/IStation.DAL.Paras/³¤½­Ë®Î»/web/TideFromWebHelper.cs
@@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text;
using IStation.Model;
namespace IStation.Common
@@ -11,21 +11,20 @@
    ///  æ½®æ±æŽ¥å£
    /// </summary>
    public class TideFromWebHelper
    {
    {
        private static readonly string _urlPrefix = "https://global-tide.nmdis.org.cn/Api/Service.ashx";
            //"http://global-tide.nmdis.org.cn/Api/Service.ashx";//主体Url
        //"http://global-tide.nmdis.org.cn/Api/Service.ashx";//主体Url
        /// <summary>
        /// èŽ·å–æ½®æ±æ•°æ®
        /// </summary>
        static public List<Model.TileLevel> GetByDay1(
            DateTime startday,
        public static List<Model.TileLevel> GetByDay1(
            DateTime startday,
            out string error)
        {
            string benchmark;
            var list = ReadList(startday, out benchmark);
            if(list == null)
            if (list == null)
            {
                list = GetByDayApi(startday, out benchmark, out error);
                if (list == null)
@@ -37,21 +36,22 @@
            }
            else
            {
                 error = null;
            }
                error = null;
            }
            return list;
        }
        /// <summary>
        ///
        ///
        /// </summary>
        /// <param name="startday"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        static public List<Model.TimeWaterLevel> GetByDay2(DateTime startday, out string error)
        public static List<Model.TimeWaterLevel> GetByDay2(DateTime startday, out string error)
        {
            var waterLevels长江 = new List<TimeWaterLevel>();
            var list = GetByDay1(startday,   out error);
            var list = GetByDay1(startday, out error);
            if (list != null)
            {
                foreach (var h in list)
@@ -69,29 +69,28 @@
        }
        /// <summary>
        ///
        ///
        /// </summary>
        /// <param name="startday"></param>
        /// <param name="endday"></param>
        /// <param name="benchmark"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        static public List<Model.TimeWaterLevel> GetByDayRange(
            DateTime startday, DateTime endday,
        public static List<Model.TimeWaterLevel> GetByDayRange(
            DateTime startday, DateTime endday,
            out string error)
        {
            error = null;
            var waterLevels长江 = new List<TimeWaterLevel>();
            var waterLevels长江 = new List<TimeWaterLevel>();
            for (DateTime currentDate = startday; currentDate <= endday; currentDate = currentDate.AddDays(1))
            {
                if (endday.Date > DateTime.Now.Date.AddDays(2))
                {
                    break ;
                    break;
                }
                var list1 = GetByDay1(startday.Date,  out error);
                var list1 = GetByDay1(startday.Date, out error);
                if (list1 != null && list1.Count > 0)
                {
                    for (int h = startday.Hour; h < 24; h++)
@@ -104,18 +103,17 @@
                }
            }
            return waterLevels长江;
        }
        /// <summary>
        ///
        ///
        /// </summary>
        /// <param name="day"></param>
        /// <param name="benchmark"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        static private List<Model.TileLevel> GetByDayApi(
        private static List<Model.TileLevel> GetByDayApi(
            DateTime day,
            out string benchmark,
            out string error)
@@ -141,20 +139,20 @@
                var result = JsonHelper.Json2Object<IStation.CalcModel.Tide.Result>(responseText);
                if (result == null)
                {
                    IStation.LogHelper.Error("read tide from web 1 é•¿æ±Ÿæ°´ä½è¯·æ±‚失败 "   );
                    IStation.LogHelper.Error("read tide from web 1 é•¿æ±Ÿæ°´ä½è¯·æ±‚失败 ");
                    error = "长江水位请求失败";
                    return null;
                }
                if (Convert.ToBoolean(result.State) && !string.IsNullOrEmpty(result.Message))
                {
                    IStation.LogHelper.Error("read tide from web 2 é•¿æ±Ÿæ°´ä½è¯·æ±‚失败 "+ result.Message);
                    IStation.LogHelper.Error("read tide from web 2 é•¿æ±Ÿæ°´ä½è¯·æ±‚失败 " + result.Message);
                    error = result.Message;
                    return null;
                }
                if (result.Data == null)
                {
                    IStation.LogHelper.Error("read tide from web 3 é•¿æ±Ÿæ°´ä½è¯·æ±‚失败 "  );
                    IStation.LogHelper.Error("read tide from web 3 é•¿æ±Ÿæ°´ä½è¯·æ±‚失败 ");
                    error = "长江水位请求失败";
                    return null;
                }
@@ -162,7 +160,7 @@
                benchmark = result.Data.Data.Benchmark;//= "在平均海面下213cm"
                List<Model.TileLevel> list = new List<Model.TileLevel>(24);
                list.Add(new Model.TileLevel()
                { Hour =   0 , Level = result.Data.SubData.a0 / 100 });
                { Hour = 0, Level = result.Data.SubData.a0 / 100 });
                list.Add(new Model.TileLevel()
                { Hour = 1, Level = result.Data.SubData.a1 / 100 });
                list.Add(new Model.TileLevel()
@@ -209,19 +207,19 @@
                { Hour = 22, Level = result.Data.SubData.a22 / 100 });
                list.Add(new Model.TileLevel()
                { Hour = 23, Level = result.Data.SubData.a23 / 100 });
                IStation.LogHelper.Info("read tide from web  day: " + day +" success");
                IStation.LogHelper.Info("read tide from web  day: " + day + " success");
                return list;
            }
            catch (Exception ex)
            {
                IStation.LogHelper.Error("read tide from web error:"+ ex);
                IStation.LogHelper.Error("read tide from web error:" + ex);
                error = ex.Message;
                //IStation.Log
            }
            return null;
        }
        static private void SaveList(DateTime day, string benchmark, List<Model.TileLevel> list)
        private static void SaveList(DateTime day, string benchmark, List<Model.TileLevel> list)
        {
            if (list == null)
                return;
@@ -234,11 +232,9 @@
                if (!System.IO.Directory.Exists(folder))
                {
                    System.IO.Directory.CreateDirectory(folder);
                }
                var fileName = System.IO.Path.Combine(folder,
                    string.Format("{0}.csv", day.ToString("yyyy-MM-dd")));
                StreamWriter streamWriter = new StreamWriter(fileName, false, System.Text.Encoding.GetEncoding("gb2312"));
@@ -258,14 +254,14 @@
                streamWriter.Flush();
                streamWriter.Close();
            }
            catch(Exception ex)
            catch (Exception ex)
            {
               IStation.LogHelper.WriteError("write tide file error:"+ex.Message, ex);
                IStation.LogHelper.Error("write tide file error:" + ex.Message, ex);
                return;
            }
        }
        static private List<Model.TileLevel> ReadList(DateTime day,out string benchmark)
        private static List<Model.TileLevel> ReadList(DateTime day, out string benchmark)
        {
            var list = new List<Model.TileLevel>();
            benchmark = null;
@@ -288,7 +284,7 @@
            System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
            System.IO.StreamReader sr = new System.IO.StreamReader(fs, Encoding.GetEncoding("gb2312"));
            string tempText  ;
            string tempText;
            int line = 0;
            while ((tempText = sr.ReadLine()) != null)
            {
@@ -298,7 +294,6 @@
                //一般第一行为标题,所以取出来作为标头
                if (line == 1)
                {
                }
                else if (line == 2)
                {
@@ -306,7 +301,7 @@
                }
                else
                {
                     list.Add(new Model.TileLevel(Convert.ToInt32(arr[0]), Convert.ToDouble(arr[1])));
                    list.Add(new Model.TileLevel(Convert.ToInt32(arr[0]), Convert.ToDouble(arr[1])));
                }
            }
@@ -317,7 +312,7 @@
        }
        //请求
        private static string HttpRequest(string url, string type, string data = null )
        private static string HttpRequest(string url, string type, string data = null)
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
@@ -327,7 +322,7 @@
            request.Method = type;
            request.ContentType = "application/json";
            request.KeepAlive = false;
            //if (version != null)
            //{
            //    //默认是HttpVersion.Version11
@@ -335,12 +330,14 @@
            //}
            #region æ³¨é‡Š
            /*request.Timeout = 10 * 1000;//请求超时时间
            if (!string.IsNullOrEmpty(data))
            {
                request.ContentLength = Encoding.UTF8.GetBytes(data).Length;
            }*/
            #endregion
            #endregion æ³¨é‡Š
            if (!string.IsNullOrEmpty(data))
            {
@@ -359,7 +356,5 @@
            }
            return responseText;
        }
    }
}