|
|
|
@ -1,24 +1,35 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Application.Domain.Entities;
|
|
|
|
|
using Infrastructure.Data;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
public HomeController(IRepository<Node> nodeRepo, IRepository<Device> deviceRepo, IRepository<Data> dataRepo, IRepository<Category> categoryRepo, IRepository<Product> productRepo)
|
|
|
|
|
public HomeController(IConfiguration cfg,
|
|
|
|
|
IRepository<Node> nodeRepo,
|
|
|
|
|
IRepository<Device> deviceRepo,
|
|
|
|
|
IRepository<Data> dataRepo,
|
|
|
|
|
IRepository<Category> categoryRepo,
|
|
|
|
|
IRepository<Product> productRepo)
|
|
|
|
|
{
|
|
|
|
|
this._cfg = cfg;
|
|
|
|
|
this._nodeRepo = nodeRepo;
|
|
|
|
|
this._deviceRepo = deviceRepo;
|
|
|
|
|
this._dataRepo = dataRepo;
|
|
|
|
@ -32,7 +43,7 @@ namespace IoTCenter.Controllers
|
|
|
|
|
return View();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IActionResult GetNodeList()
|
|
|
|
|
public JsonResult GetNodeList()
|
|
|
|
|
{
|
|
|
|
|
var categorys = this._deviceRepo.ReadOnlyTable()
|
|
|
|
|
.GroupBy(o => o.Product.Category.Name)
|
|
|
|
@ -86,21 +97,13 @@ namespace IoTCenter.Controllers
|
|
|
|
|
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)
|
|
|
|
|
public JsonResult GetNode(string number)
|
|
|
|
|
{
|
|
|
|
|
var model = this._nodeRepo.ReadOnlyTable()
|
|
|
|
|
.Include(o => o.Scenes)
|
|
|
|
@ -110,14 +113,27 @@ namespace IoTCenter.Controllers
|
|
|
|
|
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 });
|
|
|
|
|
//}
|
|
|
|
|
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))
|
|
|
|
|
{
|
|
|
|
|
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:ss")).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 });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|