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.
112 lines
4.9 KiB
112 lines
4.9 KiB
using Application.Domain.Entities;
|
|
using Application.Models;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System;
|
|
using System.Linq;
|
|
using Vibrant.InfluxDB.Client;
|
|
using Vibrant.InfluxDB.Client.Rows;
|
|
|
|
namespace IoT.Shared.Areas.Admin.Controlls
|
|
{
|
|
[Authorize]
|
|
[Area(nameof(Admin))]
|
|
public class DataController : SharedController<Data, SearchDataModel, EditDataModel, EditDataModel>
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
private readonly IRepository<Node> _nodeRepo;
|
|
private readonly AjaxController _ajax;
|
|
|
|
public DataController(IConfiguration configuration, IRepository<Node> nodeRepo, IRepository<Data> repo, AjaxController ajax, IServiceProvider sp) : base(repo, sp)
|
|
{
|
|
this._configuration = configuration;
|
|
this._nodeRepo = nodeRepo;
|
|
this._ajax = ajax;
|
|
}
|
|
|
|
public override IQueryable<Data> Include(IQueryable<Data> query)
|
|
{
|
|
return query.Include(o => o.Device).ThenInclude(o => o.Node);
|
|
}
|
|
|
|
public override IQueryable<Data> Query(SearchDataModel model, IQueryable<Data> query)
|
|
{
|
|
ViewData.SelectList(o => model.NodeId, () => this._ajax.GetNodeSelectList(model.NodeId));
|
|
ViewData.SelectList(o => model.DeviceId, () => this._ajax.GetDeviceSelectList(model.NodeId.Value, model.DeviceId), model.NodeId.HasValue);
|
|
return query.WhereIf(model.NodeId.HasValue, o => o.Device.NodeId == model.NodeId.Value)
|
|
.WhereIf(model.DeviceId.HasValue, o => o.DeviceId == model.DeviceId.Value)
|
|
.WhereIf(!string.IsNullOrEmpty(model.Keyword), o => o.Name.Contains(model.Keyword));
|
|
}
|
|
|
|
public override void ToDisplayModel(Data entity, EditDataModel model)
|
|
{
|
|
model.NodeId = entity?.Device?.NodeId;
|
|
ViewData.Add(model.DeviceId, entity.Device.Name);
|
|
ViewData.Add(model.NodeId, entity.Device.Node.Name);
|
|
}
|
|
|
|
public override void ToEditModel(Data entity, EditDataModel model)
|
|
{
|
|
if (entity != null)
|
|
{
|
|
this.ToDisplayModel(entity, model);
|
|
}
|
|
ViewData.SelectList(o => model.NodeId, () => this._ajax.GetNodeSelectList(model.NodeId));
|
|
ViewData.SelectList(o => model.DeviceId, () => this._ajax.GetDeviceSelectList(model.NodeId.Value, model.DeviceId), model.NodeId.HasValue);
|
|
}
|
|
|
|
public override string GetNodeNumber(EditDataModel model)
|
|
{
|
|
return this._nodeRepo.ReadOnlyTable().Where(o => o.Id == model.NodeId).Select(o => o.Number).FirstOrDefault();
|
|
}
|
|
|
|
[HttpGet]
|
|
public IActionResult DataHistory(Guid id, SearchDataHistoryModel model)
|
|
{
|
|
var data = this.Repo.ReadOnlyTable()
|
|
.Include(o => o.Device).ThenInclude(o => o.Node)
|
|
.Include(o => o.Device).ThenInclude(o => o.Product)
|
|
.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";
|
|
using (var client = new InfluxClient(new Uri(url), usr, pwd))
|
|
{
|
|
var parameters = new
|
|
{
|
|
start = model.Start,
|
|
end = model.End.AddDays(1)
|
|
};
|
|
var query = $"from {measurementName} where time>=$start and time<$end and DeviceNumber = '{data.Device.Number}'";
|
|
|
|
var dataQuery = $"select {data.Key} {query} limit {model.PageSize} offset {(model.PageIndex - 1) * model.PageSize}";
|
|
var result = client.ReadAsync<DynamicInfluxRow>(dbName, dataQuery, parameters: parameters).Result;
|
|
var rows = result.Results.FirstOrDefault()?
|
|
.Series.FirstOrDefault()?
|
|
.Rows;
|
|
model.List.AddRange(rows.Select(o => new DisplayDataHistoryModel
|
|
{
|
|
DeviceName = data.Device.DisplayName,
|
|
DeviceNumber = data.Device.Number,
|
|
Name = data.Name,
|
|
Unit = data.Unit,
|
|
Value = o.GetField(data.Key).ToString(),
|
|
Date = o.Timestamp
|
|
}));
|
|
|
|
var countQuery = $"select count({data.Key}) {query}";
|
|
var countResult = client.ReadAsync<DynamicInfluxRow>(dbName, countQuery, parameters: parameters).Result;
|
|
model.TotalCount = Convert.ToInt32(countResult.Results.FirstOrDefault().Series.FirstOrDefault().Rows.FirstOrDefault().GetField("count"));
|
|
|
|
ViewBag.HtmlTitle = data.Name;
|
|
return View(model);
|
|
}
|
|
}
|
|
}
|
|
} |