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

gateway can create device

上级 5d951f9c
...@@ -136,7 +136,7 @@ namespace IoTSharp.Controllers ...@@ -136,7 +136,7 @@ namespace IoTSharp.Controllers
// PUT: api/Devices/5 // PUT: api/Devices/5
[Authorize(Roles = nameof(UserRole.CustomerAdmin))] [Authorize(Roles = nameof(UserRole.CustomerAdmin))]
[HttpPut("{id}")] [HttpPut("{id}")]
public async Task<IActionResult> PutDevice(Guid id, Device device) public async Task<IActionResult> PutDevice(Guid id, DevicePutDto device)
{ {
if (id != device.Id) if (id != device.Id)
{ {
...@@ -145,7 +145,7 @@ namespace IoTSharp.Controllers ...@@ -145,7 +145,7 @@ namespace IoTSharp.Controllers
var cid = User.Claims.First(c => c.Type == IoTSharpClaimTypes.Customer); var cid = User.Claims.First(c => c.Type == IoTSharpClaimTypes.Customer);
var tid = User.Claims.First(c => c.Type == IoTSharpClaimTypes.Tenant); var tid = User.Claims.First(c => c.Type == IoTSharpClaimTypes.Tenant);
var dev = _context.Device.First(d => d.Id == device.Id); var dev = _context.Device.Include(d=>d.Tenant).Include(d=>d.Customer).First(d => d.Id == device.Id);
var tenid = dev.Tenant.Id; var tenid = dev.Tenant.Id;
var cusid = dev.Customer.Id; var cusid = dev.Customer.Id;
...@@ -157,9 +157,7 @@ namespace IoTSharp.Controllers ...@@ -157,9 +157,7 @@ namespace IoTSharp.Controllers
{ {
return BadRequest(new ApiResult(ApiCode.DoNotAllow, $"Do not allow access to devices from other customers or tenants")); return BadRequest(new ApiResult(ApiCode.DoNotAllow, $"Do not allow access to devices from other customers or tenants"));
} }
dev.Name = device.Name;
_context.Entry(device).State = EntityState.Modified;
try try
{ {
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
...@@ -199,12 +197,7 @@ namespace IoTSharp.Controllers ...@@ -199,12 +197,7 @@ namespace IoTSharp.Controllers
return NotFound(new ApiResult<Device>(ApiCode.NotFoundTenantOrCustomer, $"Not found Tenant or Customer ", device)); return NotFound(new ApiResult<Device>(ApiCode.NotFoundTenantOrCustomer, $"Not found Tenant or Customer ", device));
} }
_context.Device.Add(device); _context.Device.Add(device);
_context.DeviceIdentities.Add(new DeviceIdentity() _context.AfterCreateDevice(device);
{
Device = device,
IdentityType = IdentityType.AccessToken,
IdentityId = Guid.NewGuid().ToString().Replace("-", "")
});
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return await GetDevice(device.Id); return await GetDevice(device.Id);
} }
......
using IoTSharp.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace IoTSharp.Dtos
{
public class DevicePutDto
{
public Guid Id { get; set; }
public string Name { get; set; }
}
}
...@@ -20,7 +20,7 @@ namespace IoTSharp.Extensions ...@@ -20,7 +20,7 @@ namespace IoTSharp.Extensions
return (ok, devices.FirstOrDefault()); return (ok, devices.FirstOrDefault());
} }
/// <summary> /// <summary>
/// /// Save Data to Device's <typeparamref name="D"/> and <typeparamref name="L"/>
/// </summary> /// </summary>
/// <typeparam name="L">Latest</typeparam> /// <typeparam name="L">Latest</typeparam>
/// <typeparam name="D">Data</typeparam> /// <typeparam name="D">Data</typeparam>
...@@ -28,6 +28,24 @@ namespace IoTSharp.Extensions ...@@ -28,6 +28,24 @@ namespace IoTSharp.Extensions
/// <param name="device"></param> /// <param name="device"></param>
/// <returns></returns> /// <returns></returns>
internal static async Task<(int ret, Dic exceptions)> SaveAsync<L, D>(this ApplicationDbContext _context, Dictionary<string, object> data, Device device, DataSide dataSide) where L : DataStorage, new() where D : DataStorage, new() internal static async Task<(int ret, Dic exceptions)> SaveAsync<L, D>(this ApplicationDbContext _context, Dictionary<string, object> data, Device device, DataSide dataSide) where L : DataStorage, new() where D : DataStorage, new()
{
Dic exceptions = _context.PreparingData<L, D>( data, device, dataSide);
int ret = await _context.SaveChangesAsync();
return (ret, exceptions);
}
/// <summary>
/// Preparing Data to Device's <typeparamref name="D"/> and <typeparamref name="L"/>
/// </summary>
/// <typeparam name="L"></typeparam>
/// <typeparam name="D"></typeparam>
/// <param name="_context"></param>
/// <param name="data"></param>
/// <param name="device"></param>
/// <param name="dataSide"></param>
/// <returns></returns>
internal static Dic PreparingData<L, D>(this ApplicationDbContext _context, Dictionary<string, object> data, Device device, DataSide dataSide)
where L : DataStorage, new()
where D : DataStorage, new()
{ {
Dic exceptions = new Dic(); Dic exceptions = new Dic();
data.ToList().ForEach(kp => data.ToList().ForEach(kp =>
...@@ -59,9 +77,9 @@ namespace IoTSharp.Extensions ...@@ -59,9 +77,9 @@ namespace IoTSharp.Extensions
exceptions.Add(kp.Key, ex.Message); exceptions.Add(kp.Key, ex.Message);
} }
}); });
int ret = await _context.SaveChangesAsync(); return exceptions;
return (ret, exceptions);
} }
public static object JPropertyToObject(this JProperty property) public static object JPropertyToObject(this JProperty property)
{ {
object obj = null; object obj = null;
......
using IoTSharp.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace IoTSharp.Extensions
{
public static class DeviceExtension
{
/// <summary>
/// When creating a device, all the things that need to be done here are done
/// </summary>
/// <param name="_context"></param>
/// <param name="device"></param>
public static void AfterCreateDevice(this ApplicationDbContext _context, Device device)
{
if (device.Customer == null || device.Tenant == null || string.IsNullOrEmpty(device.Name))
{
throw new Exception("Customer or Tenant or Name is null or empty!");
}
else
{
_context.DeviceIdentities.Add(new DeviceIdentity()
{
Device = device,
IdentityType = IdentityType.AccessToken,
IdentityId = Guid.NewGuid().ToString().Replace("-", "")
});
Dictionary<string, object> pairs = new Dictionary<string, object>();
pairs.Add("CreateDateTime", DateTime.Now);
_context.PreparingData<AttributeLatest, AttributeData>(pairs, device, DataSide.ServerSide);
}
}
}
}
...@@ -129,13 +129,14 @@ namespace IoTSharp.Handlers ...@@ -129,13 +129,14 @@ namespace IoTSharp.Handlers
Device devicedatato = device; Device devicedatato = device;
if (tpary[1] != "me" && device.DeviceType == DeviceType.Gateway) if (tpary[1] != "me" && device.DeviceType == DeviceType.Gateway)
{ {
var ch = from g in _dbContext.Gateway.Include(c => c.Children) where g.Id == device.Id select g; var ch = from g in _dbContext.Gateway.Include(g=>g.Tenant).Include(g=>g.Customer).Include(c => c.Children) where g.Id == device.Id select g;
var gw = ch.FirstOrDefault(); var gw = ch.FirstOrDefault();
var subdev = from cd in gw.Children where cd.Name == tpary[1] select cd; var subdev = from cd in gw.Children where cd.Name == tpary[1] select cd;
if (!subdev.Any()) if (!subdev.Any())
{ {
devicedatato = new Device() { Id = Guid.NewGuid(), Name = tpary[1], DeviceType = DeviceType.Device, Tenant = device.Tenant, Customer = device.Customer }; devicedatato = new Device() { Id = Guid.NewGuid(), Name = tpary[1], DeviceType = DeviceType.Device, Tenant = gw.Tenant, Customer = gw.Customer, Owner=gw };
gw.Children.Add(devicedatato); gw.Children.Add(devicedatato);
_dbContext.AfterCreateDevice(devicedatato);
_dbContext.SaveChangesAsync(); _dbContext.SaveChangesAsync();
} }
else else
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册