|
|
|
@ -25,6 +25,7 @@ namespace IoTCenter.Controllers
|
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
|
private readonly IJwtHelper _jwtHelper;
|
|
|
|
|
private readonly IRepository<Node> _nodeRepo;
|
|
|
|
|
private readonly IRepository<Scene> _sceneRepo;
|
|
|
|
|
private readonly IRepository<Device> _deviceRepo;
|
|
|
|
|
private readonly IRepository<LiveRecord> _liveRecordRepo;
|
|
|
|
|
private readonly IHubContext<PageHub> _pageHubContext;
|
|
|
|
@ -32,6 +33,7 @@ namespace IoTCenter.Controllers
|
|
|
|
|
public AppController(IConfiguration configuration,
|
|
|
|
|
IJwtHelper jwtHelper,
|
|
|
|
|
IRepository<Node> nodeRepo,
|
|
|
|
|
IRepository<Scene> sceneRepo,
|
|
|
|
|
IRepository<Device> deviceRepo,
|
|
|
|
|
IRepository<LiveRecord> liveRecordRepo,
|
|
|
|
|
IHubContext<PageHub> pageHubContext)
|
|
|
|
@ -39,6 +41,7 @@ namespace IoTCenter.Controllers
|
|
|
|
|
this._configuration = configuration;
|
|
|
|
|
this._jwtHelper = jwtHelper;
|
|
|
|
|
this._nodeRepo = nodeRepo;
|
|
|
|
|
this._sceneRepo = sceneRepo;
|
|
|
|
|
this._deviceRepo = deviceRepo;
|
|
|
|
|
this._liveRecordRepo = liveRecordRepo;
|
|
|
|
|
this._pageHubContext = pageHubContext;
|
|
|
|
@ -133,11 +136,12 @@ namespace IoTCenter.Controllers
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IActionResult Sence(string connectionId, string node, Guid id)
|
|
|
|
|
public IActionResult Scene(string connectionId, string node, string name)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
this._pageHubContext.Clients.Group(node).SendAsync(nameof(INodeService.Scene), connectionId, id);
|
|
|
|
|
var scene = this._sceneRepo.ReadOnlyTable().FirstOrDefault(o => o.Node.Number == node && o.Name == name);
|
|
|
|
|
this._pageHubContext.Clients.Group(node).SendAsync(Methods.ServerToClient, Methods.CallScene, name, null);
|
|
|
|
|
return Json(ApiResponse.AsyncSuccess());
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
@ -240,6 +244,18 @@ namespace IoTCenter.Controllers
|
|
|
|
|
return Json(ApiResponse.AsyncSuccess());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IActionResult NodePowerOn(string connectionId, string node)
|
|
|
|
|
{
|
|
|
|
|
this.Power(connectionId, new string[] { node }, "On", o => o.Name.Contains("开关") || o.Name.Contains("插座"));
|
|
|
|
|
return Json(ApiResponse.AsyncSuccess());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IActionResult NodePowerOff(string connectionId, string node)
|
|
|
|
|
{
|
|
|
|
|
this.Power(connectionId, new string[] { node }, "Off", o => o.Name.Contains("开关") || o.Name.Contains("插座"));
|
|
|
|
|
return Json(ApiResponse.AsyncSuccess());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Power(string connectionId, string[] nodes, string command, Func<Device, bool> func)
|
|
|
|
|
{
|
|
|
|
|
var devices = this._deviceRepo.ReadOnlyTable()
|
|
|
|
@ -250,9 +266,16 @@ namespace IoTCenter.Controllers
|
|
|
|
|
.ToList();
|
|
|
|
|
foreach (var device in devices)
|
|
|
|
|
{
|
|
|
|
|
var api = device.Product.Apis.FirstOrDefault(o => o.Command == command);
|
|
|
|
|
var message = $"{api.Path}{api.Command}?number={device.Number}";
|
|
|
|
|
this._pageHubContext.Clients.Group(device.Node.Number).SendAsync(Methods.ServerToClient, Methods.CallApi, message, connectionId);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var api = device.Product.Apis.FirstOrDefault(o => o.Command == command);
|
|
|
|
|
var message = $"{api.Path}{api.Command}?number={device.Number}";
|
|
|
|
|
this._pageHubContext.Clients.Group(device.Node.Number).SendAsync(Methods.ServerToClient, Methods.CallApi, message, connectionId);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ex.PrintStack();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|