| | |
| | |  |
| | | 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; |
| | |
| | | /// </summary> |
| | | internal class ZyConnectHelper |
| | | { |
| | | public static bool isDebug = true; |
| | | public static bool isDebug = false; |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | //static string _url_root = "ws://localhost:40001"; |
| | | //static string _url_prj = "ws://localhost:8001"; |
| | | static string _url_root = "ws://192.168.201.17:40001"; |
| | | static string _url_prj = "ws://192.168.201.17:8001"; |
| | | |
| | | /// <summary> |
| | | /// æäº¤è°åº¦é¡¹ç® |
| | |
| | | /// <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) |
| | | public static async Task<string> SubmitPrj(IStation.CalcModel.AnaPrj currentViewPrj) |
| | | { |
| | | if (currentViewPrj == null) |
| | | { |
| | | return false; |
| | | return "currentViewPrj is null"; |
| | | } |
| | | IStation.ZyDto.SubmitPrj dto_prj = new ZyDto.SubmitPrj(); |
| | | dto_prj.Day = currentViewPrj.StartTime.ToString("yyyy-MM-dd"); |
| | |
| | | } |
| | | |
| | | var ws = new ClientWebSocket(); |
| | | await ws.ConnectAsync(new Uri(_url_prj), CancellationToken.None); |
| | | if (ws.State != WebSocketState.Open) |
| | | { |
| | | return "WebSocketState is close"; |
| | | } |
| | | |
| | | var inputJson = JsonHelper.Object2Json(dto_prj); |
| | | var inputBytes = Encoding.UTF8.GetBytes(inputJson); |
| | | await ws.SendAsync(new ArraySegment<byte>(inputBytes), WebSocketMessageType.Text, true, CancellationToken.None); |
| | | try |
| | | { |
| | | await ws.ConnectAsync(new Uri(_url_root), CancellationToken.None); |
| | | if (ws.State != WebSocketState.Open) |
| | | 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) //æ¯å¦å
³é |
| | | { |
| | | return default; |
| | | 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 outPrj=JsonHelper.Json2Object<OutPrj>(outputJson); |
| | | if (outPrj == null) |
| | | return "return is null"; |
| | | if( outPrj.result == 1) |
| | | { |
| | | //cbSuccess.Invoke(outPrj.schemeID); |
| | | } |
| | | else |
| | | { |
| | | return "outPrj result:"+ outPrj.result; |
| | | } |
| | | } |
| | | } |
| | | // result = await ws.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);//ç»§ç»çå¬Socketä¿¡æ¯ |
| | | } |
| | | 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; |
| | | IStation.LogHelper.WriteError("Error:101", ex); |
| | | return "ex:" + ex.Message; |
| | | } |
| | | ws.Abort(); |
| | | ws.Dispose(); |
| | | cbSuccess.Invoke(); |
| | | return true; |
| | | finally |
| | | { |
| | | try |
| | | { |
| | | //å
³éWebSocket |
| | | await ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "AcknowledgeCloseframe", CancellationToken.None); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | IStation.LogHelper.WriteError("Error:111", ex); |
| | | // return "ex:" + ex.Message; |
| | | } |
| | | ws.Abort(); |
| | | ws.Dispose(); |
| | | } |
| | | return null ; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /// <summary> |
| | | /// åæ°´æ»é |
| | |
| | | /// <param name="day">å¼å§æ¶é´</param> |
| | | /// <param name="resolution">æ¶é´æ¥é¿,é»è®¤300ç§</param> |
| | | /// <returns></returns> |
| | | public static async Task<double> GetTotalWaterByDay_In(DateTime day, string resolution = "300") |
| | | public static async Task<double> GetTotalWaterByDay_In(DateTime day, string resolution = "300") |
| | | { |
| | | if(isDebug) |
| | | return 125 + new Random().Next(1, 10); |
| | |
| | | Resolution = resolution, |
| | | taglist = tagDict.Select(x => x.Key).ToArray() |
| | | }; |
| | | |
| | | |
| | | double totalWaterSupply = 0; |
| | | var realScadaDataList = await GetRealScadaResult(input); |
| | | if (realScadaDataList != null && realScadaDataList.Any()) |
| | |
| | | { |
| | | 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(); |
| | | var rrr = r.MonitorRecords.Where(x => x.Value != null).Select(x => x.Value.Value).ToList(); |
| | | if (rrr == null || !rrr.Any()) |
| | | continue; |
| | | |
| | |
| | | /// <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); |
| | |
| | | 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()) |
| | |
| | | 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()); |
| | | } |
| | |
| | | return realScadaRecordList; |
| | | } |
| | | |
| | | |
| | | |
| | | /// <summary> |
| | | /// è·å宿¶æ°æ®åå
¸ |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public static async Task<List<RealScadaData>> GetRealScadaResult(ScadaDispatchInput input) |
| | | { |
| | | { |
| | | if (input == null) |
| | | return default; |
| | | var ws = new ClientWebSocket(); |
| | | await ws.ConnectAsync(new Uri(_url_root), CancellationToken.None); |
| | | await ws.ConnectAsync(new Uri(_url_root), CancellationToken.None); |
| | | if (ws.State != WebSocketState.Open) |
| | | { |
| | | return default; |
| | | } |
| | | |
| | | var realScadaDataList = new List<RealScadaData>(); |
| | | var inputJson = JsonHelper.Object2Json(input); |
| | | var inputJson = JsonHelper.Object2Json(input); |
| | | var inputBytes = Encoding.UTF8.GetBytes(inputJson); |
| | | await ws.SendAsync(new ArraySegment<byte>(inputBytes), WebSocketMessageType.Text, true, CancellationToken.None); |
| | | try |
| | |
| | | { |
| | | 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); |
| | |
| | | } |
| | | realScadaDataList.Add(data); |
| | | } |
| | | |
| | | } |
| | | break; |
| | | } |
| | |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | { |
| | | throw ex; |
| | | } |
| | | finally |
| | | { |
| | | try |
| | | { |
| | | { |
| | | //å
³éWebSocket |
| | | await ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "AcknowledgeCloseframe", CancellationToken.None); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | { |
| | | throw ex; |
| | | } |
| | | ws.Abort(); |
| | |
| | | } |
| | | |
| | | |
| | | /// <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> |
| | | /// |