From b44f7c78241aad9a0e060ffed75d83bac2b164bd Mon Sep 17 00:00:00 2001 From: ygl Date: Mon, 28 Nov 2022 18:06:58 +0800 Subject: [PATCH] u --- realm_cli/AppDbContext.cs | 4 + realm_cli/EcuCfg.cs | 21 +++++ realm_cli/EcuMdl.cs | 7 ++ realm_cli/LiveData.cs | 2 + realm_cli/LiveDataMTL.cs | 7 ++ realm_cli/LiveDataMsg.cs | 2 + .../20221121020938_AddEcuModel.Designer.cs | 54 ++++++++++++ .../Migrations/20221121020938_AddEcuModel.cs | 46 ++++++++++ ...1121021731_AddLiveDataMTLModel.Designer.cs | 69 +++++++++++++++ .../20221121021731_AddLiveDataMTLModel.cs | 29 +++++++ ...221121022400_EcuModelAddRemark.Designer.cs | 74 ++++++++++++++++ .../20221121022400_EcuModelAddRemark.cs | 24 ++++++ ...121023143_EcuModelRemoveRemark.Designer.cs | 69 +++++++++++++++ .../20221121023143_EcuModelRemoveRemark.cs | 24 ++++++ .../Migrations/AppDbContextModelSnapshot.cs | 67 +++++++++++++++ realm_cli/Program.cs | 84 +++++++++++++++++-- realm_cli/realm_cli.csproj | 8 +- 17 files changed, 580 insertions(+), 11 deletions(-) create mode 100644 realm_cli/EcuCfg.cs create mode 100644 realm_cli/EcuMdl.cs create mode 100644 realm_cli/LiveDataMTL.cs create mode 100644 realm_cli/Migrations/20221121020938_AddEcuModel.Designer.cs create mode 100644 realm_cli/Migrations/20221121020938_AddEcuModel.cs create mode 100644 realm_cli/Migrations/20221121021731_AddLiveDataMTLModel.Designer.cs create mode 100644 realm_cli/Migrations/20221121021731_AddLiveDataMTLModel.cs create mode 100644 realm_cli/Migrations/20221121022400_EcuModelAddRemark.Designer.cs create mode 100644 realm_cli/Migrations/20221121022400_EcuModelAddRemark.cs create mode 100644 realm_cli/Migrations/20221121023143_EcuModelRemoveRemark.Designer.cs create mode 100644 realm_cli/Migrations/20221121023143_EcuModelRemoveRemark.cs create mode 100644 realm_cli/Migrations/AppDbContextModelSnapshot.cs diff --git a/realm_cli/AppDbContext.cs b/realm_cli/AppDbContext.cs index 95bfe4d..3df6786 100644 --- a/realm_cli/AppDbContext.cs +++ b/realm_cli/AppDbContext.cs @@ -8,6 +8,8 @@ namespace realm_cli; public class AppDbContext: DbContext { public DbSet LvStates { get; set; } + public DbSet EcuMdls { get; set; } + public DbSet LiveDataMtls { get; set; } public AppDbContext() { @@ -28,6 +30,8 @@ public class AppDbContext: DbContext protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().ToTable("lv_states").HasKey(i=>i.ItemId); + modelBuilder.Entity().ToTable("ecus"); + //modelBuilder.Entity().Property(i => i.Remark).HasColumnName("remark"); } private void ExecutePragma(SqliteConnection conn, string pragmaCommand) diff --git a/realm_cli/EcuCfg.cs b/realm_cli/EcuCfg.cs new file mode 100644 index 0000000..952b443 --- /dev/null +++ b/realm_cli/EcuCfg.cs @@ -0,0 +1,21 @@ +using Realms; + +namespace realm_cli; + +public class EcuCfg: RealmObject +{ + [PrimaryKey] + public string Id { get; set; } + + public string Name { get; set; } + + public bool IsLiving { get; set; } +} + +public class EcuGroupCfg : RealmObject +{ + [PrimaryKey] public string Id { get; set; } + public IList Ecus { get; } + + public string Name { get; set; } +} diff --git a/realm_cli/EcuMdl.cs b/realm_cli/EcuMdl.cs new file mode 100644 index 0000000..fc53bbc --- /dev/null +++ b/realm_cli/EcuMdl.cs @@ -0,0 +1,7 @@ +namespace realm_cli; + +public class EcuMdl +{ + public long Id { get; set; } + public string Name { get; set; } +} \ No newline at end of file diff --git a/realm_cli/LiveData.cs b/realm_cli/LiveData.cs index 528511f..dc86a92 100644 --- a/realm_cli/LiveData.cs +++ b/realm_cli/LiveData.cs @@ -188,6 +188,8 @@ public class LiveData : RealmObject OnPropertyChanged(nameof(GaugeScaleInterval)); } } + + public bool IsTest { get; set; } public override string ToString() { diff --git a/realm_cli/LiveDataMTL.cs b/realm_cli/LiveDataMTL.cs new file mode 100644 index 0000000..3adf97c --- /dev/null +++ b/realm_cli/LiveDataMTL.cs @@ -0,0 +1,7 @@ +namespace realm_cli; + +public class LiveDataMTL +{ + public long Id { get; set; } + public string Name { get; set; } +} \ No newline at end of file diff --git a/realm_cli/LiveDataMsg.cs b/realm_cli/LiveDataMsg.cs index 4998d92..f9d8721 100644 --- a/realm_cli/LiveDataMsg.cs +++ b/realm_cli/LiveDataMsg.cs @@ -11,4 +11,6 @@ public class LiveDataMsg : RealmObject public DateTimeOffset? Uts { get; set; } public string Text { get; set; } + + public bool IsTest { get; set; } } \ No newline at end of file diff --git a/realm_cli/Migrations/20221121020938_AddEcuModel.Designer.cs b/realm_cli/Migrations/20221121020938_AddEcuModel.Designer.cs new file mode 100644 index 0000000..9da88a5 --- /dev/null +++ b/realm_cli/Migrations/20221121020938_AddEcuModel.Designer.cs @@ -0,0 +1,54 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using realm_cli; + +namespace realm_cli.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20221121020938_AddEcuModel")] + partial class AddEcuModel + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "5.0.17"); + + modelBuilder.Entity("realm_cli.EcuMdl", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("ecus"); + }); + + modelBuilder.Entity("realm_cli.LiveDataState", b => + { + b.Property("ItemId") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("EcuId") + .HasColumnType("INTEGER"); + + b.Property("Value") + .HasColumnType("REAL"); + + b.HasKey("ItemId"); + + b.ToTable("lv_states"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/realm_cli/Migrations/20221121020938_AddEcuModel.cs b/realm_cli/Migrations/20221121020938_AddEcuModel.cs new file mode 100644 index 0000000..392185b --- /dev/null +++ b/realm_cli/Migrations/20221121020938_AddEcuModel.cs @@ -0,0 +1,46 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace realm_cli.Migrations +{ + public partial class AddEcuModel : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "ecus", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ecus", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "lv_states", + columns: table => new + { + ItemId = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + EcuId = table.Column(type: "INTEGER", nullable: false), + Value = table.Column(type: "REAL", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_lv_states", x => x.ItemId); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ecus"); + + migrationBuilder.DropTable( + name: "lv_states"); + } + } +} diff --git a/realm_cli/Migrations/20221121021731_AddLiveDataMTLModel.Designer.cs b/realm_cli/Migrations/20221121021731_AddLiveDataMTLModel.Designer.cs new file mode 100644 index 0000000..86ab606 --- /dev/null +++ b/realm_cli/Migrations/20221121021731_AddLiveDataMTLModel.Designer.cs @@ -0,0 +1,69 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using realm_cli; + +namespace realm_cli.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20221121021731_AddLiveDataMTLModel")] + partial class AddLiveDataMTLModel + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "5.0.17"); + + modelBuilder.Entity("realm_cli.EcuMdl", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("ecus"); + }); + + modelBuilder.Entity("realm_cli.LiveDataMTL", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("LiveDataMtls"); + }); + + modelBuilder.Entity("realm_cli.LiveDataState", b => + { + b.Property("ItemId") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("EcuId") + .HasColumnType("INTEGER"); + + b.Property("Value") + .HasColumnType("REAL"); + + b.HasKey("ItemId"); + + b.ToTable("lv_states"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/realm_cli/Migrations/20221121021731_AddLiveDataMTLModel.cs b/realm_cli/Migrations/20221121021731_AddLiveDataMTLModel.cs new file mode 100644 index 0000000..892ea3e --- /dev/null +++ b/realm_cli/Migrations/20221121021731_AddLiveDataMTLModel.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace realm_cli.Migrations +{ + public partial class AddLiveDataMTLModel : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "LiveDataMtls", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_LiveDataMtls", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "LiveDataMtls"); + } + } +} diff --git a/realm_cli/Migrations/20221121022400_EcuModelAddRemark.Designer.cs b/realm_cli/Migrations/20221121022400_EcuModelAddRemark.Designer.cs new file mode 100644 index 0000000..dcc9b9a --- /dev/null +++ b/realm_cli/Migrations/20221121022400_EcuModelAddRemark.Designer.cs @@ -0,0 +1,74 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using realm_cli; + +namespace realm_cli.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20221121022400_EcuModelAddRemark")] + partial class EcuModelAddRemark + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "5.0.17"); + + modelBuilder.Entity("realm_cli.EcuMdl", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Remark") + .IsRequired() + .HasColumnType("TEXT") + .HasColumnName("remark"); + + b.HasKey("Id"); + + b.ToTable("ecus"); + }); + + modelBuilder.Entity("realm_cli.LiveDataMTL", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("LiveDataMtls"); + }); + + modelBuilder.Entity("realm_cli.LiveDataState", b => + { + b.Property("ItemId") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("EcuId") + .HasColumnType("INTEGER"); + + b.Property("Value") + .HasColumnType("REAL"); + + b.HasKey("ItemId"); + + b.ToTable("lv_states"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/realm_cli/Migrations/20221121022400_EcuModelAddRemark.cs b/realm_cli/Migrations/20221121022400_EcuModelAddRemark.cs new file mode 100644 index 0000000..79805b2 --- /dev/null +++ b/realm_cli/Migrations/20221121022400_EcuModelAddRemark.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace realm_cli.Migrations +{ + public partial class EcuModelAddRemark : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "remark", + table: "ecus", + type: "TEXT", + nullable: false, + defaultValue: ""); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "remark", + table: "ecus"); + } + } +} diff --git a/realm_cli/Migrations/20221121023143_EcuModelRemoveRemark.Designer.cs b/realm_cli/Migrations/20221121023143_EcuModelRemoveRemark.Designer.cs new file mode 100644 index 0000000..0dc32d8 --- /dev/null +++ b/realm_cli/Migrations/20221121023143_EcuModelRemoveRemark.Designer.cs @@ -0,0 +1,69 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using realm_cli; + +namespace realm_cli.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20221121023143_EcuModelRemoveRemark")] + partial class EcuModelRemoveRemark + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "5.0.17"); + + modelBuilder.Entity("realm_cli.EcuMdl", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("ecus"); + }); + + modelBuilder.Entity("realm_cli.LiveDataMTL", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("LiveDataMtls"); + }); + + modelBuilder.Entity("realm_cli.LiveDataState", b => + { + b.Property("ItemId") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("EcuId") + .HasColumnType("INTEGER"); + + b.Property("Value") + .HasColumnType("REAL"); + + b.HasKey("ItemId"); + + b.ToTable("lv_states"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/realm_cli/Migrations/20221121023143_EcuModelRemoveRemark.cs b/realm_cli/Migrations/20221121023143_EcuModelRemoveRemark.cs new file mode 100644 index 0000000..b090d3c --- /dev/null +++ b/realm_cli/Migrations/20221121023143_EcuModelRemoveRemark.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace realm_cli.Migrations +{ + public partial class EcuModelRemoveRemark : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "remark", + table: "ecus"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "remark", + table: "ecus", + type: "TEXT", + nullable: false, + defaultValue: ""); + } + } +} diff --git a/realm_cli/Migrations/AppDbContextModelSnapshot.cs b/realm_cli/Migrations/AppDbContextModelSnapshot.cs new file mode 100644 index 0000000..a27d1a1 --- /dev/null +++ b/realm_cli/Migrations/AppDbContextModelSnapshot.cs @@ -0,0 +1,67 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using realm_cli; + +namespace realm_cli.Migrations +{ + [DbContext(typeof(AppDbContext))] + partial class AppDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "5.0.17"); + + modelBuilder.Entity("realm_cli.EcuMdl", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("ecus"); + }); + + modelBuilder.Entity("realm_cli.LiveDataMTL", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("LiveDataMtls"); + }); + + modelBuilder.Entity("realm_cli.LiveDataState", b => + { + b.Property("ItemId") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("EcuId") + .HasColumnType("INTEGER"); + + b.Property("Value") + .HasColumnType("REAL"); + + b.HasKey("ItemId"); + + b.ToTable("lv_states"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/realm_cli/Program.cs b/realm_cli/Program.cs index 743eb87..c9bc6da 100644 --- a/realm_cli/Program.cs +++ b/realm_cli/Program.cs @@ -20,7 +20,7 @@ LiveDataMsg msg1 = null; Realm GetDB() { var cfg = new RealmConfiguration("c:/prjs/realm_test.realm"); - cfg.SchemaVersion = 4; + cfg.SchemaVersion = 6; return Realm.GetInstance(cfg); } @@ -177,8 +177,8 @@ async Task InsertLiveData() //item.PropertyChanged += PropChanged; // item.ItemId = i; // item.ValueLabel = item.Val.ToString(); - var db = GetDB(); - for (var i = 0; i != 10000; ++i) + using var db = GetDB(); + for (var i = 0; i != 100; ++i) { var item = new LiveData() {ItemId = i, EcuId = 0, Val = new Random().NextSingle()*i}; db.Write(() => @@ -207,6 +207,67 @@ async Task QueryLiveData() Console.WriteLine($"{(DateTime.Now - b).TotalSeconds} secs"); return null; } + +async Task UpsertLiveData() +{ + var b = DateTime.Now; + //for (var i = 0; i != 100; ++i) + //{ + // var item = new LiveData() {ItemId = i, EcuId = 0, Val = new Random().NextSingle()*i}; + //item.PropertyChanged += PropChanged; + // item.ItemId = i; + // item.ValueLabel = item.Val.ToString(); + using var db = GetDB(); + for (var i = 0; i != 100; ++i) + { + db.Write(() => + { + var obj = db.All().First(i1 => i1.ItemId == i); + obj.Val = i * 2; + db.Add(obj, update: true); + }); + } + + for (var i = 0; i != 100; ++i) + { + Console.WriteLine(db.All().First(i1=>i1.ItemId==i)); + } + Console.WriteLine($"insert needs {(DateTime.Now - b).TotalSeconds} secs"); +} + +void TestLink() +{ + using var db = GetDB(); + /* + db.Write(() => + { + var grp = new EcuGroupCfg() {Id = "g1", Name="group1"}; + var ecu1 = new EcuCfg() {Id = "e1", Name = "ecu1", IsLiving = false}; + db.Add(ecu1, update:true); + grp.Ecus.Add(ecu1); + grp.Ecus.Add(new EcuCfg() {Id="e2", Name="ecu2", IsLiving = false}); + db.Add(grp, update: true); + }); + */ + using var db1 = GetDB(); + db1.Write(() => + { + var obj = db.All().FirstOrDefault(i => i.Id == "e2"); + obj.IsLiving = true; + }); + using var db3 = GetDB(); + var res = db3.All().Filter("ANY Ecus.IsLiving ==$0", true).FirstOrDefault(); + Console.WriteLine(res.Name); + /* + var res1 = BsonTools.Clone(res); + res1.Name = "test"; + db3.Write(() => + { + db.Add(res1, update: true); + }); + */ +} +TestLink(); /* using (var db = GetDB()) { @@ -223,12 +284,15 @@ using (var db = GetDB()) //var t1 = ProduceAsync(); //var t2 = ConsumeAsync(); //Task.WaitAll(t1, t2); -/* -using (var ctx = new AppDbContext()) -{ - ctx.Database.EnsureCreated(); -} +//using (var ctx = new AppDbContext()) +//{ + //ctx.Database.EnsureCreated(); +// ctx.Database.Migrate(); +//} +//InsertLiveData(); +//await UpsertLiveData(); +/* var b = DateTime.Now; var builder = new DbContextOptionsBuilder(); @@ -295,6 +359,7 @@ using (var db = GetDB()) } */ +/* async Task OnMappingFailed(IHttpContext context, MappedResourceInfo? info) { context.Redirect("/"); @@ -316,4 +381,5 @@ using (var server = new WebServer(HttpListenerMode.EmbedIO, "http://*:9090")) { Debug.WriteLine(ex); } -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/realm_cli/realm_cli.csproj b/realm_cli/realm_cli.csproj index 1071ecb..038fa06 100644 --- a/realm_cli/realm_cli.csproj +++ b/realm_cli/realm_cli.csproj @@ -1,15 +1,19 @@ - Exe - net6.0 enable enable true + net6.0 + Exe + + all + runtime; build; native; contentfiles; analyzers; buildtransitive +