From 6fdf16c3c513cde0a478e8592e3d8e00e810391a Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Wed, 11 Sep 2019 16:43:59 +0800 Subject: [PATCH] update Former-commit-id: 1c1f4eceb07a085aea5033ba0ebdad7c36e2a6a4 --- .../Domain/Entities/DeviceDataType.cs | 17 +-- .../DeviceServices/FBee/FBeeService.cs | 15 +- .../IoTCenter/Controllers/AppController.cs | 4 +- .../IoTCenter/Controllers/HomeController.cs | 60 +++++--- projects/IoTCenter/Services/PageHub.cs | 69 ++++++++- projects/IoTCenter/wwwroot/node.default.html | 133 +++++++++++++++++- .../StreamingAssets/wwwroot/images/off.png | Bin 0 -> 803 bytes .../StreamingAssets/wwwroot/images/on.png | Bin 0 -> 794 bytes projects/projects.sln | 15 -- 9 files changed, 243 insertions(+), 70 deletions(-) create mode 100644 projects/IoTClient/Assets/StreamingAssets/wwwroot/images/off.png create mode 100644 projects/IoTClient/Assets/StreamingAssets/wwwroot/images/on.png diff --git a/projects/IoT/IoT.Shared/Application/Domain/Entities/DeviceDataType.cs b/projects/IoT/IoT.Shared/Application/Domain/Entities/DeviceDataType.cs index 9bf24ed5..e43237f4 100644 --- a/projects/IoT/IoT.Shared/Application/Domain/Entities/DeviceDataType.cs +++ b/projects/IoT/IoT.Shared/Application/Domain/Entities/DeviceDataType.cs @@ -7,23 +7,14 @@ namespace Application.Domain.Entities [Display(Name = "整数")] Int = 10, - [Display(Name = "长整数")] - Long = 20, - - [Display(Name = "浮点数")] - Float = 30, - - [Display(Name = "布尔")] - Bool = 40, + [Display(Name = "实数")] + Float = 20, [Display(Name = "字符串")] String = 50, - [Display(Name = "日期")] - Date = 60, - - [Display(Name = "时间")] - DateTime = 70, + [Display(Name = "布尔")] + Bool = 40, [Display(Name = "键值对")] SelectList = 80 diff --git a/projects/IoT/IoT.Shared/DeviceServices/FBee/FBeeService.cs b/projects/IoT/IoT.Shared/DeviceServices/FBee/FBeeService.cs index ca73fc51..f7c04107 100644 --- a/projects/IoT/IoT.Shared/DeviceServices/FBee/FBeeService.cs +++ b/projects/IoT/IoT.Shared/DeviceServices/FBee/FBeeService.cs @@ -1239,15 +1239,16 @@ namespace IoT.Shared.DeviceServices.FBee { var state = BitConverter.ToInt16(props[0x0080]); var data1 = device.CreateData(Keys.Warning, state == 1 ? "警报" : "正常", DeviceDataType.String, "状态"); - var data2 = device.CreateData(Keys.UnderVoltage, new BitArray(props[0x0080])[3], DeviceDataType.Int, "低电量"); - device.AddorUpdateData(data1); - device.AddorUpdateData(data2); var dto1 = data1.To(); dto1.DeviceNumber = device.Number; - var dto2 = data2.To(); - dto2.DeviceNumber = device.Number; + device.AddorUpdateData(data1); dataList.Add(dto1); - dataList.Add(dto2); + + //var data2 = device.CreateData(Keys.UnderVoltage, new BitArray(props[0x0080])[3], DeviceDataType.Int, "低电量"); + //device.AddorUpdateData(data2); + //var dto2 = data2.To(); + //dto2.DeviceNumber = device.Number; + //dataList.Add(dto2); } else if (clusterId == ClusterId.pm25) { @@ -1305,7 +1306,7 @@ namespace IoT.Shared.DeviceServices.FBee tempBytes.Add(0x00); var electricity = BitConverter.ToInt64(tempBytes.ToArray()) / 1000f; - var data1 = device.CreateData(Keys.Electricity, electricity, DeviceDataType.Long, "电量", "kw/h"); + var data1 = device.CreateData(Keys.Electricity, electricity, DeviceDataType.Float, "电量", "kw/h"); device.AddorUpdateData(data1); var dto1 = data1.To(); dto1.DeviceNumber = device.Number; diff --git a/projects/IoTCenter/Controllers/AppController.cs b/projects/IoTCenter/Controllers/AppController.cs index 9ea242c9..9dbd2b20 100644 --- a/projects/IoTCenter/Controllers/AppController.cs +++ b/projects/IoTCenter/Controllers/AppController.cs @@ -163,7 +163,7 @@ namespace IoTCenter.Controllers using (var client = new InfluxClient(new Uri(url), usr, pwd)) { var fileds = String.Join(',', device.Data - .Where(o => o.Type == DeviceDataType.Int || o.Type == DeviceDataType.Long || o.Type == DeviceDataType.Float) + .Where(o => o.Type == DeviceDataType.Int || o.Type == DeviceDataType.Float) .Select(o => o.Key)); var query = $"select {fileds} from {measurementName} where time>now()-{time} and DeviceNumber = '{device.Number}' limit 10000"; var result = client.ReadAsync(dbName, query).Result; @@ -171,7 +171,7 @@ namespace IoTCenter.Controllers .Series.FirstOrDefault()? .Rows; var labels = rows?.Select(o => o.Timestamp.Value).Select(o => o.ToString("MM-dd HH:mm:ss")).ToList() ?? new List(); - foreach (var data in device.Data.Where(o => o.Type == DeviceDataType.Int || o.Type == DeviceDataType.Long || o.Type == DeviceDataType.Float)) + foreach (var data in device.Data.Where(o => o.Type == DeviceDataType.Int || o.Type == DeviceDataType.Float)) { list.Add(new { diff --git a/projects/IoTCenter/Controllers/HomeController.cs b/projects/IoTCenter/Controllers/HomeController.cs index a02e13be..e540edb0 100644 --- a/projects/IoTCenter/Controllers/HomeController.cs +++ b/projects/IoTCenter/Controllers/HomeController.cs @@ -1,24 +1,35 @@ -using System; -using System.Linq; using Application.Domain.Entities; using Infrastructure.Data; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using Vibrant.InfluxDB.Client; +using Vibrant.InfluxDB.Client.Rows; namespace IoTCenter.Controllers { public class HomeController : Controller { + private readonly IConfiguration _cfg; private readonly IRepository _nodeRepo; private readonly IRepository _deviceRepo; private readonly IRepository _dataRepo; private readonly IRepository _categoryRepo; private readonly IRepository _productRepo; - public HomeController(IRepository nodeRepo, IRepository deviceRepo, IRepository dataRepo, IRepository categoryRepo, IRepository productRepo) + public HomeController(IConfiguration cfg, + IRepository nodeRepo, + IRepository deviceRepo, + IRepository dataRepo, + IRepository categoryRepo, + IRepository productRepo) { + this._cfg = cfg; this._nodeRepo = nodeRepo; this._deviceRepo = deviceRepo; this._dataRepo = dataRepo; @@ -32,7 +43,7 @@ namespace IoTCenter.Controllers return View(); } - public IActionResult GetNodeList() + public JsonResult GetNodeList() { var categorys = this._deviceRepo.ReadOnlyTable() .GroupBy(o => o.Product.Category.Name) @@ -86,21 +97,13 @@ namespace IoTCenter.Controllers return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } - //public IActionResult GetNodes() - //{ - // var model = this._nodeRepo.ReadOnlyTable() - // .Include(o => o.Devices) - // .ToList(); - // return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); - //} - [Route("/Node")] public IActionResult Node(string number) { return View(model: number); } - public IActionResult GetNode(string number) + public JsonResult GetNode(string number) { var model = this._nodeRepo.ReadOnlyTable() .Include(o => o.Scenes) @@ -110,14 +113,27 @@ namespace IoTCenter.Controllers return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } - //public IActionResult GetNode(Guid id) - //{ - // var model = this._nodeRepo.ReadOnlyTable() - // .Include(o => o.Scenes) - // .Include(o => o.Devices).ThenInclude(o => o.Data) - // //.Include(o => o.Devices).ThenInclude(o => o.Apis).ThenInclude(o => o.Parameters) - // .FirstOrDefault(o => o.Id == id); - // return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); - //} + public JsonResult GetChartData(string number, string key, string time) + { + var device = this._deviceRepo.ReadOnlyTable().Include(o => o.Node).Include(o => o.Data).FirstOrDefault(o => o.Number == number); + var url = this._cfg["influxdb:url"]; + var usr = this._cfg["influxdb:usr"]; + var pwd = this._cfg["influxdb:pwd"]; + var dbName = "iot"; + var measurementName = "data"; + var list = new List(); + using (var client = new InfluxClient(new Uri(url), usr, pwd)) + { + var query = $"select {key} from {measurementName} where time>now()-{time} and DeviceNumber = '{device.Number}' limit 10000"; + var result = client.ReadAsync(dbName, query).Result; + var rows = result.Results.FirstOrDefault()? + .Series.FirstOrDefault()? + .Rows; + var labels = rows?.Select(o => o.Timestamp.Value).Select(o => o.ToString("MM-dd HH:mm:ss")).ToList() ?? new List(); + var data = rows?.Select(o => o.GetField(key)).ToList() ?? new List(); + var model = new { labels, data }; + return Json(model, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); + } + } } } \ No newline at end of file diff --git a/projects/IoTCenter/Services/PageHub.cs b/projects/IoTCenter/Services/PageHub.cs index f7b42317..eb60d737 100644 --- a/projects/IoTCenter/Services/PageHub.cs +++ b/projects/IoTCenter/Services/PageHub.cs @@ -6,11 +6,14 @@ using Infrastructure.Web.SignalR; using IoT.Shared.Services; using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.OpenApi.Readers; using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; +using Vibrant.InfluxDB.Client; +using Vibrant.InfluxDB.Client.Rows; namespace IoTCenter.Services { @@ -64,10 +67,6 @@ namespace IoTCenter.Services { this.DeleteDevice(message); } - else if (method == Methods.EditDataResponse) - { - this.UpdateData(message); - } else if (method == Methods.DeleteDataResponse) { this.DeleteData(message); @@ -242,6 +241,10 @@ namespace IoTCenter.Services } deviceRepo.SaveChanges(); this.Clients.Group("page").SendAsync("UpdateDevice", device.ToJson()); + foreach (var dataDto in dataDtoList) + { + this.LogToInfluxdbAsync(dataDto); + } } } } @@ -396,5 +399,61 @@ namespace IoTCenter.Services this.ServerToClient(connectionId, Methods.ApiCallback, message); } } + + private async Task LogToInfluxdbAsync(EditDataModel model) + { + if (string.IsNullOrEmpty(model.Value)) + { + return; + } + if (model.Type != DeviceDataType.Int && model.Type != DeviceDataType.Float) + { + return; + } + using (var scope = this._applicationService.CreateScope()) + { + try + { + var cfg = scope.ServiceProvider.GetService(); + var url = cfg["influxdb:url"]; + var usr = cfg["influxdb:usr"]; + var pwd = cfg["influxdb:pwd"]; + var dbName = "iot"; + var measurementName = "data"; + using (var client = new InfluxClient(new Uri(url), usr, pwd)) + { + await client.CreateDatabaseAsync(dbName); + var row = new DynamicInfluxRow + { + Timestamp = DateTime.UtcNow + }; + row.Fields.Add("DeviceNumber", model.DeviceNumber); + row.Fields.Add("DeviceName", model.Name); + row.Fields.Add(model.Key, this.GetDataValue(model)); + + await client.WriteAsync(dbName, measurementName, new List { row }); + } + } + catch (Exception ex) + { + ex.PrintStack(); + } + } + } + + private object GetDataValue(EditDataModel model) + { + switch (model.Type) + { + case DeviceDataType.Int: + return Convert.ToInt32(model.Value); + + case DeviceDataType.Float: + return Convert.ToSingle(model.Value); + + default: + return model.Value; + } + } } } \ No newline at end of file diff --git a/projects/IoTCenter/wwwroot/node.default.html b/projects/IoTCenter/wwwroot/node.default.html index 8ad18787..e05f1ef6 100644 --- a/projects/IoTCenter/wwwroot/node.default.html +++ b/projects/IoTCenter/wwwroot/node.default.html @@ -168,6 +168,38 @@
+
+
+
+
+ +
+ 温湿度 +
+
+
+
+
+
+
+ +
+ 光照 +
+
+
+
+
+
+
+ +
+ 用电 +
+
+
+
+
@@ -177,13 +209,21 @@

-
- +
+
+ + + + + + +
+
+ {{device.Name||device.DisplayName}} +
-
-
插座:
@@ -192,8 +232,14 @@

-
- +
+
+ + +
+
+ {{device.Name||device.DisplayName}} +
@@ -267,6 +313,47 @@ }); } +