You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
6.8 KiB
175 lines
6.8 KiB
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<Node> _nodeRepo;
|
|
private readonly IRepository<Device> _deviceRepo;
|
|
private readonly IRepository<Data> _dataRepo;
|
|
private readonly IRepository<Category> _categoryRepo;
|
|
private readonly IRepository<Product> _productRepo;
|
|
private readonly IEventPublisher _eventPublisher;
|
|
|
|
public HomeController(IConfiguration cfg,
|
|
IRepository<Node> nodeRepo,
|
|
IRepository<Device> deviceRepo,
|
|
IRepository<Data> dataRepo,
|
|
IRepository<Category> categoryRepo,
|
|
IRepository<Product> 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<Node>(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<Data> {
|
|
// 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<object>();
|
|
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<DynamicInfluxRow>(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<string>();
|
|
var data = rows?.Select(o => o.GetField(key)).ToList() ?? new List<object>();
|
|
var model = new { labels, data };
|
|
return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Json(null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |