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 _SystemControl; private readonly IEnumerable> _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(modbusClient); _DcDcs = Enumerable .Range(1, Byte.MaxValue - 1) .Memoize(CreateDcDc); ModbusDevice CreateDcDc(Int32 i) { var mb = new ModbusTcpClient(transport, (Byte)i); return new ModbusDevice(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 { "Failed to read DCDC data".WriteLine(); return new DcDcDevicesRecord(scStatus, Array.Empty()); } } 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 } } } }