HJ212 and Documents
This commit is contained in:
@@ -1,33 +1,53 @@
|
||||
using Quartz.Logging;
|
||||
using Quartz.Logging;
|
||||
|
||||
namespace MachineJob
|
||||
{
|
||||
// simple log provider to get something to the console
|
||||
/// <summary>
|
||||
/// 控制台日志提供者
|
||||
/// 为 Quartz 调度器提供简单的控制台日志输出
|
||||
/// </summary>
|
||||
public class ConsoleLogProvider : ILogProvider
|
||||
{
|
||||
// 配置根对象
|
||||
private readonly IConfigurationRoot configuration = new ConfigurationBuilder()
|
||||
// 设置配置文件的基础路径
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
// 添加 appsettings.json 配置文件
|
||||
.AddJsonFile("appsettings.json")
|
||||
// 根据环境变量添加对应的配置文件
|
||||
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"}.json", true)
|
||||
.Build();
|
||||
|
||||
/// <summary>
|
||||
/// 获取日志记录器
|
||||
/// </summary>
|
||||
/// <param name="name">日志记录器名称</param>
|
||||
/// <returns>日志记录器委托</returns>
|
||||
public Logger GetLogger(string name)
|
||||
{
|
||||
return (level, func, exception, parameters) =>
|
||||
{
|
||||
// 如果日志级别大于等于配置的级别且有消息函数
|
||||
if (level >= configuration.GetSection("Quartz").GetValue<Quartz.Logging.LogLevel>("LogLevel") && func != null)
|
||||
{
|
||||
// 输出到控制台,格式:[时间] [级别] 消息
|
||||
Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] [" + level + "] " + func(), parameters);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打开嵌套上下文(未实现)
|
||||
/// </summary>
|
||||
public IDisposable OpenNestedContext(string message)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打开映射上下文(未实现)
|
||||
/// </summary>
|
||||
public IDisposable OpenMappedContext(string key, object value, bool destructure = false)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
||||
@@ -1,34 +1,68 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace MachineJob
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据库写入上下文
|
||||
/// 用于 Entity Framework Core 访问 MySQL 数据库
|
||||
/// </summary>
|
||||
public class DatabaseWriteContext : DbContext
|
||||
{
|
||||
// 配置根对象
|
||||
private static readonly IConfigurationRoot configuration = new ConfigurationBuilder()
|
||||
// 设置配置文件的基础路径
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
// 添加 appsettings.default.json 配置文件(必需)
|
||||
.AddJsonFile("appsettings.default.json", optional: false, reloadOnChange: true)
|
||||
// 添加 appsettings.json 配置文件(必需)
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||
// 根据环境变量添加对应的配置文件(可选)
|
||||
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true, reloadOnChange: true)
|
||||
.Build();
|
||||
|
||||
// 从配置读取数据库连接字符串
|
||||
private static readonly string connectionString = configuration.GetConnectionString("DatabaseWriteConnectionString")!;
|
||||
|
||||
/// <summary>
|
||||
/// 数据库写入实体集合
|
||||
/// </summary>
|
||||
public DbSet<DatabaseWriteEntity>? DatabaseWrites { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 配置数据库上下文
|
||||
/// 使用 MySQL 数据库
|
||||
/// </summary>
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
// 使用 MySQL,自动检测服务器版本
|
||||
optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据库写入实体类
|
||||
/// 映射到 databasewrites 表
|
||||
/// </summary>
|
||||
[Table(name: "databasewrites")]
|
||||
public partial class DatabaseWriteEntity
|
||||
{
|
||||
/// <summary>
|
||||
/// 主键 ID
|
||||
/// </summary>
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 更新时间
|
||||
/// </summary>
|
||||
public DateTime UpdateTime { get; set; }
|
||||
|
||||
// 注意:Value1 到 Value10 属性由 DatabaseWriteEntityCodeGenerator 代码生成器自动生成
|
||||
// public double? Value1 { get; set; }
|
||||
// public double? Value2 { get; set; }
|
||||
// ...
|
||||
// public double? Value10 { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,38 +2,59 @@ using MachineJob;
|
||||
using MachineJob.Service;
|
||||
using Serilog;
|
||||
|
||||
// MachineJob - 多设备定时任务调度示例
|
||||
// 作为 Windows 服务运行
|
||||
|
||||
// 配置主机服务
|
||||
IHost host = Host.CreateDefaultBuilder(args).UseWindowsService()
|
||||
// 配置应用程序配置
|
||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
// 构建配置
|
||||
var configuration = config
|
||||
// 设置配置文件的基础路径
|
||||
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
|
||||
// 添加 appsettings.json 配置文件
|
||||
.AddJsonFile("appsettings.json")
|
||||
// 根据环境变量添加对应的配置文件
|
||||
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"}.json", true)
|
||||
// 添加环境变量配置
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
|
||||
// 设置当前工作目录
|
||||
Directory.SetCurrentDirectory(hostingContext.HostingEnvironment.ContentRootPath);
|
||||
|
||||
// 配置 Serilog 日志
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
// 从配置读取日志设置
|
||||
.ReadFrom.Configuration(configuration)
|
||||
// 从日志上下文丰富日志信息
|
||||
.Enrich.FromLogContext()
|
||||
// 输出到控制台
|
||||
.WriteTo.Console()
|
||||
// 输出到文件(错误级别,按天滚动)
|
||||
.WriteTo.File("Log\\log..txt", Serilog.Events.LogEventLevel.Error, shared: true, rollingInterval: RollingInterval.Day)
|
||||
.CreateLogger();
|
||||
|
||||
// 创建日志工厂并添加 Serilog
|
||||
var loggerFactory = new LoggerFactory().AddSerilog(Log.Logger);
|
||||
|
||||
// 设置 Quartz 日志提供者
|
||||
Quartz.Logging.LogProvider.SetCurrentLogProvider(new ConsoleLogProvider());
|
||||
// 设置 Modbus.Net 日志提供者
|
||||
Modbus.Net.LogProvider.SetLogProvider(loggerFactory);
|
||||
}
|
||||
)
|
||||
})
|
||||
// 配置服务
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
// 添加后台服务 Worker
|
||||
services.AddHostedService<Worker>();
|
||||
|
||||
// 添加日志服务
|
||||
services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(Log.Logger, true));
|
||||
})
|
||||
.Build();
|
||||
|
||||
// 运行主机
|
||||
await host.RunAsync();
|
||||
|
||||
|
||||
@@ -4,6 +4,10 @@ using MultipleMachinesJobScheduler = Modbus.Net.MultipleMachinesJobScheduler<Mod
|
||||
|
||||
namespace MachineJob.Service
|
||||
{
|
||||
/// <summary>
|
||||
/// MachineJob 后台工作服务
|
||||
/// 使用 Quartz 调度器定期从多个设备读取数据并写入
|
||||
/// </summary>
|
||||
public class Worker : BackgroundService
|
||||
{
|
||||
private readonly ILogger<Worker> _logger;
|
||||
@@ -15,44 +19,32 @@ namespace MachineJob.Service
|
||||
|
||||
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
//1. 直接Coding
|
||||
// 1. 编码方式(已注释)
|
||||
// 手动配置地址单元列表
|
||||
//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" }
|
||||
// ...
|
||||
//};
|
||||
|
||||
//List<AddressUnit> _addresses2 = new List<AddressUnit>
|
||||
//{
|
||||
// new AddressUnit() { Area = "DB1", Address = 0, DataType = typeof(short), Id = "1", Name = "Test1" },
|
||||
// new AddressUnit() { Area = "DB1", Address = 2, DataType = typeof(short), Id = "2", Name = "Test2" },
|
||||
// new AddressUnit() { Area = "DB1", Address = 4, DataType = typeof(short), Id = "3", Name = "Test3" },
|
||||
// new AddressUnit() { Area = "DB1", Address = 6, DataType = typeof(short), Id = "4", Name = "Test4" },
|
||||
// new AddressUnit() { Area = "DB1", Address = 8, DataType = typeof(short), Id = "5", Name = "Test5" },
|
||||
// new AddressUnit() { Area = "DB1", Address = 10, DataType = typeof(short), Id = "6", Name = "Test6" },
|
||||
// new AddressUnit() { Area = "DB1", Address = 12, DataType = typeof(short), Id = "7", Name = "Test7" },
|
||||
// new AddressUnit() { Area = "DB1", Address = 14, DataType = typeof(short), Id = "8", Name = "Test8" },
|
||||
// new AddressUnit() { Area = "DB1", Address = 16, DataType = typeof(short), Id = "9", Name = "Test9" },
|
||||
// new AddressUnit() { Area = "DB1", Address = 18, DataType = typeof(short), Id = "10", Name = "Test10" }
|
||||
// ...
|
||||
//};
|
||||
|
||||
// 创建 Modbus 和 Siemens 机器实例
|
||||
//IMachine<string> machine = new ModbusMachine<string, string>("ModbusMachine1", ModbusType.Tcp, null, _addresses, true, 1, 2, Endian.BigEndianLsb);
|
||||
//IMachine<string> machine2 = new SiemensMachine<string, string>("SiemensMachine1", SiemensType.Tcp, null, SiemensMachineModel.S7_1200, _addresses2, true, 1, 2);
|
||||
|
||||
//var machines = new List<IMachine<string>> { machine, machine2 };
|
||||
|
||||
//2. 从参数表读取参数
|
||||
// 2. 从配置文件读取机器配置
|
||||
// 使用 MachineReader 从 appsettings.json 读取机器配置
|
||||
var machines = MachineReader.ReadMachines();
|
||||
|
||||
//3.使用MachineJobScheduler
|
||||
// 3. 使用 MachineJobScheduler(已注释)
|
||||
// 为每个机器创建独立的调度器
|
||||
//foreach (var machine in machines)
|
||||
//{
|
||||
// var scheduler = await MachineJobSchedulerCreator<IMachineMethodDatas, string, double>.CreateScheduler(machine.Id, -1, 10);
|
||||
@@ -60,47 +52,71 @@ namespace MachineJob.Service
|
||||
// await job.Run();
|
||||
//}
|
||||
|
||||
//4. 使用MultipleMachinesJobScheduler
|
||||
// 4. 使用 MultipleMachinesJobScheduler(已注释)
|
||||
// 使用多机器调度器,带时间间隔
|
||||
//return Task.Run(() => MultipleMachinesJobScheduler.RunScheduler(machines, async (machine, scheduler) =>
|
||||
//{
|
||||
// /await scheduler.From(machine.Id + ".From", machine, MachineDataType.Name).Result.Query(machine.Id + ".ConsoleQuery", QueryConsole).Result.To(machine.Id + ".To", machine).Result.Deal(machine.Id + ".Deal", OnSuccess, OnFailure).Result.Run();
|
||||
// await scheduler.From(machine.Id + ".From", machine, MachineDataType.Name).Result.Query(machine.Id + ".ConsoleQuery", QueryConsole).Result.To(machine.Id + ".To", machine).Result.Deal(machine.Id + ".Deal", OnSuccess, OnFailure).Result.Run();
|
||||
//}, -1, 10));
|
||||
|
||||
//5. 不设置固定时间,连续触发Job
|
||||
// 5. 使用固定时间,立即调度 Job
|
||||
// 当前使用的方式:立即执行,无时间间隔
|
||||
return Task.Run(() => MultipleMachinesJobScheduler.RunScheduler(machines, async (machine, scheduler) =>
|
||||
{
|
||||
// 配置作业链:From -> Query -> To -> Deal
|
||||
// From: 从机器读取数据
|
||||
// Query: 查询处理(QueryConsole)
|
||||
// To: 写入数据到机器
|
||||
// Deal: 处理结果(OnSuccess/OnFailure)
|
||||
await scheduler.From(machine.Id + ".From", machine, MachineDataType.Name).Result.Query(machine.Id + ".ConsoleQuery", QueryConsole).Result.To(machine.Id + ".To", machine).Result.Deal(machine.Id + ".Deal", OnSuccess, OnFailure).Result.Run();
|
||||
}, -1, 0));
|
||||
}
|
||||
|
||||
public override Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
// 停止所有作业调度
|
||||
return Task.Run(() => MultipleMachinesJobScheduler.CancelJob());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 成功回调
|
||||
/// 记录机器设置成功的日志
|
||||
/// </summary>
|
||||
public Task OnSuccess(string machineId)
|
||||
{
|
||||
_logger.LogInformation("Machine {0} set success", machineId);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 失败回调
|
||||
/// 记录机器设置失败的日志
|
||||
/// </summary>
|
||||
public Task OnFailure(string machineId, int errorCode, string errorMsg)
|
||||
{
|
||||
_logger.LogError("Machine {0} set failure: {1}", machineId, errorMsg);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询处理函数
|
||||
/// 处理从设备读取的数据,可以写入数据库或生成随机数据
|
||||
/// </summary>
|
||||
private Dictionary<string, double>? QueryConsole(DataReturnDef dataReturnDef)
|
||||
{
|
||||
var values = dataReturnDef.ReturnValues.Datas;
|
||||
|
||||
// 如果读取成功
|
||||
if (dataReturnDef.ReturnValues.IsSuccess == true)
|
||||
{
|
||||
// 记录日志
|
||||
foreach (var value in values)
|
||||
{
|
||||
_logger.LogDebug(dataReturnDef.MachineId + " " + value.Key + " " + value.Value.DeviceValue);
|
||||
}
|
||||
|
||||
/*
|
||||
// 写入数据库(已注释)
|
||||
try
|
||||
{
|
||||
using (var context = new DatabaseWriteContext())
|
||||
@@ -124,19 +140,24 @@ namespace MachineJob.Service
|
||||
}
|
||||
catch
|
||||
{
|
||||
//ignore
|
||||
// 忽略异常
|
||||
}
|
||||
*/
|
||||
|
||||
// 生成随机数据用于写入
|
||||
Random r = new Random();
|
||||
foreach (var value in values)
|
||||
{
|
||||
value.Value.DeviceValue = r.Next(65536) - 32768;
|
||||
}
|
||||
|
||||
// 将读取的值转换为写入格式
|
||||
return values.MapGetValuesToSetValues();
|
||||
}
|
||||
// 如果读取结果为 null(未知状态)
|
||||
else if (dataReturnDef.ReturnValues.IsSuccess == null)
|
||||
{
|
||||
// 生成随机数据
|
||||
Random r = new Random();
|
||||
|
||||
Dictionary<string, double> ans = new Dictionary<string, double>();
|
||||
@@ -148,12 +169,3 @@ namespace MachineJob.Service
|
||||
|
||||
return ans;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError(dataReturnDef.MachineId + " Return Error.");
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user