/
agontar
/
TOROS
Обзор
Документация
Войти
/
agontar
/
TOROS
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Webapplication_new/Controllers/ChartController.cs
484 строки
17 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; namespace WebApplication1.Controllers { public class ChartController : Controller { IWebHostEnvironment Environment; string wwwPath; string path;// = Path.Combine(wwwPath, "TreeData", "endpoints.json"); int eqnClass = 1; public ChartController(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 IActionResult Index(Sensors s, SetTresholds st, CheckZone cz, List<Layers> ls) { 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); s.ids = "646E879B"; // -- get thresholds ------------------ GetEqnClass(out eqnClass, s, s.finalpoints); st.AllZonesParam = cz.GetZonesParam(wwwPath); Zones? xa1 = st.AllZonesParam[eqnClass - 1][0]; // X-A, eqn. class - 1 Zones? xv1 = st.AllZonesParam[eqnClass - 1][3]; // X-V, eqn. class - 1 Zones? xm1 = st.AllZonesParam[eqnClass - 1][6]; // X-M, eqn. class - 1 Console.WriteLine($"{xa1.Amin}, {xa1.Amax}, {xa1.Bmin}, {xa1.Bmax}, {xa1.Cmin}, {xa1.Cmax}," + $" {xa1.Dmin}, {xa1.Dmax} "); if (xa1 != null) { ls.Add(new Layers(xa1)); } else ls.Add(new Layers()); if (xv1 != null) { ls.Add(new Layers(xv1)); } else ls.Add(new Layers()); if (xm1 != null) { ls.Add(new Layers(xm1)); } else ls.Add(new Layers()); // ------------------------------------ ViewBag.Data1 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Voltage, lineColor = prop.GetColor(prop.Voltage) }).ToList()); ViewBag.Data2 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Temperature }).ToList()); // vibro A ----------------------------------------------------------- ViewBag.Data3 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.X_VA }).ToList()); ViewBag.Data4 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Y_VA }).ToList()); ViewBag.Data5 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Z_VA }).ToList()); ViewBag.Data345 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[0].tha }).ToList()); ViewBag.Data678 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[0].tha1 }).ToList()); ViewBag.Data901 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[0].tha2 }).ToList()); ViewBag.Data234 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[0].tha3 }).ToList()); // vibro V ----------------------------------------------------------- ViewBag.Data6 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.X_VS }).ToList()); ViewBag.Data7 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Y_VS }).ToList()); ViewBag.Data8 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Z_VS }).ToList()); ViewBag.DataV1 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[1].thv }).ToList()); ViewBag.DataV2 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[1].thv1 }).ToList()); ViewBag.DataV3 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[1].thv2 }).ToList()); ViewBag.DataV4 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[1].thv3 }).ToList()); // vibro M ----------------------------------------------------------- ViewBag.Data9 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.X_VM }).ToList()); ViewBag.Data10 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Y_VM }).ToList()); ViewBag.Data11 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Z_VM }).ToList()); ViewBag.DataM1 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[2].thm }).ToList()); ViewBag.DataM2 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[2].thm1 }).ToList()); ViewBag.DataM3 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[2].thm2 }).ToList()); ViewBag.DataM4 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[2].thm3 }).ToList()); // signal power -------------------------------------------------------- ViewBag.Data33 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = -prop.Power }).ToList()); return View(s); } [HttpPost] public IActionResult Index(Sensors s, string id, string fromday, string fromtime, string today, string totime, string period, SetTresholds st, CheckZone cz, List<Layers> ls, string axis, string idslist) { PgSql pg = new PgSql(); s.finalpoints = ReadJsonFile(path, s.finalpoints); if (s.finalpoints == null) { s.finalpoints = new List<FinalPoint>(); } int a , v , m ; if (axis == "Y") { a = 1; v = 4; m = 7; } else if (axis == "Z") { a = 2; v = 5; m = 8; } else { a = 0; v = 3; m = 6; } //check user ids entries 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 ------------------------ s.sensors = pg.ReadSensors(s.ids + "", s.from, s.to); // -- get thresholds ------------------ GetEqnClass(out eqnClass, s, s.finalpoints); st.AllZonesParam = cz.GetZonesParam(wwwPath); Zones? xa1 = st.AllZonesParam[eqnClass - 1][a]; // X-A, eqn. class - 1 Zones? xv1 = st.AllZonesParam[eqnClass - 1][v]; // X-V, eqn. class - 1 Zones? xm1 = st.AllZonesParam[eqnClass - 1][m]; // X-M, eqn. class - 1 /* Console.WriteLine($"{xa1.Amin}, {xa1.Amax}, {xa1.Bmin}, {xa1.Bmax}, {xa1.Cmin}, {xa1.Cmax}," + $" {xa1.Dmin}, {xa1.Dmax} ");*/ if (xa1 != null) { ls.Add(new Layers(xa1)); } else ls.Add(new Layers()); if (xv1 != null) { ls.Add(new Layers(xv1)); } else ls.Add(new Layers()); if (xm1 != null) { ls.Add(new Layers(xm1)); } else ls.Add(new Layers()); // ------------------------------------ ViewBag.Data1 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Voltage, lineColor = prop.GetColor(prop.Voltage) }).ToList()); ViewBag.Data2 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Temperature }).ToList()); // vibro A ----------------------------------------------------------- ViewBag.Data3 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.X_VA }).ToList()); ViewBag.Data4 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Y_VA }).ToList()); ViewBag.Data5 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Z_VA }).ToList()); ViewBag.Data345 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[0].tha }).ToList()); ViewBag.Data678 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[0].tha1 }).ToList()); ViewBag.Data901 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[0].tha2 }).ToList()); ViewBag.Data234 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[0].tha3 }).ToList()); // vibro V ----------------------------------------------------------- ViewBag.Data6 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.X_VS }).ToList()); ViewBag.Data7 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Y_VS }).ToList()); ViewBag.Data8 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Z_VS }).ToList()); ViewBag.DataV1 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[1].thv, }).ToList()); ViewBag.DataV2 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[1].thv1, }).ToList()); ViewBag.DataV3 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[1].thv2, }).ToList()); ViewBag.DataV4 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[1].thv3, }).ToList()); // vibro M ----------------------------------------------------------- ViewBag.Data9 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.X_VM }).ToList()); ViewBag.Data10 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Y_VM }).ToList()); ViewBag.DataM1 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[2].thm }).ToList()); ViewBag.DataM2 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[2].thm1 }).ToList()); ViewBag.DataM3 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[2].thm2 }).ToList()); ViewBag.DataM4 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = ls[2].thm3 }).ToList()); // signal power -------------------------------------------------------- ViewBag.Data11 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = prop.Z_VM }).ToList()); ViewBag.Data33 = JsonConvert.SerializeObject(s.sensors.Select(prop => new { label = prop.date_time, y = -prop.Power }).ToList()); return View(s); } } }