Files
AiQ_GUI/HWAccessories/Ez.cs
2025-09-02 15:32:24 +01:00

110 lines
4.0 KiB
C#

using System.Text;
namespace AiQ_GUI
{
internal class Ez
{
public static string PoEPowerIP = "";
// Controls the EzOutlet state (ON/OFF)
public async static Task<bool> EzOutletControl(string state)
{
try
{
string currentState = await GetEzOutletState();
if (string.IsNullOrEmpty(currentState)) return false;
// Check if a state change is required
if ((currentState.Contains("1,") && state == "OFF") || (currentState.Contains("0,") && state == "ON"))
{
// Uses XOR so if only one is true. As if both are true something is wrong as it can't have both versions of code and if none are true then it failed
bool invertResult = await InvertEzOutletState();
bool ezOutlet2Result = EzOutlet2Control(ToTitleCase(state)).Result;
return invertResult ^ ezOutlet2Result;
}
return true; // State already matches
}
catch (Exception ex)
{
MainForm.Instance.AddToActionsList($"Error: {ex.Message}");
return false;
}
}
// Controls EzOutlet2 state by sending an HTTP request
public async static Task<bool> EzOutlet2Control(string state)
{
try
{
string upperState = state.ToUpper();
string URL = $"http://{PoEPowerIP}:100/overview?onoff={upperState}";
// Send ON/OFF command twice to handle login prompt, admin/admin creds true
await SendHttpRequest(URL, true);
// TODO Is twice needed??
await SendHttpRequest(URL, true);
await Task.Delay(1000); // Check flags every 1000ms
// Verify the state change
string response = await SendHttpRequest(URL.Substring(0, URL.IndexOf('?')), false); // Cut down to only get up to overview
return response.Contains($"Status: {state} Mode: Manual Control");
}
catch
{
return false;
}
}
// Inverts the current EzOutlet state
public async static Task<bool> InvertEzOutletState()
{
try
{
string response = await SendHttpRequest($"http://{PoEPowerIP}:100/invert.cgi", false);
return response.Contains(','); // Completed message includes comma
}
catch
{
return false;
}
}
// Helper to get the current EzOutlet state
private async static Task<string> GetEzOutletState()
{
return await SendHttpRequest($"http://{PoEPowerIP}:100/socket.cgi", false);
}
private static async Task<string> SendHttpRequest(string url, bool useCreds)
{
try
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
if (useCreds)
{
byte[] byteArray = Encoding.ASCII.GetBytes("admin:admin");
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
}
using HttpResponseMessage response = await Network.Client.SendAsync(request);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
MainForm.Instance.AddToActionsList($"Error: {ex.Message}");
return $"Error: {ex.Message}";
}
}
// Converts a string to TitleCase (first letter uppercase, rest lowercase)
private static string ToTitleCase(string input)
{
return string.IsNullOrEmpty(input) || input.Length == 1 ? input : char.ToUpper(input[0]) + input.Substring(1).ToLower();
}
}
}