diff --git a/Modbus.Net/Modbus.Net/Job/JobChainingJobListenerWithDataMap.cs b/Modbus.Net/Modbus.Net/Job/JobChainingJobListenerWithDataMap.cs index 9fa2f14..eedfafc 100644 --- a/Modbus.Net/Modbus.Net/Job/JobChainingJobListenerWithDataMap.cs +++ b/Modbus.Net/Modbus.Net/Job/JobChainingJobListenerWithDataMap.cs @@ -10,7 +10,7 @@ namespace Modbus.Net { public class JobChainingJobListenerWithDataMap : JobListenerSupport { - public JobChainingJobListenerWithDataMap(string name, string[] overwriteKeys) + public JobChainingJobListenerWithDataMap(string name, ICollection overwriteKeys) { Name = name; OverWriteKeys = overwriteKeys; @@ -21,7 +21,7 @@ namespace Modbus.Net public override string Name { get; } - public string[] OverWriteKeys { get; } + public ICollection OverWriteKeys { get; } /// /// Add a chain mapping - when the Job identified by the first key completes diff --git a/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs b/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs index 0877983..a14ff59 100644 --- a/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs +++ b/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs @@ -50,7 +50,7 @@ namespace Modbus.Net .WithSimpleSchedule(b => b.WithInterval(TimeSpan.FromSeconds(interval)).RepeatForever()) .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.GroupEquals("Modbus.Net.DataQuery.Group." + triggerKey)); if (await scheduler.GetTrigger(new TriggerKey(triggerKey)) != null) @@ -120,12 +120,12 @@ namespace Modbus.Net return new MachineQueryJobScheduler(_scheduler, _trigger, jobKey); } - public Task Apply(string queryId, Dictionary values, MachineDataType machineDataType) + public Task Apply(string queryId, Dictionary values, MachineDataType machineDataType) { return Apply(queryId, values, machineDataType); } - public async Task Apply(string queryId, Dictionary values, MachineDataType machineDataType) where TMachineKey : IEquatable + public async Task Apply(string queryId, Dictionary values, MachineDataType machineDataType) where TMachineKey : IEquatable { JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name); @@ -135,7 +135,7 @@ namespace Modbus.Net .Build(); job.JobDataMap.Put("DataType", machineDataType); - job.JobDataMap.Put("Value", values); + job.JobDataMap.Put("SetValue", values); job.JobDataMap.Put("QueryMethod", null); if (_parentJobKey != null) @@ -153,12 +153,12 @@ namespace Modbus.Net return new MachineQueryJobScheduler(_scheduler, _trigger, jobKey); } - public Task ApplyTo(string queryId, Dictionary values, MachineDataType machineDataType) + public Task ApplyTo(string queryId, Dictionary values, MachineDataType machineDataType) { return ApplyTo(queryId, values, machineDataType); } - public async Task ApplyTo(string queryId, Dictionary values, MachineDataType machineDataType) where TMachineKey : IEquatable + public async Task ApplyTo(string queryId, Dictionary values, MachineDataType machineDataType) where TMachineKey : IEquatable { var applyJobScheduler = await Apply(queryId, values, machineDataType); return await applyJobScheduler.Query(); @@ -180,12 +180,12 @@ namespace Modbus.Net _parentJobKey = parentJobKey; } - public Task Query(string queryId = null, Func> QueryDataFunc = null) + public Task Query(string queryId = null, Func> QueryDataFunc = null) { return Query(queryId, QueryDataFunc); } - public async Task Query(string queryId = null, Func> QueryDataFunc = null) where TMachineKey : IEquatable + public async Task Query(string queryId = null, Func> QueryDataFunc = null) where TMachineKey : IEquatable { 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("Value", out values); context.JobDetail.JobDataMap.TryGetValue("QueryMethod", out QueryMethod); - Func> QueryMethodDispatch = (Func>)QueryMethod; + Func> QueryMethodDispatch = (Func>)QueryMethod; - if (QueryMethod != null) + if (QueryMethod != null && values != null) { - context.JobDetail.JobDataMap.Put("Value", QueryMethodDispatch(new DataReturnDef() { MachineId = machine == null ? null : ((IMachineProperty)machine).GetMachineIdString(), ReturnValues = (Dictionary)values })); + context.JobDetail.JobDataMap.Put("SetValue", QueryMethodDispatch(new DataReturnDef() { MachineId = machine == null ? null : ((IMachineProperty)machine).GetMachineIdString(), ReturnValues = (Dictionary)values })); await context.Scheduler.AddJob(context.JobDetail, true, false); } } @@ -296,12 +296,15 @@ namespace Modbus.Net object machine; object machineDataType; object values; + object valuesSet; context.JobDetail.JobDataMap.TryGetValue("Machine", out machine); context.JobDetail.JobDataMap.TryGetValue("DataType", out machineDataType); context.JobDetail.JobDataMap.TryGetValue("Value", out values); - Dictionary valuesSet = ((Dictionary)values).MapGetValuesToSetValues(); + context.JobDetail.JobDataMap.TryGetValue("SetValue", out valuesSet); + if (valuesSet == null && values != null) + valuesSet = ((Dictionary)values).MapGetValuesToSetValues(); - var success = await (machine as IMachineMethodData)!.SetDatasAsync((MachineDataType)machineDataType, valuesSet); + var success = await (machine as IMachineMethodData)!.SetDatasAsync((MachineDataType)machineDataType, (Dictionary)valuesSet); } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs b/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs index a5ef938..fff4717 100644 --- a/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs +++ b/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs @@ -285,7 +285,7 @@ namespace Modbus.Net ans.Add(key, new ReturnUnit { DeviceValue = null, - UnitExtend = address.UnitExtend + AddressUnit = address.MapAddressUnitTUnitKeyToAddressUnit(), }); else ans.Add(key, @@ -296,7 +296,7 @@ namespace Modbus.Net ValueHelper.GetInstance(BaseUtility.Endian) .GetValue(datas, ref localMainPos, ref localSubPos, address.DataType)) * address.Zoom, - UnitExtend = address.UnitExtend + AddressUnit = address.MapAddressUnitTUnitKeyToAddressUnit(), }); } catch (Exception e) @@ -710,9 +710,9 @@ namespace Modbus.Net public double? DeviceValue { get; set; } /// - /// 数据的扩展 + /// 数据定义 /// - public UnitExtend UnitExtend { get; set; } + public AddressUnit AddressUnit { get; set; } } /// @@ -797,4 +797,34 @@ namespace Modbus.Net return Area.ToUpper() == other.Area.ToUpper() && Address == other.Address || Id.Equals(other.Id); } } + + /// + /// AddressUnit扩展 + /// + public static class AddressUnitExtend + { + /// + /// 映射泛型AddressUnit到字符串型AddressUnit + /// + /// + /// + public static AddressUnit MapAddressUnitTUnitKeyToAddressUnit(this AddressUnit addressUnit) where TUnitKey : IEquatable + { + 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 + }; + } + } } \ No newline at end of file diff --git a/Samples/MachineJob/Program.cs b/Samples/MachineJob/Program.cs index 5b5df1c..e17230b 100644 --- a/Samples/MachineJob/Program.cs +++ b/Samples/MachineJob/Program.cs @@ -25,14 +25,14 @@ await MachineJobSchedulerCreator.CreateScheduler("Trigger1", -1, 5).Result.From( Console.ReadLine(); -Dictionary QueryConsole(DataReturnDef dataReturnDef) +Dictionary QueryConsole(DataReturnDef dataReturnDef) { var values = dataReturnDef.ReturnValues; foreach (var value in values) { Console.WriteLine(dataReturnDef.MachineId + " " + value.Key + " " + value.Value.DeviceValue); } - + try { using (var context = new DatabaseWriteContext()) @@ -58,50 +58,50 @@ Dictionary QueryConsole(DataReturnDef dataReturnDef) { //ignore } - + Random r = new Random(); foreach (var value in values) { value.Value.DeviceValue = r.Next(65536) - 32768; } - return values; + return values.MapGetValuesToSetValues(); } -Dictionary QueryConsole2(DataReturnDef dataReturnDef) +Dictionary QueryConsole2(DataReturnDef dataReturnDef) { Random r = new Random(); - var datas = new Dictionary() + var datas = new Dictionary() { { - "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; diff --git a/Tests/Modbus.Net.PersistedTests/Program.cs b/Tests/Modbus.Net.PersistedTests/Program.cs index 5eee48a..b622d6b 100644 --- a/Tests/Modbus.Net.PersistedTests/Program.cs +++ b/Tests/Modbus.Net.PersistedTests/Program.cs @@ -92,34 +92,34 @@ ModbusMachine machine3 = new ModbusMachine(3, ModbusTy }, }, true, 4, 1); Random r = new Random(); -await MachineJobSchedulerCreator.CreateScheduler("Trigger1", -1, 10).Result.Apply(machine.Id+".Apply", new Dictionary() {{ - "4X 1.0", new ReturnUnit(){DeviceValue = r.Next() % 65536 } +await MachineJobSchedulerCreator.CreateScheduler("Trigger1", -1, 10).Result.ApplyTo(machine.Id+".Apply", new Dictionary() {{ + "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(); -await MachineJobSchedulerCreator.CreateScheduler("Trigger2", -1, 10).Result.Apply(machine2.Id + ".Apply", new Dictionary() {{ - "4X 1.0", new ReturnUnit(){DeviceValue = r.Next() % 65536 } +}, MachineDataType.Address).Result.To(machine.Id + ".To", machine).Result.Run(); +await MachineJobSchedulerCreator.CreateScheduler("Trigger2", -1, 10).Result.ApplyTo(machine2.Id + ".Apply", new Dictionary() {{ + "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(); -await MachineJobSchedulerCreator.CreateScheduler("Trigger3", -1, 10).Result.Apply(machine3.Id + ".Apply", new Dictionary() {{ - "4X 1.0", new ReturnUnit(){DeviceValue = r.Next() % 65536 } +}, MachineDataType.Address).Result.To(machine2.Id + ".To", machine2).Result.Run(); +await MachineJobSchedulerCreator.CreateScheduler("Trigger3", -1, 10).Result.ApplyTo(machine3.Id + ".Apply", new Dictionary() {{ + "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(); \ No newline at end of file