/
agontar
/
TOROS
Обзор
Документация
Войти
/
agontar
/
TOROS
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Webapplication_new/Controllers/TableController.cs
179 строк
7 KB
gav
initial upload
17 сен 2024, 16:04
17 сен 2024, 16:04
356ccc9
Код
Авторство
О чём код?
using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using WebApplication1.Models; using WebApplication1.Services; using EasyModbus; using Newtonsoft.Json; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc.Rendering; using System.Net; using System.IO; using System.Xml.Linq; using Quartz.Util; using Microsoft.Extensions.Hosting.Internal; using System.Text; namespace WebApplication1.Controllers { public class TableController : Controller { IWebHostEnvironment Environment; string wwwPath; string path;// = Path.Combine(wwwPath, "TreeData", "endpoints.json"); public TableController(ILogger<HomeController> logger, IWebHostEnvironment environment) { Environment = environment; wwwPath = Environment.WebRootPath; path = Path.Combine(wwwPath, "TreeData", "endpoints.json"); } public List<T> ReadJsonFile<T>(string path, List<T>? collection) { var rw = new WriteRead(); try { var json = rw.ReadText(path); collection = JsonConvert.DeserializeObject<List<T>>(json) as List<T>; if (collection == null) { collection = new List<T>(); } return collection; } catch (Exception ex) { Console.WriteLine(ex.ToString()); return new List<T>(); } } private void GetEqnClass(out int eqnClass, Sensors s, List<FinalPoint> finalpoints) { eqnClass = 1; if (finalpoints != null) { foreach (var item in finalpoints) { if (item.IDS == s.ids) { eqnClass = item.EqnClass; if (eqnClass == 0) { eqnClass = 1; } } } } } [HttpGet] public async Task<IActionResult> Index(Sensors s) { ViewBag.ShowReports = "hidden"; PgSql pg = new PgSql(); s.finalpoints = ReadJsonFile(path, s.finalpoints); if (s.finalpoints == null) { s.finalpoints = new List<FinalPoint>(); } DateTime dt = new DateTime(); dt = DateTime.Now; s.to = $"{dt.ToString("yyyy-MM-dd")} {dt.ToString("HH:mm:ss")}"; s.sensors = pg.ReadSensors("646E879B", s.from, s.to); await Task.Delay(100); return View(s); } [HttpPost] public IActionResult Index(Sensors s, string id, string fromday, string fromtime, string today, string totime, bool report, string reportName, string period, string idslist) { ViewBag.ShowReports = "hidden"; s.finalpoints = ReadJsonFile(path, s.finalpoints); if (s.finalpoints == null) { s.finalpoints = new List<FinalPoint>(); } if (s.ids != null) { s.ids = s.ids.ToUpper(); s.ids = s.ids.Replace(" ", ""); s.ids = s.ids.Replace("-", ""); } else s.ids = ""; if (idslist != "Выбрать") { s.ids = idslist; } if (!fromday.IsNullOrWhiteSpace() || !fromtime.IsNullOrWhiteSpace() || !today.IsNullOrWhiteSpace() || !totime.IsNullOrWhiteSpace()) { s.from = $"{fromday} {fromtime}"; s.to = $"{today} {totime}"; } // if time period selected from dropdown list if (period != "Выбрать") { Console.WriteLine("Dropdown selection!"); TimeSpan ts; DateTime dtNow = new DateTime(); dtNow = DateTime.Now; if (period == "1") ts = new TimeSpan(0, 1, 0, 0); else if (period == "2") ts = new TimeSpan(1, 0, 0, 0); else if (period == "3") ts = new TimeSpan(7, 0, 0, 0); else if (period == "4") ts = new TimeSpan(30, 0, 0, 0); else if (period == "5") ts = new TimeSpan(120, 0, 0, 0); else ts = new TimeSpan(0, 0, 0, 0); DateTime startDT = dtNow.Subtract(ts); string from = $"{startDT.ToString("yyyy-MM-dd")} {startDT.ToString("HH:mm:ss")}"; string now = $"{dtNow.ToString("yyyy-MM-dd")} {dtNow.ToString("HH:mm:ss")}"; s.from = from; s.to = now; Console.WriteLine($"{s.from} {s.to}"); } // send sql request PgSql pg = new PgSql(); s.sensors = pg.ReadSensors(s.ids + "", s.from, s.to); if (report) { string wwwPath = this.Environment.WebRootPath; string file = Path.Combine(wwwPath, "Reports", reportName + ".csv"); WriteRead wr = new WriteRead(); string separator = "\t"; StringBuilder output = new StringBuilder(); string[] headings = { "Дата, время", "Напр. бат. Вольт", "Темп. °С", "Мощн. dBm", "X-A м/с²", "Y_VA м/с²", "Z_VA м/с²", "X_VS м/с", "Y_VS м/с", "Z_VS м/с" , "X_VM мкм", "Y_VM мкм", "Z_VM мкм"}; output.AppendLine(string.Join(separator, headings)); foreach (var item in s.sensors) { string[] newLine = { item.date_time.ToString(), item.Voltage.ToString("0.00"), item.Temperature.ToString("0.0"), item.Power.ToString("-0.0"), item.X_VA.ToString("0.00"), item.Y_VA.ToString("0.00"),item.Z_VA.ToString("0.00"), item.X_VS.ToString("0.00"),item.Y_VS.ToString("0.00"),item.Z_VS.ToString("0.00"),item.X_VM.ToString("0.00"), item.Y_VM.ToString("0.00"),item.Z_VM.ToString("0.00")}; // string newLine = string.Format("{0}{1}{2}, {3}", item.date_time.ToString(), item.Voltage.ToString(), item.Temperature.ToString(), item.Power.ToString()); output.AppendLine(string.Join(separator, newLine)); } try { wr.WriteText(file, output.ToString()); Console.WriteLine("Success write report !"); ViewBag.ShowReports = ""; ViewBag.Report = "Сформирован отчет, файл: " + reportName + ".csv"; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } return View(s); } } }