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.
173 lines
5.6 KiB
173 lines
5.6 KiB
using Application.Domain.Entities;
|
|
using Application.Models;
|
|
using Infrastructure.Application.Services.Settings;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using IoTCenter.Application.Domain;
|
|
using IoTCenter.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
|
|
namespace IoTCenter.Api.Controllers
|
|
{
|
|
[ApiVersion("1.0")]
|
|
[Route("api/v{version:apiVersion}/[controller]/[action]")]
|
|
[ApiController]
|
|
public class NodeController : ControllerBase
|
|
{
|
|
private readonly ILogger<NodeController> _logger;
|
|
private readonly ISettingService _settingService;
|
|
private readonly IRepository<NodeCategoryNode> _nodeCategoryNodeRepo;
|
|
private readonly IRepository<Node> _nodeRepo;
|
|
private readonly IRepository<Device> _deviceRepo;
|
|
private readonly IHubContext<IoTCenterHub> _hub;
|
|
|
|
public NodeController(
|
|
ILogger<NodeController> logger,
|
|
ISettingService settingService,
|
|
IRepository<NodeCategoryNode> nodeCategoryNodeRepo,
|
|
IRepository<Node> nodeRepo,
|
|
IRepository<Device> deviceRepo,
|
|
IHubContext<IoTCenterHub> hub)
|
|
{
|
|
this._logger = logger;
|
|
this._settingService = settingService;
|
|
this._nodeCategoryNodeRepo = nodeCategoryNodeRepo;
|
|
this._nodeRepo = nodeRepo;
|
|
this._deviceRepo = deviceRepo;
|
|
this._hub = hub;
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult GetNodes()
|
|
{
|
|
try
|
|
{
|
|
var model = this._nodeRepo.ReadOnlyTable()
|
|
.Where(o => !o.Hidden)
|
|
.OrderBy(o => o.Name)
|
|
.ToList()
|
|
.Select(o => new
|
|
{
|
|
o.Id,
|
|
o.Name,
|
|
o.Number,
|
|
o.IsOnline,
|
|
o.Image,
|
|
o.DisplayOrder,
|
|
Count = _deviceRepo.ReadOnlyTable().Count(d => d.NodeId == o.Id),
|
|
Category = _nodeCategoryNodeRepo.ReadOnlyTable().Include(ncn => ncn.Category).FirstOrDefault(ncn => ncn.NodeId == o.Id)?.Category
|
|
});
|
|
return Ok(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Problem(ex.Message);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult GetNode([Required] string number)
|
|
{
|
|
try
|
|
{
|
|
var model = this._nodeRepo.ReadOnlyTable()
|
|
.Include(o => o.Scenes)
|
|
.Include(o => o.Devices).ThenInclude(o => o.Product)
|
|
.Include(o => o.Devices).ThenInclude(o => o.Data)
|
|
.Where(o => o.Number == number)
|
|
.FirstOrDefault();
|
|
model.Scenes = model.Scenes.Where(o => !o.Hidden).OrderBy(o => o.DisplayOrder).ToList();
|
|
return Ok(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Problem(ex.Message);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult PowerOn([FromBody][Required] string number)
|
|
{
|
|
return this.Power(number, "On");
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult PowerOff([FromBody][Required] string number)
|
|
{
|
|
return this.Power(number, "Off");
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult Upload([FromBody][Required] string number)
|
|
{
|
|
this._hub.ServerToClient(Methods.UploadNode, "", number);
|
|
return Ok();
|
|
}
|
|
|
|
public IActionResult Stop([FromBody][Required] string number)
|
|
{
|
|
this._hub.ServerToClient(Methods.StopNode, "", number);
|
|
return Ok();
|
|
}
|
|
|
|
private ActionResult Power(string number, string command)
|
|
{
|
|
var devices = this._deviceRepo.ReadOnlyTable()
|
|
.Include(o => o.Node)
|
|
.Include(o => o.Product)
|
|
.ThenInclude(o => o.Apis)
|
|
.Where(o => o.Node.Number == number)
|
|
.Where(o => o.Name.Contains("开关") || o.Name.Contains("插座"))
|
|
.ToList();
|
|
foreach (var device in devices)
|
|
{
|
|
try
|
|
{
|
|
var api = device.Product.Apis.FirstOrDefault(o => o.Command == command);
|
|
if (api != null)
|
|
{
|
|
var message = $"{api.Path}{api.Command}?number={device.Number}";
|
|
this._hub.ServerToClient(Methods.ExecApiRequest, message, device.Node.Number, null);
|
|
Delay(0);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
}
|
|
}
|
|
|
|
return Ok();
|
|
}
|
|
|
|
private void Delay(int commandDelay)
|
|
{
|
|
var delay = 0;
|
|
try
|
|
{
|
|
delay = Convert.ToInt32(this._settingService.GetSetting("delay").Value);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this._logger.LogError(ex.ToString());
|
|
}
|
|
if (commandDelay > 0)
|
|
{
|
|
delay += commandDelay;
|
|
}
|
|
if (delay > 0)
|
|
{
|
|
Thread.Sleep(delay);
|
|
}
|
|
}
|
|
}
|
|
} |