From 2768abe4e8168f0ba3462f1c369b7a1601711316 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Tue, 30 Mar 2021 11:01:44 +0800 Subject: [PATCH] update Former-commit-id: 6982abb8c7aa6088f6c7da1d22ddc1d4cd414614 Former-commit-id: 9adc7cf05677909ed8042e0f0c41ad2c901253a8 --- .../Api/{ => Api}/DeviceController.cs | 0 projects/Platform/Api/Api/InfluxController.cs | 73 ++++++++++- projects/Platform/Api/Api/QueryDataModel.cs | 18 +++ projects/Platform/Views/Shared/_Layout.cshtml | 2 + .../components/views/areas/default/data.vue | 118 ++++++++++++++++++ .../components/views/areas/default/device.vue | 4 +- .../components/views/shared/pagination.vue | 2 + projects/Platform/wwwroot/index.html | 2 + projects/Platform/wwwroot/js/axios.js | 5 +- 9 files changed, 217 insertions(+), 7 deletions(-) rename projects/Platform/Api/{ => Api}/DeviceController.cs (100%) create mode 100644 projects/Platform/Api/Api/QueryDataModel.cs create mode 100644 projects/Platform/wwwroot/components/views/areas/default/data.vue diff --git a/projects/Platform/Api/DeviceController.cs b/projects/Platform/Api/Api/DeviceController.cs similarity index 100% rename from projects/Platform/Api/DeviceController.cs rename to projects/Platform/Api/Api/DeviceController.cs diff --git a/projects/Platform/Api/Api/InfluxController.cs b/projects/Platform/Api/Api/InfluxController.cs index 2df7bf62..2d8b6747 100644 --- a/projects/Platform/Api/Api/InfluxController.cs +++ b/projects/Platform/Api/Api/InfluxController.cs @@ -1,26 +1,35 @@ -using Infrastructure.Extensions; +using Application.Domain.Entities; +using Infrastructure.Data; +using Infrastructure.Extensions; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; +using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; -namespace Platform.Apis +namespace Platform.Api.Api { [ApiVersion("1.0")] [Route("api/v{version:apiVersion}/[controller]/[action]")] [ApiController] + [Authorize] public class InfluxController : ControllerBase { private readonly IConfiguration _cfg; private readonly IHttpClientFactory _httpClientFactory; + private readonly IRepository _dataRepo; - public InfluxController(IConfiguration cfg, IHttpClientFactory httpClientFactory) + public InfluxController(IConfiguration cfg, IHttpClientFactory httpClientFactory, IRepository dataRepo) { this._cfg = cfg; this._httpClientFactory = httpClientFactory; + this._dataRepo = dataRepo; } [HttpPost] @@ -52,5 +61,63 @@ namespace Platform.Apis { return Encoding.UTF8.GetString(Convert.FromBase64String(value)); } + + [HttpPost] + public async Task History([FromBody] QueryDataModel model) + { + try + { + var data = this._dataRepo.ReadOnlyTable() + .Where(o => o.IoTDevice.Number == model.Number && o.Key == model.Key) + .FirstOrDefault(); + model.Query = data; + var format = "yyyy-MM-dd 00:00:00"; + var where = $" from data where DeviceNumber = '{model.Number}' "; + var timezone = " tz('Asia/Shanghai')"; + if (model.Start.HasValue) + { + where += $"and time >= '{model.Start.Value.ToString(format)}' "; + } + if (model.End.HasValue) + { + where += $"and time < '{model.End.Value.ToString(format)}' "; + } + var query = $"select count({model.Key}) {where} {timezone}"; + var result = await this.QueryAsync(query); + model.TotalCount = Convert.ToInt32(result["results"][0]["series"][0]["values"][0][1].ToString()); + + // + query = $"select {model.Key} {where} order by time desc limit {model.PageSize} offset {(model.PageIndex - 1) * model.PageSize} {timezone}"; + result = await this.QueryAsync(query); + var list = result["results"][0]["series"][0]["values"] + .Select(o => new IoTData { + Timestamp = ((DateTimeOffset)o[0]).ToUnixTimeMilliseconds(), + Value = o[1].ToString() + }) + .ToList(); + model.List.AddRange(list); + return Ok(model); + } + catch (Exception ex) + { + ex.PrintStack(); + return Problem(ex.Message); + } + } + + private async Task QueryAsync(string sql, string db = "iot") + { + var connectionString = this._cfg.GetConnectionStringValues("InfluxDB"); + var server = connectionString["url"]; + var url = $"{server}/query"; + var data = new Dictionary { + { "db",db}, + { "q",sql } + }; + using var content = new FormUrlEncodedContent(data); + var client = _httpClientFactory.CreateClient(); + var result = await client.PostAsync(url, content).Result.Content.ReadAsStringAsync(); + return JObject.Parse(result); + } } } \ No newline at end of file diff --git a/projects/Platform/Api/Api/QueryDataModel.cs b/projects/Platform/Api/Api/QueryDataModel.cs new file mode 100644 index 00000000..3d898785 --- /dev/null +++ b/projects/Platform/Api/Api/QueryDataModel.cs @@ -0,0 +1,18 @@ +using Application.Domain.Entities; +using Infrastructure.Application; +using System; +using System.ComponentModel.DataAnnotations; + +namespace Platform.Api.Api +{ + public class QueryDataModel : PagedListModel + { + [Required] + public string Number { get; set; } + [Required] + public string Key { get; set; } + public DateTime? Start { get; set; } + public DateTime? End { get; set; } + + } +} diff --git a/projects/Platform/Views/Shared/_Layout.cshtml b/projects/Platform/Views/Shared/_Layout.cshtml index c6e3e358..52b02ca8 100644 --- a/projects/Platform/Views/Shared/_Layout.cshtml +++ b/projects/Platform/Views/Shared/_Layout.cshtml @@ -159,6 +159,8 @@ + + diff --git a/projects/Platform/wwwroot/components/views/areas/default/data.vue b/projects/Platform/wwwroot/components/views/areas/default/data.vue new file mode 100644 index 00000000..071e65b5 --- /dev/null +++ b/projects/Platform/wwwroot/components/views/areas/default/data.vue @@ -0,0 +1,118 @@ + + \ No newline at end of file diff --git a/projects/Platform/wwwroot/components/views/areas/default/device.vue b/projects/Platform/wwwroot/components/views/areas/default/device.vue index 41cd70b0..e9984575 100644 --- a/projects/Platform/wwwroot/components/views/areas/default/device.vue +++ b/projects/Platform/wwwroot/components/views/areas/default/device.vue @@ -13,7 +13,9 @@
-

{{ getName(data) }}

+

{{ getName(data) }} + 历史数据 +

+ + diff --git a/projects/Platform/wwwroot/js/axios.js b/projects/Platform/wwwroot/js/axios.js index 8b8d9460..f12f4426 100644 --- a/projects/Platform/wwwroot/js/axios.js +++ b/projects/Platform/wwwroot/js/axios.js @@ -21,7 +21,6 @@ axios.interceptors.response.use(function (response) { return response; }, function (error) { loading.hide(); - console.error(error); if (error.response) { if (error.response.status === 401) { if (store.state.token.refreshToken && error.config.url.indexOf('refreshToken') > -1) { @@ -36,7 +35,7 @@ axios.interceptors.response.use(function (response) { }) .catch(function (error) { if (error.response.status === 401) { - store.commit('logout', response.data); + store.commit('logout'); console.log('refreshToken 已过期'); router.push('components/views/areas/default/login.vue'); } @@ -46,7 +45,7 @@ axios.interceptors.response.use(function (response) { }); } else { - store.commit('logout', response.data); + store.commit('logout'); router.push('components/views/areas/default/login.vue'); } }