Innovenergy_trunk/csharp/Lib/Protocols/Modbus/Reflection/Batch.cs

119 lines
4.2 KiB
C#
Raw Normal View History

2023-05-04 07:32:35 +00:00
using InnovEnergy.Lib.Protocols.Modbus.Clients;
2023-06-13 11:03:36 +00:00
using InnovEnergy.Lib.Protocols.Modbus.Protocol;
2023-05-04 07:32:35 +00:00
using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames;
using InnovEnergy.Lib.Protocols.Modbus.Protocol.Frames.Accessors;
namespace InnovEnergy.Lib.Protocols.Modbus.Reflection;
#pragma warning disable CS8509
2023-06-13 11:03:36 +00:00
#pragma warning disable CS8524
2023-05-04 07:32:35 +00:00
internal record Batch<R>(Action<R> Read, Action<R> Write);
public static class Batches
{
2023-06-13 11:03:36 +00:00
internal static IReadOnlyList<Batch<R>> MakeBatchesFor<R>(this ModbusClient modbusClient, Int32 addressOffset)
2023-05-04 07:32:35 +00:00
{
var members = ModbusMembers
.From<R>(addressOffset)
2023-06-13 11:03:36 +00:00
.OrderBy(m => m.Kind)
2023-05-04 07:32:35 +00:00
.ThenBy(m => m.StartAddress)
.ThenBy(m => m.EndAddress);
2023-06-13 11:03:36 +00:00
return MakeBatches<R>(modbusClient, members).ToList();
2023-05-04 07:32:35 +00:00
}
private static IEnumerable<Batch<R>> MakeBatches<R>(ModbusClient mb, IEnumerable<ModbusMember> modbusMembers)
{
var batchMembers = new List<ModbusMember>();
foreach (var member in modbusMembers)
{
if (CloseBatch(member))
{
yield return MakeBatch<R>(mb, batchMembers);
batchMembers = new List<ModbusMember>();
}
batchMembers.Add(member);
}
if (batchMembers.Count > 0)
yield return MakeBatch<R>(mb, batchMembers);
Boolean CloseBatch(ModbusMember m)
{
if (batchMembers.Count == 0)
return false;
return m.StartAddress > batchMembers[^1].EndAddress // gap between registers
|| m.EndAddress > batchMembers[0].StartAddress + Constants.MaxRegs // max batch size reached
2023-06-13 11:03:36 +00:00
|| m.Kind != batchMembers[0].Kind; // different Kind
2023-05-04 07:32:35 +00:00
}
}
private static Batch<R> MakeBatch<R>(ModbusClient modbusClient, IReadOnlyList<ModbusMember> members)
{
var startAddress = members[0].StartAddress;
var endAddress = members[^1].EndAddress;
var count = (UInt16)(endAddress - startAddress);
2023-06-13 11:03:36 +00:00
var kind = members[0].Kind;
var isWritable = kind is ModbusKind.HoldingRegister or ModbusKind.Coil;
2023-05-04 07:32:35 +00:00
return new Batch<R>(Read(), Write());
Action<R> Read()
{
2023-06-13 11:03:36 +00:00
Func<MbData> readModbus = kind switch
2023-05-04 07:32:35 +00:00
{
2023-06-13 11:03:36 +00:00
ModbusKind.InputRegister => () => modbusClient.ReadInputRegisters (startAddress, count),
ModbusKind.HoldingRegister => () => modbusClient.ReadHoldingRegisters(startAddress, count),
ModbusKind.DiscreteInput => () => modbusClient.ReadDiscreteInputs (startAddress, count),
ModbusKind.Coil => () => modbusClient.ReadCoils (startAddress, count),
2023-05-04 07:32:35 +00:00
};
2023-06-13 11:03:36 +00:00
//Console.WriteLine("start: " + startAddress + " count: " + count);
2023-05-04 07:32:35 +00:00
return record =>
{
2023-06-13 11:03:36 +00:00
var mbData = readModbus();
2023-05-04 07:32:35 +00:00
foreach (var member in members)
{
member.ModbusToRecord(mbData, record!);
}
};
}
Action<R> Write()
{
if (!isWritable)
return _ => { }; // nop
2023-06-13 11:03:36 +00:00
Func<MbData> createMbData = kind switch
2023-05-04 07:32:35 +00:00
{
2023-06-13 11:03:36 +00:00
ModbusKind.HoldingRegister => () => MbData.Registers(startAddress, count),
ModbusKind.Coil => () => MbData.Coils (startAddress, count),
2023-05-04 07:32:35 +00:00
};
2023-06-13 11:03:36 +00:00
Action<MbData> writeModbus = kind switch
2023-05-04 07:32:35 +00:00
{
2023-06-13 11:03:36 +00:00
ModbusKind.HoldingRegister => d => modbusClient.WriteRegisters(startAddress, d.GetRegisters()),
ModbusKind.Coil => d => modbusClient.WriteCoils (startAddress, d.GetCoils().Take(count).ToList()),
// ^^ TODO: Coils.count is broken, fix when refactoring to use direct binary codec
2023-05-04 07:32:35 +00:00
};
return rec =>
{
2023-06-13 11:03:36 +00:00
var mbData = createMbData();
2023-05-04 07:32:35 +00:00
foreach (var member in members)
member.RecordToModbus(rec!, mbData);
2023-06-13 11:03:36 +00:00
writeModbus(mbData);
2023-05-04 07:32:35 +00:00
};
}
}
}