Mobile API Refactoring
• Introduced new Mobile API.cs module • Encapsulates firmware checks, day/night mode switching, and snapshot operations • Methods: CheckFirmwareAsync(), SetDayModeAsync(), SetNightModeAsync() • Improves code maintainability and separates API logic from test execution UI Components • DayImgPcbx PictureBox: Displays day snapshot • NightImgPcbx PictureBox: Displays night snapshot • DayImage Label: Shows day luminance percentage • NightImage Label: Shows night luminance percentage Features Implemented • Luminance-based validation: Uses ImageProcessing.GetMeanLuminance() to quantitatively analyze day and night snapshots • Validation logic: Day luminance must exceed night luminance to pass the test • Real-time luminance display: Appends calculated luminance percentages (0-100%) to day and night image labels • Enhanced error reporting: Logs detailed luminance values when test fails for debugging purposes • Captures day mode snapshot → Analyzes luminance • Captures night mode snapshot → Analyzes luminance • Compares values and updates UI labels with percentages • Result: "Day/Night Mode = Passed" (green) or "Day/Night Mode = Failed" (red) with error details
This commit is contained in:
164
Mobile Tests/Mobile API.cs
Normal file
164
Mobile Tests/Mobile API.cs
Normal file
@@ -0,0 +1,164 @@
|
||||
using AiQ_GUI;
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AiQ_GUI.Mobile_Tests
|
||||
{
|
||||
public static class MobileAPI
|
||||
{
|
||||
public static async Task CheckFirmwareAsync()
|
||||
{
|
||||
using HttpClient client = new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri($"http://{MainForm.Instance.CamOnTest.IP}")
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
// Login and get JWT token
|
||||
string token = await LoginAsync(client, "ADMIN", "1234");
|
||||
|
||||
// Attach JWT to all requests
|
||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||
|
||||
// Request firmware information
|
||||
HttpResponseMessage response = await client.GetAsync("/app/v1/system/firmware");
|
||||
|
||||
string body = await response.Content.ReadAsStringAsync();
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
JsonElement json = JsonSerializer.Deserialize<JsonElement>(body);
|
||||
|
||||
string version = json.GetProperty("version").GetString()?.Trim();
|
||||
|
||||
// Compare against expected SRZFirmware from UniversalData
|
||||
if (string.IsNullOrEmpty(UniversalData.SRZFirmware))
|
||||
{
|
||||
MainForm.Instance.AddToActionsList($"Firmware check failed: Expected SRZFirmware not loaded in UniversalData", Level.ERROR);
|
||||
MainForm.Instance.AddLabelToPanel($"Firmware = {version}", true);
|
||||
}
|
||||
else if (version == UniversalData.SRZFirmware)
|
||||
{
|
||||
MainForm.Instance.AddLabelToPanel($"Firmware = {version}", false);
|
||||
}
|
||||
else
|
||||
{
|
||||
MainForm.Instance.AddLabelToPanel($"Firmware = {version}", true);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MainForm.Instance.AddToActionsList($"Firmware check failed: {ex.Message}", Level.ERROR);
|
||||
MainForm.Instance.AddLabelToPanel("Firmware = Unknown", true);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task ChangeDayNightModeAsync(uint mode)
|
||||
{
|
||||
// mode: 0 = Auto, 1 = Day, 2 = Night
|
||||
string modeName = mode switch
|
||||
{
|
||||
0 => "Auto",
|
||||
1 => "Day",
|
||||
2 => "Night",
|
||||
_ => "Unknown"
|
||||
};
|
||||
|
||||
// operatingMode required by firmware (!)
|
||||
// day -> 1, night -> 0
|
||||
int operatingMode = mode switch
|
||||
{
|
||||
1 => 1, // Day
|
||||
2 => 0, // Night
|
||||
_ => 1 // Auto: keep day as safe default
|
||||
};
|
||||
|
||||
using HttpClient client = new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri($"http://{MainForm.Instance.CamOnTest.IP}")
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
// Login
|
||||
string token = await LoginAsync(client, "ADMIN", "1234");
|
||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
|
||||
|
||||
var payload = new
|
||||
{
|
||||
dayNightMode = new
|
||||
{
|
||||
mode = mode,
|
||||
dayToNightSens = 4,
|
||||
nightToDaySens = 24,
|
||||
transitionDelay = 20,
|
||||
operatingMode = operatingMode
|
||||
}
|
||||
};
|
||||
|
||||
HttpResponseMessage response = await client.PostAsJsonAsync("/app/v1/camera/image", payload);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
MainForm.Instance.AddToActionsList($"Camera set to {modeName} mode successfully",Level.Success);
|
||||
}
|
||||
else
|
||||
{
|
||||
string error = await response.Content.ReadAsStringAsync();
|
||||
MainForm.Instance.AddToActionsList($"Failed to set {modeName} mode: {response.StatusCode} - {error}",Level.ERROR);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MainForm.Instance.AddToActionsList($"Error changing to {modeName} mode: {ex.Message}",Level.ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static async Task SetDayModeAsync()
|
||||
{
|
||||
await ChangeDayNightModeAsync(1);
|
||||
}
|
||||
|
||||
public static async Task SetNightModeAsync()
|
||||
{
|
||||
await ChangeDayNightModeAsync(2);
|
||||
}
|
||||
|
||||
public static async Task SetAutoModeAsync()
|
||||
{
|
||||
await ChangeDayNightModeAsync(0);
|
||||
}
|
||||
|
||||
private static async Task<string> LoginAsync(HttpClient client, string user, string password)
|
||||
{
|
||||
var payload = new
|
||||
{
|
||||
userId = user,
|
||||
userPassword = Sha256(password)
|
||||
};
|
||||
|
||||
HttpResponseMessage response = await client.PostAsJsonAsync("/app/v1/login", payload);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
JsonElement json = await response.Content.ReadFromJsonAsync<JsonElement>();
|
||||
|
||||
return json.GetProperty("token").GetString();
|
||||
}
|
||||
|
||||
private static string Sha256(string input)
|
||||
{
|
||||
using SHA256 sha = SHA256.Create();
|
||||
byte[] bytes = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
return BitConverter.ToString(bytes).Replace("-", "").ToLower();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user