using Infrastructure.Application; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using 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.IoTDevice).ThenInclude(o => o.IoTGateway).ThenInclude(o => o.Building).ThenInclude(o => o.Organ) .Include(o => o.IoTDevice).ThenInclude(o => o.IoTProduct).ThenInclude(o => o.IoTProductCategory); } public override IQueryable Query(PagedListModel model, IQueryable query) { return query .WhereIf(model.Query.OrganId.HasValue, o => o.IoTDevice.IoTGateway.Building.OrganId == model.Query.OrganId.Value) .WhereIf(model.Query.BuildingId.HasValue, o => o.IoTDevice.IoTGateway.BuildingId == model.Query.BuildingId.Value) .WhereIf(model.Query.IoTGatewayId.HasValue, o => o.IoTDevice.IoTGatewayId == model.Query.IoTGatewayId.Value) .WhereIf(model.Query.CategoryId.HasValue, o => o.IoTDevice.IoTProduct.IoTProductCategoryId == model.Query.CategoryId.Value) .WhereIf(model.Query.IoTProductId.HasValue, o => o.IoTDevice.IoTProductId == model.Query.IoTProductId.Value) .WhereIf(model.Query.IoTDeviceId.HasValue, o => o.IoTDeviceId == model.Query.IoTDeviceId.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.DataType.HasValue, o => o.DataType == model.Query.DataType.Value) .WhereIf(model.Query.ValueType.HasValue, o => o.ValueType == model.Query.ValueType.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.IoTDevice.IoTProductId).ThenBy(o => o.IoTDevice.IoTGatewayId).ThenBy(o => o.IoTDeviceId); } public override void EntityToModel(IoTData entity, EditPlatformIoTDataModel model) { model.IoTProductId = entity.IoTDevice?.IoTProductId; model.CategoryId = entity.IoTDevice?.IoTProduct?.IoTProductCategoryId; model.IoTGatewayId = entity.IoTDevice?.IoTGatewayId; model.BuildingId = entity.IoTDevice?.IoTGateway?.BuildingId; model.OrganId = entity.IoTDevice?.IoTGateway?.Building?.OrganId; } public override void ToDisplayModel(IoTData entity, EditPlatformIoTDataModel model) { if (model.BuildingId.HasValue) { var name = this._buildingRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.IoTDevice.IoTGateway.Building.Left && o.Right >= entity.IoTDevice.IoTGateway.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.IoTDevice.IoTGateway.Building.Organ.Left && o.Right >= entity.IoTDevice.IoTGateway.Building.Organ.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == entity.IoTDevice.IoTGateway.Building.OrganId)?.GetDisplayName(); ViewData.Add(model.OrganId, name); } if (model.IoTProductId.HasValue) { ViewData.Add(model.IoTProductId, entity.IoTDevice.IoTProduct.Name); } if (model.CategoryId.HasValue) { var name = this._categoryRepo.ReadOnlyTable() .Where(o => o.ParentId != null) .Where(o => o.Left <= entity.IoTDevice.IoTProduct.IoTProductCategory.Left && o.Right >= entity.IoTDevice.IoTProduct.IoTProductCategory.Right) .ToList() .ToTree() .FirstOrDefault(o => o.Id == model.CategoryId.Value)?.GetDisplayName(); ViewData.Add(model.CategoryId, name); } if (model.IoTDeviceId.HasValue) { ViewData.Add(model.IoTDeviceId, entity.IoTDevice.Name); } if (model.IoTGatewayId.HasValue) { ViewData.Add(model.IoTGatewayId, entity.IoTDevice.IoTGateway.Name); } } public override void ToEditModel(IoTData entity, EditPlatformIoTDataModel model) { 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.IoTGatewayId, () => this._ajax.GetIoTGatewayByBuilding(model.BuildingId.Value, model.IoTGatewayId).SelectList(), model.BuildingId.HasValue); ViewData.SelectList(o => model.IoTDeviceId, () => this._ajax.GetIoTDevice(model.IoTGatewayId.Value, model.IoTDeviceId).SelectList(), model.IoTGatewayId.HasValue); ViewData.SelectList(o => model.CategoryId, () => this._ajax.GetIoTProductCategory(model.CategoryId).SelectList()); ViewData.SelectList(o => model.IoTProductId, () => this._ajax.GetIoTProductByCategory(model.CategoryId.Value, model.IoTProductId).SelectList(), model.CategoryId.HasValue); } public override string GetNodeNumber(EditPlatformIoTDataModel model) { return this._nodeRepo.ReadOnlyTable().Where(o => o.Id == model.IoTGatewayId).Select(o => o.Number).FirstOrDefault(); } [HttpGet] public IActionResult DataHistory(Guid id, SearchDataHistoryModel model) { var data = this.Repo.ReadOnlyTable() .Include(o => o.IoTDevice).ThenInclude(o => o.IoTGateway) .Include(o => o.IoTDevice).ThenInclude(o => o.IoTProduct) .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.IoTDevice.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.IoTDevice.DisplayName, DeviceNumber = data.IoTDevice.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); } } }