using System; using System.Linq; using Application.Domain.Entities; using Infrastructure.Data; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; namespace IoTCenter.Controllers { public class HomeController : Controller { private readonly IRepository _nodeRepo; private readonly IRepository _deviceRepo; private readonly IRepository _dataRepo; private readonly IRepository _categoryRepo; private readonly IRepository _productRepo; public HomeController(IRepository nodeRepo, IRepository deviceRepo, IRepository dataRepo, IRepository categoryRepo, IRepository productRepo) { this._nodeRepo = nodeRepo; this._deviceRepo = deviceRepo; this._dataRepo = dataRepo; this._categoryRepo = categoryRepo; this._productRepo = productRepo; } [Authorize] public IActionResult Index() { return View(); } public IActionResult GetNodeList() { var categorys = this._deviceRepo.ReadOnlyTable() .GroupBy(o => o.Product.Category.Name) .Select(g => new { g.Key, Count = g.Count() }); var nodes = this._nodeRepo.ReadOnlyTable() .GroupBy(o => o.Type) .Select(g => new { g.Key, Count = g.Count() }); var energy = this._dataRepo.ReadOnlyTable() .Where(o => o.Key == "Electricity") .Select(o => new { o.Device.Node.Name, o.Value }).ToList() .Select(o => new { o.Name, Value = Convert.ToDouble(o.Value) }) .GroupBy(o => o.Name) .Select(g => new { g.Key, Sum = g.Sum(o => o.Value) }); var model = new { NodeChart = new { total = this._nodeRepo.ReadOnlyTable().Count(), online = this._nodeRepo.ReadOnlyTable().Count(o => o.IsOnline), offline = this._nodeRepo.ReadOnlyTable().Count(o => !o.IsOnline), data = nodes.Select(o => o.Count), labels = nodes.Select(o => o.Key) }, DeviceChart = new { total = this._deviceRepo.ReadOnlyTable().Count(), online = this._deviceRepo.ReadOnlyTable().Count(o => o.IsOnline), offline = this._deviceRepo.ReadOnlyTable().Count(o => !o.IsOnline), data = categorys.Select(o => o.Count), labels = categorys.Select(o => o.Key) }, EnergyChart = new { total = energy.Sum(o => o.Sum), data = energy.Select(o => o.Sum), labels = energy.Select(o => o.Key) }, Nodes = this._nodeRepo.ReadOnlyTable().Where(o => !o.Disabled).Include(o => o.Scenes).ToList() }; return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } //public IActionResult GetNodes() //{ // var model = this._nodeRepo.ReadOnlyTable() // .Include(o => o.Devices) // .ToList(); // return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); //} [Route("/Node")] public IActionResult Node(string number) { return View(model: number); } public IActionResult GetNode(string number) { var model = this._nodeRepo.ReadOnlyTable() .Include(o => o.Scenes) .Include(o => o.Devices) .ThenInclude(o => o.Data) .FirstOrDefault(o => o.Number == number); return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } //public IActionResult GetNode(Guid id) //{ // var model = this._nodeRepo.ReadOnlyTable() // .Include(o => o.Scenes) // .Include(o => o.Devices).ThenInclude(o => o.Data) // //.Include(o => o.Devices).ThenInclude(o => o.Apis).ThenInclude(o => o.Parameters) // .FirstOrDefault(o => o.Id == id); // return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); //} } }