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.
182 lines
8.1 KiB
182 lines
8.1 KiB
using Application.Domain.Entities;
|
|
using Application.Models;
|
|
using Infrastructure.Application;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Infrastructure.Web.Mvc;
|
|
using IoTCenter.Services;
|
|
using IoTShared.Controllers;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace IoTCenter.Areas.Admin.Controllers
|
|
{
|
|
[Authorize]
|
|
[Area(nameof(Admin))]
|
|
public class CommandController : CrudController<Command, PagedListModel<DisplayCommandModel>, DisplayCommandModel, EditCommandModel>
|
|
{
|
|
private readonly IRepository<Node> _nodeRepo;
|
|
private readonly IRepository<Scene> _sceneRepo;
|
|
private readonly IRepository<Api> _apiRepo;
|
|
private readonly IRepository<Device> _deviceRepo;
|
|
private readonly IRepository<Command> _repo;
|
|
private readonly AjaxController _ajax;
|
|
private readonly IHubContext<PageHub> _pageHubContext;
|
|
|
|
public CommandController(IRepository<Node> nodeRepo, IRepository<Scene> sceneRepo, IRepository<Api> apiRepo, IRepository<Device> deviceRepo, IRepository<Command> repo, AjaxController ajax, IHubContext<PageHub> pageHubContext) : base(repo)
|
|
{
|
|
this._repo = repo;
|
|
this._nodeRepo = nodeRepo;
|
|
this._sceneRepo = sceneRepo;
|
|
this._apiRepo = apiRepo;
|
|
this._deviceRepo = deviceRepo;
|
|
this._ajax = ajax;
|
|
this._pageHubContext = pageHubContext;
|
|
}
|
|
|
|
public override IActionResult Add(EditCommandModel model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
var number = this._nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.NodeId).Number;
|
|
model.QueryString = model.GetQueryString();
|
|
model.NodeNumber = this._nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.NodeId).Number;
|
|
model.DeviceNumber = this._deviceRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.DeviceId).Number;
|
|
model.ApiName = this._apiRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.ApiId).Name;
|
|
this._pageHubContext.Clients.Group(number).SendAsync(Methods.ServerToClient, Methods.EditCommandRequest, model.ToJson(), null);
|
|
return RedirectTo();
|
|
}
|
|
ModelState.AddModelError("", "服务器出现异常,请稍后重试");
|
|
return View(model);
|
|
}
|
|
|
|
public override IActionResult Edit(EditCommandModel model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
var number = this._nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.NodeId).Number;
|
|
model.QueryString = model.GetQueryString();
|
|
model.NodeNumber = this._nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.NodeId).Number;
|
|
model.DeviceNumber = this._deviceRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.DeviceId).Number;
|
|
model.ApiName = this._apiRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.ApiId).Name;
|
|
this._pageHubContext.Clients.Group(number).SendAsync(Methods.ServerToClient, Methods.EditCommandRequest, model.ToJson(), null);
|
|
return RedirectTo();
|
|
}
|
|
|
|
ModelState.AddModelError("", "服务器出现异常,请稍后重试");
|
|
return View(model);
|
|
}
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")]
|
|
public override IActionResult Delete(List<Guid> list)
|
|
{
|
|
if (list is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(list));
|
|
}
|
|
|
|
foreach (var id in list)
|
|
{
|
|
try
|
|
{
|
|
var entity = this._repo.ReadOnlyTable().Include(o => o.Device).ThenInclude(o => o.Node).FirstOrDefault(o => o.Id == id);
|
|
var model = entity.To<EditCommandModel>();
|
|
this._pageHubContext.Clients.Group(entity.Device.Node.Number).SendAsync(Methods.ServerToClient, Methods.DeleteCommandRequest, model.ToJson(), null);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return RedirectTo(rawMesage: ex.Message);
|
|
}
|
|
}
|
|
return RedirectTo();
|
|
}
|
|
|
|
public IActionResult Api(Guid apiId, Guid deviceId)
|
|
{
|
|
var model = this.GetParameters(apiId, deviceId, null);
|
|
return PartialView("_Api", model);
|
|
}
|
|
|
|
public override IQueryable<Command> Include(IQueryable<Command> query)
|
|
{
|
|
return query.Include(o => o.Device).ThenInclude(o => o.Node).Include(o => o.Api);
|
|
}
|
|
|
|
public override void ToDisplayModel(Command entity, DisplayCommandModel model)
|
|
{
|
|
model.NodeId = entity.Device.Node.Name;
|
|
model.DeviceId = entity.Device.DisplayName;
|
|
model.ApiId = entity.Api.Name;
|
|
}
|
|
|
|
public override void ToModel(Command entity, EditCommandModel model)
|
|
{
|
|
if (entity != null)
|
|
{
|
|
model.NodeId = entity.Device.NodeId;
|
|
model.DeviceId = entity.DeviceId;
|
|
model.ApiId = entity.Api.Id;
|
|
model.NodeNumber = entity.Device.Node.Number;
|
|
model.DeviceNumber = entity.Device.Number;
|
|
model.ApiName = entity.Api.Name;
|
|
}
|
|
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);
|
|
ViewData.SelectList(o => model.ApiId, () => this._ajax.GetApiSelectList(model.DeviceId.Value, model.ApiId), model.DeviceId.HasValue);
|
|
if (model.ApiId.HasValue)
|
|
{
|
|
model.Parameters = this.GetParameters(model.ApiId.Value, model.DeviceId.Value, entity.QueryString);
|
|
}
|
|
}
|
|
|
|
public override void ToEntity(EditCommandModel model, Command entity)
|
|
{
|
|
entity.QueryString = model.GetQueryString();
|
|
}
|
|
|
|
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);
|
|
ViewData["Number"] = device.Number;
|
|
var parameters = api.Parameters.Where(o => o.Description != "设备编号").ToList().Select(o => o.To<EditApiParameterModel>()).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;
|
|
}
|
|
}
|
|
}
|
|
if (device.Name == "红外转发器")
|
|
{
|
|
if (!api.Path.Contains("add") && !api.Path.Contains("get"))
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
} |