Innovenergy_trunk/csharp/Lib/Devices/Trumpf/TruConvertDc/TruConvertDcDcDevices.cs

78 lines
2.1 KiB
C#
Raw Normal View History

2023-06-13 11:01:01 +00:00
using InnovEnergy.Lib.Devices.Trumpf.SystemControl;
using InnovEnergy.Lib.Protocols.Modbus.Channels;
using InnovEnergy.Lib.Protocols.Modbus.Clients;
using InnovEnergy.Lib.Protocols.Modbus.Slaves;
using InnovEnergy.Lib.Utils;
namespace InnovEnergy.Lib.Devices.Trumpf.TruConvertDc;
public class TruConvertDcDcDevices
{
private readonly ModbusDevice<SystemControlRegisters> _SystemControl;
private readonly IEnumerable<ModbusDevice<DcDcRecord>> _DcDcs;
public TruConvertDcDcDevices(String hostname, UInt16 port) : this(new TcpChannel(hostname, port))
{
}
public TruConvertDcDcDevices(Channel transport)
{
var modbusClient = new ModbusTcpClient(transport, 0);
_SystemControl = new ModbusDevice<SystemControlRegisters>(modbusClient);
_DcDcs = Enumerable
.Range(1, Byte.MaxValue - 1)
.Memoize(CreateDcDc);
ModbusDevice<DcDcRecord> CreateDcDc(Int32 i)
{
var mb = new ModbusTcpClient(transport, (Byte)i);
return new ModbusDevice<DcDcRecord>(mb);
}
}
public DcDcDevicesRecord Read()
{
try
{
var scStatus = _SystemControl.Read();
var n = scStatus.NumberOfConnectedSlaves;
2023-06-13 11:01:01 +00:00
var dcDcRecords = _DcDcs
.Take(n)
.Select(dcdc => dcdc.Read())
.ToArray(n);
return new DcDcDevicesRecord(scStatus, dcDcRecords);
}
catch
2023-06-13 11:01:01 +00:00
{
"Failed to read DCDC data".WriteLine();
return new DcDcDevicesRecord(null, Array.Empty<DcDcRecord>());
2023-06-13 11:01:01 +00:00
}
}
public void Write(DcDcDevicesRecord r)
{
if (r.SystemControl is not null)
_SystemControl.Write(r.SystemControl); // must run BEFORE the attached devices
foreach (var (ctrl, device) in r.Devices.Zip(_DcDcs))
{
try
{
device.Write(ctrl);
}
catch (Exception e)
{
Console.WriteLine(e);
// TODO: log
}
}
}
}