diff --git a/projects/IoT/IoT.Shared/Application/Models/CallApiRequest.cs b/projects/IoT/IoT.Shared/Application/Models/CallApiRequest.cs index 2b514c87..55204b61 100644 --- a/projects/IoT/IoT.Shared/Application/Models/CallApiRequest.cs +++ b/projects/IoT/IoT.Shared/Application/Models/CallApiRequest.cs @@ -2,10 +2,9 @@ { public class CallApiRequest { - public string Gateway { get; set; } + public string ConnectionId { get; set; } public string Number { get; set; } public string Method { get; set; } public string Query { get; set; } - public string ConnectionId { get; set; } } } \ No newline at end of file diff --git a/projects/IoTCenter/Controllers/AppController.cs b/projects/IoTCenter/Controllers/AppController.cs index 2c456019..f5103661 100644 --- a/projects/IoTCenter/Controllers/AppController.cs +++ b/projects/IoTCenter/Controllers/AppController.cs @@ -203,5 +203,33 @@ namespace IoTCenter.Controllers } return Content(string.Empty); } + + public IActionResult AllPowerOn(string connectionId, string[] nodes) + { + this.Power(connectionId, nodes, "On"); + return Json(ApiResponse.AsyncSuccess()); + } + + public IActionResult AllPowerOff(string connectionId, string[] nodes) + { + this.Power(connectionId, nodes, "Off"); + return Json(ApiResponse.AsyncSuccess()); + } + + private void Power(string connectionId, string[] nodes, string command) + { + var devices = this._deviceRepo.ReadOnlyTable() + .Include(o => o.Node) + .Include(o => o.Product.Apis) + .Where(o => nodes.Contains(o.Node.Number)) + .Where(o => o.Name.Contains("开关") || o.Name.Contains("插座")) + .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); + } + } } } \ No newline at end of file diff --git a/projects/IoTCenter/Views/Home/Index.cshtml b/projects/IoTCenter/Views/Home/Index.cshtml index e8a68587..fa19064d 100644 --- a/projects/IoTCenter/Views/Home/Index.cshtml +++ b/projects/IoTCenter/Views/Home/Index.cshtml @@ -64,8 +64,8 @@