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

88 lines
2.3 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()
{
SystemControlRegisters? scStatus;
try
{
scStatus = _SystemControl.Read();
}
catch (Exception e)
{
Console.WriteLine(e);
return DcDcDevicesRecord.Null;
}
var n = scStatus.NumberOfConnectedSlaves;
try
{
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();
2023-06-13 11:01:01 +00:00
return new DcDcDevicesRecord(scStatus, Array.Empty<DcDcRecord>());
}
}
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
}
}
}
}