| | |
| | | using IStation.Common; |
| | | using IStation.Dto; |
| | | using IStation.ZyDto; |
| | | using Microsoft.Ajax.Utilities; |
| | | 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 static IStation.ZyConnectHelper; |
| | | |
| | | namespace IStation.WebApi.Controllers |
| | | { |
| | |
| | | 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 (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 (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(); |
| | | var tagDict = new Dictionary<string, string> |
| | | { |
| | | { "_0402010204012103002", "二取水1号泵累计流量" }, |
| | | { "_0402010204012203002", "二取水2号泵累计流量" }, |
| | | { "_0402010204012303002", "二取水3号泵累计流量" }, |
| | | { "_0402010204012403002", "二取水4号泵累计流量" }, |
| | | { "_0402010204012503002", "二取水5号泵累计流量" }, |
| | | }; |
| | | var input = new ScadaDispatchInput |
| | | { |
| | | search = "history", |
| | | 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 = "300", |
| | | taglist = tagDict.Select(x => x.Key).ToArray() |
| | | }; |
| | | if (dayD == DateTime.Today) |
| | | { |
| | | input.endtime = DateTime.Now.AddMinutes(-5).ToString("G"); |
| | | } |
| | | |
| | | TotalWaterInHelper.Save(d, sum); |
| | | sum = 0; |
| | | var realScadaDataList = await GetRealScadaResult(input); |
| | | 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); |
| | | } |
| | | |
| | | |
| | |
| | | /// <returns></returns> |
| | | [Route("GetLastDay3")] |
| | | [HttpGet] |
| | | public IStation.Dto.ApiResult GetLastDay3( ) |
| | | { |
| | | DateTime yest1 = DateTime.Today.AddDays(-1); |
| | | public async Task<IStation.Dto.ApiResult> GetLastDay3() |
| | | { |
| | | 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); |
| | | } |
| | |
| | | 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); |
| | | } |
| | |
| | | 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 }); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | |
| | | |
| | | |
| | | |
| | | private string _url = "ws://192.168.201.17:40001"; |
| | | private async Task<List<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<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 RealScadaData(); |
| | | data.TagName = dict.Key; |
| | | data.MonitorRecords = new List<IStation.ZyDto.MonitorRecord>(); |
| | | foreach (var item in dict.Value) |
| | | { |
| | | var record = new IStation.ZyDto.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; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |
| | | } |