Simplified code
This commit is contained in:
@@ -1,32 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace Modbus.Net
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 获取扩展方法的类
|
|
||||||
/// </summary>
|
|
||||||
public static class ExtendedMethodHelper
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 获取程序集中的所有扩展方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="extendedType">扩展方法的第一个参数类即扩展类</param>
|
|
||||||
/// <param name="assembly">程序集</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static IEnumerable<MethodInfo> GetExtensionMethods(this Type extendedType, Assembly assembly)
|
|
||||||
{
|
|
||||||
var query = from type in assembly.GetTypes()
|
|
||||||
where type.IsSealed && !type.IsGenericType && !type.IsNested
|
|
||||||
from method in type.GetMethods(BindingFlags.Static
|
|
||||||
| BindingFlags.Public | BindingFlags.NonPublic)
|
|
||||||
where method.IsDefined(typeof(ExtensionAttribute), false)
|
|
||||||
where method.GetParameters()[0].ParameterType == extendedType
|
|
||||||
select method;
|
|
||||||
return query.ToList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,8 +3,6 @@ using Quartz.Impl;
|
|||||||
using Quartz.Impl.Matchers;
|
using Quartz.Impl.Matchers;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Modbus.Net
|
namespace Modbus.Net
|
||||||
@@ -146,15 +144,13 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name);
|
JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name);
|
||||||
|
|
||||||
IJobDetail job = JobBuilder.Create<MachineGetDataJob<TReturnUnit>>()
|
IJobDetail job = JobBuilder.Create<MachineGetDataJob<TMachineMethod, TReturnUnit>>()
|
||||||
.WithIdentity(jobKey)
|
.WithIdentity(jobKey)
|
||||||
.StoreDurably(true)
|
.StoreDurably(true)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
Type methodType = typeof(TMachineMethod);
|
|
||||||
job.JobDataMap.Put("DataType", machineDataType);
|
job.JobDataMap.Put("DataType", machineDataType);
|
||||||
job.JobDataMap.Put("Machine", machine);
|
job.JobDataMap.Put("Machine", machine);
|
||||||
job.JobDataMap.Put("MethodType", methodType);
|
|
||||||
|
|
||||||
if (_parentJobKey != null)
|
if (_parentJobKey != null)
|
||||||
{
|
{
|
||||||
@@ -316,14 +312,12 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name);
|
JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name);
|
||||||
|
|
||||||
IJobDetail job = JobBuilder.Create<MachineSetDataJob<TReturnUnit>>()
|
IJobDetail job = JobBuilder.Create<MachineSetDataJob<TMachineMethod, TReturnUnit>>()
|
||||||
.WithIdentity(jobKey)
|
.WithIdentity(jobKey)
|
||||||
.StoreDurably(true)
|
.StoreDurably(true)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
Type methodType = typeof(TMachineMethod);
|
|
||||||
job.JobDataMap.Put("Machine", machine);
|
job.JobDataMap.Put("Machine", machine);
|
||||||
job.JobDataMap.Put("MethodType", methodType);
|
|
||||||
|
|
||||||
var listener = _scheduler.ListenerManager.GetJobListener("Modbus.Net.DataQuery.Chain." + _trigger.Key.Name) as JobChainingJobListenerWithDataMap;
|
var listener = _scheduler.ListenerManager.GetJobListener("Modbus.Net.DataQuery.Chain." + _trigger.Key.Name) as JobChainingJobListenerWithDataMap;
|
||||||
if (listener == null) throw new NullReferenceException("Listener " + "Modbus.Net.DataQuery.Chain." + _trigger.Key.Name + " is null");
|
if (listener == null) throw new NullReferenceException("Listener " + "Modbus.Net.DataQuery.Chain." + _trigger.Key.Name + " is null");
|
||||||
@@ -416,20 +410,17 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取数据任务
|
/// 获取数据任务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class MachineGetDataJob<TReturnUnit> : IJob where TReturnUnit : struct
|
public class MachineGetDataJob<TMachineMethod, TReturnUnit> : IJob where TMachineMethod : IMachineMethod where TReturnUnit : struct
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task Execute(IJobExecutionContext context)
|
public async Task Execute(IJobExecutionContext context)
|
||||||
{
|
{
|
||||||
object machine;
|
object machine;
|
||||||
object machineDataType;
|
object machineDataType;
|
||||||
object methodType;
|
|
||||||
context.JobDetail.JobDataMap.TryGetValue("Machine", out machine);
|
context.JobDetail.JobDataMap.TryGetValue("Machine", out machine);
|
||||||
context.JobDetail.JobDataMap.TryGetValue("DataType", out machineDataType);
|
context.JobDetail.JobDataMap.TryGetValue("DataType", out machineDataType);
|
||||||
context.JobDetail.JobDataMap.TryGetValue("MethodType", out methodType);
|
|
||||||
|
|
||||||
MethodInfo invokeGetGenericMethod = typeof(IMachineMethod).GetExtensionMethods(GetType().Assembly).First(p => p.Name == "InvokeGet").MakeGenericMethod((Type)methodType, typeof(Dictionary<string, ReturnUnit<TReturnUnit>>));
|
var values = await ((IMachineMethod)machine).InvokeGet<TMachineMethod, Dictionary<string, ReturnUnit<TReturnUnit>>>(new object[] { (MachineDataType)machineDataType });
|
||||||
var values = await (Task<ReturnStruct<Dictionary<string, ReturnUnit<TReturnUnit>>>>)invokeGetGenericMethod.Invoke(machine, new object[] { machine, new object[] { (MachineDataType)machineDataType } });
|
|
||||||
|
|
||||||
context.JobDetail.JobDataMap.Put("Value", values);
|
context.JobDetail.JobDataMap.Put("Value", values);
|
||||||
await context.Scheduler.AddJob(context.JobDetail, true, false);
|
await context.Scheduler.AddJob(context.JobDetail, true, false);
|
||||||
@@ -463,7 +454,7 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 写数据任务
|
/// 写数据任务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class MachineSetDataJob<TReturnUnit> : IJob where TReturnUnit : struct
|
public class MachineSetDataJob<TMachineMethod, TReturnUnit> : IJob where TMachineMethod : IMachineMethod where TReturnUnit : struct
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public async Task Execute(IJobExecutionContext context)
|
public async Task Execute(IJobExecutionContext context)
|
||||||
@@ -472,12 +463,10 @@ namespace Modbus.Net
|
|||||||
object machineDataType;
|
object machineDataType;
|
||||||
object values;
|
object values;
|
||||||
object valuesSet;
|
object valuesSet;
|
||||||
object methodType;
|
|
||||||
context.JobDetail.JobDataMap.TryGetValue("Machine", out machine);
|
context.JobDetail.JobDataMap.TryGetValue("Machine", out machine);
|
||||||
context.JobDetail.JobDataMap.TryGetValue("DataType", out machineDataType);
|
context.JobDetail.JobDataMap.TryGetValue("DataType", out machineDataType);
|
||||||
context.JobDetail.JobDataMap.TryGetValue("Value", out values);
|
context.JobDetail.JobDataMap.TryGetValue("Value", out values);
|
||||||
context.JobDetail.JobDataMap.TryGetValue("SetValue", out valuesSet);
|
context.JobDetail.JobDataMap.TryGetValue("SetValue", out valuesSet);
|
||||||
context.JobDetail.JobDataMap.TryGetValue("MethodType", out methodType);
|
|
||||||
if (valuesSet == null && values != null)
|
if (valuesSet == null && values != null)
|
||||||
{
|
{
|
||||||
valuesSet = ((ReturnStruct<Dictionary<string, ReturnUnit<TReturnUnit>>>)values).Datas.MapGetValuesToSetValues();
|
valuesSet = ((ReturnStruct<Dictionary<string, ReturnUnit<TReturnUnit>>>)values).Datas.MapGetValuesToSetValues();
|
||||||
@@ -488,8 +477,7 @@ namespace Modbus.Net
|
|||||||
context.JobDetail.JobDataMap.Put("Success", false);
|
context.JobDetail.JobDataMap.Put("Success", false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
MethodInfo invokeGetGenericMethod = typeof(IMachineMethod).GetExtensionMethods(GetType().Assembly).First(p => p.Name == "InvokeSet").MakeGenericMethod((Type)methodType, typeof(Dictionary<string, double>));
|
var success = await ((IMachineMethod)machine).InvokeSet<TMachineMethod, Dictionary<string, double>>(new object[] { (MachineDataType)machineDataType }, (Dictionary<string, double>)valuesSet);
|
||||||
var success = await (Task<ReturnStruct<bool>>)invokeGetGenericMethod.Invoke(machine, new object[] { machine, new object[] { (MachineDataType)machineDataType }, (Dictionary<string, double>)valuesSet });
|
|
||||||
|
|
||||||
context.JobDetail.JobDataMap.Put("Success", success);
|
context.JobDetail.JobDataMap.Put("Success", success);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
using Modbus.Net;
|
using Modbus.Net;
|
||||||
using Modbus.Net.Modbus;
|
using Modbus.Net.Modbus;
|
||||||
using Modbus.Net.Siemens;
|
using Modbus.Net.Siemens;
|
||||||
using ModbusMachine = Modbus.Net.Modbus.ModbusMachine<string, string>;
|
|
||||||
using SiemensMachine = Modbus.Net.Siemens.SiemensMachine<string, string>;
|
|
||||||
using MultipleMachinesJobScheduler = Modbus.Net.MultipleMachinesJobScheduler<Modbus.Net.IMachineMethodDatas, string, double>;
|
|
||||||
using DataReturnDef = Modbus.Net.DataReturnDef<string, double>;
|
using DataReturnDef = Modbus.Net.DataReturnDef<string, double>;
|
||||||
|
using ModbusMachine = Modbus.Net.Modbus.ModbusMachine<string, string>;
|
||||||
|
using MultipleMachinesJobScheduler = Modbus.Net.MultipleMachinesJobScheduler<Modbus.Net.IMachineMethodDatas, string, double>;
|
||||||
|
using SiemensMachine = Modbus.Net.Siemens.SiemensMachine<string, string>;
|
||||||
|
|
||||||
namespace MachineJob.Service
|
namespace MachineJob.Service
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user