From d59bf1f9bf70ecaedc35fb6b19c6e8a0864fe363 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Wed, 11 Sep 2019 10:40:15 +0800 Subject: [PATCH] update Former-commit-id: fd5286298fcf76e2261cdff8d899b95c1d44bd22 --- .../Application/Models/CallApiRequest.cs | 3 +- .../IoTCenter/Controllers/AppController.cs | 28 ++++++ projects/IoTCenter/Views/Home/Index.cshtml | 8 +- projects/IoTCenter/Views/Home/Node.cshtml | 2 + projects/IoTCenter/wwwroot/home.default.html | 93 +++++++++++++++++-- projects/IoTCenter/wwwroot/node.default.html | 31 ++++++- 6 files changed, 148 insertions(+), 17 deletions(-) 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 @@