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.
iot/projects/IoTNode/Areas/IoTCenter/Controllers/IoTDataController.cs

67 lines
3.0 KiB

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 System.Linq;
namespace IoT.Shared.Areas.IoTCenter.Controlls
{
[Authorize]
[ApiController]
[Route("IoTCenter/[controller]/[action]")]
[Area("IoTCenter")]
[ControllerScope(ControllerScopeType.Platform)]
public class IoTDataController : CrudController<IoTData, EditDataModel>
{
private readonly AjaxBaseController _ajax;
public IoTDataController(IRepository<IoTData> repo, AjaxBaseController ajax) : base(repo)
{
this._ajax = ajax;
}
public override IQueryable<IoTData> Include(IQueryable<IoTData> query)
{
return query.Include(o => o.Device).ThenInclude(o => o.Node);
}
public override IQueryable<IoTData> Query(PagedListModel<EditDataModel> model, IQueryable<IoTData> query)
{
return query.WhereIf(model.Query.NodeId.HasValue, o => o.Device.NodeId == model.Query.NodeId.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, 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(IoTData entity, EditDataModel model)
{
model.NodeId = entity?.Device?.NodeId;
ViewData.SelectList(o => model.NodeId, () => this._ajax.GetIoTGateway(model.NodeId).SelectList());
if (model.NodeId.HasValue)
{
ViewData.SelectList(o => model.DeviceId, () => this._ajax.GetIoTDevice(model.NodeId.Value, model.DeviceId).SelectList());
}
}
}
}