From 7ab8d1ad084b4e831ce1d787668ad0223f8ced6f Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Wed, 11 Mar 2020 15:49:32 +0800 Subject: [PATCH] update Former-commit-id: 9427f50189b771723e1e89525609009d938f1eb8 --- projects/IoTCenter/Api/NodeController.cs | 49 +++++++++++++++- projects/IoTCenter/Views/Home/Nodes.cshtml | 60 +++++++++++++++++++- projects/IoTCenter/Views/Home/Product.cshtml | 2 + projects/IoTCenter/wwwroot/js/util.js | 14 +++++ 4 files changed, 122 insertions(+), 3 deletions(-) diff --git a/projects/IoTCenter/Api/NodeController.cs b/projects/IoTCenter/Api/NodeController.cs index 229ce2db..2629db53 100644 --- a/projects/IoTCenter/Api/NodeController.cs +++ b/projects/IoTCenter/Api/NodeController.cs @@ -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 _nodeRepo; private readonly IRepository _nodeCategoryRepo; private readonly IRepository _deviceRepo; + private readonly IHubContext _hub; public NodeController(IConfiguration cfg, IRepository nodeRepo, IRepository nodeCategoryRepo, - IRepository deviceRepo) + IRepository deviceRepo, + IHubContext 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(); + } } } \ No newline at end of file diff --git a/projects/IoTCenter/Views/Home/Nodes.cshtml b/projects/IoTCenter/Views/Home/Nodes.cshtml index ebccfc4e..d69254d0 100644 --- a/projects/IoTCenter/Views/Home/Nodes.cshtml +++ b/projects/IoTCenter/Views/Home/Nodes.cshtml @@ -35,7 +35,63 @@ @section scripts{ + + + + + - - } \ No newline at end of file diff --git a/projects/IoTCenter/Views/Home/Product.cshtml b/projects/IoTCenter/Views/Home/Product.cshtml index b73b1b65..df5b3690 100644 --- a/projects/IoTCenter/Views/Home/Product.cshtml +++ b/projects/IoTCenter/Views/Home/Product.cshtml @@ -136,6 +136,8 @@ for (var i = 0; i < numbers.length; i++) { execApi(numbers[i], this.product.path + method, null); } + } else { + toastr.error('没有选中任何项'); } } } diff --git a/projects/IoTCenter/wwwroot/js/util.js b/projects/IoTCenter/wwwroot/js/util.js index 8da55560..05e3647c 100644 --- a/projects/IoTCenter/wwwroot/js/util.js +++ b/projects/IoTCenter/wwwroot/js/util.js @@ -88,6 +88,20 @@ function execScene(id) { loading.show(); }); } + +function nodePower(number,command) { + loading.show(); + axios.post('/IoTCenter/api/v1/node/power'+command, '"' + number + '"', { headers: { 'Content-Type': 'application/json;charset=UTF-8' } }) + .then(function (response) { + console.log(response); + }) + .catch(function (error) { + console.log(error); + }) + .finally(function () { + loading.show(); + }); +} // function Select(e) { var checkbox = $(e.target);