提交 a15d376f 编写于 作者: 麦壳饼's avatar 麦壳饼

修改了设备最新属性的获取和权限判断

上级 849ac9cb
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.6" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.7" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" /> <PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" /> <PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
......
...@@ -19,6 +19,8 @@ using MQTTnet.Client.Options; ...@@ -19,6 +19,8 @@ using MQTTnet.Client.Options;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using IoTSharp.Storage; using IoTSharp.Storage;
using k8s.Models;
using Newtonsoft.Json.Linq;
namespace IoTSharp.Controllers namespace IoTSharp.Controllers
{ {
...@@ -104,83 +106,113 @@ namespace IoTSharp.Controllers ...@@ -104,83 +106,113 @@ namespace IoTSharp.Controllers
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResult), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(ApiResult), StatusCodes.Status404NotFound)]
[ProducesDefaultResponseType] [ProducesDefaultResponseType]
public async Task<ActionResult<List<AttributeLatest>>> GetAttributeLatest(Guid deviceId) public async Task<ActionResult<List<AttributeDataDto>>> GetAttributeLatest(Guid deviceId)
{ {
var devid = from dev in _context.AttributeLatest where dev.DeviceId == deviceId select dev ; Device dev = Found(deviceId);
if (dev == null)
{
return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found "));
}
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()) if (!devid.Any())
{ {
return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found ")); return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found "));
} }
return await devid.ToListAsync(); return await devid.ToListAsync();
} }
}
/// <summary> /// <summary>
///获取指定设备的最新遥测数据 /// 获取指定设备指定keys的最新属性
/// </summary> /// </summary>
/// <param name="deviceId"></param> /// <param name="deviceId">Which device do you read?</param>
/// <param name="keys">Specify key name list , use , or space or ; to split </param>
/// <returns></returns> /// <returns></returns>
[Authorize(Roles = nameof(UserRole.NormalUser))] [Authorize(Roles = nameof(UserRole.NormalUser))]
[HttpGet("{deviceId}/TelemetryLatest")] [HttpGet("{deviceId}/AttributeLatest/{keys}")]
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResult), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(ApiResult), StatusCodes.Status404NotFound)]
[ProducesDefaultResponseType] [ProducesDefaultResponseType]
public async Task<ActionResult<List<TelemetryDataDto>>> GetTelemetryLatest(Guid deviceId) public async Task<ActionResult<List<AttributeDataDto>>> GetAttributeLatest(Guid deviceId, string keys)
{ {
var devid = from dev in _context.TelemetryLatest where dev.DeviceId == deviceId select dev; Device dev = Found(deviceId);
if (!devid.Any()) if (dev == null)
{ {
return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found ")); 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<Device> 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;
}
/// <summary> /// <summary>
/// 获取指定设备的指定key 的遥测数据 ///获取指定设备的最新遥测数据
/// </summary> /// </summary>
/// <param name="deviceId">Which device do you read?</param> /// <param name="deviceId"></param>
/// <param name="keys">指定键值列表, 使用分号或者逗号分割 。 </param>
/// <returns></returns> /// <returns></returns>
[Authorize(Roles = nameof(UserRole.NormalUser))] [Authorize(Roles = nameof(UserRole.NormalUser))]
[HttpGet("{deviceId}/TelemetryLatest/{keys}")] [HttpGet("{deviceId}/TelemetryLatest")]
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResult), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(ApiResult), StatusCodes.Status404NotFound)]
[ProducesDefaultResponseType] [ProducesDefaultResponseType]
public async Task<ActionResult<List<TelemetryDataDto>>> GetTelemetryLatest(Guid deviceId, string keys) public async Task<ActionResult<List<TelemetryDataDto>>> GetTelemetryLatest(Guid deviceId)
{ {
var dev = _context.Device.Find(deviceId); Device dev = Found(deviceId);
if (dev == null) if (dev==null)
{ {
return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found ")); return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found "));
} }
else return await _storage.GetTelemetryLatest(deviceId);
{
return await _storage.GetTelemetryLatest(deviceId,keys);
}
} }
/// <summary> /// <summary>
/// 获取指定设备指定keys的最新属性 /// 获取指定设备的指定key 的遥测数据
/// </summary> /// </summary>
/// <param name="deviceId">Which device do you read?</param> /// <param name="deviceId">Which device do you read?</param>
/// <param name="keys">Specify key name list , use , or space or ; to split </param> /// <param name="keys">指定键值列表, 使用分号或者逗号分割 。 </param>
/// <returns></returns> /// <returns></returns>
[Authorize(Roles = nameof(UserRole.NormalUser))] [Authorize(Roles = nameof(UserRole.NormalUser))]
[HttpGet("{deviceId}/AttributeLatest/{keys}")] [HttpGet("{deviceId}/TelemetryLatest/{keys}")]
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResult), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(ApiResult), StatusCodes.Status404NotFound)]
[ProducesDefaultResponseType] [ProducesDefaultResponseType]
public async Task<ActionResult<object>> GetAttributeLatest(Guid deviceId,string keys) public async Task<ActionResult<List<TelemetryDataDto>>> GetTelemetryLatest(Guid deviceId, string keys)
{ {
var dev = _context.Device.Find(deviceId); Device dev = Found(deviceId);
if (dev == null) if (dev == null)
{ {
return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found ")); return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found "));
} }
else else
{ {
var kv = from t in _context.AttributeLatest where t.DeviceId == dev.Id && keys.Split(',', ' ', ';').Contains(t.KeyName) select t; return await _storage.GetTelemetryLatest(deviceId,keys);
return (await kv.FirstOrDefaultAsync())?.ToObject();
} }
} }
/// <summary> /// <summary>
/// 获取指定设备和指定时间, 指定key的数据 /// 获取指定设备和指定时间, 指定key的数据
/// </summary> /// </summary>
...@@ -195,7 +227,7 @@ namespace IoTSharp.Controllers ...@@ -195,7 +227,7 @@ namespace IoTSharp.Controllers
[ProducesDefaultResponseType] [ProducesDefaultResponseType]
public async Task<ActionResult<List<TelemetryDataDto>>> GetTelemetryData(Guid deviceId, string keys, DateTime begin) public async Task<ActionResult<List<TelemetryDataDto>>> GetTelemetryData(Guid deviceId, string keys, DateTime begin)
{ {
var dev = _context.Device.Find(deviceId); Device dev = Found(deviceId);
if (dev == null) if (dev == null)
{ {
return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found ")); return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found "));
...@@ -221,7 +253,7 @@ namespace IoTSharp.Controllers ...@@ -221,7 +253,7 @@ namespace IoTSharp.Controllers
[ProducesDefaultResponseType] [ProducesDefaultResponseType]
public async Task<ActionResult<List<TelemetryDataDto>>> GetTelemetryData(Guid deviceId, string keys, DateTime begin,DateTime end ) public async Task<ActionResult<List<TelemetryDataDto>>> GetTelemetryData(Guid deviceId, string keys, DateTime begin,DateTime end )
{ {
var dev = _context.Device.Find(deviceId); Device dev = Found(deviceId);
if (dev == null) if (dev == null)
{ {
return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found ")); return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found "));
...@@ -249,13 +281,11 @@ namespace IoTSharp.Controllers ...@@ -249,13 +281,11 @@ namespace IoTSharp.Controllers
[ProducesDefaultResponseType] [ProducesDefaultResponseType]
public async Task<ActionResult<Device>> GetDevice(Guid id) public async Task<ActionResult<Device>> GetDevice(Guid id)
{ {
var device = await _context.Device.FindAsync(id); Device device = await FoundAsync(id);
if (device == null) if (device == null)
{ {
return NotFound(new ApiResult<Guid>(ApiCode.NotFoundDevice, $"Device {id} not found ", id)); return NotFound(new ApiResult<Guid>(ApiCode.NotFoundDevice, $"Device {id} not found ", id));
} }
return device; return device;
} }
...@@ -353,7 +383,7 @@ namespace IoTSharp.Controllers ...@@ -353,7 +383,7 @@ namespace IoTSharp.Controllers
[ProducesDefaultResponseType] [ProducesDefaultResponseType]
public async Task<ActionResult<Device>> DeleteDevice(Guid id) public async Task<ActionResult<Device>> DeleteDevice(Guid id)
{ {
var device = await _context.Device.FindAsync(id); Device device = Found(id);
if (device == null) if (device == null)
{ {
return NotFound(new ApiResult<Guid>(ApiCode.NotFoundDevice, $"Device {id} not found ", id)); return NotFound(new ApiResult<Guid>(ApiCode.NotFoundDevice, $"Device {id} not found ", id));
......
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; }
}
}
using IoTSharp.Data; using IoTSharp.Data;
using Microsoft.AspNetCore.Mvc;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
...@@ -8,6 +9,7 @@ namespace IoTSharp.Extensions ...@@ -8,6 +9,7 @@ namespace IoTSharp.Extensions
{ {
public static class DeviceExtension public static class DeviceExtension
{ {
/// <summary> /// <summary>
/// When creating a device, all the things that need to be done here are done /// When creating a device, all the things that need to be done here are done
/// </summary> /// </summary>
......
...@@ -41,18 +41,18 @@ ...@@ -41,18 +41,18 @@
<PackageReference Include="AspNetCore.HealthChecks.Uris" Version="3.1.2" /> <PackageReference Include="AspNetCore.HealthChecks.Uris" Version="3.1.2" />
<PackageReference Include="AspNetCore.HealthChecks.Network" Version="3.1.1" /> <PackageReference Include="AspNetCore.HealthChecks.Network" Version="3.1.1" />
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="3.1.1" /> <PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="3.1.1" />
<PackageReference Include="EFCore.Sharding" Version="3.1.6.8" /> <PackageReference Include="EFCore.Sharding" Version="3.1.6.9" />
<PackageReference Include="EFCore.Sharding.PostgreSql" Version="3.1.6.8" /> <PackageReference Include="EFCore.Sharding.PostgreSql" Version="3.1.6.9" />
<PackageReference Include="IoTSharp.CoAP.NET" Version="2.0.8" /> <PackageReference Include="IoTSharp.CoAP.NET" Version="2.0.8" />
<PackageReference Include="IoTSharp.X509Extensions" Version="1.4.9" /> <PackageReference Include="IoTSharp.X509Extensions" Version="1.4.9" />
<PackageReference Include="kimbus" Version="2.0.1" /> <PackageReference Include="kimbus" Version="2.0.1" />
<PackageReference Include="LiteDB" Version="5.0.9" /> <PackageReference Include="LiteDB" Version="5.0.9" />
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="3.1.6" /> <PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="3.1.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.6"> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.7">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.6" /> <PackageReference Include="Microsoft.Extensions.Options" Version="3.1.7" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" /> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
<PackageReference Include="MQTTnet" Version="3.0.12" /> <PackageReference Include="MQTTnet" Version="3.0.12" />
...@@ -60,7 +60,7 @@ ...@@ -60,7 +60,7 @@
<PackageReference Include="MQTTnet.Extensions.Rpc" Version="3.0.12" /> <PackageReference Include="MQTTnet.Extensions.Rpc" Version="3.0.12" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
<PackageReference Include="NSwag.AspNetCore" Version="13.7.0" /> <PackageReference Include="NSwag.AspNetCore" Version="13.7.0" />
<PackageReference Include="OptimizedPriorityQueue" Version="4.2.0" /> <PackageReference Include="OptimizedPriorityQueue" Version="5.0.0" />
<PackageReference Include="ProxyKit" Version="2.3.3" /> <PackageReference Include="ProxyKit" Version="2.3.3" />
<PackageReference Include="Silkier" Version="1.0.121" /> <PackageReference Include="Silkier" Version="1.0.121" />
<PackageReference Include="Silkier.AspNetCore" Version="1.0.121" /> <PackageReference Include="Silkier.AspNetCore" Version="1.0.121" />
...@@ -68,15 +68,15 @@ ...@@ -68,15 +68,15 @@
<PackageReference Include="SilkierQuartz" Version="1.0.46" /> <PackageReference Include="SilkierQuartz" Version="1.0.46" />
<PackageReference Include="System.ServiceModel.Primitives" Version="4.7.0" /> <PackageReference Include="System.ServiceModel.Primitives" Version="4.7.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.1" /> <PackageReference Include="System.Text.Encoding.CodePages" Version="4.7.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.6" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.7" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.6" /> <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.7" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="3.1.6" /> <PackageReference Include="Microsoft.AspNetCore.Hosting.WindowsServices" Version="3.1.7" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.6" /> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.7" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="3.1.6" /> <PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="3.1.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.6" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.6" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.6" /> <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.1.6" /> <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.1.7" />
<PackageReference Include="MQTTnet.AspNetCoreEx" Version="3.0.11" /> <PackageReference Include="MQTTnet.AspNetCoreEx" Version="3.0.11" />
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="3.0.12" /> <PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="3.0.12" />
</ItemGroup> </ItemGroup>
......
...@@ -176,6 +176,14 @@ ...@@ -176,6 +176,14 @@
<param name="deviceId"></param> <param name="deviceId"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:IoTSharp.Controllers.DevicesController.GetAttributeLatest(System.Guid,System.String)">
<summary>
获取指定设备指定keys的最新属性
</summary>
<param name="deviceId">Which device do you read?</param>
<param name="keys">Specify key name list , use , or space or ; to split </param>
<returns></returns>
</member>
<member name="M:IoTSharp.Controllers.DevicesController.GetTelemetryLatest(System.Guid)"> <member name="M:IoTSharp.Controllers.DevicesController.GetTelemetryLatest(System.Guid)">
<summary> <summary>
获取指定设备的最新遥测数据 获取指定设备的最新遥测数据
...@@ -191,14 +199,6 @@ ...@@ -191,14 +199,6 @@
<param name="keys">指定键值列表, 使用分号或者逗号分割 。 </param> <param name="keys">指定键值列表, 使用分号或者逗号分割 。 </param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:IoTSharp.Controllers.DevicesController.GetAttributeLatest(System.Guid,System.String)">
<summary>
获取指定设备指定keys的最新属性
</summary>
<param name="deviceId">Which device do you read?</param>
<param name="keys">Specify key name list , use , or space or ; to split </param>
<returns></returns>
</member>
<member name="M:IoTSharp.Controllers.DevicesController.GetTelemetryData(System.Guid,System.String,System.DateTime)"> <member name="M:IoTSharp.Controllers.DevicesController.GetTelemetryData(System.Guid,System.String,System.DateTime)">
<summary> <summary>
获取指定设备和指定时间, 指定key的数据 获取指定设备和指定时间, 指定key的数据
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册