Using channel instead of tcp channel and create channel based on the device state.

This commit is contained in:
atef 2023-10-23 10:47:57 +02:00
parent 9b1dd755e2
commit 1654d6f4dd
1 changed files with 72 additions and 40 deletions

View File

@ -1,11 +1,11 @@
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Reactive.Threading.Tasks; using System.Reactive.Threading.Tasks;
using Flurl.Http; using Flurl.Http;
using InnovEnergy.App.SaliMax.Devices;
using InnovEnergy.App.SaliMax.Ess; using InnovEnergy.App.SaliMax.Ess;
using InnovEnergy.App.SaliMax.SaliMaxRelays; using InnovEnergy.App.SaliMax.SaliMaxRelays;
using InnovEnergy.App.SaliMax.System; using InnovEnergy.App.SaliMax.System;
using InnovEnergy.App.SaliMax.SystemConfig; using InnovEnergy.App.SaliMax.SystemConfig;
using InnovEnergy.App.SaliMax.VirtualDevices;
using InnovEnergy.Lib.Devices.AMPT; using InnovEnergy.Lib.Devices.AMPT;
using InnovEnergy.Lib.Devices.Battery48TL; using InnovEnergy.Lib.Devices.Battery48TL;
using InnovEnergy.Lib.Devices.EmuMeter; using InnovEnergy.Lib.Devices.EmuMeter;
@ -20,6 +20,7 @@ using InnovEnergy.Lib.Time.Unix;
using InnovEnergy.Lib.Units; using InnovEnergy.Lib.Units;
using InnovEnergy.Lib.Utils; using InnovEnergy.Lib.Utils;
using static InnovEnergy.Lib.Devices.Trumpf.SystemControl.DataTypes.SystemConfig; using static InnovEnergy.Lib.Devices.Trumpf.SystemControl.DataTypes.SystemConfig;
using DeviceState = InnovEnergy.App.SaliMax.Devices.DeviceState;
#pragma warning disable IL2026 #pragma warning disable IL2026
@ -32,32 +33,40 @@ internal static class Program
private static readonly IReadOnlyList<Byte> BatteryNodes; private static readonly IReadOnlyList<Byte> BatteryNodes;
private static readonly TcpChannel TruConvertAcChannel ; private static readonly Channel TruConvertAcChannel ;
private static readonly TcpChannel TruConvertDcChannel ; private static readonly Channel TruConvertDcChannel ;
private static readonly TcpChannel GridMeterChannel ; private static readonly Channel GridMeterChannel ;
private static readonly TcpChannel IslandBusLoadChannel; private static readonly Channel IslandBusLoadChannel;
private static readonly TcpChannel AmptChannel ; private static readonly Channel PvOnDc ;
private static readonly TcpChannel RelaysChannel ; private static readonly Channel PvOnAcGrid ;
private static readonly TcpChannel BatteriesChannel ; private static readonly Channel PvOnAcIsland ;
private static readonly Channel RelaysChannel ;
private static readonly Channel BatteriesChannel ;
static Program() static Program()
{ {
var config = Config.Load(); var config = Config.Load();
var d = config.Devices; var d = config.Devices;
TruConvertAcChannel = new TcpChannel(d.TruConvertAcIp); Channel CreateChannel(SalimaxDevice device) => device.DeviceState == DeviceState.Disabled
TruConvertDcChannel = new TcpChannel(d.TruConvertDcIp); ? new NullChannel()
GridMeterChannel = new TcpChannel(d.GridMeterIp); : new TcpChannel(device);
IslandBusLoadChannel = new TcpChannel(d.IslandBusLoadMeterIp);
AmptChannel = new TcpChannel(d.AmptIp);
RelaysChannel = new TcpChannel(d.RelaysIp);
BatteriesChannel = new TcpChannel(d.BatteryIp);
BatteryNodes = config TruConvertAcChannel = CreateChannel(d.TruConvertAcIp);
.Devices TruConvertDcChannel = CreateChannel(d.TruConvertDcIp);
.BatteryNodes GridMeterChannel = CreateChannel(d.GridMeterIp);
.Select(n => n.ConvertTo<Byte>()) IslandBusLoadChannel = CreateChannel(d.IslandBusLoadMeterIp);
.ToArray(config.Devices.BatteryNodes.Length); PvOnDc = CreateChannel(d.PvOnDc);
PvOnAcGrid = CreateChannel(d.PvOnAcGrid);
PvOnAcIsland = CreateChannel(d.PvOnAcIsland);
RelaysChannel = CreateChannel(d.RelaysIp);
BatteriesChannel = CreateChannel(d.BatteryIp);
BatteryNodes = config
.Devices
.BatteryNodes
.Select(n => n.ConvertTo<Byte>())
.ToArray(config.Devices.BatteryNodes.Length);
} }
public static async Task Main(String[] args) public static async Task Main(String[] args)
@ -91,26 +100,35 @@ internal static class Program
var dcDcDevices = new TruConvertDcDcDevices(TruConvertDcChannel); var dcDcDevices = new TruConvertDcDcDevices(TruConvertDcChannel);
var gridMeterDevice = new EmuMeterDevice(GridMeterChannel); var gridMeterDevice = new EmuMeterDevice(GridMeterChannel);
var acIslandLoadMeter = new EmuMeterDevice(IslandBusLoadChannel); var acIslandLoadMeter = new EmuMeterDevice(IslandBusLoadChannel);
var amptDevice = new AmptDevices(AmptChannel); var pvOnDcDevice = new AmptDevices(PvOnDc);
var pvOnAcGridDevice = new AmptDevices(PvOnAcGrid);
var pvOnAcIslandDevice = new AmptDevices(PvOnAcIsland);
var saliMaxRelaysDevice = new RelaysDevice(RelaysChannel); var saliMaxRelaysDevice = new RelaysDevice(RelaysChannel);
StatusRecord ReadStatus() StatusRecord ReadStatus()
{ {
var config = Config.Load();
var devices = config.Devices;
var acDc = acDcDevices.Read(); var acDc = acDcDevices.Read();
var dcDc = dcDcDevices.Read(); var dcDc = dcDcDevices.Read();
var relays = saliMaxRelaysDevice.Read(); var relays = saliMaxRelaysDevice.Read();
var loadOnAcIsland = acIslandLoadMeter.Read(); var loadOnAcIsland = acIslandLoadMeter.Read();
var gridMeter = gridMeterDevice.Read(); var gridMeter = gridMeterDevice.Read();
var pvOnDc = amptDevice.Read(); var pvOnDc = pvOnDcDevice.Read();
var battery = batteryDevices.Read(); var battery = batteryDevices.Read();
var pvOnAcGrid = new AcPowerDevice { Power = 0 }; // TODO var pvOnAcGrid = pvOnAcGridDevice.Read();
var pvOnAcIsland = new AcPowerDevice { Power = 0 }; // TODO var pvOnAcIsland = pvOnAcIslandDevice.Read();
var gridBusToIslandBus = Topology.CalculateGridBusToIslandBusPower(pvOnAcIsland, loadOnAcIsland, acDc); var gridBusToIslandBus = Topology.CalculateGridBusToIslandBusPower(pvOnAcIsland, loadOnAcIsland, acDc);
var gridBusLoad = Topology.CalculateGridBusLoad(gridMeter, pvOnAcGrid, gridBusToIslandBus);
var dcLoad = Topology.CalculateDcLoad(acDc, pvOnDc, dcDc); var gridBusLoad = devices.LoadOnAcGrid.DeviceState == DeviceState.Disabled ?
new AcPowerDevice { Power = 0 } :
Topology.CalculateGridBusLoad(gridMeter, pvOnAcGrid, gridBusToIslandBus);
var dcLoad = devices.LoadOnDc.DeviceState == DeviceState.Disabled ?
new DcPowerDevice() { Power = 0 } :
Topology.CalculateDcLoad(acDc, pvOnDc, dcDc);
return new StatusRecord return new StatusRecord
{ {
@ -132,7 +150,7 @@ internal static class Program
StateMachine = StateMachine.Default, StateMachine = StateMachine.Default,
EssControl = EssControl.Default, EssControl = EssControl.Default,
Log = new SystemLog{Led = LedState.Green, Message = null}, //TODO: Put real stuff Log = new SystemLog{Led = LedState.Green, Message = null}, //TODO: Put real stuff
Config = Config.Load() // load from disk every iteration, so config can be changed while running Config = config // load from disk every iteration, so config can be changed while running
}; };
} }
@ -152,28 +170,42 @@ internal static class Program
await Observable await Observable
.Interval(UpdateInterval) .Interval(UpdateInterval)
.Select(_ => RunIteration()) .Select(_ => RunIteration())
.SelectMany(r => UploadCsv(r, DateTime.UtcNow)) .SelectMany(r => UploadCsv(r, UnixTime.Now.RoundTo(UpdateInterval)))
.SelectError() .SelectError()
.ToTask(); .ToTask();
} }
// var iterations = from _ in Observable.Interval(UpdateInterval.ToTimeSpan())
// let t = UnixTime.Now.RoundTo(UpdateInterval)
// let record = RunIteration()
// from uploaded in UploadCsv(record, t)
// select uploaded;
//
// using var running = iterations.Subscribe();
StatusRecord RunIteration() StatusRecord RunIteration()
{ {
Watchdog.NotifyAlive(); Watchdog.NotifyAlive();
var record = ReadStatus(); var record = ReadStatus();
// If control Special Error return true, we must stop the system.(break;) /*
var i = 2;
if (record.Battery is not null)
{
foreach (var r in record.Battery.Devices)
{
var y = r.BusCurrentHex;
var x = r.CellsCurrentHex;
r.SerialNumber.WriteLine(" serial number " + i);
("--------------").WriteLine();
i++;
}
}*/
//record.Log = new SystemLog
//{
// Led = alarmCondition is null ? LedState.Green : LedState.Orange,
// Message = alarmCondition
//};
var alarmCondition = record.DetectAlarmStates(); var alarmCondition = record.DetectAlarmStates();
if (alarmCondition is not null) if (alarmCondition is not null)