From d32fea3dd2990d8a10b48cb757439d672893e5c7 Mon Sep 17 00:00:00 2001 From: duheng <2784771470@qq.com> Date: 星期三, 27 三月 2024 13:52:20 +0800 Subject: [PATCH] Merge branch 'master' of http://47.103.154.90:83/r/IStation/shys/ChEqCalc.V1.3 --- WebApi/众毅调度/ZyConnectHelper.cs | 397 +++++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 292 insertions(+), 105 deletions(-) diff --git "a/WebApi/\344\274\227\346\257\205\350\260\203\345\272\246/ZyConnectHelper.cs" "b/WebApi/\344\274\227\346\257\205\350\260\203\345\272\246/ZyConnectHelper.cs" index 24b39be..ed6f0d9 100644 --- "a/WebApi/\344\274\227\346\257\205\350\260\203\345\272\246/ZyConnectHelper.cs" +++ "b/WebApi/\344\274\227\346\257\205\350\260\203\345\272\246/ZyConnectHelper.cs" @@ -1,12 +1,18 @@ -锘� +锘� +using IStation.Model; using IStation.ZyDto; +using Microsoft.Ajax.Utilities; using System; using System.Collections.Generic; using System.Linq; using System.Net.WebSockets; +using System.Security.Permissions; using System.Text; using System.Threading; using System.Threading.Tasks; +using IsOpen = IStation.ZyDto.IsOpen; +using PumpIsopen = IStation.ZyDto.PumpIsopen; +using PumpTotalelectricity = IStation.ZyDto.PumpTotalelectricity; namespace IStation { @@ -15,67 +21,13 @@ /// </summary> internal class ZyConnectHelper { - public static bool isDebug = true; + public static bool isDebug = true ; /// <summary> /// /// </summary> - static string _url_root = "ws://192.168.201.17:40001"; - - /// <summary> - /// 鎻愪氦璋冨害椤圭洰 - /// </summary> - /// <param name="currentViewPrj"></param> - /// <param name="cbSuccess"></param> - /// <param name="cbFail"></param> - /// <returns></returns> - public static async Task<bool> SubmitPrj(IStation.CalcModel.AnaPrj currentViewPrj, Action cbSuccess, Action<string> cbFail) - { - if (currentViewPrj == null) - { - return false; - } - IStation.ZyDto.SubmitPrj dto_prj = new ZyDto.SubmitPrj(); - dto_prj.Day = currentViewPrj.StartTime.ToString("yyyy-MM-dd"); - dto_prj.Items = new List<ZyDto.SubmitPrjItem>(); - - foreach (var bt in currentViewPrj.BlockTimes) - { - if (bt.OpenPumpCount == 0) - continue; - ZyDto.SubmitPrjItem dto_item = new ZyDto.SubmitPrjItem(); - dto_item.StartTime = bt.StartTime; - dto_item.EndTime = bt.EndTime; - dto_item.OpenPumpCount = bt.OpenPumpCount; - dto_prj.Items.Add(dto_item); - } - - var ws = new ClientWebSocket(); - - try - { - await ws.ConnectAsync(new Uri(_url_root), CancellationToken.None); - if (ws.State != WebSocketState.Open) - { - return default; - } - var inputJson = JsonHelper.Object2Json(dto_prj); - var inputBytes = Encoding.UTF8.GetBytes(inputJson); - await ws.SendAsync(new ArraySegment<byte>(inputBytes), WebSocketMessageType.Text, true, CancellationToken.None); - //鍏抽棴WebSocket - await ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "AcknowledgeCloseframe", CancellationToken.None); - } - catch (Exception ex) - { - cbFail.Invoke(ex.Message); - ws.Abort(); ws.Dispose(); - return false; - } - ws.Abort(); - ws.Dispose(); - cbSuccess.Invoke(); - return true; - } - + //static string _url_root = "ws://localhost:40001"; + //static string _url_prj = "ws://localhost:8001"; + public static string _url_root = "ws://192.168.201.17:40001"; /// <summary> @@ -87,6 +39,8 @@ /// <returns></returns> public static async Task<double> GetTotalWaterByTimeRange_In(DateTime startTime, DateTime endTime, string resolution = "300") { + if (isDebug) + return 0; var tagDict = new Dictionary<string, string> { { "_0402010204012103002", "浜屽彇姘�1鍙锋车绱娴侀噺" }, @@ -133,8 +87,8 @@ /// <returns></returns> public static async Task<double> GetTotalWaterByDay_In(DateTime day, string resolution = "300") { - if(isDebug) - return 125 + new Random().Next(1, 10); + if (isDebug) + return 125 + new Random().Next(1, 10); var tagDict = new Dictionary<string, string> { @@ -162,7 +116,6 @@ if (r.MonitorRecords == null || !r.MonitorRecords.Any()) continue; var rrr = r.MonitorRecords.Where(x => x.Value != null).Select(x => x.Value.Value).ToList(); - //var rrr = (from x in r.MonitorRecords where x.Value != null orderby x select x.Value.Value)?.ToList(); if (rrr == null || !rrr.Any()) continue; @@ -229,7 +182,7 @@ /// <param name="endTime">缁撴潫鏃堕棿</param> /// <param name="resolution">鏃堕棿姝ラ暱,榛樿300绉�</param> /// <returns></returns> - public static async Task<double> GetTotalWaterByDay_Out(DateTime day, string resolution = "300") + public static async Task<double> GetTotalWaterByDay_Out(DateTime dayD, string resolution = "300") { if (isDebug) return 135 + new Random().Next(10); @@ -244,12 +197,15 @@ var input = new ZyDto.ScadaDispatchInput { search = "history", - starttime = new DateTime(day.Year, day.Month, day.Day, 0, 0, 0).ToString("G"), - endtime = new DateTime(day.Year, day.Month, day.Day, 23, 59, 59).ToString("G"), + starttime = new DateTime(dayD.Year, dayD.Month, dayD.Day, 0, 0, 0).ToString("G"), + endtime = new DateTime(dayD.Year, dayD.Month, dayD.Day, 23, 59, 59).ToString("G"), Resolution = resolution, taglist = tagDict.Select(x => x.Key).ToArray() }; - + if (dayD == DateTime.Today) + { + input.endtime = DateTime.Now.AddMinutes(-5).ToString("G"); + } double totalWaterSupply = 0; var realScadaDataList = await GetRealScadaResult(input); if (realScadaDataList != null && realScadaDataList.Any()) @@ -261,7 +217,10 @@ 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())) + { + throw new Exception("鑾峰彇鐩戞帶鏁版嵁澶辫触"); + } totalWaterSupply += (rrr.Last() - rrr.First()); } @@ -354,7 +313,7 @@ /// <param name="endTime">缁撴潫鏃堕棿</param> /// <param name="resolution">鏃堕棿姝ラ暱,榛樿300绉�</param> /// <returns></returns> - public static async Task<List<IStation.ZyDto.MonitorRecord>> GetRealRiverWaterLevel (DateTime startTime, DateTime endTime, string resolution = "300") + public static async Task<List<IStation.ZyModel.MonitorRecord>> GetRealRiverWaterLevel(DateTime startTime, DateTime endTime, string resolution = "300") { var tag = "_0402010204089903001"; var input = new ScadaDispatchInput @@ -374,11 +333,251 @@ + + + /// <summary> + /// 鑾峰彇娉电殑寮�鏈烘儏鍐� + /// </summary> + /// <param name="startTime">寮�濮嬫椂闂�</param> + /// <param name="endTime">缁撴潫鏃堕棿</param> + /// <param name="resolution">鏃堕棿姝ラ暱,榛樿300绉�</param> + /// <returns></returns> + public static async Task<List<ZyDto.PumpIsopen>> GetPumpOpenStatusBySingleDay(DateTime StartTime, string resolution = "300") + { + List<IsOpen> listopen = new List<IsOpen> { + new IsOpen + { + DateTime = DateTime.Now.AddDays(1), + Isopen = "0" + } + }; + List<PumpIsopen> pumpIsopens = new List<PumpIsopen> { + new PumpIsopen + { + Tag = "_0402010204012101001", + Values = listopen + } + }; + + + if (isDebug) + { + return pumpIsopens; + } + var tagDict = new Dictionary<string, string> + { + { "_0402010204012101001", "浜屽彇姘�1鍙锋车寮�鍏崇姸鎬�" }, + { "_0402010204012201001", "浜屽彇姘�2鍙锋车寮�鍏崇姸鎬�" }, + { "_0402010204012301001","浜屽彇姘�3鍙锋车寮�鍏崇姸鎬�" }, + { "_0402010204012401001", "浜屽彇姘�4鍙锋车寮�鍏崇姸鎬�" }, + {" _0402010204012501001", "浜屽彇姘�5鍙锋车寮�鍏崇姸鎬�" }, + }; + var input = new ScadaDispatchInput + { + search = "history", + starttime = new DateTime(StartTime.Year, StartTime.Month, StartTime.Day, 0, 0, 0).ToString("G"), + endtime = new DateTime(StartTime.Year, StartTime.Month, StartTime.Day, 23, 59, 59).ToString("G"), + Resolution = resolution, + taglist = tagDict.Select(x => x.Key).ToArray() + }; + var realScadaDataList = await GetRealScadaResultSate(input); + return realScadaDataList; + } + + + /// <summary> + /// 鑾峰彇瀹炴椂鏁版嵁瀛楀吀 + /// 鑾峰彇寮�鏈虹姸鎬� + /// </summary> + /// <returns></returns> + public static async Task<List<ZyDto.PumpIsopen>> GetRealScadaResultSate(ScadaDispatchInput input) + { + if (input == null) + return default; + var ws = new ClientWebSocket(); + await ws.ConnectAsync(new Uri(_url_root), CancellationToken.None); + if (ws.State != WebSocketState.Open) + { + return default; + } + var realScadaDataList = new List<PumpIsopen>(); + var inputJson = JsonHelper.Object2Json(input); + var inputBytes = Encoding.UTF8.GetBytes(inputJson); + await ws.SendAsync(new ArraySegment<byte>(inputBytes), WebSocketMessageType.Text, true, CancellationToken.None); + try + { + List<byte> outputBytes = new List<byte>();//鍏ㄩ儴娑堟伅瀹瑰櫒 + var buffer = new byte[1024 * 4];//缂撳啿鍖� + var result = await ws.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); //鐩戝惉Socket淇℃伅 + while (!result.CloseStatus.HasValue) //鏄惁鍏抽棴 + { + if (result.MessageType == WebSocketMessageType.Text)//鏂囨湰娑堟伅 + { + outputBytes.AddRange(buffer.Take(result.Count)); + if (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 PumpIsopen(); + data.Tag = dict.Key; + foreach (var item in dict.Value) + { + var State = new IsOpen(); + State.DateTime = item.Key; + State.Isopen = item.Value; + data.Values.Add(State); + } + realScadaDataList.Add(data); + } + + } + break; + } + } + result = await ws.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);//缁х画鐩戝惉Socket淇℃伅 + } + } + catch (Exception ex) + { + throw ex; + } + finally + { + try + { + //鍏抽棴WebSocket + await ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "AcknowledgeCloseframe", CancellationToken.None); + } + catch (Exception ex) + { + throw ex; + } + ws.Abort(); + ws.Dispose(); + } + + return realScadaDataList; + } + + + /// <summary> + /// 鑾峰彇鏌愭鏃堕棿鐨勭敤鐢甸噺 + /// </summary> + /// <param name="StartTime"></param> + /// <param name="EndTime"></param> + /// <param name="resolution"></param> + /// <returns></returns> + public static async Task<List<ZyDto.PumpTotalelectricity>> GetPumpTotalElectricity(DateTime StartTime, DateTime EndTime, string resolution = "300") + { + var tagDict = new Dictionary<string, string> + { + { "_0402010204040193046", "浜屽彇姘�1鍙锋车鏈夊姛鐢佃兘" }, + { "_0402010204040193047", "浜屽彇姘�2鍙锋车鏈夊姛鐢佃兘" }, + { "_0402010204012308007", "浜屽彇姘�3鍙锋车鏈夊姛鐢佃兘" }, + { "_0402010204012408007", "浜屽彇姘�4鍙锋车鏈夊姛鐢佃兘" }, + {" _0402010204012508007", "浜屽彇姘�5鍙锋车鏈夊姛鐢佃兘" }, + }; + var input = new ScadaDispatchInput + { + search = "history", + starttime = new DateTime(StartTime.Year, StartTime.Month, StartTime.Day, 0, 0, 0).ToString("G"), + endtime = new DateTime(EndTime.Year, EndTime.Month, EndTime.Day, 23, 59, 59).ToString("G"), + Resolution = resolution, + taglist = tagDict.Select(x => x.Key).ToArray() + }; + var realScadaDataList = await GetRealScadaResultElectricity(input); + return realScadaDataList; + } + + + /// <summary> + /// 鑾峰彇瀹炴椂鏁版嵁瀛楀吀 + /// 鐢ㄧ數閲� + /// </summary> + /// <returns></returns> + public static async Task<List<ZyDto.PumpTotalelectricity>> GetRealScadaResultElectricity(ScadaDispatchInput input) + { + if (input == null) + return default; + var ws = new ClientWebSocket(); + await ws.ConnectAsync(new Uri(_url_root), CancellationToken.None); + if (ws.State != WebSocketState.Open) + { + return default; + } + var realScadaDataList = new List<PumpTotalelectricity>(); + var inputJson = JsonHelper.Object2Json(input); + var inputBytes = Encoding.UTF8.GetBytes(inputJson); + await ws.SendAsync(new ArraySegment<byte>(inputBytes), WebSocketMessageType.Text, true, CancellationToken.None); + try + { + List<byte> outputBytes = new List<byte>();//鍏ㄩ儴娑堟伅瀹瑰櫒 + var buffer = new byte[1024 * 4];//缂撳啿鍖� + var result = await ws.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); //鐩戝惉Socket淇℃伅 + while (!result.CloseStatus.HasValue) //鏄惁鍏抽棴 + { + if (result.MessageType == WebSocketMessageType.Text)//鏂囨湰娑堟伅 + { + outputBytes.AddRange(buffer.Take(result.Count)); + if (result.EndOfMessage)//娑堟伅鏄惁宸叉帴鏀跺畬鍏� + { + //鍙戦�佽繃鏉ョ殑娑堟伅 + string outputJson = Encoding.UTF8.GetString(outputBytes.ToArray(), 0, outputBytes.Count); + var scadaDict = JsonHelper.Json2Object<Dictionary<DateTime, double>>(outputJson); + if (scadaDict != null && scadaDict.Any()) + { + foreach (var dict in scadaDict) + { + var data = new PumpTotalelectricity(); + data.DateTime = dict.Key; + data.value = dict.Value; + realScadaDataList.Add(data); + } + } + break; + } + } + result = await ws.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);//缁х画鐩戝惉Socket淇℃伅 + } + } + catch (Exception ex) + { + throw ex; + } + finally + { + try + { + //鍏抽棴WebSocket + await ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "AcknowledgeCloseframe", CancellationToken.None); + } + catch (Exception ex) + { + throw ex; + } + ws.Abort(); + ws.Dispose(); + } + + return realScadaDataList; + } + + + + + + + /// <summary> /// 鑾峰彇瀹炴椂鏁版嵁瀛楀吀 /// </summary> /// <returns></returns> - public static async Task<List<RealScadaRecord>> GetRealScadaResult(List<string> tags, object input) + public static async Task<List<ZyModel.RealScadaRecord>> GetRealScadaResult(List<string> tags, object input) { if (tags == null || !tags.Any()) return default; @@ -391,7 +590,7 @@ return default; } - var realScadaRecordList = new List<RealScadaRecord>(); + var realScadaRecordList = new List<ZyModel.RealScadaRecord>(); var inputJson = JsonHelper.Object2Json(input); var inputBytes = Encoding.UTF8.GetBytes(inputJson); await ws.SendAsync(new ArraySegment<byte>(inputBytes), WebSocketMessageType.Text, true, CancellationToken.None); @@ -417,7 +616,7 @@ var tag = dict.Value.keys.Trim(); if (tags.Contains(tag)) { - var data = new RealScadaRecord(); + var data = new ZyModel.RealScadaRecord(); data.TagName = tag; if (double.TryParse(dict.Value.vals, out double value)) @@ -460,7 +659,7 @@ /// 鑾峰彇瀹炴椂鏁版嵁瀛楀吀 /// </summary> /// <returns></returns> - public static async Task<List<RealScadaData>> GetRealScadaResult(ScadaDispatchInput input) + public static async Task<List<ZyModel.RealScadaData>> GetRealScadaResult(ScadaDispatchInput input) { if (input == null) return default; @@ -470,8 +669,7 @@ { return default; } - - var realScadaDataList = new List<RealScadaData>(); + var realScadaDataList = new List<ZyModel.RealScadaData>(); var inputJson = JsonHelper.Object2Json(input); var inputBytes = Encoding.UTF8.GetBytes(inputJson); await ws.SendAsync(new ArraySegment<byte>(inputBytes), WebSocketMessageType.Text, true, CancellationToken.None); @@ -494,12 +692,12 @@ { foreach (var dict in scadaDict) { - var data = new RealScadaData(); + var data = new ZyModel.RealScadaData(); data.TagName = dict.Key; - data.MonitorRecords = new List<IStation.ZyDto.MonitorRecord>(); + data.MonitorRecords = new List<IStation.ZyModel.MonitorRecord>(); foreach (var item in dict.Value) { - var record = new IStation.ZyDto.MonitorRecord(); + var record = new IStation.ZyModel.MonitorRecord(); record.Time = item.Key; if (double.TryParse(item.Value, out double value)) { @@ -509,6 +707,7 @@ } realScadaDataList.Add(data); } + } break; } @@ -735,6 +934,21 @@ } + /// <summary> + /// Test1 + /// </summary> + /// <returns></returns> + public static async Task<double> async_debug() + { + //var ws = new ClientWebSocket(); + // await ws.ConnectAsync(new Uri(_url_root), CancellationToken.None); + await Task.Run(() => + { + Thread.Sleep(1000); + }); + + return 1; + } /// <summary> /// @@ -746,33 +960,6 @@ } - /// <summary> - /// - /// </summary> - public class RealScadaData - { - /// <summary> - /// - /// </summary> - public string TagName { get; set; } - /// <summary> - /// - /// </summary> - public List<IStation.ZyDto.MonitorRecord> MonitorRecords { get; set; } - } - /// <summary> - /// - /// </summary> - public class RealScadaRecord - { - /// <summary> - /// - /// </summary> - public string TagName { get; set; } - /// <summary> - /// - /// </summary> - public double Value { get; set; } - } + } } \ No newline at end of file -- Gitblit v1.9.3