ygl 3 years ago
parent 21082bebf8
commit b44f7c7824
  1. 4
      realm_cli/AppDbContext.cs
  2. 21
      realm_cli/EcuCfg.cs
  3. 7
      realm_cli/EcuMdl.cs
  4. 2
      realm_cli/LiveData.cs
  5. 7
      realm_cli/LiveDataMTL.cs
  6. 2
      realm_cli/LiveDataMsg.cs
  7. 54
      realm_cli/Migrations/20221121020938_AddEcuModel.Designer.cs
  8. 46
      realm_cli/Migrations/20221121020938_AddEcuModel.cs
  9. 69
      realm_cli/Migrations/20221121021731_AddLiveDataMTLModel.Designer.cs
  10. 29
      realm_cli/Migrations/20221121021731_AddLiveDataMTLModel.cs
  11. 74
      realm_cli/Migrations/20221121022400_EcuModelAddRemark.Designer.cs
  12. 24
      realm_cli/Migrations/20221121022400_EcuModelAddRemark.cs
  13. 69
      realm_cli/Migrations/20221121023143_EcuModelRemoveRemark.Designer.cs
  14. 24
      realm_cli/Migrations/20221121023143_EcuModelRemoveRemark.cs
  15. 67
      realm_cli/Migrations/AppDbContextModelSnapshot.cs
  16. 84
      realm_cli/Program.cs
  17. 8
      realm_cli/realm_cli.csproj

@ -8,6 +8,8 @@ namespace realm_cli;
public class AppDbContext: DbContext
{
public DbSet<LiveDataState> LvStates { get; set; }
public DbSet<EcuMdl> EcuMdls { get; set; }
public DbSet<LiveDataMTL> LiveDataMtls { get; set; }
public AppDbContext()
{
@ -28,6 +30,8 @@ public class AppDbContext: DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<LiveDataState>().ToTable("lv_states").HasKey(i=>i.ItemId);
modelBuilder.Entity<EcuMdl>().ToTable("ecus");
//modelBuilder.Entity<EcuMdl>().Property(i => i.Remark).HasColumnName("remark");
}
private void ExecutePragma(SqliteConnection conn, string pragmaCommand)

@ -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<EcuCfg> Ecus { get; }
public string Name { get; set; }
}

@ -0,0 +1,7 @@
namespace realm_cli;
public class EcuMdl
{
public long Id { get; set; }
public string Name { get; set; }
}

@ -188,6 +188,8 @@ public class LiveData : RealmObject
OnPropertyChanged(nameof(GaugeScaleInterval));
}
}
public bool IsTest { get; set; }
public override string ToString()
{

@ -0,0 +1,7 @@
namespace realm_cli;
public class LiveDataMTL
{
public long Id { get; set; }
public string Name { get; set; }
}

@ -11,4 +11,6 @@ public class LiveDataMsg : RealmObject
public DateTimeOffset? Uts { get; set; }
public string Text { get; set; }
public bool IsTest { get; set; }
}

@ -0,0 +1,54 @@
// <auto-generated />
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<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ecus");
});
modelBuilder.Entity("realm_cli.LiveDataState", b =>
{
b.Property<int>("ItemId")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<ushort>("EcuId")
.HasColumnType("INTEGER");
b.Property<float>("Value")
.HasColumnType("REAL");
b.HasKey("ItemId");
b.ToTable("lv_states");
});
#pragma warning restore 612, 618
}
}
}

@ -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<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ecus", x => x.Id);
});
migrationBuilder.CreateTable(
name: "lv_states",
columns: table => new
{
ItemId = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
EcuId = table.Column<ushort>(type: "INTEGER", nullable: false),
Value = table.Column<float>(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");
}
}
}

@ -0,0 +1,69 @@
// <auto-generated />
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<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ecus");
});
modelBuilder.Entity("realm_cli.LiveDataMTL", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("LiveDataMtls");
});
modelBuilder.Entity("realm_cli.LiveDataState", b =>
{
b.Property<int>("ItemId")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<ushort>("EcuId")
.HasColumnType("INTEGER");
b.Property<float>("Value")
.HasColumnType("REAL");
b.HasKey("ItemId");
b.ToTable("lv_states");
});
#pragma warning restore 612, 618
}
}
}

@ -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<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_LiveDataMtls", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "LiveDataMtls");
}
}
}

@ -0,0 +1,74 @@
// <auto-generated />
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<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Remark")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnName("remark");
b.HasKey("Id");
b.ToTable("ecus");
});
modelBuilder.Entity("realm_cli.LiveDataMTL", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("LiveDataMtls");
});
modelBuilder.Entity("realm_cli.LiveDataState", b =>
{
b.Property<int>("ItemId")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<ushort>("EcuId")
.HasColumnType("INTEGER");
b.Property<float>("Value")
.HasColumnType("REAL");
b.HasKey("ItemId");
b.ToTable("lv_states");
});
#pragma warning restore 612, 618
}
}
}

@ -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<string>(
name: "remark",
table: "ecus",
type: "TEXT",
nullable: false,
defaultValue: "");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "remark",
table: "ecus");
}
}
}

@ -0,0 +1,69 @@
// <auto-generated />
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<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ecus");
});
modelBuilder.Entity("realm_cli.LiveDataMTL", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("LiveDataMtls");
});
modelBuilder.Entity("realm_cli.LiveDataState", b =>
{
b.Property<int>("ItemId")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<ushort>("EcuId")
.HasColumnType("INTEGER");
b.Property<float>("Value")
.HasColumnType("REAL");
b.HasKey("ItemId");
b.ToTable("lv_states");
});
#pragma warning restore 612, 618
}
}
}

@ -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<string>(
name: "remark",
table: "ecus",
type: "TEXT",
nullable: false,
defaultValue: "");
}
}
}

@ -0,0 +1,67 @@
// <auto-generated />
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<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ecus");
});
modelBuilder.Entity("realm_cli.LiveDataMTL", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("LiveDataMtls");
});
modelBuilder.Entity("realm_cli.LiveDataState", b =>
{
b.Property<int>("ItemId")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<ushort>("EcuId")
.HasColumnType("INTEGER");
b.Property<float>("Value")
.HasColumnType("REAL");
b.HasKey("ItemId");
b.ToTable("lv_states");
});
#pragma warning restore 612, 618
}
}
}

@ -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<LiveData> 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<LiveData>().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<LiveData>().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<EcuCfg>().FirstOrDefault(i => i.Id == "e2");
obj.IsLiving = true;
});
using var db3 = GetDB();
var res = db3.All<EcuGroupCfg>().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<AppDbContext>();
@ -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);
}
}
}
*/

@ -1,15 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishSingleFile>true</PublishSingleFile>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EmbedIO" Version="3.5.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.17" />
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2" />
<PackageReference Include="Realm" Version="10.18.0" />

Loading…
Cancel
Save