using Application.Domain.Entities; using Infrastructure.Data; using Infrastructure.Domain; using Infrastructure.Extensions; using Infrastructure.Web; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using Vibrant.InfluxDB.Client; using Vibrant.InfluxDB.Client.Rows; namespace IoTCenter.Controllers { [Device] public class HomeController : Controller { private readonly IConfiguration _cfg; private readonly IRepository _nodeRepo; private readonly IRepository _deviceRepo; private readonly IRepository _dataRepo; private readonly IRepository _categoryRepo; private readonly IRepository _productRepo; private readonly IEventPublisher _eventPublisher; public HomeController(IConfiguration cfg, IRepository nodeRepo, IRepository deviceRepo, IRepository dataRepo, IRepository categoryRepo, IRepository productRepo, IEventPublisher eventPublisher) { this._cfg = cfg; this._nodeRepo = nodeRepo; this._deviceRepo = deviceRepo; this._dataRepo = dataRepo; this._categoryRepo = categoryRepo; this._productRepo = productRepo; this._eventPublisher = eventPublisher; } [Authorize] public IActionResult Index() { //this._eventPublisher.Publish(new EntityInsertedEvent(new Node())); //var temp = this._nodeRepo.Table().Include(o => o.Devices).ThenInclude(o => o.Data).FirstOrDefault(o => o.Name == "207"); //if (!temp.Devices.Any(o => o.Name == "207")) //{ // temp.Devices.Add(new Device // { // Name = "²âÊÔÉ豸", // Number = "2019091602", // ProductId = this._productRepo.ReadOnlyTable().FirstOrDefault().Id, // Data = new List { // new Data{ // Key="Electricity", // Value="659", // } // } // }); // this._nodeRepo.SaveChanges(); //} return View(); } public JsonResult 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 }); } [Authorize] [Route("/Node")] public IActionResult Node(string number) { return View(model: number); } public JsonResult GetNode(string number) { var model = this._nodeRepo.ReadOnlyTable() .Include(o => o.Scenes) .Include(o => o.Devices) .ThenInclude(o => o.Data) .Where(o => o.Number == number) .FirstOrDefault(o => o.Number == number); return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } public JsonResult GetChartData(string number, string key, string time) { var device = this._deviceRepo.ReadOnlyTable().Include(o => o.Node).Include(o => o.Data).FirstOrDefault(o => o.Number == number); var url = this._cfg["influxdb:url"]; var usr = this._cfg["influxdb:usr"]; var pwd = this._cfg["influxdb:pwd"]; var dbName = "iot"; var measurementName = "data"; var list = new List(); using (var client = new InfluxClient(new Uri(url), usr, pwd)) { try { var query = $"select {key} from {measurementName} where time>now()-{time} and DeviceNumber = '{device.Number}' limit 10000"; var result = client.ReadAsync(dbName, query).Result; var rows = result.Results.FirstOrDefault()? .Series.FirstOrDefault()? .Rows; var labels = rows?.Select(o => o.Timestamp.Value).Select(o => o.ToString("MM-dd HH:mm")).ToList() ?? new List(); var data = rows?.Select(o => o.GetField(key)).ToList() ?? new List(); var model = new { labels, data }; return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } catch (Exception ex) { ex.PrintStack(); return Json(null); } } } } }