From 8c0186279a95950bde4239c00842e3d92cd9fa73 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Mon, 20 Jan 2020 11:28:55 +0800 Subject: [PATCH] update Former-commit-id: a37be4468c75101b42fc6883efd2b83a950f2511 --- projects/Infrastructure/wwwroot/js/site.js | 4 +- .../Application/Models/ApiRequestModel.cs | 18 ++++ projects/IoTCenter/Api/ApiController.cs | 91 +++++++++++++++++++ projects/IoTNode/Startup.cs | 8 +- projects/WebApp/wwwroot/js/message.js | 1 + projects/WebApp/wwwroot/js/store.js | 26 +++++- projects/WebApp/wwwroot/js/util.js | 59 +++++++++--- projects/WebApp/wwwroot/pages/iot/node.html | 4 +- 8 files changed, 187 insertions(+), 24 deletions(-) create mode 100644 projects/IoT.Shared/Application/Models/ApiRequestModel.cs create mode 100644 projects/IoTCenter/Api/ApiController.cs diff --git a/projects/Infrastructure/wwwroot/js/site.js b/projects/Infrastructure/wwwroot/js/site.js index 5c0292aa..5f5333cd 100644 --- a/projects/Infrastructure/wwwroot/js/site.js +++ b/projects/Infrastructure/wwwroot/js/site.js @@ -13,7 +13,7 @@ $.validator.unobtrusive.adapters.addBool("radio", "required"); function updateSelectList(url, id) { $.getJSON(url, function (data) { $.each(data, function (i, v) { - $(id).append(''); + $(id).append(''); }); }); } @@ -303,7 +303,7 @@ function update(url, parent, targetId) { url += '?parentId=' + encodeURI(parent); $.getJSON(url, function (data) { $.each(data, function (i, v) { - $(targetId).append(''); + $(targetId).append(''); }); }); } diff --git a/projects/IoT.Shared/Application/Models/ApiRequestModel.cs b/projects/IoT.Shared/Application/Models/ApiRequestModel.cs new file mode 100644 index 00000000..ddf5546f --- /dev/null +++ b/projects/IoT.Shared/Application/Models/ApiRequestModel.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace Application.Models +{ + public class ApiRequestModel + { + [Required] + public string ConnectionId { get; set; } + + [Required] + public string Number { get; set; } + + [Required] + public string Method { get; set; } + + public string Query { get; set; } + } +} \ No newline at end of file diff --git a/projects/IoTCenter/Api/ApiController.cs b/projects/IoTCenter/Api/ApiController.cs new file mode 100644 index 00000000..109259a8 --- /dev/null +++ b/projects/IoTCenter/Api/ApiController.cs @@ -0,0 +1,91 @@ +using Application.Domain.Entities; +using Application.Models; +using Infrastructure.Data; +using Infrastructure.Extensions; +using Infrastructure.Jwt; +using IoTCenter.Services; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Newtonsoft.Json; +using System; +using System.ComponentModel.DataAnnotations; +using System.Linq; + +namespace UserCenter.Controllers +{ + [ApiVersion("1.0")] + [Route("api/v{version:apiVersion}/[controller]/[action]")] + [ApiController] + public class ApiController : Controller + { + private readonly IHostEnvironment _env; + private readonly IConfiguration _configuration; + private readonly IJwtHelper _jwtHelper; + private readonly IRepository _categoryRepo; + private readonly IRepository _productRepo; + private readonly IRepository _nodeRepo; + private readonly IRepository _sceneRepo; + private readonly IRepository _sceneCommandRepo; + private readonly IRepository _commandRepo; + private readonly IRepository _deviceRepo; + private readonly IRepository _liveRecordRepo; + private readonly IHubContext _hub; + + public ApiController( + IHostEnvironment env, + IConfiguration configuration, + IJwtHelper jwtHelper, + IRepository categoryRepo, + IRepository productRepo, + IRepository nodeRepo, + IRepository sceneRepo, + IRepository sceneCommandRepo, + IRepository commandRepo, + IRepository deviceRepo, + IRepository liveRecordRepo, + IHubContext hub) + { + this._env = env; + this._configuration = configuration; + this._jwtHelper = jwtHelper; + this._categoryRepo = categoryRepo; + this._productRepo = productRepo; + this._nodeRepo = nodeRepo; + this._sceneRepo = sceneRepo; + this._sceneCommandRepo = sceneCommandRepo; + this._commandRepo = commandRepo; + this._deviceRepo = deviceRepo; + this._liveRecordRepo = liveRecordRepo; + this._hub = hub; + } + + [HttpPost] + public ActionResult Exec([FromBody]ApiRequestModel model) + { + try + { + this.CallApi(model.ConnectionId, model.Number, model.Method, model.Query); + return Ok(); + } + catch (Exception ex) + { + ex.PrintStack(); + return Problem(ex.Message); + } + } + + private void CallApi(string connectionId, string number, string method, string query) + { + var device = this._deviceRepo.ReadOnlyTable().Include(o => o.Node).FirstOrDefault(o => o.Number == number); + if (device != null) + { + var message = $"{method}?number={number}{(string.IsNullOrEmpty(query) ? "" : "&")}{query}"; + var group = device.Node.Number; + this._hub.ServerToClient(Methods.ExecApiRequest, message, group, connectionId); + } + } + } +} \ No newline at end of file diff --git a/projects/IoTNode/Startup.cs b/projects/IoTNode/Startup.cs index 74afb13d..395e432e 100644 --- a/projects/IoTNode/Startup.cs +++ b/projects/IoTNode/Startup.cs @@ -37,10 +37,10 @@ namespace IoTNode services.AddHostedService(o => o.GetService()); services.AddSingleton(); services.AddHostedService(o => o.GetService()); - //services.AddSingleton(); - //services.AddHostedService(o => o.GetService()); - //services.AddSingleton(); - //services.AddHostedService(o => o.GetService()); + services.AddSingleton(); + services.AddHostedService(o => o.GetService()); + services.AddSingleton(); + services.AddHostedService(o => o.GetService()); base.ConfigureServices(services); } diff --git a/projects/WebApp/wwwroot/js/message.js b/projects/WebApp/wwwroot/js/message.js index a5bbdc18..e0f75bf0 100644 --- a/projects/WebApp/wwwroot/js/message.js +++ b/projects/WebApp/wwwroot/js/message.js @@ -1,5 +1,6 @@ var hubUrl = "/IoTCenter/hub?group=page"; var connection = new signalR.HubConnectionBuilder().withUrl(hubUrl).build(); +var connectionId; function connect() { if (connection.state === signalR.HubConnectionState.Disconnected) { connection.start().then(function () { diff --git a/projects/WebApp/wwwroot/js/store.js b/projects/WebApp/wwwroot/js/store.js index 267891b0..fdc00179 100644 --- a/projects/WebApp/wwwroot/js/store.js +++ b/projects/WebApp/wwwroot/js/store.js @@ -52,10 +52,32 @@ const store = new Vuex.Store({ updateByNumber(state.nodes, model); } if (state.node) { - copy(data.model, state.node); + if (state.node.number === model.number) { + copy(model, state.node); + } } }, - + dataEntityUpdated(state, data) { + var model = data.model; + if (state.node) { + var device = Enumerable.from(state.node.devices).where(function (o) { return o.id === model.deviceId; }).firstOrDefault(); + if (device) { + updateByKey(device.data, model); + } + } + if (state.product) { + var device = Enumerable.from(state.product.devices).where(function (o) { return o.id === model.deviceId; }).firstOrDefault(); + if (device) { + updateByKey(device.data, model); + } + } + if (state.device) { + var device =state.device.number === model.number; + if (device) { + updateByKey(device.data, model); + } + } + } //update(state,method,message,to,from) { // alert(1); //}, diff --git a/projects/WebApp/wwwroot/js/util.js b/projects/WebApp/wwwroot/js/util.js index 82b3b084..e3979bbd 100644 --- a/projects/WebApp/wwwroot/js/util.js +++ b/projects/WebApp/wwwroot/js/util.js @@ -1,32 +1,63 @@ -function copy(from, to) { - for (var attr in to) { - if (from.hasOwnProperty(attr)) { - if (!(from[attr] instanceof Array)) { - to[attr] = from[attr]; - } - } - } +//update +function updateById(list, item) { + return update(list, item, 'id'); +} + +function updateByNumber(list, item) { + return update(list, item, 'number'); +} + +function updateByKey(list, item) { + return update(list, item, 'key'); } -function updateByNumber(list, from) { + +function update(list, item, key) { + key = key || 'number'; var result = false; - var to = Enumerable.from(list).where(function (o) { return o.number === from.number; }).firstOrDefault(); + var to = Enumerable.from(list).where(function (o) { return o[key] === item[key]; }).firstOrDefault(); if (to) { - copy(from, to); + copy(item, to); } else { - list.push(from); + list.push(item); result = true; } + console.log(result?'insert':'update'+' data by '+key); return result; } -function deleteByNumber(list, item) { +//delete +function remove(list, item,key) { var result = false; for (var i = 0; i < list.length; i++) { - if (list[i].number == item.number) { + if (list[i][key] == item[key]) { list.splice(i, 1); result = true; break; } } return result; +} +//util +function copy(from, to) { + for (var attr in to) { + if (from.hasOwnProperty(attr)) { + if (!(from[attr] instanceof Array)) { + to[attr] = from[attr]; + } + } + } +} + +function callApi(number, method, query) { + var loading = weui.loading('提交中...'); + axios.post('/IoTCenter/api/v1/api/exec', { connectionId,number,method, query }) + .then(function (response) { + console.log(response); + }) + .catch(function (error) { + console.log(error); + }) + .finally(function () { + loading.hide(); + }); } \ No newline at end of file diff --git a/projects/WebApp/wwwroot/pages/iot/node.html b/projects/WebApp/wwwroot/pages/iot/node.html index 6696c56e..0ef1a02c 100644 --- a/projects/WebApp/wwwroot/pages/iot/node.html +++ b/projects/WebApp/wwwroot/pages/iot/node.html @@ -222,8 +222,8 @@
- - + +