using AutoMapper; using LangGuan.Command.AutoMapper; using LangGuan.Command.Model; using LangGuan.Command.Model.EntityModel; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; namespace LangGuan.Services { /// /// /// public class RadarService : BaseService { private readonly IMapper _mapper; private DeviceSerive _deviceSerive; private RadarItemService _radarItemService; /// /// /// /// /// /// /// public RadarService(IConfiguration config, IMapper mapper, DeviceSerive deviceSerive, RadarItemService radarItemService) : base(config, nameof(Radar)) { _mapper = mapper; _deviceSerive = deviceSerive; _radarItemService = radarItemService; } /// /// 新加数据 /// /// /// public async Task Add(RadarView request) { var entity = _mapper.Map(request); await _deviceSerive.Add(new Device() { deviceId = request.deviceId, deviceIp = request.deviceIp, state = 1 }); //数据量过大,这里要分开存储 var result = await base.CreateAsync(entity); List items = new List(); foreach (var item in request.Signal) { items.Add(new RadarItems() { pid = result.Id, Signal = item }); } await _radarItemService.AddMany(items); return new ApiResult() { code = 0, msg = "" }; } /// /// 首页用图 /// /// /// public async Task> Top(int n) { Expression> exp = filter => true; var query = await base.GetListWithExp(exp); var items = query.OrderByDescending(s => s.AcqTime).Take(n) .Select(s => new { s.Id, s.deviceId, s.AcqTime, s.DataPointNum, s.DetectRange, s.ExposureTime, s.ScanAngle, s.SystemBlind }).ToList(); List result = new List(); Parallel.ForEach(items, async entry => { result.Add(new { datetime = entry.AcqTime.AddHours(8).ToString("yyyy-MM-dd HH mm "), items = await ConvertData(entry.DetectRange, entry.DataPointNum, entry.SystemBlind, entry.Id) }); }); return result; } /// /// /// /// /// /// public async Task GetList(DateTime start, DateTime end) { //Stopwatch stopwatch = new Stopwatch(); //var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "radar", "Signal_2023.json"); //stopwatch.Start(); //FileStream fileStream = new FileStream(path, FileMode.Open); //using (StreamReader reader = new StreamReader(fileStream)) //{ // string line = await reader.ReadToEndAsync(); //} //stopwatch.Stop(); //Console.WriteLine("StreamReader:" + stopwatch.ElapsedMilliseconds); //string txt = ""; //byte[] allBytes = null; //byte[] buffer = new byte[1024];//一个1K的缓冲字节容器 //stopwatch.Reset(); //stopwatch.Start(); //using (MemoryStream ms = new MemoryStream()) //{ // using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) // { // int positon = 0; // while ((positon = await fs.ReadAsync(buffer, 0, buffer.Length)) > 0) // { // await ms.WriteAsync(buffer, 0, positon); // } // allBytes = ms.ToArray(); // } //} //if (null != allBytes) //{ // //尝试将字节转成字符串 // txt = System.Text.Encoding.UTF8.GetString(allBytes); // // this.richTextBox_Result.Text = txt; //} //var path1 = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "radar", DateTime.Now.ToString("yyyy-MM-dd") + ".txt"); //using (FileStream fs = new FileStream(path1, FileMode.OpenOrCreate, FileAccess.Write)) //{ // await fs.WriteAsync(allBytes, 0, allBytes.Length); //} //// string[] txtToArray = txt.Split('\r'); //// ReadData_List.Add(txtToArray); //stopwatch.Stop(); //Console.WriteLine("MemoryStream" + stopwatch.ElapsedMilliseconds); end = end.AddDays(1); Expression> exp = filter => filter.AcqTime >= start && filter.AcqTime < end; var query = await base.GetListWithExp(exp); var items = query.OrderByDescending(s => s.AcqTime) .Select(s => new { s.Id, s.deviceId, s.AcqTime, s.DataPointNum, s.DetectRange, s.ExposureTime, s.ScanAngle, s.SystemBlind }).ToList(); List result = new List(); Parallel.ForEach(items, async entry => { result.Add(new { datetime = entry.AcqTime.ToString("yyyy-MM-dd HH mm "), items = await ConvertData(entry.DetectRange, entry.DataPointNum, entry.SystemBlind, entry.Id) }); }); //var result = items.Select(async s => new //{ // datetime = s.AcqTime.AddHours(8).ToString("yyyy-MM-dd HH mm "), // items = await ConvertData(s.DetectRange, s.DataPointNum, s.SystemBlind, s.Id) //}).ToList(); return new ApiResult() { code = 0, data = new { items = result } }; } private async Task> ConvertData(double rang, double DataPointNum, double SystemBlind, string id) { // var count = await _radarItemService.getcount(id); List radarModels = new List(); //里面先取度数 //取1000米的数, 1000/3 List> vs = new List>(); vs = await _radarItemService.GetByPid(id); //角度, var t1 = convertangle(); var t2 = convertDistance(rang, SystemBlind); for (int i = 0; i < t1.Length; i++) { var temp = (vs[i]).ToList(); int m = 0; foreach (var v in t2) { //如果有第一个,就直接取, m = v == SystemBlind ? m : (int)((v - SystemBlind) / 3); m = m == 0 ? m : m - 1; radarModels.Add(new radarModel() { angle = t1[i], distance = v, value = Math.Round(m > temp.Count() ? temp[temp.Count() - 1] : temp[m], 2) }); } } return radarModels; } private int[] convertangle() { List reuslt = new List(); int i = 0; while (i < 360) { reuslt.Add(i); i = i + 10; } return reuslt.ToArray(); } /// /// 计算圈数 /// /// /// /// private int[] convertDistance(double DetectRange, double SystemBlind) { List reuslt = new List(); reuslt.Add((int)SystemBlind); var d1 = DetectRange - SystemBlind; int n = 1; while (d1 - 1000 > 0) { if (n == 1) { reuslt.Add(1000); } else { reuslt.Add(n * 1000); } d1 = d1 - 1000; n++; } reuslt.Add((int)DetectRange); return reuslt.ToArray(); } /// /// 返回结果 /// public class radarModel { /// /// 角度 /// public int angle { get; set; } /// /// 距离 /// public int distance { get; set; } /// /// 结果 /// public double value { get; set; } } } }