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/Platform/Areas/IoTCenter/Controllers/IoTCommandController.cs

236 lines
10 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.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Platform.Areas.IoTCenter.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
namespace IoT.Shared.Areas.IoTCenter.Controlls
{
[Authorize]
[ApiController]
[Route("IoTCenter/[controller]/[action]")]
[Area("IoTCenter")]
[ControllerScope(ControllerScopeType.Platform)]
public class IoTCommandController : CrudController<IoTCommand, EditIoTCommandModel>
{
private readonly IRepository<Organ> _organRepo;
private readonly IRepository<Building> _buildingRepo;
private readonly IRepository<IoTGateway> _nodeRepo;
private readonly IRepository<IoTApi> _apiRepo;
private readonly IRepository<IoTDevice> _deviceRepo;
private readonly AjaxController _ajax;
public IoTCommandController(IRepository<Organ> organRepo,
IRepository<Building> buildingRepo,
IRepository<IoTGateway> nodeRepo,
IRepository<IoTCommand> repo,
IRepository<IoTApi> apiRepo,
IRepository<IoTDevice> deviceRepo,
AjaxController ajax) : base(repo)
{
this._organRepo = organRepo;
this._buildingRepo = buildingRepo;
this._nodeRepo = nodeRepo;
this._apiRepo = apiRepo;
this._deviceRepo = deviceRepo;
this._ajax = ajax;
}
public override IQueryable<IoTCommand> Include(IQueryable<IoTCommand> query)
{
return query
.Include(o => o.IoTDevice).ThenInclude(o => o.IoTGateway).ThenInclude(o => o.Building).ThenInclude(o => o.Organ)
.Include(o => o.Api);
}
public override IQueryable<IoTCommand> Query(PagedListModel<EditIoTCommandModel> model, IQueryable<IoTCommand> 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.DeviceId.HasValue, o => o.DeviceId == model.Query.DeviceId.Value)
.WhereIf(model.Query.ApiId.HasValue, o => o.ApiId == model.Query.ApiId.Value)
.WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name))
.WhereIf(!string.IsNullOrEmpty(model.Query.QueryString), o => o.QueryString.Contains(model.Query.QueryString))
.WhereIf(model.Query.Disabled.HasValue, o => o.Disabled == model.Query.Disabled.Value)
.WhereIf(model.Query.Delay.HasValue, o => o.Delay == model.Query.Delay.Value);
}
[HttpGet]
public IActionResult Ajax(Guid apiId, Guid deviceId)
{
var model =
new EditIoTCommandModel
{
Parameters = this.GetParameters(apiId, deviceId, null)
};
//if (this.IsJsonRequest())
//{
// return Json(new
// {
// schema = this.GetJsonSchema<EditApiParameterModel>(),
// model,
// errors = ModelState.Where(o => o.Value.ValidationState == ModelValidationState.Invalid),
// data = ViewData
// }, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(), ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
//}
return PartialView("_Ajax", model);
}
[HttpGet]
public IActionResult Code(string name, int type, int code, Guid deviceId)
{
var model = new EditIRCodeViewModel
{
InputName = name,
Name = "指令",
Type = type,
Code = code,
Buttons = this._deviceRepo.ReadOnlyTable().Include(o => o.Data).FirstOrDefault(o => o.Id == deviceId).Data.FirstOrDefault(o => o.Name == "指令").Value
};
return PartialView("_Code", model);
}
public override void ToDisplayModel(IoTCommand entity, EditIoTCommandModel model)
{
if (entity != null)
{
model.IoTGatewayId = entity.IoTDevice?.IoTGatewayId;
model.BuildingId = entity.IoTDevice?.IoTGateway?.BuildingId;
model.OrganId = entity.IoTDevice?.IoTGateway?.Building?.OrganId;
}
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.IoTGatewayId.HasValue)
{
ViewData.Add(model.IoTGatewayId, entity.IoTDevice.IoTGateway.Name);
}
if (model.DeviceId.HasValue)
{
ViewData.Add(model.DeviceId, entity.IoTDevice.Name);
}
if (model.ApiId.HasValue)
{
ViewData.Add(model.ApiId, entity.Api.Name);
}
}
public override void ToEditModel(IoTCommand entity, EditIoTCommandModel model)
{
if (entity != null)
{
model.IoTGatewayId = entity.IoTDevice?.IoTGatewayId;
model.BuildingId = entity.IoTDevice?.IoTGateway?.BuildingId;
model.OrganId = entity.IoTDevice?.IoTGateway?.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.IoTGatewayId, () => this._ajax.GetIoTGatewayByBuilding(model.BuildingId.Value, model.IoTGatewayId).SelectList(), model.BuildingId.HasValue);
ViewData.SelectList(o => model.DeviceId, () => this._ajax.GetIoTDeviceForCommand(model.IoTGatewayId.Value, model.DeviceId).SelectList(), model.IoTGatewayId.HasValue);
ViewData.SelectList(o => model.ApiId, () => this._ajax.GetIoTApiByDevice(model.DeviceId.Value, model.ApiId).SelectList(), model.DeviceId.HasValue);
if (model.ApiId.HasValue)
{
model.Parameters = this.GetParameters(model.ApiId.Value, model.DeviceId.Value, model.QueryString);
}
}
public override void ToEntity(EditIoTCommandModel model, IoTCommand entity)
{
entity.QueryString = model.GetQueryString();
if (!model.Delay.HasValue)
{
model.Delay = default(int);
}
if (!model.Disabled.HasValue)
{
model.Disabled = default(bool);
}
}
private List<EditApiParameterModel> GetParameters(Guid apiId, Guid deviceId, string queryString)
{
var api = this._apiRepo.ReadOnlyTable().Include(o => o.Parameters).FirstOrDefault(o => o.Id == apiId);
var device = this._deviceRepo.ReadOnlyTable().Include(o => o.Data).FirstOrDefault(o => o.Id == deviceId);
ViewBag.Device = device;
var parameters = api.Parameters.ToList().Select(o => new EditApiParameterModel
{
Type = o.Type,
Name = o.Name,
Description = o.Description,
Required = o.Required,
Minimum = o.Minimum,
Maxinum = o.Minimum,
}).ToList();
if (!string.IsNullOrEmpty(queryString))
{
foreach (var item in queryString.Split('&'))
{
var array = item.Split('=');
var name = array[0];
var vlaue = array[1];
var param = parameters.FirstOrDefault(o => o.Name == name);
if (param != null)
{
param.Value = vlaue;
}
}
}
var numberParameter = parameters.FirstOrDefault(o => o.Name == "number");
if (numberParameter != null && string.IsNullOrEmpty(numberParameter.Value))
{
numberParameter.Value = device.Number;
}
if (device.Name == "红外转发器")
{
if (!api.Path.Contains("add", StringComparison.OrdinalIgnoreCase) && !api.Path.Contains("get", StringComparison.OrdinalIgnoreCase))
{
foreach (var item in parameters)
{
if (item.Name == "code")
{
var list = device.Data.Where(o => o.Name != "状态").ToList()
.Select(o => new SelectListItem { Text = o.Name, Value = o.Value });
item.SelectList = new SelectList(list, "Value", "Text", item.Value);
}
}
}
}
return parameters;
}
}
}