diff --git a/IoTSharp.Test/IoTSharp.Test.csproj b/IoTSharp.Test/IoTSharp.Test.csproj index c9a9dcdeab26d800658bd65798833572b02e6826..39b24636330800d365450ea598b5780b5df41b5a 100644 --- a/IoTSharp.Test/IoTSharp.Test.csproj +++ b/IoTSharp.Test/IoTSharp.Test.csproj @@ -11,7 +11,7 @@ - + diff --git a/IoTSharp/Controllers/DevicesController.cs b/IoTSharp/Controllers/DevicesController.cs index a1930d354cb6b79cdc6ae121534dd9cbe97c76aa..d54319277895b012a93a520fdcb0685e474dd6ad 100644 --- a/IoTSharp/Controllers/DevicesController.cs +++ b/IoTSharp/Controllers/DevicesController.cs @@ -19,6 +19,8 @@ using MQTTnet.Client.Options; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Logging; using IoTSharp.Storage; +using k8s.Models; +using Newtonsoft.Json.Linq; namespace IoTSharp.Controllers { @@ -104,82 +106,112 @@ namespace IoTSharp.Controllers [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ApiResult), StatusCodes.Status404NotFound)] [ProducesDefaultResponseType] - public async Task>> GetAttributeLatest(Guid deviceId) + public async Task>> GetAttributeLatest(Guid deviceId) { - var devid = from dev in _context.AttributeLatest where dev.DeviceId == deviceId select dev ; - if (!devid.Any()) + Device dev = Found(deviceId); + if (dev == null) { return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found ")); } - return await devid.ToListAsync(); + else + { + var devid = from t in _context.AttributeLatest + where t.DeviceId == deviceId + select new AttributeDataDto() + { + DataSide = t.DataSide, + DateTime = t.DateTime, + KeyName = t.KeyName, + Value = t.ToObject() + }; + if (!devid.Any()) + { + return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found ")); + } + return await devid.ToListAsync(); + } } - /// - ///获取指定设备的最新遥测数据 + /// 获取指定设备指定keys的最新属性 /// - /// + /// Which device do you read? + /// Specify key name list , use , or space or ; to split /// [Authorize(Roles = nameof(UserRole.NormalUser))] - [HttpGet("{deviceId}/TelemetryLatest")] + [HttpGet("{deviceId}/AttributeLatest/{keys}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ApiResult), StatusCodes.Status404NotFound)] [ProducesDefaultResponseType] - public async Task>> GetTelemetryLatest(Guid deviceId) + public async Task>> GetAttributeLatest(Guid deviceId, string keys) { - var devid = from dev in _context.TelemetryLatest where dev.DeviceId == deviceId select dev; - if (!devid.Any()) + Device dev = Found(deviceId); + if (dev == null) { return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found ")); } - return await _storage.GetTelemetryLatest(deviceId); + else + { + var kv = from t in _context.AttributeLatest where t.DeviceId == t.DeviceId && keys.Split(',', ' ', ';').Contains(t.KeyName) select new AttributeDataDto() { DataSide = t.DataSide, DateTime = t.DateTime, KeyName = t.KeyName, Value = t.ToObject() }; + return await kv.ToListAsync(); + } + } + + private Device Found( Guid deviceId) + { + var cid = User.Claims.First(c => c.Type == IoTSharpClaimTypes.Customer); + var dev = _context.Device.Include(d => d.Customer).FirstOrDefault(d => d.Id == deviceId && d.Customer.Id.ToString() == cid.Value); + return dev; + } + private Task FoundAsync(Guid deviceId) + { + var cid = User.Claims.First(c => c.Type == IoTSharpClaimTypes.Customer); + var dev = _context.Device.Include(d => d.Customer).FirstOrDefaultAsync(d => d.Id == deviceId && d.Customer.Id.ToString() == cid.Value); + return dev; } + /// - /// 获取指定设备的指定key 的遥测数据 + ///获取指定设备的最新遥测数据 /// - /// Which device do you read? - /// 指定键值列表, 使用分号或者逗号分割 。 + /// /// [Authorize(Roles = nameof(UserRole.NormalUser))] - [HttpGet("{deviceId}/TelemetryLatest/{keys}")] + [HttpGet("{deviceId}/TelemetryLatest")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ApiResult), StatusCodes.Status404NotFound)] [ProducesDefaultResponseType] - public async Task>> GetTelemetryLatest(Guid deviceId, string keys) + public async Task>> GetTelemetryLatest(Guid deviceId) { - var dev = _context.Device.Find(deviceId); - if (dev == null) + Device dev = Found(deviceId); + if (dev==null) { return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found ")); } - else - { - return await _storage.GetTelemetryLatest(deviceId,keys); - } + return await _storage.GetTelemetryLatest(deviceId); } /// - /// 获取指定设备指定keys的最新属性 + /// 获取指定设备的指定key 的遥测数据 /// /// Which device do you read? - /// Specify key name list , use , or space or ; to split + /// 指定键值列表, 使用分号或者逗号分割 。 /// [Authorize(Roles = nameof(UserRole.NormalUser))] - [HttpGet("{deviceId}/AttributeLatest/{keys}")] + [HttpGet("{deviceId}/TelemetryLatest/{keys}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ApiResult), StatusCodes.Status404NotFound)] [ProducesDefaultResponseType] - public async Task> GetAttributeLatest(Guid deviceId,string keys) + public async Task>> GetTelemetryLatest(Guid deviceId, string keys) { - var dev = _context.Device.Find(deviceId); + Device dev = Found(deviceId); if (dev == null) { return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found ")); } else { - var kv = from t in _context.AttributeLatest where t.DeviceId == dev.Id && keys.Split(',', ' ', ';').Contains(t.KeyName) select t; - return (await kv.FirstOrDefaultAsync())?.ToObject(); + return await _storage.GetTelemetryLatest(deviceId,keys); } } + /// /// 获取指定设备和指定时间, 指定key的数据 @@ -195,7 +227,7 @@ namespace IoTSharp.Controllers [ProducesDefaultResponseType] public async Task>> GetTelemetryData(Guid deviceId, string keys, DateTime begin) { - var dev = _context.Device.Find(deviceId); + Device dev = Found(deviceId); if (dev == null) { return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found ")); @@ -221,7 +253,7 @@ namespace IoTSharp.Controllers [ProducesDefaultResponseType] public async Task>> GetTelemetryData(Guid deviceId, string keys, DateTime begin,DateTime end ) { - var dev = _context.Device.Find(deviceId); + Device dev = Found(deviceId); if (dev == null) { return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found ")); @@ -249,13 +281,11 @@ namespace IoTSharp.Controllers [ProducesDefaultResponseType] public async Task> GetDevice(Guid id) { - var device = await _context.Device.FindAsync(id); - + Device device = await FoundAsync(id); if (device == null) { return NotFound(new ApiResult(ApiCode.NotFoundDevice, $"Device {id} not found ", id)); } - return device; } @@ -353,7 +383,7 @@ namespace IoTSharp.Controllers [ProducesDefaultResponseType] public async Task> DeleteDevice(Guid id) { - var device = await _context.Device.FindAsync(id); + Device device = Found(id); if (device == null) { return NotFound(new ApiResult(ApiCode.NotFoundDevice, $"Device {id} not found ", id)); diff --git a/IoTSharp/Dtos/AttributeDataDto.cs b/IoTSharp/Dtos/AttributeDataDto.cs new file mode 100644 index 0000000000000000000000000000000000000000..a998a24bada8340b0d58a64152919a33d8649089 --- /dev/null +++ b/IoTSharp/Dtos/AttributeDataDto.cs @@ -0,0 +1,20 @@ +using IoTSharp.Data; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Threading.Tasks; + +namespace IoTSharp.Dtos +{ + public class AttributeDataDto + { + public string KeyName { get; set; } + + public DataSide DataSide { get; set; } + + public DateTime DateTime { get; set; } + + public object Value{ get; set; } + } +} diff --git a/IoTSharp/Extensions/DeviceExtension.cs b/IoTSharp/Extensions/DeviceExtension.cs index a7fbba7971a598b1873110c25cd3ab11798d91e1..3b65e6ce51d5259baa4961fe20875457e14610cc 100644 --- a/IoTSharp/Extensions/DeviceExtension.cs +++ b/IoTSharp/Extensions/DeviceExtension.cs @@ -1,4 +1,5 @@ using IoTSharp.Data; +using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; @@ -8,6 +9,7 @@ namespace IoTSharp.Extensions { public static class DeviceExtension { + /// /// When creating a device, all the things that need to be done here are done /// diff --git a/IoTSharp/IoTSharp.csproj b/IoTSharp/IoTSharp.csproj index f4cc543feb3ccbba60f5172fdcbce6018a530c8a..c8178502f1d754d4476677f8ada3ddc5db0791ed 100644 --- a/IoTSharp/IoTSharp.csproj +++ b/IoTSharp/IoTSharp.csproj @@ -41,18 +41,18 @@ - - + + - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + @@ -60,7 +60,7 @@ - + @@ -68,15 +68,15 @@ - - - - - - - - - + + + + + + + + + diff --git a/IoTSharp/IoTSharp.xml b/IoTSharp/IoTSharp.xml index d57e39df558470ebc17c707b29d13181270d5e70..8e25a9bbfff89200ad7614b4d4e34a092dc18d71 100644 --- a/IoTSharp/IoTSharp.xml +++ b/IoTSharp/IoTSharp.xml @@ -176,6 +176,14 @@ + + + 获取指定设备指定keys的最新属性 + + Which device do you read? + Specify key name list , use , or space or ; to split + + 获取指定设备的最新遥测数据 @@ -191,14 +199,6 @@ 指定键值列表, 使用分号或者逗号分割 。 - - - 获取指定设备指定keys的最新属性 - - Which device do you read? - Specify key name list , use , or space or ; to split - - 获取指定设备和指定时间, 指定key的数据