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

add device owner and auto create device

上级 f3f171da
// <auto-generated />
using System;
using IoTSharp.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace IoTSharp.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20190518041926_DeviceOwner")]
partial class DeviceOwner
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn)
.HasAnnotation("ProductVersion", "2.2.4-servicing-10062")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
modelBuilder.Entity("IoTSharp.Data.AuditLog", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ActionData")
.HasColumnType("jsonb");
b.Property<string>("ActionName");
b.Property<string>("ActionResult")
.HasColumnType("jsonb");
b.Property<DateTime>("ActiveDateTime");
b.Property<Guid?>("CustomerId");
b.Property<Guid>("ObjectID");
b.Property<string>("ObjectName");
b.Property<int>("ObjectType");
b.Property<Guid?>("TenantId");
b.Property<string>("UserId");
b.Property<string>("UserName");
b.HasKey("Id");
b.HasIndex("CustomerId");
b.HasIndex("TenantId");
b.ToTable("AuditLog");
});
modelBuilder.Entity("IoTSharp.Data.Customer", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Address");
b.Property<string>("City");
b.Property<string>("Country");
b.Property<string>("Email");
b.Property<string>("Name");
b.Property<string>("Phone");
b.Property<string>("Province");
b.Property<string>("Street");
b.Property<Guid?>("TenantId");
b.Property<int>("ZipCode");
b.HasKey("Id");
b.HasIndex("TenantId");
b.ToTable("Customer");
});
modelBuilder.Entity("IoTSharp.Data.DataStorage", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("Catalog");
b.Property<int>("DataSide");
b.Property<DateTime>("DateTime");
b.Property<string>("KeyName")
.IsRequired();
b.Property<int>("Type");
b.Property<byte[]>("Value_Binary");
b.Property<bool>("Value_Boolean");
b.Property<DateTime>("Value_DateTime");
b.Property<double>("Value_Double");
b.Property<string>("Value_Json")
.HasColumnType("jsonb");
b.Property<long>("Value_Long");
b.Property<string>("Value_String");
b.Property<string>("Value_XML")
.HasColumnType("xml");
b.HasKey("Id");
b.ToTable("DataStorage");
b.HasDiscriminator<int>("Catalog").HasValue(0);
});
modelBuilder.Entity("IoTSharp.Data.Device", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<Guid?>("CustomerId");
b.Property<int>("DeviceType");
b.Property<string>("Name");
b.Property<Guid?>("OwnerId");
b.Property<Guid?>("TenantId");
b.HasKey("Id");
b.HasIndex("CustomerId");
b.HasIndex("OwnerId");
b.HasIndex("TenantId");
b.ToTable("Device");
b.HasDiscriminator<int>("DeviceType").HasValue(0);
});
modelBuilder.Entity("IoTSharp.Data.DeviceIdentity", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<Guid>("DeviceId");
b.Property<string>("IdentityId")
.IsRequired();
b.Property<int>("IdentityType");
b.Property<string>("IdentityValue");
b.HasKey("Id");
b.HasIndex("DeviceId");
b.ToTable("DeviceIdentities");
});
modelBuilder.Entity("IoTSharp.Data.Relationship", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<Guid?>("CustomerId");
b.Property<string>("IdentityUserId");
b.Property<Guid?>("TenantId");
b.HasKey("Id");
b.HasIndex("CustomerId");
b.HasIndex("IdentityUserId");
b.HasIndex("TenantId");
b.ToTable("Relationship");
});
modelBuilder.Entity("IoTSharp.Data.Tenant", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Address");
b.Property<string>("City");
b.Property<string>("Country");
b.Property<string>("EMail");
b.Property<string>("Name");
b.Property<string>("Phone");
b.Property<string>("Province");
b.Property<string>("Street");
b.Property<int>("ZipCode");
b.HasKey("Id");
b.ToTable("Tenant");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<string>("Name")
.HasMaxLength(256);
b.Property<string>("NormalizedName")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("NormalizedName")
.IsUnique()
.HasName("RoleNameIndex");
b.ToTable("AspNetRoles");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ClaimType");
b.Property<string>("ClaimValue");
b.Property<string>("RoleId")
.IsRequired();
b.HasKey("Id");
b.HasIndex("RoleId");
b.ToTable("AspNetRoleClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("AccessFailedCount");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<string>("Email")
.HasMaxLength(256);
b.Property<bool>("EmailConfirmed");
b.Property<bool>("LockoutEnabled");
b.Property<DateTimeOffset?>("LockoutEnd");
b.Property<string>("NormalizedEmail")
.HasMaxLength(256);
b.Property<string>("NormalizedUserName")
.HasMaxLength(256);
b.Property<string>("PasswordHash");
b.Property<string>("PhoneNumber");
b.Property<bool>("PhoneNumberConfirmed");
b.Property<string>("SecurityStamp");
b.Property<bool>("TwoFactorEnabled");
b.Property<string>("UserName")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("NormalizedEmail")
.HasName("EmailIndex");
b.HasIndex("NormalizedUserName")
.IsUnique()
.HasName("UserNameIndex");
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ClaimType");
b.Property<string>("ClaimValue");
b.Property<string>("UserId")
.IsRequired();
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("AspNetUserClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.Property<string>("LoginProvider");
b.Property<string>("ProviderKey");
b.Property<string>("ProviderDisplayName");
b.Property<string>("UserId")
.IsRequired();
b.HasKey("LoginProvider", "ProviderKey");
b.HasIndex("UserId");
b.ToTable("AspNetUserLogins");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.Property<string>("UserId");
b.Property<string>("RoleId");
b.HasKey("UserId", "RoleId");
b.HasIndex("RoleId");
b.ToTable("AspNetUserRoles");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.Property<string>("UserId");
b.Property<string>("LoginProvider");
b.Property<string>("Name");
b.Property<string>("Value");
b.HasKey("UserId", "LoginProvider", "Name");
b.ToTable("AspNetUserTokens");
});
modelBuilder.Entity("IoTSharp.Data.AttributeData", b =>
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.Property<Guid>("DeviceId");
b.HasIndex("DeviceId");
b.HasDiscriminator().HasValue(1);
});
modelBuilder.Entity("IoTSharp.Data.AttributeLatest", b =>
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.Property<Guid>("DeviceId")
.HasColumnName("AttributeLatest_DeviceId");
b.HasIndex("DeviceId");
b.HasDiscriminator().HasValue(2);
});
modelBuilder.Entity("IoTSharp.Data.TelemetryData", b =>
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.Property<Guid>("DeviceId")
.HasColumnName("TelemetryData_DeviceId");
b.HasIndex("DeviceId");
b.HasDiscriminator().HasValue(3);
});
modelBuilder.Entity("IoTSharp.Data.TelemetryLatest", b =>
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.Property<Guid>("DeviceId")
.HasColumnName("TelemetryLatest_DeviceId");
b.HasIndex("DeviceId");
b.HasDiscriminator().HasValue(4);
});
modelBuilder.Entity("IoTSharp.Data.Gateway", b =>
{
b.HasBaseType("IoTSharp.Data.Device");
b.HasDiscriminator().HasValue(1);
});
modelBuilder.Entity("IoTSharp.Data.AuditLog", b =>
{
b.HasOne("IoTSharp.Data.Customer", "Customer")
.WithMany()
.HasForeignKey("CustomerId");
b.HasOne("IoTSharp.Data.Tenant", "Tenant")
.WithMany()
.HasForeignKey("TenantId");
});
modelBuilder.Entity("IoTSharp.Data.Customer", b =>
{
b.HasOne("IoTSharp.Data.Tenant", "Tenant")
.WithMany("Customers")
.HasForeignKey("TenantId");
});
modelBuilder.Entity("IoTSharp.Data.Device", b =>
{
b.HasOne("IoTSharp.Data.Customer", "Customer")
.WithMany("Devices")
.HasForeignKey("CustomerId");
b.HasOne("IoTSharp.Data.Gateway", "Owner")
.WithMany("Children")
.HasForeignKey("OwnerId");
b.HasOne("IoTSharp.Data.Tenant", "Tenant")
.WithMany("Devices")
.HasForeignKey("TenantId");
});
modelBuilder.Entity("IoTSharp.Data.DeviceIdentity", b =>
{
b.HasOne("IoTSharp.Data.Device", "Device")
.WithMany()
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Data.Relationship", b =>
{
b.HasOne("IoTSharp.Data.Customer", "Customer")
.WithMany()
.HasForeignKey("CustomerId");
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", "IdentityUser")
.WithMany()
.HasForeignKey("IdentityUserId");
b.HasOne("IoTSharp.Data.Tenant", "Tenant")
.WithMany()
.HasForeignKey("TenantId");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Data.AttributeData", b =>
{
b.HasOne("IoTSharp.Data.Device", "Device")
.WithMany("AttributeData")
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Data.AttributeLatest", b =>
{
b.HasOne("IoTSharp.Data.Device", "Device")
.WithMany("AttributeLatest")
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Data.TelemetryData", b =>
{
b.HasOne("IoTSharp.Data.Device", "Device")
.WithMany("TelemetryData")
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Data.TelemetryLatest", b =>
{
b.HasOne("IoTSharp.Data.Device", "Device")
.WithMany("TelemetryLatest")
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
#pragma warning restore 612, 618
}
}
}
using Microsoft.EntityFrameworkCore.Migrations;
namespace IoTSharp.Migrations
{
public partial class DeviceOwner : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Device_Device_GatewayId",
table: "Device");
migrationBuilder.RenameColumn(
name: "GatewayId",
table: "Device",
newName: "OwnerId");
migrationBuilder.RenameIndex(
name: "IX_Device_GatewayId",
table: "Device",
newName: "IX_Device_OwnerId");
migrationBuilder.AddForeignKey(
name: "FK_Device_Device_OwnerId",
table: "Device",
column: "OwnerId",
principalTable: "Device",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Device_Device_OwnerId",
table: "Device");
migrationBuilder.RenameColumn(
name: "OwnerId",
table: "Device",
newName: "GatewayId");
migrationBuilder.RenameIndex(
name: "IX_Device_OwnerId",
table: "Device",
newName: "IX_Device_GatewayId");
migrationBuilder.AddForeignKey(
name: "FK_Device_Device_GatewayId",
table: "Device",
column: "GatewayId",
principalTable: "Device",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
}
}
......@@ -139,17 +139,17 @@ namespace IoTSharp.Migrations
b.Property<int>("DeviceType");
b.Property<Guid?>("GatewayId");
b.Property<string>("Name");
b.Property<Guid?>("OwnerId");
b.Property<Guid?>("TenantId");
b.HasKey("Id");
b.HasIndex("CustomerId");
b.HasIndex("GatewayId");
b.HasIndex("OwnerId");
b.HasIndex("TenantId");
......@@ -464,9 +464,9 @@ namespace IoTSharp.Migrations
.WithMany("Devices")
.HasForeignKey("CustomerId");
b.HasOne("IoTSharp.Data.Gateway")
b.HasOne("IoTSharp.Data.Gateway", "Owner")
.WithMany("Children")
.HasForeignKey("GatewayId");
.HasForeignKey("OwnerId");
b.HasOne("IoTSharp.Data.Tenant", "Tenant")
.WithMany("Devices")
......
......@@ -69,7 +69,7 @@ namespace IoTSharp.Handlers
var tpary = topic.Split('/');
if (tpary.Length >= 3 && tpary[0] == "devices" && Devices.ContainsKey(e.ClientId))
{
Device device = JudgeDevice(tpary, Devices[e.ClientId]);
Device device = JudgeOrCreateNewDevice(tpary, Devices[e.ClientId]);
if (device != null)
{
if (tpary[2] == "telemetry")
......@@ -111,15 +111,24 @@ namespace IoTSharp.Handlers
}
private Device JudgeDevice(string[] tpary, Device device)
private Device JudgeOrCreateNewDevice(string[] tpary, Device device)
{
Device devicedatato = device;
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.Children;
//if (ch.Any)
//var subdev = from cd in ().FirstOrDefault() where cd.Name == tpary[1] select cd ;
//devicedatato = subdev.FirstOrDefault();
var ch = from g in _dbContext.Gateway.Include(c => c.Children) where g.Id == device.Id select g;
var gw = ch.FirstOrDefault();
var subdev = from cd in gw.Children where cd.Name == tpary[1] select cd;
if (!subdev.Any())
{
devicedatato = new Device() { Id = Guid.NewGuid(), Name = tpary[1], DeviceType = DeviceType.Device, Tenant = device.Tenant, Customer = device.Customer };
gw.Children.Add(devicedatato);
_dbContext.SaveChangesAsync();
}
else
{
devicedatato = subdev.FirstOrDefault();
}
}
return devicedatato;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册