Add project files.

This commit is contained in:
2025-09-02 15:32:24 +01:00
parent 6a633eed7a
commit 50bb9c9781
53 changed files with 9925 additions and 0 deletions

88
HWAccessories/TestTube.cs Normal file
View File

@@ -0,0 +1,88 @@
using Newtonsoft.Json;
namespace AiQ_GUI
{
internal class TestTube
{
public static string TTPiPicoIP = "";
// Gets the API JSON from the Test Tube Pi Pico
public static async Task<TestTubeAPI> GetAPI()
{
string url = $"http://{TTPiPicoIP}/api";
try
{
using CancellationTokenSource cts = new CancellationTokenSource(2000);
string JSON = await Network.Client.GetStringAsync(url, cts.Token);
TestTubeAPI? result = JsonConvert.DeserializeObject<TestTubeAPI>(JSON);
if (result == null)
{
MainForm.Instance.AddToActionsList("JSON deserialization from test tube returned null");
return new TestTubeAPI();
}
return result;
}
catch (TaskCanceledException)
{
MainForm.Instance.AddToActionsList($"Timeout calling {url} after 2s.");
return new TestTubeAPI();
}
catch (Exception ex)
{
MainForm.Instance.AddToActionsList($"Error in GetAPI: {ex.Message}");
return new TestTubeAPI();
}
}
// Sets LED's to medium or safe depending on whether the switch is pressed down or not
public static async Task<bool> CheckInTestTube(string CamIP)
{
if (await InTestTube()) // Switch pressed down
{
string LEDreply = await FlexiAPI.APIHTTPLED(CamIP, LEDPOWER.MID); // Set LED's to medium (0x30)
if (!LEDreply.Contains("Power levels set successfully"))
MainForm.Instance.AddToActionsList($"LED level could not be set: {LEDreply}");
return true;
}
else
{
string LEDreply = await FlexiAPI.APIHTTPLED(CamIP, LEDPOWER.SAFE); // Set LED's to safe (0x0E)
if (!LEDreply.Contains("Power levels set successfully"))
MainForm.Instance.AddToActionsList($"LED level could not be set: {LEDreply}");
await MainForm.Instance.DisplayOK("Please put camera in test tube then click OK"); // Awaited till OK has been clicked
return await InTestTube(); // Check again after user says they have put it in the test tube
}
}
// Checks whether the TestTube has the switch presses down or not
public async static Task<bool> InTestTube()
{
TestTubeAPI TTAPI = await GetAPI();
try
{
if (TTAPI.Switch[0] == true) // Switch not pressed down
return true;
}
catch (Exception ex)
{
MainForm.Instance.AddToActionsList($"Error checking Test Tube switch state: {ex.Message}");
}
return false;
}
}
public class TestTubeAPI
{
public List<bool> Switch { get; set; } = [];
}
}