From 36a0ce55799c95821f39aab5c13566a5f238970b Mon Sep 17 00:00:00 2001 From: duheng <2784771470@qq.com> Date: 星期二, 11 二月 2025 12:56:55 +0800 Subject: [PATCH] 修复预测接口问题 --- WebApi/Controllers/TotalWaterInController.cs | 237 ++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 208 insertions(+), 29 deletions(-) diff --git a/WebApi/Controllers/TotalWaterInController.cs b/WebApi/Controllers/TotalWaterInController.cs index f7adf50..a68c2fd 100644 --- a/WebApi/Controllers/TotalWaterInController.cs +++ b/WebApi/Controllers/TotalWaterInController.cs @@ -1,13 +1,18 @@ 锘縰sing IStation.Common; using IStation.Dto; +using IStation.Untity; +using IStation.ZyModel; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; +using System.Net.WebSockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; using System.Web; -using System.Web.Http; -using System.Web.UI.WebControls; +using System.Web.Http; namespace IStation.WebApi.Controllers { @@ -18,35 +23,131 @@ public class TotalWaterInController : ApiController { /// <summary> + /// + /// </summary> + /// <param name="day"></param> + /// <returns></returns> + [Route("Test1")] + [HttpGet] + public async Task<IStation.Dto.ApiResult> Test1() + { + var sum = await ZyConnectHelper.async_debug(); + return new IStation.Dto.ApiResult<double>(sum); + } + + /// <summary> + /// + /// </summary> + /// <param name="day"></param> + /// <returns></returns> + [Route("Test2")] + [HttpGet] + public IStation.Dto.ApiResult Test2() + { + var sum = ZyConnectHelper.async_debug().GetAwaiter().GetResult(); + return new IStation.Dto.ApiResult<double>(sum); + } + + /// <summary> + /// + /// </summary> + /// <param name="day"></param> + /// <returns></returns> + [Route("GetByDayDebug")] + [HttpGet] + public IStation.Dto.ApiResult GetByDayDebug(string day) + { + if (ZyConnectHelper.isDebug) + return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "Debug is Unacess " }; + if (day == null) + { + return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" }; + } + if (!DateTime.TryParse(day, out DateTime dayD)) + { + return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 鏍煎紡涓嶆纭�" }; + } + if (dayD > DateTime.Today) + { + return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "涓嶈兘鏌ヨ浠婃棩鐨勬暟鎹�" }; + } + + var sum = ZyConnectHelper.GetTotalWaterByDay_In(dayD).Result; + return new IStation.Dto.ApiResult<double>(sum); + } + + /// <summary> /// 鑾峰彇鏌愭棩鐨勫彇姘存�婚噺 /// </summary> /// <param name="day"></param> /// <returns></returns> [Route("GetByDay")] [HttpGet] - public IStation.Dto.ApiResult GetByDay(string day) + public async Task<IStation.Dto.ApiResult> GetByDay(string day) { + if (ZyConnectHelper.isDebug) + return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "Debug is Unacess " }; if (day == null) { return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day null" }; } - DateTime d; - if (!DateTime.TryParse(day, out d)) + if (!DateTime.TryParse(day, out DateTime dayD)) { return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "day 鏍煎紡涓嶆纭�" }; } - var sum = TotalWaterInHelper.Read(d); + if (dayD > DateTime.Today) + { + return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "涓嶈兘鏌ヨ浠婃棩鐨勬暟鎹�" }; + } + double sum = -1; + if (dayD < DateTime.Today) + { + sum = TotalWaterInHelper.Read(dayD); + } + if (sum < 0) { - sum = ZyConnectHelper.GetTotalWaterByDay_In(DateTime.Parse(day)).Result;//.GetAwaiter().GetResult(); - - TotalWaterInHelper.Save(d, sum); + var tagDict = new Dictionary<string, string> + { + { "1#", "浜屽彇姘�1鍙蜂富姘存车.绱娴侀噺" }, + { "2#", "浜屽彇姘�2鍙蜂富姘存车.绱娴侀噺" }, + { "3#", "浜屽彇姘�3鍙蜂富姘存车.绱娴侀噺" }, + { "4#", "浜屽彇姘�4鍙蜂富姘存车.绱娴侀噺" }, + { "5#", "浜屽彇姘�5鍙蜂富姘存车.绱娴侀噺" }, + }; + var starttime = new DateTime(dayD.Year, dayD.Month, dayD.Day, 0, 0, 0).ToString("G"); + var endtime = new DateTime(dayD.Year, dayD.Month, dayD.Day, 23, 59, 59).ToString("G"); + if (dayD == DateTime.Today) + { + endtime = DateTime.Now.AddMinutes(-5).ToString("G"); + } + sum = 0; + string joinedValues = string.Join(",", tagDict.Values); + var result = HttpClientHelper.Get<List<DataPoint>>(ZyConnectHelper.ZyApiUrl, starttime, endtime, "first", "300s", joinedValues); + var realScadaDataList = ZyConnectHelper.GetScadaData(result); + if (realScadaDataList != null && realScadaDataList.Any()) + { + foreach (var r in realScadaDataList) + { + if (r.MonitorRecords == null || !r.MonitorRecords.Any()) + continue; + var rrr = r.MonitorRecords.Where(x => x.Value != null).Select(x => x.Value.Value).ToList(); + if (rrr == null || !rrr.Any()) + continue; + if (double.IsNaN(rrr.Last()) || double.IsNaN(rrr.First())) + { + return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "鑾峰彇鐩戞帶鏁版嵁澶辫触" }; + } + sum += (rrr.Last() - rrr.First()); + } + } } - - return new IStation.Dto.ApiResult<double>(sum); + if (dayD < DateTime.Today) + { + TotalWaterInHelper.Save(dayD, sum); + } + return new IStation.Dto.ApiResult<double>(sum / 10000); } - - /// <summary> /// 鑾峰彇鏈�杩戜笁澶╃殑鍙栨按鎬婚噺 @@ -55,14 +156,15 @@ /// <returns></returns> [Route("GetLastDay3")] [HttpGet] - public IStation.Dto.ApiResult GetLastDay3( ) - { - DateTime yest1 = DateTime.Today.AddDays(-1); + public async Task<IStation.Dto.ApiResult> GetLastDay3() + { + if (ZyConnectHelper.isDebug) + return new IStation.Dto.ApiResult() { Code = ApiResultCode.Error, Message = "Debug is Unacess " }; + DateTime yest1 = DateTime.Today.AddDays(-1); var sum1 = TotalWaterInHelper.Read(yest1); if (sum1 < 0) { - sum1 = ZyConnectHelper.GetTotalWaterByDay_In(yest1).Result;//.GetAwaiter().GetResult(); - + sum1 = await ZyConnectHelper.GetTotalWaterByDay_In(yest1);//.GetAwaiter().GetResult(); TotalWaterInHelper.Save(yest1, sum1); } @@ -70,8 +172,7 @@ var sum2 = TotalWaterInHelper.Read(yest2); if (sum2 < 0) { - sum2 = ZyConnectHelper.GetTotalWaterByDay_In(yest2).Result;//.GetAwaiter().GetResult(); - + sum2 = await ZyConnectHelper.GetTotalWaterByDay_In(yest2);//.GetAwaiter().GetResult(); TotalWaterInHelper.Save(yest2, sum2); } @@ -79,18 +180,13 @@ var sum3 = TotalWaterInHelper.Read(yest3); if (sum3 < 0) { - sum3 = ZyConnectHelper.GetTotalWaterByDay_In(yest3).Result;//.GetAwaiter().GetResult(); + sum3 = await ZyConnectHelper.GetTotalWaterByDay_In(yest3);//.GetAwaiter().GetResult(); TotalWaterInHelper.Save(yest3, sum3); } - return new IStation.Dto.ApiResult<List<double>>(new List<double> { sum1 , sum2, sum3}); + return new IStation.Dto.ApiResult<List<double>>(new List<double> { sum1 / 10000, sum2 / 10000, sum3 / 10000 }); } - - - - - /// <summary> /// 鑾峰彇web瀹㈡埛绔痠p @@ -150,8 +246,91 @@ return userIP; } + private string _url = "ws://192.168.201.17:40001"; + private async Task<List<ZyModel.RealScadaData>> Get(string inputJson) + { + if (string.IsNullOrEmpty(inputJson)) + return default; + var ws = new ClientWebSocket(); + IStation.LogHelper.Info(" var ws = new ClientWebSocket();"); + await ws.ConnectAsync(new Uri(_url), CancellationToken.None); + IStation.LogHelper.Info(" ws.ConnectAsync(new Uri(_url_root), CancellationToken.None);"); + if (ws.State != WebSocketState.Open) + { + return default; + } + IStation.LogHelper.Info("ws.ConnectAsync"); + var realScadaDataList = new List<ZyModel.RealScadaData>(); + var inputBytes = Encoding.UTF8.GetBytes(inputJson); + await ws.SendAsync(new ArraySegment<byte>(inputBytes), WebSocketMessageType.Text, true, CancellationToken.None); + try + { + IStation.LogHelper.Info("ws.SendAsync"); + List<byte> outputBytes = new List<byte>();//鍏ㄩ儴娑堟伅瀹瑰櫒 + var buffer = new byte[1024 * 4];//缂撳啿鍖� + var result = ws.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None).Result; //鐩戝惉Socket淇℃伅 + while (!result.CloseStatus.HasValue) //鏄惁鍏抽棴 + { + if (result.MessageType == WebSocketMessageType.Text)//鏂囨湰娑堟伅 + { + outputBytes.AddRange(buffer.Take(result.Count)); + if (result.EndOfMessage)//娑堟伅鏄惁宸叉帴鏀跺畬鍏� + { + IStation.LogHelper.Info("result.EndOfMessage"); + //鍙戦�佽繃鏉ョ殑娑堟伅 + string outputJson = Encoding.UTF8.GetString(outputBytes.ToArray(), 0, outputBytes.Count); + var scadaDict = JsonHelper.Json2Object<Dictionary<string, Dictionary<DateTime, string>>>(outputJson); + if (scadaDict != null && scadaDict.Any()) + { + foreach (var dict in scadaDict) + { + var data = new ZyModel.RealScadaData(); + data.TagName = dict.Key; + data.MonitorRecords = new List<IStation.ZyModel.MonitorRecord>(); + foreach (var item in dict.Value) + { + var record = new IStation.ZyModel.MonitorRecord(); + record.Time = item.Key; + if (double.TryParse(item.Value, out double value)) + { + record.Value = value; + } + data.MonitorRecords.Add(record); + } + realScadaDataList.Add(data); + } + IStation.LogHelper.Info("scadaDict != null && scadaDict.Any()"); + } + break; + } + } - + result = ws.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None).Result;//缁х画鐩戝惉Socket淇℃伅 + // IStation.LogHelper.Info("ws.ReceiveAsync"); + } + } + catch (Exception ex) + { + IStation.LogHelper.Info(ex.Message); + throw ex; + } + finally + { + try + { //鍏抽棴WebSocket + await ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "AcknowledgeCloseframe", CancellationToken.None); + IStation.LogHelper.Info("ws.CloseOutputAsync"); + } + catch (Exception ex) + { + IStation.LogHelper.Info(ex.Message); + throw ex; + } + ws.Abort(); + ws.Dispose(); + } + return realScadaDataList; + } } -} +} \ No newline at end of file -- Gitblit v1.9.3