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.Threading.Tasks;
using Flurl.Http;
using InnovEnergy.App.SaliMax.Devices;
using InnovEnergy.App.SaliMax.Ess;
using InnovEnergy.App.SaliMax.SaliMaxRelays;
using InnovEnergy.App.SaliMax.System;
using InnovEnergy.App.SaliMax.SystemConfig;
using InnovEnergy.App.SaliMax.VirtualDevices;
using InnovEnergy.Lib.Devices.AMPT;
using InnovEnergy.Lib.Devices.Battery48TL;
using InnovEnergy.Lib.Devices.EmuMeter;
@ -20,6 +20,7 @@ using InnovEnergy.Lib.Time.Unix;
using InnovEnergy.Lib.Units;
using InnovEnergy.Lib.Utils;
using static InnovEnergy.Lib.Devices.Trumpf.SystemControl.DataTypes.SystemConfig;
using DeviceState = InnovEnergy.App.SaliMax.Devices.DeviceState;
#pragma warning disable IL2026
@ -32,26 +33,34 @@ internal static class Program
private static readonly IReadOnlyList<Byte> BatteryNodes;
private static readonly TcpChannel TruConvertAcChannel ;
private static readonly TcpChannel TruConvertDcChannel ;
private static readonly TcpChannel GridMeterChannel ;
private static readonly TcpChannel IslandBusLoadChannel;
private static readonly TcpChannel AmptChannel ;
private static readonly TcpChannel RelaysChannel ;
private static readonly TcpChannel BatteriesChannel ;
private static readonly Channel TruConvertAcChannel ;
private static readonly Channel TruConvertDcChannel ;
private static readonly Channel GridMeterChannel ;
private static readonly Channel IslandBusLoadChannel;
private static readonly Channel PvOnDc ;
private static readonly Channel PvOnAcGrid ;
private static readonly Channel PvOnAcIsland ;
private static readonly Channel RelaysChannel ;
private static readonly Channel BatteriesChannel ;
static Program()
{
var config = Config.Load();
var d = config.Devices;
TruConvertAcChannel = new TcpChannel(d.TruConvertAcIp);
TruConvertDcChannel = new TcpChannel(d.TruConvertDcIp);
GridMeterChannel = new TcpChannel(d.GridMeterIp);
IslandBusLoadChannel = new TcpChannel(d.IslandBusLoadMeterIp);
AmptChannel = new TcpChannel(d.AmptIp);
RelaysChannel = new TcpChannel(d.RelaysIp);
BatteriesChannel = new TcpChannel(d.BatteryIp);
Channel CreateChannel(SalimaxDevice device) => device.DeviceState == DeviceState.Disabled
? new NullChannel()
: new TcpChannel(device);
TruConvertAcChannel = CreateChannel(d.TruConvertAcIp);
TruConvertDcChannel = CreateChannel(d.TruConvertDcIp);
GridMeterChannel = CreateChannel(d.GridMeterIp);
IslandBusLoadChannel = CreateChannel(d.IslandBusLoadMeterIp);
PvOnDc = CreateChannel(d.PvOnDc);
PvOnAcGrid = CreateChannel(d.PvOnAcGrid);
PvOnAcIsland = CreateChannel(d.PvOnAcIsland);
RelaysChannel = CreateChannel(d.RelaysIp);
BatteriesChannel = CreateChannel(d.BatteryIp);
BatteryNodes = config
.Devices
@ -91,26 +100,35 @@ internal static class Program
var dcDcDevices = new TruConvertDcDcDevices(TruConvertDcChannel);
var gridMeterDevice = new EmuMeterDevice(GridMeterChannel);
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);
StatusRecord ReadStatus()
{
var config = Config.Load();
var devices = config.Devices;
var acDc = acDcDevices.Read();
var dcDc = dcDcDevices.Read();
var relays = saliMaxRelaysDevice.Read();
var loadOnAcIsland = acIslandLoadMeter.Read();
var gridMeter = gridMeterDevice.Read();
var pvOnDc = amptDevice.Read();
var pvOnDc = pvOnDcDevice.Read();
var battery = batteryDevices.Read();
var pvOnAcGrid = new AcPowerDevice { Power = 0 }; // TODO
var pvOnAcIsland = new AcPowerDevice { Power = 0 }; // TODO
var pvOnAcGrid = pvOnAcGridDevice.Read();
var pvOnAcIsland = pvOnAcIslandDevice.Read();
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
{
@ -132,7 +150,7 @@ internal static class Program
StateMachine = StateMachine.Default,
EssControl = EssControl.Default,
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
.Interval(UpdateInterval)
.Select(_ => RunIteration())
.SelectMany(r => UploadCsv(r, DateTime.UtcNow))
.SelectMany(r => UploadCsv(r, UnixTime.Now.RoundTo(UpdateInterval)))
.SelectError()
.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()
{
Watchdog.NotifyAlive();
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();
if (alarmCondition is not null)