/
agontar
/
TOROS
Обзор
Документация
Войти
/
agontar
/
TOROS
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Webapplication_new/Controllers/TreeController.cs
606 строк
23 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 System.Data; using System.Drawing; using System.Reflection; namespace WebApplication1.Controllers { public class TreeController : Controller { IWebHostEnvironment Environment; private readonly ILogger<HomeController> _logger; public TreeController(ILogger<HomeController> logger, IWebHostEnvironment environment) { _logger = logger; Environment = environment; } public List<T> ReadJsonFile<T>(string path, List<T> collection) { //List<T> list = new List<T>(); var webClient = new WebClient(); var json = webClient.DownloadString(path); collection = JsonConvert.DeserializeObject<List<T>>(json) as List<T>; return collection; } 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); } } [HttpGet] public ActionResult RemoveNode(AddNode m, string selectedItems) { Singleton1 s1 = Singleton1.Instance; int Id = -1; Console.WriteLine(s1.selectedItems); selectedItems = s1.selectedItems; List<TreeViewNode>? items = JsonConvert.DeserializeObject<List<TreeViewNode>>(selectedItems); if (items == null) { m.SelectTeaType = TeaType.Завод; return View(m); } if (items != null) items.ForEach(x => { Console.WriteLine(x.text); }); Console.WriteLine("---------------------"); string name = items[0].text.ToString(); string nodeId = items[0].id.ToString(); //string parent = items[0].parent.ToString(); Console.WriteLine(name); Console.WriteLine(nodeId); int nodeLevel = 0; if (nodeId.IndexOf("-") != -1) { string[] levels = nodeId.Split("-"); nodeLevel = levels.Length - 1; int.TryParse(levels[nodeLevel], out Id); } else { int.TryParse(nodeId, out Id); } Console.WriteLine("Node level is: " + nodeLevel); Console.WriteLine("Local Id is: " + Id); //Console.WriteLine("Parent is: " + parent); //Console.WriteLine(m.SelectTeaType); m.SelectTeaType = (TeaType)nodeLevel; m.Name = name; m.Id = Id; // m.Parent = parent; // m.SelectedTeaIds = new[] { 2 }; // m.TeaList = GetAllTeaTypes(); return View(m); } [HttpPost] public ActionResult RemoveNode(AddNode m) { var selectedValue = m.Selected; int nodeType = (int)selectedValue; string wwwPath = this.Environment.WebRootPath; Console.WriteLine("Node type: " + nodeType); if (nodeType == 3) { string path = Path.Combine(wwwPath, "TreeData", "endpoints.json"); List<FinalPoint> finalpoints = new List<FinalPoint>(); finalpoints = ReadJsonFile(path, finalpoints); if (finalpoints == null) { return View(m); } else { finalpoints.ForEach(x => { Console.WriteLine(x.Id + " " + x.Name); }); if (m.Name.IsNullOrWhiteSpace()) { m.Result = "Неправильное название"; Console.WriteLine(m.Name); return View(m); } Console.WriteLine("====" + m.Id); if (finalpoints.Find(x => x.Id == m.Id) == null) { m.Result = "Такого узла не существует"; return View(m); } FinalPoint finalpoint = new FinalPoint(); finalpoint = finalpoints.Find(x => x.Id == m.Id); finalpoints.Remove(finalpoint); finalpoints.ForEach(x => { Console.WriteLine(x.Id + " " + x.Name); }); WriteJsonFile(path, finalpoints); m.Result = "Успешно удалено"; } } if (nodeType == 2) { string path1 = Path.Combine(wwwPath, "TreeData", "points1.json"); string path2 = Path.Combine(wwwPath, "TreeData", "endpoints.json"); List<MetricPoint> points = new List<MetricPoint>(); List<FinalPoint> finalpoints = new List<FinalPoint>(); points = ReadJsonFile(path1, points); finalpoints = ReadJsonFile(path2, finalpoints); if (points == null) { return View(m); } else { //int[] ids = new int[points.Count]; points.ForEach(x => { Console.WriteLine(x.Id + " " + x.Name); }); if (m.Name.IsNullOrWhiteSpace()) { m.Result = "Неправильное название"; Console.WriteLine(m.Name); return View(m); } Console.WriteLine("====" + m.Id); if (points.Find(x => x.Id == m.Id) == null) { m.Result = "Такого узла не существует"; return View(m); } MetricPoint point = new MetricPoint(); point = points.Find(x => x.Id == m.Id); if (finalpoints.Find(x => x.MetricPointId == point.Id) != null) { m.Result = "Не удалить, сначала удалите дочерние объекты"; return View(m); } points.Remove(point); points.ForEach(x => { Console.WriteLine(x.Id + " " + x.Name); }); path1 = Path.Combine(wwwPath, "TreeData", "points1.json"); WriteJsonFile(path1, points); m.Result = "Успешно удалено"; } } if (nodeType == 1) { string path1 = Path.Combine(wwwPath, "TreeData", "points1.json"); string path2 = Path.Combine(wwwPath, "TreeData", "subtypes1.json"); List<MetricPoint> points = new List<MetricPoint>(); List<VehicleSubType> subtypes = new List<VehicleSubType>(); points = ReadJsonFile(path1, points); subtypes = ReadJsonFile(path2, subtypes); if (subtypes == null) { return View(m); } else { //int[] ids = new int[points.Count]; subtypes.ForEach(x => { Console.WriteLine(x.Id + " " + x.Name); }); if (m.Name.IsNullOrWhiteSpace()) { m.Result = "Неправильное название"; Console.WriteLine(m.Name); return View(m); } Console.WriteLine("====" + m.Id); if (subtypes.Find(x => x.Id == m.Id) == null) { m.Result = "Такого узла не существует"; return View(m); } VehicleSubType subtype = new VehicleSubType(); subtype = subtypes.Find(x => x.Id == m.Id); if (points.Find(x => x.VehicleId == subtype.Id) != null) { m.Result = "Не удалить, сначала удалите дочерние объекты"; return View(m); } subtypes.Remove(subtype); subtypes.ForEach(x => { Console.WriteLine(x.Id + " " + x.Name); }); path2 = Path.Combine(wwwPath, "TreeData", "subtypes1.json"); WriteJsonFile(path2, subtypes); m.Result = "Успешно удалено"; } } if (nodeType == 0) { string path1 = Path.Combine(wwwPath, "TreeData", "subtypes1.json"); string path2 = Path.Combine(wwwPath, "TreeData", "types1.json"); // List<MetricPoint> points = new List<MetricPoint>(); List<VehicleSubType> subtypes = new List<VehicleSubType>(); List<VehicleType> types = new List<VehicleType>(); types = ReadJsonFile(path2, types); subtypes = ReadJsonFile(path1, subtypes); if (types == null) { return View(m); } else { //int[] ids = new int[points.Count]; types.ForEach(x => { Console.WriteLine(x.Id + " " + x.Name); }); if (m.Name.IsNullOrWhiteSpace()) { m.Result = "Неправильное название"; Console.WriteLine(m.Name); return View(m); } Console.WriteLine("====" + m.Id); if (types.Find(x => x.Id == m.Id) == null) { m.Result = "Такого узла не существует"; return View(m); } VehicleType type = new VehicleType(); type = types.Find(x => x.Id == m.Id); if (subtypes.Find(x => x.VehicleTypeId == type.Id) != null) { m.Result = "Не удалить, сначала удалите дочерние объекты"; return View(m); } types.Remove(type); types.ForEach(x => { Console.WriteLine(x.Id + " " + x.Name); }); path2 = Path.Combine(wwwPath, "TreeData", "types1.json"); WriteJsonFile(path2, types); m.Result = "Успешно удалено"; } } ViewBag.TeaType = selectedValue.ToString(); return View(m); } [HttpGet] public IActionResult EditNode(AddNode m, string selectedItems, string listbox, string listbox2, OutputInterpreter interpreter) { string wwwPath = this.Environment.WebRootPath; string path = Path.Combine(wwwPath, "Uploads"); string[] files = Directory.GetFiles(path); for (int i = 0; i < files.Length; i++) { string file = files[i].Replace(wwwPath + "\\Uploads\\", ""); Console.WriteLine(file); m.Files.Add(file); } Singleton1 s1 = Singleton1.Instance; int Id = -1; Console.WriteLine(s1.selectedItems); selectedItems = s1.selectedItems; //List<TreeViewNode>? nodes = s1.TreeNodes; List<TreeViewNode>? items = JsonConvert.DeserializeObject<List<TreeViewNode>>(selectedItems); if (items == null) { m.SelectTeaType = TeaType.Завод; ViewBag.D1 = "disabled"; return View(m); } if (items != null) items.ForEach(x => { Console.WriteLine(x.text); }); Console.WriteLine("---------------------"); string name = items[0].text.ToString(); string nodeId = items[0].id.ToString(); //string parent = items[0].parent.ToString(); Console.WriteLine(name); Console.WriteLine(nodeId); int nodeLevel = 0; if (nodeId.IndexOf("-") != -1) { string[] levels = nodeId.Split("-"); nodeLevel = levels.Length - 1; int.TryParse(levels[nodeLevel], out Id); } else { int.TryParse(nodeId, out Id); } //TreeViewNode node = new TreeViewNode(); //node = nodes.Find(x => x.id == nodeId); Console.WriteLine("Node level is: " + (nodeLevel)); Console.WriteLine("Node Id is: " + Id); Console.WriteLine("Node Name is: " + name); if (nodeLevel == 3) { ViewBag.D1 = ""; path = Path.Combine(wwwPath, "Bases", "BaseStations.json"); List<BaseStation> stations = new List<BaseStation>(); stations = ReadJsonFile(path, stations); List<Sensor> sensors = new List<Sensor>(); foreach (BaseStation item in stations) { item.SensorsGroup.sensors = interpreter.StationSensors(item.IP); foreach (var sensor in item.SensorsGroup.sensors) { sensor.SensorIP = item.IP; sensors.Add(sensor); } } foreach (var sensor in sensors) { string s = $"{sensor.id}-->{sensor.SensorIP}"; m.Available.Add(s); } } else { ViewBag.D1 = "disabled"; } m.IP = s1.IP; m.IDS = s1.IDS; //Console.WriteLine(m.SelectTeaType); m.SelectTeaType = (TeaType)(nodeLevel); m.Name = name; m.Id = Id; m.ImgFileName = s1.ImgFileName; return View(m); } [HttpPost] public IActionResult EditNode(AddNode m, string listbox, string listbox1, string listbox2) { Console.WriteLine($"Id: {m.Id}, Name: {m.Name}"); Singleton1 s1 = Singleton1.Instance; // dropdown var selectedValue = m.Selected; int nodeType = (int)selectedValue; string wwwPath = this.Environment.WebRootPath; if (listbox != "Выбрать" && !listbox.IsNullOrWhiteSpace()) { m.ImgFileName = listbox; } if (m.ImgFileName.IsNullOrWhiteSpace()) { m.ImgFileName = ""; } /* string[] sensorData = listbox1.Split("-->"); if (sensorData.Length == 2) { m.IDS = sensorData[0]; m.IP = sensorData[1]; } */ Console.WriteLine("Node type: " + nodeType); if (nodeType == 0) { string path = Path.Combine(wwwPath, "TreeData", "types1.json"); List<VehicleType> types = new List<VehicleType>(); types = ReadJsonFile(path, types); if (types == null) { return View(m); } else { types.ForEach(x => { Console.WriteLine(x.Id + " " + x.Name); }); if (types.Find(x => x.Name == m.Name) != null) { //m.Result = "Такое предприятие уже существует"; // return View(m); } if (m.Name.IsNullOrWhiteSpace()) { m.Result = "Неправильное название"; Console.WriteLine(m.Name); return View(m); } if (types.Find(x => x.Id == m.Id) == null) { m.Result = "Такого узла не существует"; return View(m); } VehicleType type = new VehicleType(); type = types.Find(x => x.Id == m.Id); type.Name = m.Name; type.ImgFileName = m.ImgFileName; WriteJsonFile(path, types); m.Result = "Успешно изменено"; } } if (nodeType == 1) { string path = Path.Combine(wwwPath, "TreeData", "subtypes1.json"); List<VehicleSubType> subtypes = new List<VehicleSubType>(); subtypes = ReadJsonFile(path, subtypes); if (subtypes == null) { return View(m); } else { subtypes.ForEach(x => { Console.WriteLine(x.Id + " " + x.Name); }); if (m.Name.IsNullOrWhiteSpace()) { m.Result = "Неправильное название"; Console.WriteLine(m.Name); return View(m); } if (subtypes.Find(x => x.Id == m.Id) == null) { m.Result = "Такого узла не существует"; return View(m); } VehicleSubType subtype = new VehicleSubType(); subtype = subtypes.Find(x => x.Id == m.Id); subtype.Name = m.Name; subtype.ImgFileName = m.ImgFileName; WriteJsonFile(path, subtypes); m.Result = "Успешно изменено"; } } if (nodeType == 2) { string path = Path.Combine(wwwPath, "TreeData", "points1.json"); List<MetricPoint> points = new List<MetricPoint>(); points = ReadJsonFile(path, points); if (points == null) { return View(m); } else { points.ForEach(x => { Console.WriteLine(x.Id + " " + x.Name); }); if (m.Name.IsNullOrWhiteSpace()) { m.Result = "Неправильное название"; Console.WriteLine(m.Name); return View(m); } if (points.Find(x => x.Id == m.Id) == null) { m.Result = "Такого узла не существует"; return View(m); } MetricPoint point = new MetricPoint(); point = points.Find(x => x.Id == m.Id); point.Name = m.Name; point.ImgFileName = m.ImgFileName; WriteJsonFile(path, points); m.Result = "Успешно изменено"; } } if (nodeType == 3) { string[] sensorData = listbox1.Split("-->"); if (sensorData.Length == 2) { m.IDS = sensorData[0]; m.IP = sensorData[1]; } int eqnClass; // equipment vibro class int.TryParse(listbox2, out eqnClass); // set vibro class string path = Path.Combine(wwwPath, "TreeData", "endpoints.json"); List<FinalPoint> finalpoints = new List<FinalPoint>(); finalpoints = ReadJsonFile(path, finalpoints); if (finalpoints == null) { return View(m); } else { finalpoints.ForEach(x => { Console.WriteLine(x.Id + " " + x.Name); }); if (m.Name.IsNullOrWhiteSpace()) { m.Result = "Неправильное название"; Console.WriteLine(m.Name); return View(m); } if (finalpoints.Find(x => x.Id == m.Id) == null) { m.Result = "Такого узла не существует"; return View(m); } FinalPoint? finalpoint = new FinalPoint(); finalpoint = finalpoints.Find(x => x.Id == m.Id); if (finalpoint != null) { finalpoint.Name = m.Name; finalpoint.IP = m.IP; finalpoint.IDS = m.IDS; finalpoint.SensorType = m.SensorType; finalpoint.EqnClass = eqnClass; } s1.IP = m.IP; s1.IDS = m.IDS; WriteJsonFile(path, finalpoints); m.Result = "Успешно изменено"; } } //return View(m); s1.Name = m.Name; s1.DisplInfo = m.Name; s1.ImgFileName = m.ImgFileName; return Redirect("/Home/Index"); } // GET: TreeController/Details/5 public ActionResult Details(int id) { return View(); } // GET: TreeController/Create public ActionResult Create() { return View(); } // POST: TreeController/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(IFormCollection collection) { try { return RedirectToAction(nameof(Index)); } catch { return View(); } } // GET: TreeController/Edit/5 public ActionResult Edit(int id) { return View(); } // POST: TreeController/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(int id, IFormCollection collection) { try { return RedirectToAction(nameof(Index)); } catch { return View(); } } // GET: TreeController/Delete/5 public ActionResult Delete(int id) { return View(); } // POST: TreeController/Delete/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Delete(int id, IFormCollection collection) { try { return RedirectToAction(nameof(Index)); } catch { return View(); } } } }