| | |
| | | using System.Collections.Generic; |
| | | using System.IO; |
| | | using System.Net; |
| | | using System.Text; |
| | | using System.Text; |
| | | using IStation.Model; |
| | | |
| | | namespace IStation.Common |
| | |
| | | /// æ½®æ±æ¥å£ |
| | | /// </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) |
| | |
| | | } |
| | | 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) |
| | |
| | | } |
| | | |
| | | /// <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++) |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | 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) |
| | |
| | | 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; |
| | | } |
| | |
| | | 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() |
| | |
| | | { 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; |
| | |
| | | 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")); |
| | | |
| | |
| | | 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; |
| | |
| | | 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) |
| | | { |
| | |
| | | //ä¸è¬ç¬¬ä¸è¡ä¸ºæ é¢ï¼æä»¥ååºæ¥ä½ä¸ºæ 头 |
| | | if (line == 1) |
| | | { |
| | | |
| | | } |
| | | else if (line == 2) |
| | | { |
| | |
| | | } |
| | | 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]))); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | //è¯·æ± |
| | | 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; |
| | |
| | | request.Method = type; |
| | | request.ContentType = "application/json"; |
| | | request.KeepAlive = false; |
| | | |
| | | |
| | | //if (version != null) |
| | | //{ |
| | | // //é»è®¤æ¯HttpVersion.Version11 |
| | |
| | | //} |
| | | |
| | | #region 注é |
| | | |
| | | /*request.Timeout = 10 * 1000;//请æ±è¶
æ¶æ¶é´ |
| | | if (!string.IsNullOrEmpty(data)) |
| | | { |
| | | request.ContentLength = Encoding.UTF8.GetBytes(data).Length; |
| | | }*/ |
| | | #endregion |
| | | |
| | | #endregion 注é |
| | | |
| | | if (!string.IsNullOrEmpty(data)) |
| | | { |
| | |
| | | } |
| | | return responseText; |
| | | } |
| | | |
| | | |
| | | } |
| | | } |