|
|
|
@ -9,23 +9,35 @@ using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
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 AppController : Controller
|
|
|
|
|
{
|
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
|
private readonly IJwtHelper _jwtHelper;
|
|
|
|
|
private readonly INodeService _nodeService;
|
|
|
|
|
private readonly IRepository<Node> _nodeRepo;
|
|
|
|
|
private readonly IRepository<Device> _deviceRepo;
|
|
|
|
|
private readonly IHubContext<PageHub> _pageHubContext;
|
|
|
|
|
|
|
|
|
|
public AppController(IJwtHelper jwtHelper, IRepository<Node> nodeRepo, IRepository<Device> deviceRepo, IHubContext<PageHub> pageHubContext)
|
|
|
|
|
public AppController(IConfiguration configuration,
|
|
|
|
|
IJwtHelper jwtHelper,
|
|
|
|
|
INodeService nodeService,
|
|
|
|
|
IRepository<Node> nodeRepo,
|
|
|
|
|
IRepository<Device> deviceRepo,
|
|
|
|
|
IHubContext<PageHub> pageHubContext)
|
|
|
|
|
{
|
|
|
|
|
this._configuration = configuration;
|
|
|
|
|
this._jwtHelper = jwtHelper;
|
|
|
|
|
this._nodeService = nodeService;
|
|
|
|
|
this._nodeRepo = nodeRepo;
|
|
|
|
|
this._deviceRepo = deviceRepo;
|
|
|
|
|
this._pageHubContext = pageHubContext;
|
|
|
|
@ -39,7 +51,7 @@ namespace IoTCenter.Controllers
|
|
|
|
|
|
|
|
|
|
public IActionResult GetNodes(string token)
|
|
|
|
|
{
|
|
|
|
|
var userName = this._jwtHelper.GetPayload(token)["UserName"].ToString();
|
|
|
|
|
//var userName = this._jwtHelper.GetPayload(token)["UserName"].ToString();
|
|
|
|
|
var model = this._nodeRepo.ReadOnlyTable()
|
|
|
|
|
.Select(o => new { o.Id, o.Number, o.Name, o.DisplayOrder, Count = o.Devices.Count })
|
|
|
|
|
.ToList();
|
|
|
|
@ -48,7 +60,7 @@ namespace IoTCenter.Controllers
|
|
|
|
|
|
|
|
|
|
public IActionResult GetNode(string token, string number)
|
|
|
|
|
{
|
|
|
|
|
var userName = this._jwtHelper.GetPayload(token)["UserName"].ToString();
|
|
|
|
|
//var userName = this._jwtHelper.GetPayload(token)["UserName"].ToString();
|
|
|
|
|
var model = this._nodeRepo.ReadOnlyTable()
|
|
|
|
|
.Include(o => o.Sences)
|
|
|
|
|
.Include(o => o.Devices).ThenInclude(o => o.Data)
|
|
|
|
@ -58,7 +70,7 @@ namespace IoTCenter.Controllers
|
|
|
|
|
|
|
|
|
|
public IActionResult GetDevice(string token, string number)
|
|
|
|
|
{
|
|
|
|
|
var userName = this._jwtHelper.GetPayload(token)["UserName"].ToString();
|
|
|
|
|
//var userName = this._jwtHelper.GetPayload(token)["UserName"].ToString();
|
|
|
|
|
var model = this._deviceRepo.ReadOnlyTable()
|
|
|
|
|
.Include(o => o.Data)
|
|
|
|
|
.Include(o => o.Apis).ThenInclude(o => o.Parameters)
|
|
|
|
@ -75,8 +87,7 @@ namespace IoTCenter.Controllers
|
|
|
|
|
{
|
|
|
|
|
query = query.SetParam(item.Key, item.Value);
|
|
|
|
|
}
|
|
|
|
|
this._pageHubContext.Clients.Group(node).SendAsync(nameof(INodeService.Exec), connectionId, id, cmd, query);
|
|
|
|
|
return Json(ApiResponse.AsyncSuccess());
|
|
|
|
|
return Json(this._nodeService.Exec(id, cmd, query));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
@ -94,7 +105,7 @@ namespace IoTCenter.Controllers
|
|
|
|
|
{
|
|
|
|
|
query = query.SetParam(item.Key, item.Value);
|
|
|
|
|
}
|
|
|
|
|
this._pageHubContext.Clients.Group(node).SendAsync(nameof(INodeService.ExecAll), connectionId, id, cmd, query);
|
|
|
|
|
this._nodeService.ExecAll(id, cmd, query);
|
|
|
|
|
return Json(ApiResponse.AsyncSuccess());
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
@ -108,7 +119,7 @@ namespace IoTCenter.Controllers
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
this._pageHubContext.Clients.Group(node).SendAsync(nameof(INodeService.Sence), connectionId, id);
|
|
|
|
|
this._nodeService.Sence(id);
|
|
|
|
|
return Json(ApiResponse.AsyncSuccess());
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
@ -117,5 +128,37 @@ namespace IoTCenter.Controllers
|
|
|
|
|
return Json(ApiResponse.Error(ex));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IActionResult Data(Guid id, string time = "10m")
|
|
|
|
|
{
|
|
|
|
|
var device = this._deviceRepo.ReadOnlyTable().Include(o => o.Node).Include(o => o.Data).FirstOrDefault(o => o.Id == id);
|
|
|
|
|
var url = this._configuration["influxdb:url"];
|
|
|
|
|
var usr = this._configuration["influxdb:usr"];
|
|
|
|
|
var pwd = this._configuration["influxdb:pwd"];
|
|
|
|
|
var dbName = "iot";
|
|
|
|
|
var measurementName = "data";
|
|
|
|
|
var list = new List<object>();
|
|
|
|
|
using (var client = new InfluxClient(new Uri(url), usr, pwd))
|
|
|
|
|
{
|
|
|
|
|
var fileds = String.Join(',', device.Data.Where(o => o.Type == "Int" || o.Type == "Float").Select(o => o.Key));
|
|
|
|
|
var query = $"select {fileds} 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>();
|
|
|
|
|
foreach (var data in device.Data.Where(o => o.Type == "Int" || o.Type == "Float"))
|
|
|
|
|
{
|
|
|
|
|
list.Add(new
|
|
|
|
|
{
|
|
|
|
|
id = data.Key,
|
|
|
|
|
label = data.Name,
|
|
|
|
|
labels,
|
|
|
|
|
data = rows?.Select(o => o.GetField(data.Key)).ToList() ?? new List<object>()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Json(list);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|