Innovenergy_trunk/csharp/App/SaliMax/src/Program.cs

230 lines
8.1 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Flurl.Http;
using InnovEnergy.App.SaliMax.Controller;
using InnovEnergy.App.SaliMax.Log;
using InnovEnergy.App.SaliMax.SaliMaxRelays;
using InnovEnergy.App.SaliMax.SystemConfig;
using InnovEnergy.Lib.Devices.AMPT;
using InnovEnergy.Lib.Devices.Battery48TL;
2023-02-16 12:57:06 +00:00
using InnovEnergy.Lib.Devices.EmuMeter;
using InnovEnergy.Lib.Devices.Trumpf.TruConvertAc;
using InnovEnergy.Lib.Devices.Trumpf.TruConvertDc;
using InnovEnergy.Lib.Time.Unix;
2023-02-16 12:57:06 +00:00
#pragma warning disable IL2026
namespace InnovEnergy.App.SaliMax;
2023-02-16 12:57:06 +00:00
internal static class Program
{
private const UInt32 UpdateIntervalSeconds = 2;
2023-02-16 12:57:06 +00:00
public static async Task Main(String[] args)
{
try
{
await Run();
}
catch (Exception e)
{
await File.AppendAllTextAsync(Config.LogSalimaxLog,
String.Join(Environment.NewLine, UnixTime.Now + " \n" + e));
2023-02-16 12:57:06 +00:00
throw;
}
}
private static async Task Run()
{
Console.WriteLine("Starting SaliMax");
var s3Config = new S3Config
{
Bucket = "saliomameiringen",
Region = "sos-ch-dk-2",
Provider = "exo.io",
ContentType = "text/plain; charset=utf-8",
Key = "EXO2bf0cbd97fbfa75aa36ed46f",
Secret = "Bn1CDPqOG-XpDSbYjfIJxojcHTm391vZTc8z8l_fEPs"
};
#if DEBUG
var inverterDevice = new TruConvertAcDevice("127.0.0.1", 5001);
var dcDcDevice = new TruConvertDcDevice("127.0.0.1", 5002);
var gridMeterDevice = new EmuMeterDevice("127.0.0.1", 5003);
var saliMaxRelaysDevice = new SaliMaxRelaysDevice("127.0.0.1", 5004);
var amptDevice = new AmptCommunicationUnit("127.0.0.1", 5005);
2023-02-16 12:57:06 +00:00
var acInToAcOutMeterDevice = new EmuMeterDevice("127.0.0.1", 5003); // TODO: use real device
var secondBattery48TlDevice = Battery48TlDevice.Fake();
var firstBattery48TlDevice =Battery48TlDevice.Fake();;
2023-02-16 12:57:06 +00:00
var salimaxConfig = new SalimaxConfig();
#else
#if BatteriesAllowed
var firstBattery48TlDevice = new Battery48TlDevice("/dev/ttyUSB0", 2);
var secondBattery48TlDevice = new Battery48TlDevice("/dev/ttyUSB0", 3);
#endif
var inverterDevice = new TruConvertAcDevice("192.168.1.2");
var dcDcDevice = new TruConvertDcDevice("192.168.1.3");
var gridMeterDevice = new EmuMeterDevice("192.168.1.241");
var acInToAcOutMeterDevice = new EmuMeterDevice("192.168.1.241"); // TODO: use real device
var amptDevice = new AmptCommunicationUnit("192.168.1.249");
var saliMaxRelaysDevice = new SaliMaxRelaysDevice("192.168.1.242");
var salimaxConfig = new SalimaxConfig();
2023-02-16 12:57:06 +00:00
#endif
// This is will be always add manually ? or do we need to read devices automatically in a range of IP @
#if BatteriesAllowed
2023-02-16 12:57:06 +00:00
var battery48TlDevices = new[] { firstBattery48TlDevice, secondBattery48TlDevice };
#endif
2023-02-16 12:57:06 +00:00
var dcDcDevices = new[] { dcDcDevice };
var inverterDevices = new[] { inverterDevice};
2023-02-16 12:57:06 +00:00
StatusRecord ReadStatus()
{
#if BatteriesAllowed
var battery48TlStatusArray = battery48TlDevices.Select(b => b.ReadStatus()).NotNull().ToArray();
#endif
// var dcDcStatusArray = dcDcDevices.Select(b => b.ReadStatus()).NotNull().ToArray();
// var inverterStatusArray = inverterDevices.Select(b => b.ReadStatus()).NotNull().ToArray();
2023-02-16 12:57:06 +00:00
return new StatusRecord
{
InverterStatus = inverterDevice.ReadStatus(),
DcDcStatus = dcDcDevice.ReadStatus(),
#if BatteriesAllowed
BatteriesStatus = battery48TlStatusArray,
AvgBatteriesStatus = AvgBatteriesStatus.ReadBatteriesStatus(battery48TlStatusArray),
#else
BatteriesStatus = null,
AvgBatteriesStatus = null,
#endif
2023-02-16 12:57:06 +00:00
AcInToAcOutMeterStatus = acInToAcOutMeterDevice.ReadStatus(),
GridMeterStatus = gridMeterDevice.ReadStatus(),
SaliMaxRelayStatus = saliMaxRelaysDevice.ReadStatus(),
AmptStatus = amptDevice.ReadStatus(),
SalimaxConfig = salimaxConfig.Load().Result,
};
}
var startTime = UnixTime.Now;
2023-02-16 12:57:06 +00:00
const Int32 delayTime = 10;
await UploadTopology(s3Config, Salimax.TopologyToLog(startTime), startTime);
2023-02-16 12:57:06 +00:00
DebugWriteTopology(startTime);
Console.WriteLine("press ctrl-C to stop");
while (true)
{
var t = UnixTime.Now;
while (t.Ticks % UpdateIntervalSeconds != 0)
{
await Task.Delay(delayTime);
t = UnixTime.Now;
}
2023-02-16 12:57:06 +00:00
var status = ReadStatus();
#if BatteriesAllowed
2023-02-16 12:57:06 +00:00
var jsonLog = status.ToLog(t);
await UploadTimeSeries(s3Config, jsonLog, t);
2023-02-16 12:57:06 +00:00
var controlRecord = Controller.Controller.SaliMaxControl(status);
Controller.Controller.WriteControlRecord(controlRecord, inverterDevice, dcDcDevice, saliMaxRelaysDevice);
//JsonSerializer.Serialize(jsonLog, JsonOptions).WriteLine(ConsoleColor.DarkBlue);
#endif
Topology.Print(status);
while (UnixTime.Now == t)
2023-02-16 12:57:06 +00:00
await Task.Delay(delayTime);
}
// ReSharper disable once FunctionNeverReturns
}
// to delete not used anymore
2023-02-16 12:57:06 +00:00
[Conditional("RELEASE")]
private static void ReleaseWriteLog(JsonObject jsonLog, UnixTime timestamp)
{
// WriteToFile(jsonLog, "/home/debian/DataSaliMax/" + timestamp); // this is was for beaglebone TODO
2023-02-16 12:57:06 +00:00
}
// [Conditional("RELEASE")]
private static JsonObject ReleaseWriteTopology(UnixTime timestamp)
2023-02-16 12:57:06 +00:00
{
var topologyJson = Salimax.TopologyToLog(timestamp);
// WriteToFile(topologyJson, "/home/debian/DataSaliMax/topology" + timestamp); // this is was for beaglebone
return topologyJson;
2023-02-16 12:57:06 +00:00
}
[Conditional("DEBUG")]
private static void DebugWriteTopology(UnixTime timestamp)
{
var topologyJson = Salimax.TopologyToLog(timestamp);
WriteToFile(topologyJson, "/home/atef/JsonData/topology" + timestamp);
}
[Conditional("DEBUG")]
private static void DebugWriteLog(JsonObject jsonLog, UnixTime timestamp)
{
WriteToFile(jsonLog, "/home/atef/JsonData/" + timestamp);
}
private static void WriteToFile(Object obj, String fileName)
{
var jsonString = JsonSerializer.Serialize(obj, JsonOptions);
File.WriteAllText(fileName, jsonString);
}
private static readonly JsonSerializerOptions JsonOptions = new()
{
WriteIndented = true,
IgnoreReadOnlyProperties = false,
Converters = { new JsonStringEnumConverter() },
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
//TODO
};
private static async Task UploadTimeSeries(S3Config config, JsonObject json, UnixTime unixTime)
{
var payload = JsonSerializer.Serialize(json, JsonOptions);
var s3Path = unixTime.Ticks + ".json";
var request = config.CreatePutRequest(s3Path);
var response = await request.PutAsync(new StringContent(payload));
if (response.StatusCode != 200)
{
Console.WriteLine("ERROR: PUT");
var error = response.GetStringAsync();
Console.WriteLine(error);
}
}
private static async Task UploadTopology(S3Config config, JsonObject json, UnixTime unixTime)
{
var payload = JsonSerializer.Serialize(json, JsonOptions);
var s3Path = "topology" + unixTime.Ticks + ".json";
var request = config.CreatePutRequest(s3Path);
var response = await request.PutAsync(new StringContent(payload));
if (response.StatusCode != 200)
{
Console.WriteLine("ERROR: PUT");
var error = response.GetStringAsync();
Console.WriteLine(error);
}
}
2023-02-16 12:57:06 +00:00
}