Contents Reappend

This commit is contained in:
luosheng
2023-02-13 18:14:23 +08:00
parent 5191801823
commit 4e70553bfc
84 changed files with 74695 additions and 19 deletions

View File

@@ -22,7 +22,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{3597
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Modbus.Net.Modbus.Test", "..\Samples\Modbus.Net.Modbus.Test\Modbus.Net.Modbus.Test.csproj", "{22A35CA8-CDCF-416D-BA84-08C933B4A3DE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modbus.Net.Modbus.NA200H", "Modbus.Net.Modbus.NA200H\Modbus.Net.Modbus.NA200H.csproj", "{D4AF0E1E-676E-43B6-BAA3-BFC329D68C80}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Modbus.Net.Modbus.NA200H", "Modbus.Net.Modbus.NA200H\Modbus.Net.Modbus.NA200H.csproj", "{D4AF0E1E-676E-43B6-BAA3-BFC329D68C80}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Modbus.Net.PersistedTests", "..\Tests\Modbus.Net.PersistedTests\Modbus.Net.PersistedTests.csproj", "{4B946C56-D09F-4EEB-BE88-4A2C97815A77}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnyType", "..\Samples\AnyType\AnyType.csproj", "{1857DA63-3335-428F-84D8-1FA4F8178643}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -54,6 +58,14 @@ Global
{D4AF0E1E-676E-43B6-BAA3-BFC329D68C80}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D4AF0E1E-676E-43B6-BAA3-BFC329D68C80}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D4AF0E1E-676E-43B6-BAA3-BFC329D68C80}.Release|Any CPU.Build.0 = Release|Any CPU
{4B946C56-D09F-4EEB-BE88-4A2C97815A77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4B946C56-D09F-4EEB-BE88-4A2C97815A77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B946C56-D09F-4EEB-BE88-4A2C97815A77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4B946C56-D09F-4EEB-BE88-4A2C97815A77}.Release|Any CPU.Build.0 = Release|Any CPU
{1857DA63-3335-428F-84D8-1FA4F8178643}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1857DA63-3335-428F-84D8-1FA4F8178643}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1857DA63-3335-428F-84D8-1FA4F8178643}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1857DA63-3335-428F-84D8-1FA4F8178643}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -61,6 +73,8 @@ Global
GlobalSection(NestedProjects) = preSolution
{3BB01E98-3D45-454A-A1BD-49D7B2C83B74} = {D8DD32FC-CF39-4A1A-8FBF-9E82C5278C34}
{22A35CA8-CDCF-416D-BA84-08C933B4A3DE} = {3597B5C5-45B9-4ECB-92A3-D0FFBE47920A}
{4B946C56-D09F-4EEB-BE88-4A2C97815A77} = {D8DD32FC-CF39-4A1A-8FBF-9E82C5278C34}
{1857DA63-3335-428F-84D8-1FA4F8178643} = {3597B5C5-45B9-4ECB-92A3-D0FFBE47920A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AF00D64E-3C70-474A-8A81-E9E48017C4B5}

View File

@@ -4,15 +4,16 @@ using Serilog;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
namespace Modbus.Net
{
public class JobChainingJobListenerWithDataMap : JobListenerSupport
{
public JobChainingJobListenerWithDataMap(string name, bool overwrite)
public JobChainingJobListenerWithDataMap(string name, string[] overwriteKeys)
{
Name = name;
OverWrite = overwrite;
OverWriteKeys = overwriteKeys;
chainLinks = new Dictionary<JobKey, JobKey>();
}
@@ -20,7 +21,7 @@ namespace Modbus.Net
public override string Name { get; }
public bool OverWrite { get; }
public string[] OverWriteKeys { get; }
/// <summary>
/// Add a chain mapping - when the Job identified by the first key completes
@@ -51,7 +52,7 @@ namespace Modbus.Net
var sjJobDetail = await context.Scheduler.GetJobDetail(sj);
foreach (var entry in context.JobDetail.JobDataMap)
{
if (!OverWrite && !sjJobDetail.JobDataMap.ContainsKey(entry.Key) || OverWrite)
if (!sjJobDetail.JobDataMap.ContainsKey(entry.Key) || sjJobDetail.JobDataMap.ContainsKey(entry.Key) && OverWriteKeys != null && OverWriteKeys.Contains(entry.Key))
{
sjJobDetail.JobDataMap.Put(entry.Key, entry.Value);
}

View File

@@ -7,9 +7,32 @@ using System.Threading.Tasks;
namespace Modbus.Net
{
/// <summary>
/// 返回结果的定义类
/// </summary>
public class DataReturnDef : DataReturnDef<string>
{
}
/// <summary>
/// 返回结果的定义类
/// </summary>
public class DataReturnDef<TMachineKey> where TMachineKey : IEquatable<TMachineKey>
{
/// <summary>
/// 设备的Id
/// </summary>
public TMachineKey MachineId { get; set; }
/// <summary>
/// 返回的数据值
/// </summary>
public Dictionary<string, ReturnUnit> ReturnValues { get; set; }
}
public sealed class MachineJobSchedulerCreator
{
public static async Task<MachineGetJobScheduler> CreateScheduler(string triggerKey, int count, int interval)
public static async Task<MachineGetJobScheduler> CreateScheduler(string triggerKey, int count, double interval)
{
IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler();
@@ -18,13 +41,13 @@ namespace Modbus.Net
trigger = TriggerBuilder.Create()
.WithIdentity("Modbus.Net.DataQuery.Trigger." + triggerKey, "Modbus.Net.DataQuery.Group")
.StartNow()
.WithSimpleSchedule(b => b.WithIntervalInSeconds(interval).WithRepeatCount(count))
.WithSimpleSchedule(b => b.WithInterval(TimeSpan.FromSeconds(interval)).WithRepeatCount(count))
.Build();
else
trigger = TriggerBuilder.Create()
.WithIdentity("Modbus.Net.DataQuery.Trigger." + triggerKey, "Modbus.Net.DataQuery.Group")
.StartNow()
.WithSimpleSchedule(b => b.WithIntervalInSeconds(interval).RepeatForever())
.WithSimpleSchedule(b => b.WithInterval(TimeSpan.FromSeconds(interval)).RepeatForever())
.Build();
return new MachineGetJobScheduler(scheduler, trigger);
@@ -59,11 +82,16 @@ namespace Modbus.Net
return new MachineQueryJobScheduler(_scheduler, _trigger, jobKey);
}
public async Task<MachineQueryJobScheduler> Apply(string queryId, Dictionary<string, ReturnUnit> values, MachineDataType machineDataType)
public Task<MachineQueryJobScheduler> Apply(string queryId, Dictionary<string, ReturnUnit> values, MachineDataType machineDataType)
{
return Apply<string>(queryId, values, machineDataType);
}
public async Task<MachineQueryJobScheduler> Apply<TMachineKey>(string queryId, Dictionary<string, ReturnUnit> values, MachineDataType machineDataType) where TMachineKey : IEquatable<TMachineKey>
{
JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group");
IJobDetail job = JobBuilder.Create<MachineQueryDataJob>()
IJobDetail job = JobBuilder.Create<MachineQueryDataJob<TMachineKey>>()
.WithIdentity(jobKey)
.Build();
@@ -92,7 +120,12 @@ namespace Modbus.Net
_parentJobKey = parentJobKey;
}
public async Task<MachineSetJobScheduler> Query(string queryId = null, Func<Dictionary<string, ReturnUnit>, Dictionary<string, ReturnUnit>> QueryDataFunc = null)
public Task<MachineSetJobScheduler> Query(string queryId = null, Func<DataReturnDef, Dictionary<string, ReturnUnit>> QueryDataFunc = null)
{
return Query<string>(queryId, QueryDataFunc);
}
public async Task<MachineSetJobScheduler> Query<TMachineKey>(string queryId = null, Func<DataReturnDef, Dictionary<string, ReturnUnit>> QueryDataFunc = null) where TMachineKey : IEquatable<TMachineKey>
{
JobChainingJobListenerWithDataMap listener;
try
@@ -101,7 +134,7 @@ namespace Modbus.Net
}
catch
{
listener = new JobChainingJobListenerWithDataMap("Modbus.Net.DataQuery.Chain", false);
listener = new JobChainingJobListenerWithDataMap("Modbus.Net.DataQuery.Chain", new string[1]{ "Value" });
_scheduler.ListenerManager.AddJobListener(listener, GroupMatcher<JobKey>.GroupEquals("Modbus.Net.DataQuery.Group"));
}
@@ -109,7 +142,7 @@ namespace Modbus.Net
JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group");
IJobDetail job = JobBuilder.Create<MachineQueryDataJob>()
IJobDetail job = JobBuilder.Create<MachineQueryDataJob<TMachineKey>>()
.WithIdentity(jobKey)
.StoreDurably(true)
.Build();
@@ -184,20 +217,21 @@ namespace Modbus.Net
}
}
public class MachineQueryDataJob : IJob
public class MachineQueryDataJob<TMachineKey> : IJob where TMachineKey : IEquatable<TMachineKey>
{
public async Task Execute(IJobExecutionContext context)
{
object machine;
object values;
object QueryMethod;
context.MergedJobDataMap.TryGetValue("Machine", out machine);
context.MergedJobDataMap.TryGetValue("Value", out values);
context.MergedJobDataMap.TryGetValue("QueryMethod", out QueryMethod);
Func<Dictionary<string, ReturnUnit>, Dictionary<string, ReturnUnit>> QueryMethodDispatch = (Func<Dictionary<string, ReturnUnit>, Dictionary<string, ReturnUnit>>)QueryMethod;
Func<DataReturnDef, Dictionary<string, ReturnUnit>> QueryMethodDispatch = (Func<DataReturnDef, Dictionary<string, ReturnUnit>>)QueryMethod;
if (QueryMethod != null)
{
context.JobDetail.JobDataMap.Put("Value", QueryMethodDispatch((Dictionary<string, ReturnUnit>)values));
context.JobDetail.JobDataMap.Put("Value", QueryMethodDispatch(new DataReturnDef() { MachineId = ((IMachineProperty<TMachineKey>)machine).GetMachineIdString(), ReturnValues = (Dictionary<string, ReturnUnit>)values }));
await context.Scheduler.AddJob(context.JobDetail, true, false);
}
}