Samples Reappend

This commit is contained in:
luosheng
2023-02-13 19:58:15 +08:00
parent 4e70553bfc
commit eb4a7cffd6
471 changed files with 151790 additions and 6 deletions

View File

@@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.ComponentModel.DataAnnotations;
namespace MachineJob
{
public class DatabaseWriteContext : DbContext
{
static readonly string connectionString = new ConfigurationBuilder().AddJsonFile($"appsettings.json").Build().GetConnectionString("DatabaseWriteConnectionString")!;
public DbSet<DatabaseWriteEntity> DatabaseWrites { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
}
}
public class DatabaseWriteEntity
{
[Key]
public int Id { get; set; }
public double? Value1 { get; set; }
public double? Value2 { get; set; }
public double? Value3 { get; set; }
public double? Value4 { get; set; }
public double? Value5 { get; set; }
public double? Value6 { get; set; }
public double? Value7 { get; set; }
public double? Value8 { get; set; }
public double? Value9 { get; set; }
public double? Value10 { get; set; }
public DateTime UpdateTime { get; set; }
}
}

View File

@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Remove="appsettings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Modbus.Net\Modbus.Net.Modbus\Modbus.Net.Modbus.csproj" />
<ProjectReference Include="..\..\Modbus.Net\Modbus.Net\Modbus.Net.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,71 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MachineJob;
#nullable disable
namespace Modbus.Net.Modbus.Test.Migrations
{
[DbContext(typeof(DatabaseWriteContext))]
[Migration("20230212103438_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.2")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("Modbus.Net.Modbus.Test.DatabaseWriteEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<DateTime>("UpdateTime")
.HasColumnType("datetime(6)");
b.Property<double?>("Value1")
.HasColumnType("double");
b.Property<double?>("Value10")
.HasColumnType("double");
b.Property<double?>("Value2")
.HasColumnType("double");
b.Property<double?>("Value3")
.HasColumnType("double");
b.Property<double?>("Value4")
.HasColumnType("double");
b.Property<double?>("Value5")
.HasColumnType("double");
b.Property<double?>("Value6")
.HasColumnType("double");
b.Property<double?>("Value7")
.HasColumnType("double");
b.Property<double?>("Value8")
.HasColumnType("double");
b.Property<double?>("Value9")
.HasColumnType("double");
b.HasKey("Id");
b.ToTable("DatabaseWrites");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,50 @@
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Modbus.Net.Modbus.Test.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateTable(
name: "DatabaseWrites",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Value1 = table.Column<double>(type: "double", nullable: true),
Value2 = table.Column<double>(type: "double", nullable: true),
Value3 = table.Column<double>(type: "double", nullable: true),
Value4 = table.Column<double>(type: "double", nullable: true),
Value5 = table.Column<double>(type: "double", nullable: true),
Value6 = table.Column<double>(type: "double", nullable: true),
Value7 = table.Column<double>(type: "double", nullable: true),
Value8 = table.Column<double>(type: "double", nullable: true),
Value9 = table.Column<double>(type: "double", nullable: true),
Value10 = table.Column<double>(type: "double", nullable: true),
UpdateTime = table.Column<DateTime>(type: "datetime(6)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_DatabaseWrites", x => x.Id);
})
.Annotation("MySql:CharSet", "utf8mb4");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "DatabaseWrites");
}
}
}

View File

@@ -0,0 +1,68 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MachineJob;
#nullable disable
namespace Modbus.Net.Modbus.Test.Migrations
{
[DbContext(typeof(DatabaseWriteContext))]
partial class DatabaseWriteContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.2")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("Modbus.Net.Modbus.Test.DatabaseWriteEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<DateTime>("UpdateTime")
.HasColumnType("datetime(6)");
b.Property<double?>("Value1")
.HasColumnType("double");
b.Property<double?>("Value10")
.HasColumnType("double");
b.Property<double?>("Value2")
.HasColumnType("double");
b.Property<double?>("Value3")
.HasColumnType("double");
b.Property<double?>("Value4")
.HasColumnType("double");
b.Property<double?>("Value5")
.HasColumnType("double");
b.Property<double?>("Value6")
.HasColumnType("double");
b.Property<double?>("Value7")
.HasColumnType("double");
b.Property<double?>("Value8")
.HasColumnType("double");
b.Property<double?>("Value9")
.HasColumnType("double");
b.HasKey("Id");
b.ToTable("DatabaseWrites");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,61 @@
// See https://aka.ms/new-console-template for more information
using MachineJob;
using Modbus.Net;
using Modbus.Net.Modbus;
List<AddressUnit> _addresses = new List<AddressUnit>
{
new AddressUnit() { Area = "4X", Address = 1, DataType = typeof(short), Id = "1", Name = "Test1" },
new AddressUnit() { Area = "4X", Address = 2, DataType = typeof(short), Id = "2", Name = "Test2" },
new AddressUnit() { Area = "4X", Address = 3, DataType = typeof(short), Id = "3", Name = "Test3" },
new AddressUnit() { Area = "4X", Address = 4, DataType = typeof(short), Id = "4", Name = "Test4" },
new AddressUnit() { Area = "4X", Address = 5, DataType = typeof(short), Id = "5", Name = "Test5" },
new AddressUnit() { Area = "4X", Address = 6, DataType = typeof(short), Id = "6", Name = "Test6" },
new AddressUnit() { Area = "4X", Address = 7, DataType = typeof(short), Id = "7", Name = "Test7" },
new AddressUnit() { Area = "4X", Address = 8, DataType = typeof(short), Id = "8", Name = "Test8" },
new AddressUnit() { Area = "4X", Address = 9, DataType = typeof(short), Id = "9", Name = "Test9" },
new AddressUnit() { Area = "4X", Address = 10, DataType = typeof(short), Id = "10", Name = "Test10" }
};
IMachine<string> machine = new ModbusMachine<string, string>("ModbusMachine1", ModbusType.Tcp, "192.168.0.172:502", _addresses, true, 1, 2, Endian.BigEndianLsb);
await MachineJobSchedulerCreator.CreateScheduler("Trigger1", -1, 5).Result.From(machine.Id, machine, MachineDataType.Name).Result.Query("ConsoleQuery", QueryConsole).Result.To(machine.Id + ".To", machine).Result.Run();
Console.ReadLine();
Dictionary<string, ReturnUnit> QueryConsole(DataReturnDef dataReturnDef)
{
var values = dataReturnDef.ReturnValues;
foreach (var value in values)
{
Console.WriteLine(dataReturnDef.MachineId + " " + value.Key + " " + value.Value.DeviceValue);
}
using (var context = new DatabaseWriteContext())
{
context.DatabaseWrites.Add(new DatabaseWriteEntity
{
Value1 = values["Test1"].DeviceValue,
Value2 = values["Test2"].DeviceValue,
Value3 = values["Test3"].DeviceValue,
Value4 = values["Test4"].DeviceValue,
Value5 = values["Test5"].DeviceValue,
Value6 = values["Test6"].DeviceValue,
Value7 = values["Test7"].DeviceValue,
Value8 = values["Test8"].DeviceValue,
Value9 = values["Test9"].DeviceValue,
Value10 = values["Test10"].DeviceValue,
UpdateTime = DateTime.Now,
});
context.SaveChanges();
}
foreach (var value in values)
{
value.Value.DeviceValue = new Random().Next(65536) - 32768;
}
return values;
}

View File

@@ -0,0 +1,8 @@
{
"ConnectionStrings": {
"DatabaseWriteConnectionString": "Server=localhost; User ID=root; Password=123456; Database=modbusnettest;"
},
"Config": {
"FetchSleepTime": "100"
}
}