From 03a0d99de9c5fed6bea8bc83b49ce27786bda38c Mon Sep 17 00:00:00 2001 From: tangxu <tangxu76880903> Date: 星期五, 07 二月 2025 09:29:59 +0800 Subject: [PATCH] 添加OPENAPI接口 --- WebApi/Logs/Tests/20240731.txt | 0 WebApi/Logs/Debug/20241021.txt | 0 WebApi/Logs/Info/20241021.txt | 22 +++ Calc/IStation.Calc.Core/CalcPrj.cs | 18 ++ WebApi/Controllers/TideController.cs | 5 WebApi/Controllers/ZyPrjController.cs | 15 +- Calc/IStation.Calc.Core/计算基类/ErQuCalcBaseHelper_长江.cs | 2 WebApi/Controllers/OpenPredictController.cs | 95 +++++++++++++++ BLL/IStation.BLL.Connect/AnaPrj.cs | 5 WebApi/Logs/Error/20241021.txt | 18 +++ WebApi/IStation.ChEr.WebApi.csproj | 2 DAL/IStation.DAL.Paras/长江水位/book/TideFromBookHelper.cs | 36 ++++++ WebApi/Controllers/OpenTideController.cs | 105 +++++++++++++++++ 13 files changed, 307 insertions(+), 16 deletions(-) diff --git a/BLL/IStation.BLL.Connect/AnaPrj.cs b/BLL/IStation.BLL.Connect/AnaPrj.cs index 3d073c8..55d280e 100644 --- a/BLL/IStation.BLL.Connect/AnaPrj.cs +++ b/BLL/IStation.BLL.Connect/AnaPrj.cs @@ -41,7 +41,10 @@ { if (!IStation.BLL.ServiceParas.IsRemote) { - IStation.Common.HistoryAnaPrjFileHelper.SavePrj(ana_settting, ana_request == null ? null : new CalcModel.AnaRequestBase(ana_request), prj); return; + IStation.Common.HistoryAnaPrjFileHelper.SavePrj(ana_settting, + true, + ana_request == null ? null : new CalcModel.AnaRequestBase(ana_request), prj); + return; } if (string.IsNullOrEmpty(ServiceParas.ApiServiceUrl)) diff --git a/Calc/IStation.Calc.Core/CalcPrj.cs b/Calc/IStation.Calc.Core/CalcPrj.cs index 76c3770..23ceefb 100644 --- a/Calc/IStation.Calc.Core/CalcPrj.cs +++ b/Calc/IStation.Calc.Core/CalcPrj.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Permissions; using System.Text; using System.Threading.Tasks; @@ -24,10 +25,13 @@ optPrj = null; return "鏁版嵁鍒濆鍖栧け璐�"; } - InitialElePrice(); - + if(! InitialElePrice()) + { + optPrj = null; + return "ElecPrice 涓虹┖"; + } var calcHelper = GetCalcHelper(anaRequest); - + string error_info; optPrj = calcHelper.CalcOptPrj(out error_info); @@ -263,10 +267,16 @@ /// <summary> /// /// </summary> - private void InitialElePrice() + private bool InitialElePrice() { var allPrices = IStation.Common.ElecPrice.GetAll(); + if(allPrices == null) + { + IStation.LogHelper.Error("ElecPrice 涓虹┖"); + return false; + } _elePriceMonthList = allPrices.First().Settings.MonthList; + return true; } #endregion 鍒濆鍖� diff --git "a/Calc/IStation.Calc.Core/\350\256\241\347\256\227\345\237\272\347\261\273/ErQuCalcBaseHelper_\351\225\277\346\261\237.cs" "b/Calc/IStation.Calc.Core/\350\256\241\347\256\227\345\237\272\347\261\273/ErQuCalcBaseHelper_\351\225\277\346\261\237.cs" index dab30b2..d121242 100644 --- "a/Calc/IStation.Calc.Core/\350\256\241\347\256\227\345\237\272\347\261\273/ErQuCalcBaseHelper_\351\225\277\346\261\237.cs" +++ "b/Calc/IStation.Calc.Core/\350\256\241\347\256\227\345\237\272\347\261\273/ErQuCalcBaseHelper_\351\225\277\346\261\237.cs" @@ -63,7 +63,7 @@ protected void SetRiverWaterLevels(List<IStation.Model.TimeWaterLevel> riverWaterLevels) { this._riverWaterLevels = riverWaterLevels; - if (this._riverWaterLevels != null) + if (this._riverWaterLevels != null && this._riverWaterLevels.Count>0) { _riverWaterLevelStart = this._riverWaterLevels.First().Time; _riverWaterLevelEnd = this._riverWaterLevels.Last().Time; diff --git "a/DAL/IStation.DAL.Paras/\351\225\277\346\261\237\346\260\264\344\275\215/book/TideFromBookHelper.cs" "b/DAL/IStation.DAL.Paras/\351\225\277\346\261\237\346\260\264\344\275\215/book/TideFromBookHelper.cs" index 58852bf..03747f8 100644 --- "a/DAL/IStation.DAL.Paras/\351\225\277\346\261\237\346\260\264\344\275\215/book/TideFromBookHelper.cs" +++ "b/DAL/IStation.DAL.Paras/\351\225\277\346\261\237\346\260\264\344\275\215/book/TideFromBookHelper.cs" @@ -74,7 +74,43 @@ error = null; return waterLevels闀挎睙; } + /// <summary> + /// 璇诲彇 + /// </summary> + /// <param name="Startday"></param> + /// <param name="Endday"></param> + /// <param name="folder"></param> + /// <returns></returns> + public static List<Model.TimeWaterLevel> GetByTimeRange(DateTime StartTime, DateTime EndTime, out string error) + { + + DateTime Startday = StartTime.Date; + DateTime Endday = EndTime.Date.AddDays(1); + var waterLevels闀挎睙 = new List<TimeWaterLevel>(); + for (DateTime currentDate = Startday; currentDate <= Endday; currentDate = currentDate.AddDays(1)) + { + var list1 = ReadList(currentDate); + if (list1 != null && list1.Count > 0) + { + for (int h = 0; h < 24; h++) + { + var t = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, h, 0, 0); + + if (t >= StartTime && t <= EndTime) + waterLevels闀挎睙.Add(new TimeWaterLevel(t, list1[h].Level)); + } + } + } + error = null; + return waterLevels闀挎睙; + } + + /// <summary> + /// + /// </summary> + /// <param name="day"></param> + /// <returns></returns> private static List<Model.TileLevel> ReadList(DateTime day) { var list = new List<Model.TileLevel>(); diff --git a/WebApi/Controllers/OpenPredictController.cs b/WebApi/Controllers/OpenPredictController.cs new file mode 100644 index 0000000..39b8f9c --- /dev/null +++ b/WebApi/Controllers/OpenPredictController.cs @@ -0,0 +1,95 @@ +锘縰sing IStation.Common; +using IStation.Dto; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using System.Web; +using System.Web.Http; + +namespace IStation.WebApi.Controllers +{ + /// <summary> + /// 鐢ㄦ按閲忛娴嬪�� (瀵瑰寮�鏀�) + /// </summary> + [RoutePrefix("OpenApi/WaterSupplyPredict")] + public class OpenPredictController : ApiController + { + /// <summary> + /// 鑾峰彇 + /// </summary> + /// <param name="day"></param> + /// <returns></returns> + [Route("GetByRange")] + [HttpGet] + public IStation.Dto.ApiResult GetByRange(string start, string end) + { + DateTime startTime, endTime; + if (!DateTime.TryParse(start, out startTime)) + { + return new IStation.Dto.ApiResult<string>("start 鍙傛暟涓嶅悎鐞�", Dto.ApiResultCode.Error); + } + if (!DateTime.TryParse(end, out endTime)) + { + return new IStation.Dto.ApiResult<string>("end 鍙傛暟涓嶅悎鐞�", Dto.ApiResultCode.Error); + } + + + + IStation.DAL.WaterPredictRecord dal = new DAL.WaterPredictRecord(); + var water_records = dal.GetByHourRangle(startTime, endTime); + + if (water_records == null || water_records.Count == 0) + { + return new IStation.Dto.ApiResult<string>() { Code = ApiResultCode.Error, Data = "姘翠綅棰勬祴鏁版嵁涓虹┖,璇峰厛淇濇寔姘翠綅棰勬祴鏈嶅姟姝e父杩愯" }; + } + + List<PredictValue> list2 = new List<PredictValue>(); + + int offset=0; + foreach (var r in water_records) + { + if (r.States == 0) + { + var error_info11 = string.Format("棰勬祴鏁版嵁寮傚父,鏃犳硶杩涜姘翠綅璁$畻:{0} {1},{2}", r.DayHour, r.LastPredictValue, r.Description); + + return new IStation.Dto.ApiResult<string>() + { + Code = ApiResultCode.Error, + Data = error_info11 + }; + } + + list2.Add(new PredictValue(r) { Time = startTime.AddHours(offset) }); + offset++; + } + + + + + + + return new IStation.Dto.ApiResult<List<PredictValue>>(list2); + } + + + + + public class PredictValue + { + public PredictValue() { } + public PredictValue(IStation.Model.WaterPredictRecord rhs) + { + this.Value = rhs.LastPredictValue; + } + public PredictValue(DateTime time, double level) { this.Time = time; this.Value = level; } + public DateTime Time { get; set; } + public double Value { get; set; } + } + + + + } +} diff --git a/WebApi/Controllers/OpenTideController.cs b/WebApi/Controllers/OpenTideController.cs new file mode 100644 index 0000000..deab4b5 --- /dev/null +++ b/WebApi/Controllers/OpenTideController.cs @@ -0,0 +1,105 @@ +锘縰sing IStation.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using System.Web; +using System.Web.Http; + +namespace IStation.WebApi.Controllers +{ + /// <summary> + /// 闀挎睙Tide (瀵瑰寮�鏀�) + /// </summary> + [RoutePrefix("OpenApi/Tide")] + public class OpenTideController : ApiController + { + /// <summary> + /// 鑾峰彇 + /// </summary> + /// <param name="day"></param> + /// <returns></returns> + [Route("GetByRange")] + [HttpGet] + public IStation.Dto.ApiResult GetByRange(string start, string end) + { + string error; + + DateTime startTime, endTime; + if (!DateTime.TryParse(start, out startTime)) + { + return new IStation.Dto.ApiResult<string>("start 鍙傛暟涓嶅悎鐞�", Dto.ApiResultCode.Error); + } + if (!DateTime.TryParse(end, out endTime)) + { + return new IStation.Dto.ApiResult<string>("end 鍙傛暟涓嶅悎鐞�", Dto.ApiResultCode.Error); + } + var list = TideFromBookHelper.GetByTimeRange(startTime, endTime, out error); + + if (list == null || list.Count() == 0) + { + return new IStation.Dto.ApiResult<string>("鑾峰彇澶辫触", Dto.ApiResultCode.Error); + } + List<TideValue> list2 = new List<TideValue>(); + foreach (var m in list) + { + list2.Add(new TideValue(m)); + } + + return new IStation.Dto.ApiResult<List<TideValue>>(list2); + } + + + /// <summary> + /// 鑾峰彇鏌愭棩 + /// </summary> + /// <param name="day"></param> + /// <returns></returns> + [Route("GetByDay")] + [HttpGet] + public IStation.Dto.ApiResult GetByDay(string day) + { + string error; + + + DateTime startTime; + if (!DateTime.TryParse(day, out startTime)) + { + return new IStation.Dto.ApiResult<string>("day 鍙傛暟涓嶅悎鐞�", Dto.ApiResultCode.Error); + } + var list = TideFromBookHelper.GetByDay2(startTime, out error); + + if(list == null || list.Count()==0) + { + return new IStation.Dto.ApiResult<string>("鑾峰彇澶辫触", Dto.ApiResultCode.Error); + } + List<TideValue> list2 = new List<TideValue>(); + foreach(var m in list) + { + list2.Add(new TideValue(m)); + } + + return new IStation.Dto.ApiResult<List<TideValue>>(list2); + } + + + + public class TideValue + { + public TideValue() { } + public TideValue(Model.TimeWaterLevel rhs) + { + this.Time = rhs.Time; + this.Value = rhs.Level; + } + public TideValue(DateTime time, double level) { this.Time = time; this.Value = level; } + public DateTime Time { get; set; } + public double Value { get; set; } + } + + + + } +} diff --git a/WebApi/Controllers/TideController.cs b/WebApi/Controllers/TideController.cs index ad9309d..07435be 100644 --- a/WebApi/Controllers/TideController.cs +++ b/WebApi/Controllers/TideController.cs @@ -16,6 +16,8 @@ [RoutePrefix("Tide")] public class TideController : ApiController { + + /// <summary> /// 鑾峰彇鏈�杩戜笁澶� /// </summary> @@ -38,7 +40,6 @@ else { list = TideFromBookHelper.GetByDayRange(DateTime.Parse(startday), DateTime.Parse(endday), out error); - } @@ -95,9 +96,7 @@ [HttpPost] public IStation.Dto.ApiResult ImportByDay(ImportByDayRequest request) { - var ret = TideFromBookHelper.SaveList(DateTime.Parse(request.day), request.level); - return new IStation.Dto.ApiResult<bool>(ret); } diff --git a/WebApi/Controllers/ZyPrjController.cs b/WebApi/Controllers/ZyPrjController.cs index 48a1211..bf39aa0 100644 --- a/WebApi/Controllers/ZyPrjController.cs +++ b/WebApi/Controllers/ZyPrjController.cs @@ -94,12 +94,16 @@ IStation.LogHelper.Error(string.Format("ZyPrjRequest Time:{0}-{1} 鏃堕棿鑼冨洿閿欒,涓嶈兘瓒呰繃24灏忔椂", start_timme, end_timme)); return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "鏃堕棿鑼冨洿閿欒,涓嶈兘瓒呰繃24灏忔椂" }; } - + IStation.CalcModel.AnaRequest anaRequest = new AnaRequest(); anaRequest.CalcOptType = (eCalcOptType)IStation.AnaGlobalParas.Setting.OptSortType; anaRequest.StartTime = start_timme; anaRequest.EndTime = end_timme; anaRequest.WaterLevels闀挎睙 = GetTide3Day(_tideSouce, start_timme); + if(anaRequest.WaterLevels闀挎睙 ==null || anaRequest.WaterLevels闀挎睙.Count()==0) + { + return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "鏃犳硶鑾峰彇闀挎睙姘翠綅鏁版嵁" }; + } #region 鍙栨按鎬婚噺 double TotalFlowIn = 0; @@ -120,7 +124,7 @@ } #endregion 鍙栨按鎬婚噺 - + #region 姘翠綅瑕佹眰 if (!string.IsNullOrEmpty(request.MaxReservoirLevelTime)) @@ -275,11 +279,8 @@ anaRequest.StartReservoirLevel) }; } - //bool isTemp = false; - //if (!string.IsNullOrEmpty(request.IsTemporary)) - //{ - // isTemp = Convert.ToBoolean(request.IsTemporary); - //} + + IStation.LogHelper.Info("kashi 璁$畻鍙栨按鎬婚噺 "); var calcHelper = new IStation.CalcPrjHelper(); AnaPrj defaultPrj = null; diff --git a/WebApi/IStation.ChEr.WebApi.csproj b/WebApi/IStation.ChEr.WebApi.csproj index e3c8307..db30315 100644 --- a/WebApi/IStation.ChEr.WebApi.csproj +++ b/WebApi/IStation.ChEr.WebApi.csproj @@ -238,6 +238,8 @@ <Compile Include="Areas\HelpPage\SampleGeneration\SampleDirection.cs" /> <Compile Include="Areas\HelpPage\SampleGeneration\TextSample.cs" /> <Compile Include="Areas\HelpPage\XmlDocumentationProvider.cs" /> + <Compile Include="Controllers\OpenPredictController.cs" /> + <Compile Include="Controllers\OpenTideController.cs" /> <Compile Include="Controllers\ZyPrjController.cs" /> <Compile Include="Controllers\AnaPrjController.cs" /> <Compile Include="Controllers\HomeController.cs" /> diff --git a/WebApi/Logs/Debug/20241021.txt b/WebApi/Logs/Debug/20241021.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/WebApi/Logs/Debug/20241021.txt diff --git a/WebApi/Logs/Error/20241021.txt b/WebApi/Logs/Error/20241021.txt new file mode 100644 index 0000000..19f88e7 --- /dev/null +++ b/WebApi/Logs/Error/20241021.txt @@ -0,0 +1,18 @@ +2024-10-21 13:39:03,268|UnknowException,Exception:缺少WaterPredictDsPath配置,StackTrace: 在 IStation.DAL.WaterPredictRecord.CreateConnection(Int32 year) 位置 D:\WorkData\IStation\ChEq\CodeV1.3\DAL\IStation.DAL.Project\DAL\WaterPredictRecord.cs:行号 27 + 在 IStation.DAL.WaterPredictRecord.GetByHourRangle(DateTime start_time, DateTime end_time) 位置 D:\WorkData\IStation\ChEq\CodeV1.3\DAL\IStation.DAL.Project\DAL\WaterPredictRecord.cs:行号 78 + 在 IStation.WebApi.Controllers.ZyPrjController.Calc(ZyPrjRequest request) 位置 D:\WorkData\IStation\ChEq\CodeV1.3\WebApi\Controllers\ZyPrjController.cs:行号 170 + 在 lambda_method(Closure , Object , Object[] ) + 在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.<GetExecutor>b__2(Object instance, Object[] methodParameters) + 在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) +--- 引发异常的上一位置中堆栈跟踪的末尾 --- + 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) + 在 System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext() +--- 引发异常的上一位置中堆栈跟踪的末尾 --- + 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) + 在 System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext() +--- 引发异常的上一位置中堆栈跟踪的末尾 --- + 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) + 在 System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__6.MoveNext()| diff --git a/WebApi/Logs/Info/20241021.txt b/WebApi/Logs/Info/20241021.txt new file mode 100644 index 0000000..7379dd6 --- /dev/null +++ b/WebApi/Logs/Info/20241021.txt @@ -0,0 +1,22 @@ +2024-10-21 13:36:51,791|--------------------------START-----------------------------------------| +2024-10-21 13:36:51,803|Zy Prj Calc Request: {"StartTime":"2024-10-21 22:00:00","EndTime":"2024-10-22 22:00:00","StartReservoirLevel":null,"StartOpenPumpIndexArray":"","TotalFlowIn":"","TotalFlowOut":"","MaxReservoirLevel":"6.8","MaxReservoirLevelTime":"2024-10-22 09:00:00"}| +2024-10-21 13:39:19,265|--------------------------START-----------------------------------------| +2024-10-21 13:39:19,284|Zy Prj Calc Request: {"StartTime":"2024-10-21 22:00:00","EndTime":"2024-10-22 22:00:00","StartReservoirLevel":null,"StartOpenPumpIndexArray":"","TotalFlowIn":"","TotalFlowOut":"","MaxReservoirLevel":"6.8","MaxReservoirLevelTime":"2024-10-22 09:00:00"}| +2024-10-21 13:39:23,386|获取水量预测值: 52891,52830,66099,66106,66112,66119,66125,66132,66138,66144,66151,66157,66164,66170,66177,66183,66190,66196,66203,66209,66215,66222,66228,66235 , 预测值总供水总量:1561396 | +2024-10-21 13:39:23,386| 未设置开始时水库水位 (Error: StartReservoirLevel is null)| +2024-10-21 13:39:57,087|--------------------------START-----------------------------------------| +2024-10-21 13:39:57,495|Zy Prj Calc Request: {"StartTime":"2024-10-21 22:00:00","EndTime":"2024-10-22 22:00:00","StartReservoirLevel":null,"StartOpenPumpIndexArray":"","TotalFlowIn":"","TotalFlowOut":"","MaxReservoirLevel":"6.8","MaxReservoirLevelTime":"2024-10-22 09:00:00"}| +2024-10-21 13:40:18,624|获取水量预测值: 52891,52830,66099,66106,66112,66119,66125,66132,66138,66144,66151,66157,66164,66170,66177,66183,66190,66196,66203,66209,66215,66222,66228,66235 , 预测值总供水总量:1561396 | +2024-10-21 13:40:22,835| 未设置开始时水库水位 (Error: StartReservoirLevel is null)| +2024-10-21 13:40:45,145|--------------------------START-----------------------------------------| +2024-10-21 13:40:45,145|Zy Prj Calc Request: {"StartTime":"2024-10-21 22:00:00","EndTime":"2024-10-22 22:00:00","StartReservoirLevel":null,"StartOpenPumpIndexArray":"","TotalFlowIn":"","TotalFlowOut":"","MaxReservoirLevel":"6.8","MaxReservoirLevelTime":"2024-10-22 09:00:00"}| +2024-10-21 13:41:16,702|获取水量预测值: 52891,52830,66099,66106,66112,66119,66125,66132,66138,66144,66151,66157,66164,66170,66177,66183,66190,66196,66203,66209,66215,66222,66228,66235 , 预测值总供水总量:1561396 | +2024-10-21 13:41:29,983| 未设置开始时水库水位 (Error: StartReservoirLevel is null)| +2024-10-21 13:41:55,079|--------------------------START-----------------------------------------| +2024-10-21 13:41:55,079|Zy Prj Calc Request: {"StartTime":"2024-10-21 22:00:00","EndTime":"2024-10-22 22:00:00","StartReservoirLevel":null,"StartOpenPumpIndexArray":"","TotalFlowIn":"","TotalFlowOut":"","MaxReservoirLevel":"6.8","MaxReservoirLevelTime":"2024-10-22 09:00:00"}| +2024-10-21 13:41:55,088|获取水量预测值: 52891,52830,66099,66106,66112,66119,66125,66132,66138,66144,66151,66157,66164,66170,66177,66183,66190,66196,66203,66209,66215,66222,66228,66235 , 预测值总供水总量:1561396 | +2024-10-21 13:41:58,922| 未设置开始时水库水位 (Error: StartReservoirLevel is null)| +2024-10-21 13:42:32,108|--------------------------START-----------------------------------------| +2024-10-21 13:42:32,124|Zy Prj Calc Request: {"StartTime":"2024-10-21 22:00:00","EndTime":"2024-10-22 22:00:00","StartReservoirLevel":"6","StartOpenPumpIndexArray":"","TotalFlowIn":"","TotalFlowOut":"","MaxReservoirLevel":"6.8","MaxReservoirLevelTime":"2024-10-22 09:00:00"}| +2024-10-21 13:42:32,705|获取水量预测值: 52891,52830,66099,66106,66112,66119,66125,66132,66138,66144,66151,66157,66164,66170,66177,66183,66190,66196,66203,66209,66215,66222,66228,66235 , 预测值总供水总量:1561396 | +2024-10-21 13:43:36,188|-----------------------------END--------------------------------------| diff --git a/WebApi/Logs/Tests/20240731.txt b/WebApi/Logs/Tests/20240731.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/WebApi/Logs/Tests/20240731.txt -- Gitblit v1.9.3