Skip to content

Commit

Permalink
Upgrading to .net 8.0 and fixing a bunch of bugs due to async checks …
Browse files Browse the repository at this point in the history
…of the lights
  • Loading branch information
astutejoe committed Nov 15, 2023
1 parent 2197fed commit 0e86213
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 48 deletions.
4 changes: 2 additions & 2 deletions AutoKeyLight.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32602.215
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoKeyLight", "AutoKeyLight\AutoKeyLight.csproj", "{EFEF84FA-A98D-4C5A-B8BE-DDE4772D6CBE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoKeyLight", "AutoKeyLight\AutoKeyLight.csproj", "{EFEF84FA-A98D-4C5A-B8BE-DDE4772D6CBE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
8 changes: 1 addition & 7 deletions AutoKeyLight/AutoKeyLight.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -12,11 +11,9 @@
<PackageIcon>TrayIconUnlit.png</PackageIcon>
<Authors>Gabriel Garcia</Authors>
</PropertyGroup>

<ItemGroup>
<Content Include="Resources\Icon.ico" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
Expand All @@ -29,14 +26,12 @@
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<None Update="Resources\TrayIconUnlit.png">
<Pack>True</Pack>
Expand All @@ -47,5 +42,4 @@
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>

</Project>
83 changes: 44 additions & 39 deletions AutoKeyLight/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void ReloadLights()

private void MainForm_Load(object sender, EventArgs e)
{
RefreshCameraState();
RefreshCamerasState();
tmrCameraCheck_Tick(sender, e);

ReloadLights();
Expand Down Expand Up @@ -129,69 +129,74 @@ private void MainForm_Load(object sender, EventArgs e)
Icon = Properties.Resources.Icon;
}

private async void RefreshCameraState()
private void RefreshCamerasState()
{
foreach (string IP in lbIPs.Items)
{
RefreshCameraState(IP);
}
}

private async void RefreshCameraState( string IP )
{
lblError.Visible = false;

foreach (string IP in lbIPs.Items)
try
{
try
{
string keylightURL = $"http://{IP}:9123/elgato/lights";
string keylightURL = $"http://{IP}:9123/elgato/lights";

HttpResponseMessage response = await httpClient.GetAsync(keylightURL, requestStateToken);
HttpResponseMessage response = await httpClient.GetAsync(keylightURL, requestStateToken);

if (response.IsSuccessStatusCode)
{
bool success = false;
if (response.IsSuccessStatusCode)
{
bool success = false;

JsonNode? responseRoot = JsonNode.Parse(await response.Content.ReadAsStringAsync(requestStateToken));
if (responseRoot != null)
JsonNode? responseRoot = JsonNode.Parse(await response.Content.ReadAsStringAsync(requestStateToken));
if (responseRoot != null)
{
JsonNode? lightsArray = responseRoot.AsObject()["lights"];
if (lightsArray != null)
{
JsonNode? lightsArray = responseRoot.AsObject()["lights"];
if (lightsArray != null)
JsonNode? lightObject = lightsArray.AsArray()[0];
if (lightObject != null)
{
JsonNode? lightObject = lightsArray.AsArray()[0];
if (lightObject != null)
JsonNode? onProperty = lightObject.AsObject()["on"];
if (onProperty != null)
{
JsonNode? onProperty = lightObject.AsObject()["on"];
if (onProperty != null)
{
isCameraOn = onProperty.GetValue<int>() == 1;
lblLightState.Text = isCameraOn ? "Lights are ON" : "Lights are OFF";
lblLightState.ForeColor = isCameraOn ? Color.LawnGreen : Color.IndianRed;
success = true;
}
isCameraOn = onProperty.GetValue<int>() == 1;
lblLightState.Text = isCameraOn ? "Lights are ON" : "Lights are OFF";
lblLightState.ForeColor = isCameraOn ? Color.LawnGreen : Color.IndianRed;
success = true;
}
}
}

if (!success)
{
lblError.Visible = true;
lblError.Text = "Malformed JSON please\ncreate an Issue on GitHub";
}
}
else

if (!success)
{
lblError.Visible = true;
lblLightState.Text = $"Error, check IP: {IP}";
lblError.Text = "Malformed JSON please\ncreate an Issue on GitHub";
}
}
catch
else
{
lblError.Visible = true;
lblError.Text = $"Error, check IP: {IP}";

requestStateTokenSource = new CancellationTokenSource();
requestStateToken = requestStateTokenSource.Token;
lblLightState.Text = $"Error, check IP: {IP}";
}
}
catch
{
lblError.Visible = true;
lblError.Text = $"Error, check IP: {IP}";

requestStateTokenSource = new CancellationTokenSource();
requestStateToken = requestStateTokenSource.Token;
}
}

private void CameraIsOn()
{
RefreshCameraState();
RefreshCamerasState();

lblCameraState.Text = "Camera is ON";
lblCameraState.ForeColor = Color.LawnGreen;
Expand All @@ -214,7 +219,7 @@ private void CameraIsOn()

private void CameraIsOff()
{
RefreshCameraState();
RefreshCamerasState();
niTray.Icon = TrayIconUnlit;

lblCameraState.Text = "Camera is OFF";
Expand Down

0 comments on commit 0e86213

Please sign in to comment.