using System.Net;
using System.Web;
using adas_core.Domain.Models.MongoModels;
using Serilog;
namespace adas_core.Domain.Utils;
///
/// Provides helper methods for relaying operations, data, or commands between components.
///
///
public class RelayHelper
{
///
/// Retrieves the current status of a by calling a local REST API endpoint built from its connection parameters, returning when the relay is reported as active and when the response is not , the payload cannot be converted to a boolean, or any exception is raised during the request.
///
/// The whose status is queried; its is used in the URL path while , , and are passed as query parameters.
/// if the API responds with and the response body converts to a boolean value of ; otherwise, .
///
public static bool GetRelayStatusFromApiRest(Relay relay)
{
UriBuilder builder = new()
{
Scheme = "https",
Host = "localhost",
Port = 7186,
Path = $"{relay.RelayNumber}/status"
};
var query = HttpUtility.ParseQueryString(builder.Query);
query["driver"] = relay.Driver;
query["host"] = relay.Ip;
query["port"] = relay.Port.ToString();
query["relays"] = "8"; //todo:tendría que recoger el total
builder.Query = query.ToString();
HttpRequestMessage request = new()
{
RequestUri = builder.Uri,
Method = HttpMethod.Get
};
try
{
using var client = new HttpClient();
var httpResponse = client.SendAsync(request).Result;
if (httpResponse.StatusCode.Equals(HttpStatusCode.OK))
{
var responseContent = httpResponse.Content;
var response = responseContent.ReadAsStringAsync().Result;
return Convert.ToBoolean(response);
}
return false;
}
catch (Exception e)
{
Log.Debug("Exception Getting Relay Status From Api Rest: {relay}", e);
return false;
}
}
//TODO son provisionales mientras se añade como nuget Smacs.Divers
///
/// Powers on the specified relay by constructing a request to the local relay control endpoint at https://localhost:7186, where the relay is identified by its in the path.
///
/// The relay to power on.
///
public static void PowerOnRelay(Relay relay)
{
UriBuilder builder = new()
{
Scheme = "https",
Host = "localhost",
Port = 7186,
Path = $"{relay.RelayNumber}/powerOn"
};
PowerRelay(relay, builder);
}
///
/// Sends a power off command to the specified relay by building a request to the local relay control endpoint
/// using the relay's number as the path identifier, then forwarding the call to the underlying relay handler.
///
/// The relay to power off, identified by its which is used to construct the request path.
///
public static void PowerOffRelay(Relay relay)
{
UriBuilder builder = new()
{
Scheme = "https",
Host = "localhost",
Port = 7186,
Path = $"{relay.RelayNumber}/powerOff"
};
PowerRelay(relay, builder);
}
///
/// Sends an HTTP POST request to power a through the endpoint described by , enriching the query string with the relay's driver, IP address, port, and a fixed channel count of 8. Logs an information message when the response status is not and logs any exception raised during the call at debug level instead of propagating it.
///
/// The relay to power, whose , and values are written into the request query string.
/// The whose query string is populated and whose identifies the target endpoint of the POST request.
///
private static void PowerRelay(Relay relay, UriBuilder builder)
{
var query = HttpUtility.ParseQueryString(builder.Query);
query["driver"] = relay.Driver;
query["host"] = relay.Ip;
query["port"] = relay.Port.ToString();
query["relays"] = "8"; //todo:tendría que recoger el total
builder.Query = query.ToString();
HttpRequestMessage request = new()
{
RequestUri = builder.Uri,
Method = HttpMethod.Post
};
try
{
using var client = new HttpClient();
var httpResponse = client.SendAsync(request).Result;
if (!httpResponse.StatusCode.Equals(HttpStatusCode.OK)) Log.Information("relay: {relay} powered", relay);
}
catch (Exception e)
{
Log.Debug("Exception {e} powering Relay: {relay}", e.Message, relay);
}
}
///
/// Retrieves the current status of the specified relay. Currently always returns false, indicating the relay status is not available or is treated as inactive.
///
/// The relay whose status is being queried.
/// true if the relay is active; otherwise, false.
///
public static bool GetRelayStatus(Relay relay)
{
return false;
}
}