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.
103 lines
3.9 KiB
103 lines
3.9 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 TimerController : CrudController<IoTTimer, PagedListModel<IoTTimer>, IoTTimer, EditTimerModel>
|
|
{
|
|
private readonly IRepository<Node> _nodeRepo;
|
|
private readonly IRepository<IoTTimer> _repo;
|
|
private readonly AjaxController _ajax;
|
|
private readonly IHubContext<PageHub> _pageHubContext;
|
|
|
|
public TimerController(IRepository<Node> nodeRepo, IRepository<IoTTimer> repo, AjaxController ajax, IHubContext<PageHub> pageHubContext) : base(repo)
|
|
{
|
|
this._repo = repo;
|
|
this._nodeRepo = nodeRepo;
|
|
this._ajax = ajax;
|
|
this._pageHubContext = pageHubContext;
|
|
}
|
|
|
|
public override IQueryable<IoTTimer> Include(IQueryable<IoTTimer> query)
|
|
{
|
|
return query.Include(o => o.Node);
|
|
}
|
|
|
|
public override IQueryable<IoTTimer> Query(PagedListModel<IoTTimer> model, IQueryable<IoTTimer> query)
|
|
{
|
|
return query.Where(o => o.NodeId != null);
|
|
}
|
|
|
|
public override void ToDisplayModel(IoTTimer entity, IoTTimer model)
|
|
{
|
|
//model.NodeName = entity.Node.Name;
|
|
}
|
|
|
|
public override IActionResult Add(EditTimerModel model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
var number = this._nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.NodeId).Number;
|
|
model.NodeNumber = number;
|
|
this._pageHubContext.Clients.Group(number).SendAsync(Methods.ServerToClient, Methods.EditTimerRequest, model.ToJson(), null);
|
|
return RedirectTo();
|
|
}
|
|
ModelState.AddModelError("", "服务器出现异常,请稍后重试");
|
|
return View(model);
|
|
}
|
|
|
|
public override IActionResult Edit(EditTimerModel model)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
var number = this._nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.NodeId).Number;
|
|
model.NodeNumber = number;
|
|
this._pageHubContext.Clients.Group(number).SendAsync(Methods.ServerToClient, Methods.EditTimerRequest, model.ToJson(), null);
|
|
return RedirectTo();
|
|
}
|
|
ModelState.AddModelError("", "服务器出现异常,请稍后重试");
|
|
return View(model);
|
|
}
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:不捕获常规异常类型", Justification = "<挂起>")]
|
|
public override IActionResult Delete(List<Guid> list)
|
|
{
|
|
foreach (var id in list)
|
|
{
|
|
try
|
|
{
|
|
var entity = this._repo.ReadOnlyTable().Include(o => o.Node).FirstOrDefault(o => o.Id == id);
|
|
var model = entity.To<EditTimerModel>();
|
|
model.NodeNumber = entity.Node.Number;
|
|
this._pageHubContext.Clients.Group(entity.Node.Number).SendAsync(Methods.ServerToClient, Methods.DeleteTimerRequest, model.ToJson(), null);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
}
|
|
}
|
|
return RedirectTo();
|
|
}
|
|
|
|
public override void ToModel(IoTTimer entity, EditTimerModel model)
|
|
{
|
|
ViewData.SelectList(o => model.NodeId, () => this._ajax.GetNodeSelectList(model.NodeId));
|
|
base.ToModel(entity, model);
|
|
}
|
|
}
|
|
} |