From 728cb7aefb24efabfd81de751b2574c7f8e51d58 Mon Sep 17 00:00:00 2001 From: ig Date: Fri, 25 Aug 2023 17:15:46 +0200 Subject: [PATCH] make AmptStatus implement IMppt --- csharp/Lib/Devices/AMPT/Ampt.csproj | 1 + .../Lib/Devices/AMPT/AmptCommunicationUnit.cs | 90 ------------------- csharp/Lib/Devices/AMPT/AmptDevices.cs | 26 ++++-- csharp/Lib/Devices/AMPT/AmptStatus.cs | 35 +------- .../Devices/Battery48TL/Battery48TlDevices.cs | 2 - .../Trumpf/TruConvertDc/DcDcDevicesRecord.cs | 3 +- csharp/Lib/StatusApi/DeviceTypes/IMppt.cs | 1 - 7 files changed, 24 insertions(+), 134 deletions(-) delete mode 100644 csharp/Lib/Devices/AMPT/AmptCommunicationUnit.cs diff --git a/csharp/Lib/Devices/AMPT/Ampt.csproj b/csharp/Lib/Devices/AMPT/Ampt.csproj index eebc28a52..b3e4acee8 100644 --- a/csharp/Lib/Devices/AMPT/Ampt.csproj +++ b/csharp/Lib/Devices/AMPT/Ampt.csproj @@ -9,6 +9,7 @@ + diff --git a/csharp/Lib/Devices/AMPT/AmptCommunicationUnit.cs b/csharp/Lib/Devices/AMPT/AmptCommunicationUnit.cs deleted file mode 100644 index 2592c22d3..000000000 --- a/csharp/Lib/Devices/AMPT/AmptCommunicationUnit.cs +++ /dev/null @@ -1,90 +0,0 @@ -// using InnovEnergy.Lib.Protocols.Modbus.Channels; -// using InnovEnergy.Lib.Protocols.Modbus.Clients; -// using InnovEnergy.Lib.Protocols.Modbus.Conversions; -// using InnovEnergy.Lib.Protocols.Modbus.Slaves; -// using InnovEnergy.Lib.Units.Composite; -// -// namespace InnovEnergy.Lib.Devices.AMPT; -// -// public class AmptCommunicationUnit : ModbusDevice -// { -// -// private const UInt16 RegistersPerDevice = 16; -// private const UInt16 FirstDeviceOffset = 85; -// -// -// public AmptCommunicationUnit(String hostname, UInt16 port = 502, Byte slaveAddress = 1) : this -// ( -// channel: new TcpChannel(hostname, port), -// slaveAddress -// ) -// {} -// -// -// public AmptCommunicationUnit(Channel channel, Byte slaveAddress) : this -// ( -// client: new ModbusTcpClient(channel, slaveAddress) -// ) -// {} -// -// public AmptCommunicationUnit(ModbusClient client) : base(client) -// { -// } -// -// -// private AmptCommunicationUnitStatus TryReadStatus() -// { -// var r = new ModbusRegisters(116, Modbus.ReadHoldingRegisters(1, 116).ToArray()) ; // TODO -// -// var currentFactor = Pow(10.0m, r.GetInt16(73)); -// var voltageFactor = Pow(10.0m, r.GetInt16(74)); -// var energyFactor = Pow(10.0m, r.GetInt16(76) + 3); // +3 => converted from Wh to kWh -// var nbrOfDevices = r.GetUInt16(78); -// -// var devices = Enumerable -// .Range(0, nbrOfDevices) -// .Select(ReadDeviceStatus) -// .ToList(); -// -// return new AmptCommunicationUnitStatus -// { -// Sid = r.GetUInt32(1), -// IdSunSpec = r.GetUInt16(3), -// Manufacturer = r.GetString(5, 16), -// Model = r.GetString(21, 16), -// Version = r.GetString(45, 8), -// SerialNumber = r.GetString(53, 16), -// DeviceAddress = r.GetInt16(69), -// IdVendor = r.GetUInt16(71), -// Devices = devices -// }; -// -// AmptStatus ReadDeviceStatus(Int32 deviceNumber) -// { -// var baseAddress = (UInt16)(FirstDeviceOffset + deviceNumber * RegistersPerDevice); // base address -// -// return new AmptStatus -// { -// Dc = new DcBus -// { -// Voltage = r.GetUInt32((UInt16)(baseAddress + 6)) * voltageFactor, -// Current = r.GetUInt16((UInt16)(baseAddress + 5)) * currentFactor -// }, -// Strings = new DcBus[] -// { -// new() -// { -// Voltage = r.GetUInt32((UInt16)(baseAddress + 8)) * voltageFactor, -// Current = r.GetUInt16((UInt16)(baseAddress + 14)) * currentFactor -// }, -// new() -// { -// Voltage = r.GetUInt32((UInt16)(baseAddress + 9)) * voltageFactor, -// Current = r.GetUInt16((UInt16)(baseAddress + 15)) * currentFactor -// } -// }, -// ProductionToday = r.GetUInt32((UInt16)(baseAddress + 12)) * energyFactor, -// }; -// } -// } -// } \ No newline at end of file diff --git a/csharp/Lib/Devices/AMPT/AmptDevices.cs b/csharp/Lib/Devices/AMPT/AmptDevices.cs index 69235615f..4247af309 100644 --- a/csharp/Lib/Devices/AMPT/AmptDevices.cs +++ b/csharp/Lib/Devices/AMPT/AmptDevices.cs @@ -1,6 +1,7 @@ using InnovEnergy.Lib.Protocols.Modbus.Channels; using InnovEnergy.Lib.Protocols.Modbus.Clients; using InnovEnergy.Lib.Protocols.Modbus.Slaves; +using InnovEnergy.Lib.Time.Unix; using InnovEnergy.Lib.Units.Composite; using InnovEnergy.Lib.Utils; @@ -25,22 +26,25 @@ public class AmptDevices _StringOptimizers = StringOptimizers(modbusClient); } - public AmptStatus Read() + public AmptStatus? Read() { - CommunicationUnitRegisters? cuStatus = null; - try { - cuStatus = _CommunicationUnit.Read(); + return TryRead(); } catch (Exception e) { Console.WriteLine("Failed to read Ampt data \n" + e.Message); - // TODO: log + return null; } - + } + + public AmptStatus TryRead() + { + var cuStatus = _CommunicationUnit.Read(); + // CommunicationUnit knows how many StringOptimizers are connected - var nStringOptimizers = cuStatus?.NumberOfStringOptimizers ?? 0; + var nStringOptimizers = cuStatus.NumberOfStringOptimizers; // hardcoded: every SO has 2 strings (produced like this by AMPT) var nStrings = nStringOptimizers * 2; @@ -62,9 +66,13 @@ public class AmptDevices var dc = DcBus.FromVoltageCurrent(busVoltage, busCurrent); // flatten the 2 strings of each SO into one array - var strings = soStati.SelectMany(GetStrings).ToArray(nStrings); + var strings = soStati.SelectMany(GetStrings).ToArray(nStrings); - return new AmptStatus(dc, strings); + return new AmptStatus + { + Dc = dc, + Strings = strings + }; } diff --git a/csharp/Lib/Devices/AMPT/AmptStatus.cs b/csharp/Lib/Devices/AMPT/AmptStatus.cs index 19977095e..49f118011 100644 --- a/csharp/Lib/Devices/AMPT/AmptStatus.cs +++ b/csharp/Lib/Devices/AMPT/AmptStatus.cs @@ -1,38 +1,11 @@ +using InnovEnergy.Lib.StatusApi.DeviceTypes; using InnovEnergy.Lib.Units.Composite; namespace InnovEnergy.Lib.Devices.AMPT; -public class AmptStatus +public class AmptStatus : IMppt { - public AmptStatus(DcBus? dc, IReadOnlyList strings) - { - Dc = dc; - Strings = strings; - } - - public DcBus? Dc { get; } - public IReadOnlyList Strings { get; } - - public static AmptStatus Null => new AmptStatus(null, Array.Empty()); + public required DcBus Dc { get; init; } + public required IReadOnlyList Strings { get; init; } } - -// public static AmptStatus Parallel(IReadOnlyList stati) - // { - // if (stati.Count == 0) - // { - // return new AmptStatus - // ( - // Dc: DcBus.FromVoltageCurrent(0, 0), - // Strings: Array.Empty() - // ); - // } - // - // var voltage = stati.Average(s => s.Dc.Voltage.Value); - // var current = stati.Sum(s => s.Dc.Current.Value); - // var dc = DcBus.FromVoltageCurrent(voltage, current); - // - // var strings = stati.SelectMany(s => s.Strings).ToList(); - // - // return new AmptStatus(dc, strings); - // } \ No newline at end of file diff --git a/csharp/Lib/Devices/Battery48TL/Battery48TlDevices.cs b/csharp/Lib/Devices/Battery48TL/Battery48TlDevices.cs index 7a2c3cb45..6d6a98c63 100644 --- a/csharp/Lib/Devices/Battery48TL/Battery48TlDevices.cs +++ b/csharp/Lib/Devices/Battery48TL/Battery48TlDevices.cs @@ -25,7 +25,5 @@ public class Battery48TlDevices return Battery48TlRecords.Null; } - - } } \ No newline at end of file diff --git a/csharp/Lib/Devices/Trumpf/TruConvertDc/DcDcDevicesRecord.cs b/csharp/Lib/Devices/Trumpf/TruConvertDc/DcDcDevicesRecord.cs index e1491a49e..605081415 100644 --- a/csharp/Lib/Devices/Trumpf/TruConvertDc/DcDcDevicesRecord.cs +++ b/csharp/Lib/Devices/Trumpf/TruConvertDc/DcDcDevicesRecord.cs @@ -16,7 +16,8 @@ public class DcDcDevicesRecord public DcStatus Dc => new DcStatus { - Battery = Devices.Count == 0 ? NoDevice + Battery = Devices.Count == 0 + ? NoDevice : DcBus.FromVoltageCurrent ( Devices.Average(r => r.Status.Dc.Battery.Voltage.Value), diff --git a/csharp/Lib/StatusApi/DeviceTypes/IMppt.cs b/csharp/Lib/StatusApi/DeviceTypes/IMppt.cs index 70e208ae0..198bc91a5 100644 --- a/csharp/Lib/StatusApi/DeviceTypes/IMppt.cs +++ b/csharp/Lib/StatusApi/DeviceTypes/IMppt.cs @@ -1,5 +1,4 @@ using InnovEnergy.Lib.StatusApi.Connections; -using InnovEnergy.Lib.Units.Composite; namespace InnovEnergy.Lib.StatusApi.DeviceTypes;