using Infrastructure.Application; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using IoT.Shared.Application.Domain.Entities; using IoT.Shared.Application.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Platform.Application.Models.IoTCenter; using Platform.Areas.IoTCenter.Controllers; using System; using System.Linq; using Vibrant.InfluxDB.Client; using Vibrant.InfluxDB.Client.Rows; namespace IoT.Shared.Areas.IoTCenter.Controlls { [Authorize] [ApiController] [Route("IoTCenter/[controller]/[action]")] [Area("IoTCenter")] [ControllerScope(ControllerScopeType.Platform)] public class IoTDataController : IoTSharedController { private readonly IConfiguration _configuration; private readonly IRepository _nodeRepo; private readonly AjaxController _ajax; private readonly IRepository _categoryRepo; private readonly IRepository _organRepo; private readonly IRepository _buildingRepo; public IoTDataController(IConfiguration configuration, IRepository nodeRepo, IRepository repo, AjaxController ajax, IRepository categoryRepo, IRepository organRepo, IRepository buildingRepo, IServiceProvider sp) : base(repo, sp) { this._configuration = configuration; this._nodeRepo = nodeRepo; this._ajax = ajax; this._categoryRepo = categoryRepo; this._organRepo = organRepo; this._buildingRepo = buildingRepo; } public override IQueryable Include(IQueryable query) { return query.Include(o => o.Device).ThenInclude(o => o.Node).ThenInclude(o => o.Building).ThenInclude(o => o.Organ) .Include(o => o.Device).ThenInclude(o => o.Product).ThenInclude(o => o.Category); } public override IQueryable Query(PagedListModel model, IQueryable query) { return query.WhereIf(model.Query.NodeId.HasValue, o => o.Device.NodeId == model.Query.NodeId.Value) .WhereIf(model.Query.OrganId.HasValue, o => o.Device.Node.Building.OrganId == model.Query.OrganId.Value) .WhereIf(model.Query.BuildingId.HasValue, o => o.Device.Node.BuildingId == model.Query.BuildingId.Value) .WhereIf(model.Query.NodeId.HasValue, o => o.Device.NodeId == model.Query.NodeId.Value) .WhereIf(model.Query.CategoryId.HasValue, o => o.Device.Product.CategoryId == model.Query.CategoryId.Value) .WhereIf(model.Query.ProductId.HasValue, o => o.Device.ProductId == model.Query.ProductId.Value) .WhereIf(model.Query.DeviceId.HasValue, o => o.DeviceId == model.Query.DeviceId.Value) .WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name)) .WhereIf(!string.IsNullOrEmpty(model.Query.Key), o => o.Key.Contains(model.Query.Key)) .WhereIf(!string.IsNullOrEmpty(model.Query.Value), o => o.Value.Contains(model.Query.Value)) .WhereIf(model.Query.Type.HasValue, o => o.Type == model.Query.Type.Value) .WhereIf(!string.IsNullOrEmpty(model.Query.Unit), o => o.Unit.Contains(model.Query.Unit)) .WhereIf(!string.IsNullOrEmpty(model.Query.Description), o => o.Description.Contains(model.Query.Description)) .WhereIf(model.Query.Timestamp.HasValue, o => o.Timestamp == model.Query.Timestamp.Value) .WhereIf(model.Query.Hidden.HasValue, o => o.Hidden == model.Query.Hidden.Value) .OrderBy(o => o.Device.ProductId).ThenBy(o => o.Device.NodeId).ThenBy(o => o.DeviceId); } public override void ToDisplayModel(IoTData entity, EditPlatformIoTDataModel model) { if (entity != null) { model.ProductId = entity.Device.ProductId; model.CategoryId = entity.Device.Product.CategoryId; model.NodeId = entity.Device.NodeId; model.BuildingId = entity.Device.Node.BuildingId; model.OrganId = entity.Device.Node.Building.OrganId; } if (model.BuildingId.HasValue) { var name = this._buildingRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.Device.Node.Building.Left && o.Right >= entity.Device.Node.Building.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == model.BuildingId.Value)?.GetDisplayName(); ViewData.Add(model.BuildingId.Value, name); } if (model.OrganId.HasValue) { var name = this._organRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.Device.Node.Building.Organ.Left && o.Right >= entity.Device.Node.Building.Organ.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == entity.Device.Node.Building.OrganId)?.GetDisplayName(); ViewData.Add(model.OrganId, name); } if (model.ProductId.HasValue) { ViewData.Add(model.ProductId, entity.Device.Product.Name); } if (model.CategoryId.HasValue) { var name = this._categoryRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.Device.Product.Category.Left && o.Right >= entity.Device.Product.Category.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == model.CategoryId.Value)?.GetDisplayName(); ViewData.Add(model.CategoryId, name); } if (model.DeviceId.HasValue) { ViewData.Add(model.DeviceId, entity.Device.Name); } if (model.NodeId.HasValue) { ViewData.Add(model.NodeId, entity.Device.Node.Name); } } public override void ToEditModel(IoTData entity, EditPlatformIoTDataModel model) { if (entity != null) { model.BuildingId = entity.Device.Node.BuildingId; model.OrganId = entity.Device.Node.Building.OrganId; model.CategoryId = entity.Device.Product.CategoryId; model.BuildingId = entity.Device.Node.BuildingId; model.OrganId = entity.Device.Node.Building.OrganId; } ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList()); ViewData.SelectList(o => model.BuildingId, () => this._ajax.GetBuilding(model.OrganId.Value, model.BuildingId).SelectList(), model.OrganId.HasValue); ViewData.SelectList(o => model.NodeId, () => this._ajax.GetIoTGatewayByBuilding(model.BuildingId.Value, model.NodeId).SelectList(), model.BuildingId.HasValue); ViewData.SelectList(o => model.DeviceId, () => this._ajax.GetIoTDevice(model.NodeId.Value, model.DeviceId).SelectList(), model.NodeId.HasValue); ViewData.SelectList(o => model.CategoryId, () => this._ajax.GetIoTProductCategory(model.CategoryId).SelectList()); ViewData.SelectList(o => model.ProductId, () => this._ajax.GetIoTProductByCategory(model.CategoryId.Value, model.ProductId).SelectList(), model.CategoryId.HasValue); } public override string GetNodeNumber(EditPlatformIoTDataModel 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(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(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); } } }