GetValue Return change, Job SetValue param change.

This commit is contained in:
luosheng
2023-02-16 07:03:54 +08:00
parent 62f63c142a
commit f2d96af1a1
5 changed files with 83 additions and 50 deletions

View File

@@ -10,7 +10,7 @@ namespace Modbus.Net
{ {
public class JobChainingJobListenerWithDataMap : JobListenerSupport public class JobChainingJobListenerWithDataMap : JobListenerSupport
{ {
public JobChainingJobListenerWithDataMap(string name, string[] overwriteKeys) public JobChainingJobListenerWithDataMap(string name, ICollection<string> overwriteKeys)
{ {
Name = name; Name = name;
OverWriteKeys = overwriteKeys; OverWriteKeys = overwriteKeys;
@@ -21,7 +21,7 @@ namespace Modbus.Net
public override string Name { get; } public override string Name { get; }
public string[] OverWriteKeys { get; } public ICollection<string> OverWriteKeys { get; }
/// <summary> /// <summary>
/// Add a chain mapping - when the Job identified by the first key completes /// Add a chain mapping - when the Job identified by the first key completes

View File

@@ -50,7 +50,7 @@ namespace Modbus.Net
.WithSimpleSchedule(b => b.WithInterval(TimeSpan.FromSeconds(interval)).RepeatForever()) .WithSimpleSchedule(b => b.WithInterval(TimeSpan.FromSeconds(interval)).RepeatForever())
.Build(); .Build();
var listener = new JobChainingJobListenerWithDataMap("Modbus.Net.DataQuery.Chain." + triggerKey, new string[1] { "Value" }); var listener = new JobChainingJobListenerWithDataMap("Modbus.Net.DataQuery.Chain." + triggerKey, new string[2] { "Value", "SetValue" });
scheduler.ListenerManager.AddJobListener(listener, GroupMatcher<JobKey>.GroupEquals("Modbus.Net.DataQuery.Group." + triggerKey)); scheduler.ListenerManager.AddJobListener(listener, GroupMatcher<JobKey>.GroupEquals("Modbus.Net.DataQuery.Group." + triggerKey));
if (await scheduler.GetTrigger(new TriggerKey(triggerKey)) != null) if (await scheduler.GetTrigger(new TriggerKey(triggerKey)) != null)
@@ -120,12 +120,12 @@ namespace Modbus.Net
return new MachineQueryJobScheduler(_scheduler, _trigger, jobKey); return new MachineQueryJobScheduler(_scheduler, _trigger, jobKey);
} }
public Task<MachineQueryJobScheduler> Apply(string queryId, Dictionary<string, ReturnUnit> values, MachineDataType machineDataType) public Task<MachineQueryJobScheduler> Apply(string queryId, Dictionary<string, double> values, MachineDataType machineDataType)
{ {
return Apply<string>(queryId, values, 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> public async Task<MachineQueryJobScheduler> Apply<TMachineKey>(string queryId, Dictionary<string, double> values, MachineDataType machineDataType) where TMachineKey : IEquatable<TMachineKey>
{ {
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);
@@ -135,7 +135,7 @@ namespace Modbus.Net
.Build(); .Build();
job.JobDataMap.Put("DataType", machineDataType); job.JobDataMap.Put("DataType", machineDataType);
job.JobDataMap.Put("Value", values); job.JobDataMap.Put("SetValue", values);
job.JobDataMap.Put("QueryMethod", null); job.JobDataMap.Put("QueryMethod", null);
if (_parentJobKey != null) if (_parentJobKey != null)
@@ -153,12 +153,12 @@ namespace Modbus.Net
return new MachineQueryJobScheduler(_scheduler, _trigger, jobKey); return new MachineQueryJobScheduler(_scheduler, _trigger, jobKey);
} }
public Task<MachineSetJobScheduler> ApplyTo(string queryId, Dictionary<string, ReturnUnit> values, MachineDataType machineDataType) public Task<MachineSetJobScheduler> ApplyTo(string queryId, Dictionary<string, double> values, MachineDataType machineDataType)
{ {
return ApplyTo<string>(queryId, values, machineDataType); return ApplyTo<string>(queryId, values, machineDataType);
} }
public async Task<MachineSetJobScheduler> ApplyTo<TMachineKey>(string queryId, Dictionary<string, ReturnUnit> values, MachineDataType machineDataType) where TMachineKey : IEquatable<TMachineKey> public async Task<MachineSetJobScheduler> ApplyTo<TMachineKey>(string queryId, Dictionary<string, double> values, MachineDataType machineDataType) where TMachineKey : IEquatable<TMachineKey>
{ {
var applyJobScheduler = await Apply<TMachineKey>(queryId, values, machineDataType); var applyJobScheduler = await Apply<TMachineKey>(queryId, values, machineDataType);
return await applyJobScheduler.Query(); return await applyJobScheduler.Query();
@@ -180,12 +180,12 @@ namespace Modbus.Net
_parentJobKey = parentJobKey; _parentJobKey = parentJobKey;
} }
public Task<MachineSetJobScheduler> Query(string queryId = null, Func<DataReturnDef, Dictionary<string, ReturnUnit>> QueryDataFunc = null) public Task<MachineSetJobScheduler> Query(string queryId = null, Func<DataReturnDef, Dictionary<string, double>> QueryDataFunc = null)
{ {
return Query<string>(queryId, QueryDataFunc); 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> public async Task<MachineSetJobScheduler> Query<TMachineKey>(string queryId = null, Func<DataReturnDef, Dictionary<string, double>> QueryDataFunc = null) where TMachineKey : IEquatable<TMachineKey>
{ {
if (queryId == null) return new MachineSetJobScheduler(_scheduler, _trigger, _parentJobKey); if (queryId == null) return new MachineSetJobScheduler(_scheduler, _trigger, _parentJobKey);
@@ -279,11 +279,11 @@ namespace Modbus.Net
context.JobDetail.JobDataMap.TryGetValue("Machine", out machine); context.JobDetail.JobDataMap.TryGetValue("Machine", out machine);
context.JobDetail.JobDataMap.TryGetValue("Value", out values); context.JobDetail.JobDataMap.TryGetValue("Value", out values);
context.JobDetail.JobDataMap.TryGetValue("QueryMethod", out QueryMethod); context.JobDetail.JobDataMap.TryGetValue("QueryMethod", out QueryMethod);
Func<DataReturnDef, Dictionary<string, ReturnUnit>> QueryMethodDispatch = (Func<DataReturnDef, Dictionary<string, ReturnUnit>>)QueryMethod; Func<DataReturnDef, Dictionary<string, double>> QueryMethodDispatch = (Func<DataReturnDef, Dictionary<string, double>>)QueryMethod;
if (QueryMethod != null) if (QueryMethod != null && values != null)
{ {
context.JobDetail.JobDataMap.Put("Value", QueryMethodDispatch(new DataReturnDef() { MachineId = machine == null ? null : ((IMachineProperty<TMachineKey>)machine).GetMachineIdString(), ReturnValues = (Dictionary<string, ReturnUnit>)values })); context.JobDetail.JobDataMap.Put("SetValue", QueryMethodDispatch(new DataReturnDef() { MachineId = machine == null ? null : ((IMachineProperty<TMachineKey>)machine).GetMachineIdString(), ReturnValues = (Dictionary<string, ReturnUnit>)values }));
await context.Scheduler.AddJob(context.JobDetail, true, false); await context.Scheduler.AddJob(context.JobDetail, true, false);
} }
} }
@@ -296,12 +296,15 @@ namespace Modbus.Net
object machine; object machine;
object machineDataType; object machineDataType;
object values; object values;
object valuesSet;
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);
Dictionary<string, double> valuesSet = ((Dictionary<string, ReturnUnit>)values).MapGetValuesToSetValues(); context.JobDetail.JobDataMap.TryGetValue("SetValue", out valuesSet);
if (valuesSet == null && values != null)
valuesSet = ((Dictionary<string, ReturnUnit>)values).MapGetValuesToSetValues();
var success = await (machine as IMachineMethodData)!.SetDatasAsync((MachineDataType)machineDataType, valuesSet); var success = await (machine as IMachineMethodData)!.SetDatasAsync((MachineDataType)machineDataType, (Dictionary<string, double>)valuesSet);
} }
} }
} }

View File

@@ -285,7 +285,7 @@ namespace Modbus.Net
ans.Add(key, new ReturnUnit ans.Add(key, new ReturnUnit
{ {
DeviceValue = null, DeviceValue = null,
UnitExtend = address.UnitExtend AddressUnit = address.MapAddressUnitTUnitKeyToAddressUnit(),
}); });
else else
ans.Add(key, ans.Add(key,
@@ -296,7 +296,7 @@ namespace Modbus.Net
ValueHelper.GetInstance(BaseUtility.Endian) ValueHelper.GetInstance(BaseUtility.Endian)
.GetValue(datas, ref localMainPos, ref localSubPos, .GetValue(datas, ref localMainPos, ref localSubPos,
address.DataType)) * address.Zoom, address.DataType)) * address.Zoom,
UnitExtend = address.UnitExtend AddressUnit = address.MapAddressUnitTUnitKeyToAddressUnit(),
}); });
} }
catch (Exception e) catch (Exception e)
@@ -710,9 +710,9 @@ namespace Modbus.Net
public double? DeviceValue { get; set; } public double? DeviceValue { get; set; }
/// <summary> /// <summary>
/// 数据的扩展 /// 数据定义
/// </summary> /// </summary>
public UnitExtend UnitExtend { get; set; } public AddressUnit AddressUnit { get; set; }
} }
/// <summary> /// <summary>
@@ -797,4 +797,34 @@ namespace Modbus.Net
return Area.ToUpper() == other.Area.ToUpper() && Address == other.Address || Id.Equals(other.Id); return Area.ToUpper() == other.Area.ToUpper() && Address == other.Address || Id.Equals(other.Id);
} }
} }
/// <summary>
/// AddressUnit扩展
/// </summary>
public static class AddressUnitExtend
{
/// <summary>
/// 映射泛型AddressUnit到字符串型AddressUnit
/// </summary>
/// <param name="addressUnit"></param>
/// <returns></returns>
public static AddressUnit MapAddressUnitTUnitKeyToAddressUnit<TUnitKey>(this AddressUnit<TUnitKey> addressUnit) where TUnitKey : IEquatable<TUnitKey>
{
return new AddressUnit()
{
Id = addressUnit.ToString(),
Area = addressUnit.Area,
Address = addressUnit.Address,
SubAddress = addressUnit.SubAddress,
DataType = addressUnit.DataType,
Zoom = addressUnit.Zoom,
DecimalPos = addressUnit.DecimalPos,
CommunicationTag = addressUnit.CommunicationTag,
Name = addressUnit.Name,
Unit = addressUnit.Unit,
CanWrite = addressUnit.CanWrite,
UnitExtend = addressUnit.UnitExtend
};
}
}
} }

View File

@@ -25,14 +25,14 @@ await MachineJobSchedulerCreator.CreateScheduler("Trigger1", -1, 5).Result.From(
Console.ReadLine(); Console.ReadLine();
Dictionary<string, ReturnUnit> QueryConsole(DataReturnDef dataReturnDef) Dictionary<string, double> QueryConsole(DataReturnDef dataReturnDef)
{ {
var values = dataReturnDef.ReturnValues; var values = dataReturnDef.ReturnValues;
foreach (var value in values) foreach (var value in values)
{ {
Console.WriteLine(dataReturnDef.MachineId + " " + value.Key + " " + value.Value.DeviceValue); Console.WriteLine(dataReturnDef.MachineId + " " + value.Key + " " + value.Value.DeviceValue);
} }
try try
{ {
using (var context = new DatabaseWriteContext()) using (var context = new DatabaseWriteContext())
@@ -58,50 +58,50 @@ Dictionary<string, ReturnUnit> QueryConsole(DataReturnDef dataReturnDef)
{ {
//ignore //ignore
} }
Random r = new Random(); Random r = new Random();
foreach (var value in values) foreach (var value in values)
{ {
value.Value.DeviceValue = r.Next(65536) - 32768; value.Value.DeviceValue = r.Next(65536) - 32768;
} }
return values; return values.MapGetValuesToSetValues();
} }
Dictionary<string, ReturnUnit> QueryConsole2(DataReturnDef dataReturnDef) Dictionary<string, double> QueryConsole2(DataReturnDef dataReturnDef)
{ {
Random r = new Random(); Random r = new Random();
var datas = new Dictionary<string, ReturnUnit>() var datas = new Dictionary<string, double>()
{ {
{ {
"Test1", new ReturnUnit(){DeviceValue = r.Next(65536) - 32768 } "Test1", r.Next(65536) - 32768
}, },
{ {
"Test2", new ReturnUnit(){DeviceValue = r.Next(65536) - 32768 } "Test2", r.Next(65536) - 32768
}, },
{ {
"Test3", new ReturnUnit(){DeviceValue = r.Next(65536) - 32768 } "Test3", r.Next(65536) - 32768
}, },
{ {
"Test4", new ReturnUnit(){DeviceValue = r.Next(65536) - 32768 } "Test4", r.Next(65536) - 32768
}, },
{ {
"Test5", new ReturnUnit(){DeviceValue = r.Next(65536) - 32768 } "Test5", r.Next(65536) - 32768
}, },
{ {
"Test6", new ReturnUnit(){DeviceValue = r.Next(65536) - 32768 } "Test6", r.Next(65536) - 32768
}, },
{ {
"Test7", new ReturnUnit(){DeviceValue = r.Next(65536) - 32768 } "Test7", r.Next(65536) - 32768
}, },
{ {
"Test8", new ReturnUnit(){DeviceValue = r.Next(65536) - 32768 } "Test8", r.Next(65536) - 32768
}, },
{ {
"Test9", new ReturnUnit(){DeviceValue = r.Next(65536) - 32768 } "Test9", r.Next(65536) - 32768
}, },
{ {
"Test10", new ReturnUnit(){DeviceValue = r.Next(65536) - 32768 } "Test10", r.Next(65536) - 32768
} }
}; };
return datas; return datas;

View File

@@ -92,34 +92,34 @@ ModbusMachine<int, string> machine3 = new ModbusMachine<int, string>(3, ModbusTy
}, },
}, true, 4, 1); }, true, 4, 1);
Random r = new Random(); Random r = new Random();
await MachineJobSchedulerCreator.CreateScheduler("Trigger1", -1, 10).Result.Apply(machine.Id+".Apply", new Dictionary<string, ReturnUnit>() {{ await MachineJobSchedulerCreator.CreateScheduler("Trigger1", -1, 10).Result.ApplyTo(machine.Id+".Apply", new Dictionary<string, double>() {{
"4X 1.0", new ReturnUnit(){DeviceValue = r.Next() % 65536 } "4X 1.0", r.Next() % 65536
}, },
{ {
"4X 2.0", new ReturnUnit(){DeviceValue = r.Next() % 65536 } "4X 2.0", r.Next() % 65536
}, },
{ {
"4X 3.0", new ReturnUnit(){DeviceValue = r.Next() % 65536 } "4X 3.0", r.Next() % 65536
} }
}, MachineDataType.Address).Result.Query().Result.To(machine.Id + ".To", machine).Result.Run(); }, MachineDataType.Address).Result.To(machine.Id + ".To", machine).Result.Run();
await MachineJobSchedulerCreator.CreateScheduler("Trigger2", -1, 10).Result.Apply(machine2.Id + ".Apply", new Dictionary<string, ReturnUnit>() {{ await MachineJobSchedulerCreator.CreateScheduler("Trigger2", -1, 10).Result.ApplyTo(machine2.Id + ".Apply", new Dictionary<string, double>() {{
"4X 1.0", new ReturnUnit(){DeviceValue = r.Next() % 65536 } "4X 1.0", r.Next() % 65536
}, },
{ {
"4X 2.0", new ReturnUnit(){DeviceValue = r.Next() % 65536 } "4X 2.0", r.Next() % 65536
}, },
{ {
"4X 3.0", new ReturnUnit(){DeviceValue = r.Next() % 65536 } "4X 3.0", r.Next() % 65536
} }
}, MachineDataType.Address).Result.Query().Result.To(machine2.Id + ".To", machine2).Result.Run(); }, MachineDataType.Address).Result.To(machine2.Id + ".To", machine2).Result.Run();
await MachineJobSchedulerCreator.CreateScheduler("Trigger3", -1, 10).Result.Apply(machine3.Id + ".Apply", new Dictionary<string, ReturnUnit>() {{ await MachineJobSchedulerCreator.CreateScheduler("Trigger3", -1, 10).Result.ApplyTo(machine3.Id + ".Apply", new Dictionary<string, double>() {{
"4X 1.0", new ReturnUnit(){DeviceValue = r.Next() % 65536 } "4X 1.0", r.Next() % 65536
}, },
{ {
"4X 2.0", new ReturnUnit(){DeviceValue = r.Next() % 65536 } "4X 2.0", r.Next() % 65536
}, },
{ {
"4X 3.0", new ReturnUnit(){DeviceValue = r.Next() % 65536 } "4X 3.0", r.Next() % 65536
} }
}, MachineDataType.Address).Result.Query().Result.To(machine3.Id + ".To", machine3).Result.Run(); }, MachineDataType.Address).Result.To(machine3.Id + ".To", machine3).Result.Run();
Console.ReadLine(); Console.ReadLine();