/
agontar
/
TOROS
Обзор
Документация
Войти
/
agontar
/
TOROS
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Webapplication_new/Controllers/BaseStationsController.cs
433 строки
15 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 static System.Net.WebRequestMethods; using Microsoft.AspNetCore.Hosting; namespace WebApplication1.Controllers { public class BaseStationsController : Controller { IWebHostEnvironment Environment; private readonly ILogger<HomeController> _logger; //Monitoring monitoring; string wwwPath; string path; Singleton1 s1; public BaseStationsController(ILogger<HomeController> logger, IWebHostEnvironment environment) { _logger = logger; Environment = environment; wwwPath = Environment.WebRootPath; path = Path.Combine(wwwPath, "Bases", "BaseStations.json"); //monitoring = new Monitoring(); s1 = Singleton1.Instance; } public List<T> ReadJsonFile<T>(string path, List<T>? collection) { //List<T> list = new List<T>(); var rw = new WriteRead(); //var webClient = new WebClient(); //var json = webClient.DownloadString(path); var json = rw.ReadText(path); collection = JsonConvert.DeserializeObject<List<T>>(json) as List<T>; if (collection != null) return collection; else return new List<T>(); } public void WriteJsonFile<T>(string path, List<T> collection) { JsonSerializer serializer = new JsonSerializer(); using (StreamWriter sw = new StreamWriter(path)) using (JsonWriter writer = new JsonTextWriter(sw)) { serializer.Serialize(writer, collection); } } public void MapSensorsToNodes(List<FinalPoint> finalPoints, List<Sensor> sensors, CheckZone cz, string wwwPath) { foreach (var finalpoint in finalPoints) { finalpoint.IDS = finalpoint.IDS.Replace(" ", ""); finalpoint.IDS = finalpoint.IDS.Replace("-", ""); finalpoint.IDS = finalpoint.IDS.ToUpper(); foreach (var sensor in sensors) { sensor.id = sensor.id.Replace(" ", ""); sensor.id = sensor.id.Replace("-", ""); sensor.id = sensor.id.ToUpper(); if (sensor.id == finalpoint.IDS) { sensor.SensorName += finalpoint.Name + " "; sensor.SensorIP = finalpoint.IP; sensor.SensorType = finalpoint.SensorType; float v = sensor.Z_VS; int zone = cz.GetZoneNumber(wwwPath, 1, v); Console.WriteLine($"===> {finalpoint.Name}, {v}, {zone}"); if (zone == 1) finalpoint.PointStyle.style = "color: green; font-weight: bold;"; else if (zone == 2) finalpoint.PointStyle.style = "color: orange; font-weight: bold;"; else if (zone == 3) finalpoint.PointStyle.style = "color: red; font-weight: bold;"; else if (zone == 0) finalpoint.PointStyle.style = "color: black; font-weight: bold;"; } } } } public IActionResult Index(BaseStation s) { s.BaseStations = ReadJsonFile(path, s.BaseStations); foreach (var item in s.BaseStations) { Console.WriteLine($"{item.Id} {item.Name} {item.ids} {item.IP}"); } return View(s); } [HttpGet] public IActionResult Add(BaseStation s) { s.BaseStations = ReadJsonFile(path, s.BaseStations); foreach (var item in s.BaseStations) { Console.WriteLine($"{item.Id} {item.Name} {item.ids} {item.IP}"); } return View(s); } [HttpPost] public IActionResult Add(BaseStation s, string Name) { s.BaseStations = ReadJsonFile(path, s.BaseStations); int nextId; if (s.BaseStations == null) { return View(s); } else { if (s.BaseStations.Count == 0) { nextId = 1; } else { int[] ids = new int[s.BaseStations.Count]; for (int i = 0; i < ids.Length; i++) { ids[i] = s.BaseStations[i].Id; } nextId = ids.Max() + 1; } BaseStation bs = new BaseStation(); bs.Id = nextId; bs.Name = s.Name; bs.ids = s.ids; bs.IP = s.IP; s.BaseStations.Add(bs); WriteJsonFile(path, s.BaseStations); s1.Monitor = false; return RedirectToAction("Index"); } } [HttpGet] public IActionResult Delete(BaseStation s, int id) { Console.WriteLine("=============" + id); s.BaseStations = ReadJsonFile(path, s.BaseStations); foreach (var item in s.BaseStations) { Console.WriteLine($"{item.Id} {item.Name} {item.ids} {item.IP}"); } if (id > 0) { foreach (var item in s.BaseStations) { //Console.WriteLine($"{item.Id} {item.Name} {item.ids} {item.IP}"); if (item.Id == id) { s.BaseStations.Remove(item); break; } } WriteJsonFile(path, s.BaseStations); s1.Monitor = false; return RedirectToAction("Index"); } return View(s); } [HttpGet] public IActionResult Display(BaseStation s, OutputInterpreter interpreter, CheckZone cz, VibroSensors vs) { try { s.BaseStations = ReadJsonFile(path, s.BaseStations); string pathToTree = Path.Combine(wwwPath, "TreeData", "endpoints.json"); string pathToSensors = Path.Combine(wwwPath, "TreeData", "sensors.json"); List<FinalPoint> finalPoints = new List<FinalPoint>(); finalPoints = ReadJsonFile(pathToTree, finalPoints); Console.WriteLine(s.ids); // create overall list of connected sensors List<Sensor> sensors = new List<Sensor>(); foreach (var item in s.BaseStations) { Console.WriteLine($"{item.Id} {item.Name} {item.ids} {item.IP}"); // map physically connected sensors info to the corresponding tree sensor nodes item.SensorsGroup.sensors = interpreter.StationSensors(item.IP); vs.MapSensorsToNodes(finalPoints, item.SensorsGroup.sensors, cz, wwwPath); foreach (var sensor in item.SensorsGroup.sensors) { string str = sensor.id; str = $"{str.Substring(0, 2)}-{str.Substring(2, 2)}-{str.Substring(4, 2)}-{str.Substring(6, 2)}"; sensor.id = str; sensors.Add(sensor); } } // map physically connected sensors info to the corresponding tree sensor nodes //MapSensorsToNodes(finalPoints, sensors, cz); WriteJsonFile(pathToTree, finalPoints); // write to the tree sensor nodes info file WriteJsonFile(pathToSensors, sensors); foreach (var item in s.BaseStations) { item.SensorsGroup.sensors.ForEach(x => { Console.WriteLine(x.Voltage); }); } return View(s); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return View(s); } } [HttpGet] public IActionResult Edit(BaseStation s, int id) { //Console.WriteLine("=============" + id); s.BaseStations = ReadJsonFile(path, s.BaseStations); foreach (var item in s.BaseStations) { // Console.WriteLine($"{item.Id} {item.Name} {item.ids} {item.IP}"); } if (id > 0) { foreach (var item in s.BaseStations) { //Console.WriteLine($"{item.Id} {item.Name} {item.ids} {item.IP}"); if (item.Id == id) { //s.BaseStations.Remove(item); // Console.WriteLine("Found " + item.Name); s.Id = id; s.ids = item.ids; s.Name = item.Name; s.IP = item.IP; break; } } //WriteJsonFile(path, s.BaseStations); s1.Monitor = false; return View(s); } return View(s); } [HttpPost] public IActionResult Edit(BaseStation s, string Name) { // Console.WriteLine("=============" + s.Id); s.BaseStations = ReadJsonFile(path, s.BaseStations); foreach (var item in s.BaseStations) { // Console.WriteLine($"{item.Id} {item.Name} {item.ids} {item.IP}"); if (item.Id == s.Id) { item.Name = s.Name; item.ids = s.ids; item.IP = s.IP; // Console.WriteLine("Found " + item.Name); WriteJsonFile(path, s.BaseStations); break; } } //WriteJsonFile(path, s.BaseStations); return RedirectToAction("Index"); //return RedirectToAction("Index"); } [HttpGet] public IActionResult Thresholds(SetTresholds st, CheckZone cz, ZonesParam zp) { st.AllZones = cz.GetZones(wwwPath); st.AllZonesParam = cz.GetZonesParam(wwwPath); Console.WriteLine($"Selected: {st.ParamSelect}"); int k = st.ParamSelect; //for (int i = 0; i < 10; i++) { foreach (var item in st.AllZonesParam) { if (item[k] != null) { Console.WriteLine($"{item[k].Amin}, {item[k].Amax}, {item[k].Bmin}, {item[k].Bmax}, {item[k].Cmin}, {item[k].Cmax}," + $" {item[k].Dmin}, {item[k].Dmax} "); } else { Console.WriteLine("null"); } } Console.WriteLine("----------------------------------------"); } return View(st); } [HttpPost] public IActionResult Thresholds(SetTresholds st, Zones z, ZonesParam zp, string parameter, string Operation) { Console.WriteLine(Operation); if (Operation == "13") { int par; int.TryParse(parameter, out par); st.ParamSelect = par; return RedirectToAction("Thresholds", st); } EqnClass selectClass = st.SelectClass; int selection = (int)selectClass; string path = Path.Combine(wwwPath, "Zones", "Class" + (selection + 1).ToString() + ".json"); try { st.SetFloatValues(); z.Amax = st.amax; z.Bmax = st.bmax; z.Cmax = st.cmax; z.Dmax = st.dmax; z.Amin = st.amin; z.Bmin = st.bmin; z.Cmin = st.cmin; z.Dmin = st.dmin; WriteRead rw = new WriteRead(); string json = rw.ReadText(path); rw.WriteJson(path, z); //----- path = Path.Combine(wwwPath, "Zones", "ClassP" + (selection + 1).ToString() + ".json"); json = rw.ReadText(path); Zones[]? zones = JsonConvert.DeserializeObject<Zones[]>(json); if (zones != null) { zp.ParamZones = zones; } //zp.ParamZones = JsonConvert.DeserializeObject<Zones[]>(json); Console.WriteLine($"Parameter {parameter}"); /*if (zp.ParamZones == null) { zp.ParamZones = new Zones[10]; }*/ int par; int.TryParse(parameter, out par); zp.ParamZones[par] = z; st.ParamSelect = par; rw.WriteJson(path, zp.ParamZones); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return View(st); } return RedirectToAction("Thresholds", st); } [HttpGet] public IActionResult Monitor() { Singleton1 s1 = Singleton1.Instance; if (s1.Monitor) { ViewBag.Disabled = "disabled"; } return View(); } [HttpPost] public IActionResult Monitor(BaseStation s, int cost) { s.BaseStations = ReadJsonFile(path, s.BaseStations); Console.WriteLine(cost); /* List<string> ips = new List<string>(); foreach (var item in s.BaseStations) { ips.Add(item.IP); }*/ if (cost == 1) { Monitoring.monitor = true; Monitoring.MonitorLoop(s.BaseStations); //ViewBag.Monitor = "Включен"; //ViewBag.Disabled = "disabled"; WriteToDB.enableRec = true; WriteToDB.RecordingDB(s.BaseStations); } if (Monitoring.monitor == true) { ViewBag.Monitor = "Включен"; ViewBag.Disabled = "disabled"; } if (cost == 2) { Monitoring.monitor = false; ViewBag.Monitor = "Выключен"; ViewBag.Disabled = ""; WriteToDB.enableRec = false; } if (true) { } Console.WriteLine(Monitoring.monitor); return View(); } public IActionResult OpenModelPopup(BaseStation s) { //can send some data also. return PartialView("Add", s); } } }