89 lines
3.0 KiB
C#
89 lines
3.0 KiB
C#
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; } = [];
|
|
}
|
|
}
|