using System.Text; using Microsoft.AspNetCore.Mvc; namespace DataCollectorWebApp; public class LoginResponse { public string Token { get; set; } public object User { get; set; } // or a User class if needed public bool AccessToSalimax { get; set; } public bool AccessToSalidomo { get; set; } public bool AccessToSodiohome { get; set; } public bool AccessToSodistoreMax { get; set; } } public class Installation { //Each installation has 2 roles, a read role and a write role. //There are 2 keys per role a public key and a secret //Product can be 0 or 1, 0 for Salimax, 1 for Salidomo public String Name { get; set; } public String Location { get; set; } public String Region { get; set; } = ""; public String Country { get; set; } = ""; public String VpnIp { get; set; } = ""; public String InstallationName { get; set; } = ""; public String S3Region { get; set; } = "sos-ch-dk-2"; public String S3Provider { get; set; } = "exo.io"; public String S3WriteKey { get; set; } = ""; public String S3Key { get; set; } = ""; public String S3WriteSecret { get; set; } = ""; public String S3Secret { get; set; } = ""; public int S3BucketId { get; set; } = 0; public String ReadRoleId { get; set; } = ""; public String WriteRoleId { get; set; } = ""; public Boolean TestingMode { get; set; } = false; public int Status { get; set; } = -1; public int Product { get; set; } = 0; public int Device { get; set; } = 0; public string SerialNumber { get; set; } = ""; public String OrderNumbers { get; set; } public String VrmLink { get; set; } = ""; } [Controller] public class InstallationsController : Controller { [HttpGet] [Route("/Installations")] [Produces("text/html")] public async Task Index() { const string HtmlHeader = @" Inesco Energy Installations Overview

Installation Overview

"; const string HtmlFooter = @"
Name Product Location VPN IP Status
"; string GetProductName(int productId) { return productId switch { 0 => "Salimax", 1 => "Salidomo", 2 => "SodioHome", 3 => "SodistoreMax", }; } string GetStatusHtml(int status) { if (status == -1) { return "×"; } var statusClass = $"status-{status}"; var title = status switch { 0 => "Online", 1 => "Warning", 2 => "Error", _ => "Unknown" }; return $""; } string BuildRowHtml(Installation i) => $@" {i.Name} {GetProductName(i.Product)} {i.Location} {i.VpnIp} {GetStatusHtml(i.Status)} "; var installations = await FetchInstallationsFromApi(); var sb = new StringBuilder(); sb.Append(HtmlHeader); foreach (var i in installations) { sb.Append(BuildRowHtml(i)); } sb.Append(HtmlFooter); return Content(sb.ToString(), "text/html"); } public async Task?> FetchInstallationsFromApi() { var username = "baumgartner@innov.energy"; var password = "1234"; using var http = new HttpClient { BaseAddress = new Uri("https://monitor.inesco.energy/api/") }; // Step 1: Login var loginResponse = await http.PostAsync($"Login?username={username}&password={password}", null); if (!loginResponse.IsSuccessStatusCode) { Console.WriteLine("Login failed with status code {StatusCode}", loginResponse.StatusCode); return null; } var loginData = await loginResponse.Content.ReadFromJsonAsync(); if (loginData?.Token is null) { Console.WriteLine("Login succeeded but token was missing"); return null; } var token = loginData.Token; Console.WriteLine($"Token: {token}"); var installations = new List(); var getInstallationsRequestResponse = await http.GetAsync($"GetAllInstallationsFromProduct?product=0&authToken={token}"); var newInstallations= await getInstallationsRequestResponse.Content.ReadFromJsonAsync>(); if (newInstallations != null) { installations.AddRange(newInstallations); } getInstallationsRequestResponse = await http.GetAsync($"GetAllInstallationsFromProduct?product=1&authToken={token}"); newInstallations= await getInstallationsRequestResponse.Content.ReadFromJsonAsync>(); if (newInstallations != null) { installations.AddRange(newInstallations); } getInstallationsRequestResponse = await http.GetAsync($"GetAllInstallationsFromProduct?product=2&authToken={token}"); newInstallations= await getInstallationsRequestResponse.Content.ReadFromJsonAsync>(); if (newInstallations != null) { installations.AddRange(newInstallations); } getInstallationsRequestResponse = await http.GetAsync($"GetAllInstallationsFromProduct?product=3&authToken={token}"); newInstallations= await getInstallationsRequestResponse.Content.ReadFromJsonAsync>(); if (newInstallations != null) { installations.AddRange(newInstallations); } //Console.WriteLine("Installations retrieved ",installations); return installations; } }