Innovenergy_trunk/csharp/App/RemoteSupportConsole/VrmInfo.cs

45 lines
1.2 KiB
C#

using Flurl.Http;
using InnovEnergy.Lib.Utils;
using InnovEnergy.Lib.Victron.VictronVRM;
using static System.ConsoleColor;
namespace InnovEnergy.App.RemoteSupportConsole;
public static class VrmInfo
{
public static async Task<IReadOnlyList<Installation>> GetInstallations(this VrmAccount account)
{
"Fetching installations from VRM... ".Write();
try
{
var installations = await account.GetInstallations();
$"success ({installations.Count})".WriteLine(Green);
if (installations.Count <= 0)
{
"No installations found".WriteLine(Red);
Environment.Exit(1);
}
return installations;
}
catch (AggregateException ae) when (ae.InnerExceptions.FirstOrDefault() is FlurlHttpException fhe &&
fhe.Call.Completed)
{
fhe.Call.Response.StatusCode.ToString().WriteLine();
"failed!".WriteLine(Red);
Environment.Exit(1);
}
catch (Exception e)
{
e.Message.WriteLine();
"failed!".WriteLine(Red);
Environment.Exit(1);
}
return Array.Empty<Installation>();
}
}