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/IoTCenter/Areas/Admin/Controllers/IoT/SceneCommandController.cs

111 lines
4.6 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.SignalR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
namespace IoTCenter.Areas.Admin.Controllers
{
[Authorize]
[Area(nameof(Admin))]
public class SceneCommandController : CrudController<SceneCommand, PagedListModel<EditSceneCommandModel>, EditSceneCommandModel, EditSceneCommandModel>
{
private readonly IRepository<Node> _nodeRepo;
private readonly IRepository<Scene> _sceneRepo;
private readonly IRepository<Api> _apiRepo;
private readonly IRepository<Device> _deviceRepo;
private readonly IRepository<SceneCommand> _repo;
private readonly AjaxController _ajax;
private readonly IHubContext<PageHub> _pageHubContext;
public SceneCommandController(IRepository<Node> nodeRepo, IRepository<Scene> sceneRepo, IRepository<Api> apiRepo, IRepository<Device> deviceRepo, IRepository<SceneCommand> 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 IQueryable<SceneCommand> Include(IQueryable<SceneCommand> query)
{
return query.Include(o => o.Scene).ThenInclude(o => o.Node).Include(o => o.Command).ThenInclude(o => o.Device);
}
public override IQueryable<SceneCommand> Query(PagedListModel<EditSceneCommandModel> model, IQueryable<SceneCommand> query)
{
return query.Where(o => o.Scene.NodeId != null);
}
public override void ToModel(SceneCommand entity, EditSceneCommandModel model)
{
if (entity != null)
{
model.NodeId = entity.Scene.NodeId;
}
ViewData.SelectList(o => model.NodeId, () => this._ajax.GetNodeSelectList(model.SceneId));
ViewData.SelectList(o => model.SceneId, () => this._ajax.GetSceneSelectList(model.NodeId.Value, model.SceneId), model.NodeId.HasValue);
ViewData.SelectList(o => model.CommandId, () => this._ajax.GetCommandSelectList(model.NodeId.Value, model.CommandId), model.NodeId.HasValue);
}
public override IActionResult Add(EditSceneCommandModel model)
{
if (ModelState.IsValid)
{
var number = this._nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.NodeId).Number;
this._pageHubContext.Clients.Group(number).SendAsync(Methods.ServerToClient, Methods.EditSceneCommandRequest, model.ToJson(), null);
return RedirectTo();
}
ModelState.AddModelError("", "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB3A3><EFBFBD><EFBFBD><EFBFBD>Ժ<EFBFBD><D4BA><EFBFBD><EFBFBD><EFBFBD>");
return View(model);
}
public override IActionResult Edit(EditSceneCommandModel model)
{
if (ModelState.IsValid)
{
var number = this._nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.NodeId).Number;
this._pageHubContext.Clients.Group(number).SendAsync(Methods.ServerToClient, Methods.EditSceneCommandRequest, model.ToJson(), null);
return RedirectTo();
}
ModelState.AddModelError("", "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB3A3><EFBFBD><EFBFBD><EFBFBD>Ժ<EFBFBD><D4BA><EFBFBD><EFBFBD><EFBFBD>");
return View(model);
}
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.Scene).ThenInclude(o => o.Node).FirstOrDefault(o => o.Id == id);
var model = entity.To<EditSceneCommandModel>();
this._pageHubContext.Clients.Group(entity.Scene.Node.Number).SendAsync(Methods.ServerToClient, Methods.DeleteSceneCommandRequest, model.ToJson(), null);
}
catch (Exception ex)
{
ex.PrintStack();
return RedirectTo(rawMesage: ex.Message);
}
}
return RedirectTo();
}
}
}