|
|
|
@ -1,7 +1,10 @@
|
|
|
|
|
using Application.Domain.Entities;
|
|
|
|
|
using Application.Models;
|
|
|
|
|
using Infrastructure.Data;
|
|
|
|
|
using Infrastructure.Extensions;
|
|
|
|
|
using IoTCenter.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using System;
|
|
|
|
@ -19,16 +22,19 @@ namespace UserCenter.Controllers
|
|
|
|
|
private readonly IRepository<Node> _nodeRepo;
|
|
|
|
|
private readonly IRepository<NodeCategory> _nodeCategoryRepo;
|
|
|
|
|
private readonly IRepository<Device> _deviceRepo;
|
|
|
|
|
private readonly IHubContext<IoTCenterHub> _hub;
|
|
|
|
|
|
|
|
|
|
public NodeController(IConfiguration cfg,
|
|
|
|
|
IRepository<Node> nodeRepo,
|
|
|
|
|
IRepository<NodeCategory> nodeCategoryRepo,
|
|
|
|
|
IRepository<Device> deviceRepo)
|
|
|
|
|
IRepository<Device> deviceRepo,
|
|
|
|
|
IHubContext<IoTCenterHub> hub)
|
|
|
|
|
{
|
|
|
|
|
this._cfg = cfg;
|
|
|
|
|
this._nodeRepo = nodeRepo;
|
|
|
|
|
this._nodeCategoryRepo = nodeCategoryRepo;
|
|
|
|
|
this._deviceRepo = deviceRepo;
|
|
|
|
|
this._hub = hub;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
@ -75,5 +81,46 @@ namespace UserCenter.Controllers
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ex.PrintStack();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|