Innovenergy_trunk/csharp/Lib/Devices/WITGrowatt4-15K/WITGrowattDevices.cs

56 lines
1.4 KiB
C#

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.WITGrowatt4_15K;
public class WITGrowattDevices
{
private readonly IEnumerable<ModbusDevice<WITGrowatRecord>> _AcDcs;
private readonly IReadOnlyList<WITGrowatDevice> _Devices;
public WITGrowattDevices(IReadOnlyList<WITGrowatDevice> devices) => _Devices = devices;
public WitGrowattDevicesRecord? Read()
{
var records = _Devices
.Select(TryRead)
.NotNull()
.ToList();
return new WitGrowattDevicesRecord(records);
}
private static WITGrowatRecord? TryRead(WITGrowatDevice d)
{
try
{
return d.Read();
}
catch (Exception e)
{
Console.WriteLine($"Failed to read Battery node {d.SlaveId}\n{e.Message}");
// TODO: log
return null;
}
}
public void Write(WitGrowattDevicesRecord r)
{
try
{
foreach (var (ctrl, device) in r.Devices.Zip(_AcDcs))
{
device.Write(ctrl);
}
}
catch (Exception e)
{
Console.WriteLine(e);
// TODO: log
}
}
}