Skip to content

Commit 0e86213

Browse files
committed
Upgrading to .net 8.0 and fixing a bunch of bugs due to async checks of the lights
1 parent 2197fed commit 0e86213

File tree

3 files changed

+47
-48
lines changed

3 files changed

+47
-48
lines changed

AutoKeyLight.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
1+
22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.2.32602.215
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoKeyLight", "AutoKeyLight\AutoKeyLight.csproj", "{EFEF84FA-A98D-4C5A-B8BE-DDE4772D6CBE}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoKeyLight", "AutoKeyLight\AutoKeyLight.csproj", "{EFEF84FA-A98D-4C5A-B8BE-DDE4772D6CBE}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution

AutoKeyLight/AutoKeyLight.csproj

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
43
<OutputType>WinExe</OutputType>
5-
<TargetFramework>net6.0-windows</TargetFramework>
4+
<TargetFramework>net8.0-windows</TargetFramework>
65
<Nullable>enable</Nullable>
76
<UseWindowsForms>true</UseWindowsForms>
87
<ImplicitUsings>enable</ImplicitUsings>
@@ -12,11 +11,9 @@
1211
<PackageIcon>TrayIconUnlit.png</PackageIcon>
1312
<Authors>Gabriel Garcia</Authors>
1413
</PropertyGroup>
15-
1614
<ItemGroup>
1715
<Content Include="Resources\Icon.ico" />
1816
</ItemGroup>
19-
2017
<ItemGroup>
2118
<Compile Update="Properties\Resources.Designer.cs">
2219
<DesignTime>True</DesignTime>
@@ -29,14 +26,12 @@
2926
<DependentUpon>Settings.settings</DependentUpon>
3027
</Compile>
3128
</ItemGroup>
32-
3329
<ItemGroup>
3430
<EmbeddedResource Update="Properties\Resources.resx">
3531
<Generator>ResXFileCodeGenerator</Generator>
3632
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
3733
</EmbeddedResource>
3834
</ItemGroup>
39-
4035
<ItemGroup>
4136
<None Update="Resources\TrayIconUnlit.png">
4237
<Pack>True</Pack>
@@ -47,5 +42,4 @@
4742
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
4843
</None>
4944
</ItemGroup>
50-
5145
</Project>

AutoKeyLight/MainForm.cs

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void ReloadLights()
7474

7575
private void MainForm_Load(object sender, EventArgs e)
7676
{
77-
RefreshCameraState();
77+
RefreshCamerasState();
7878
tmrCameraCheck_Tick(sender, e);
7979

8080
ReloadLights();
@@ -129,69 +129,74 @@ private void MainForm_Load(object sender, EventArgs e)
129129
Icon = Properties.Resources.Icon;
130130
}
131131

132-
private async void RefreshCameraState()
132+
private void RefreshCamerasState()
133+
{
134+
foreach (string IP in lbIPs.Items)
135+
{
136+
RefreshCameraState(IP);
137+
}
138+
}
139+
140+
private async void RefreshCameraState( string IP )
133141
{
134142
lblError.Visible = false;
135143

136-
foreach (string IP in lbIPs.Items)
144+
try
137145
{
138-
try
139-
{
140-
string keylightURL = $"http://{IP}:9123/elgato/lights";
146+
string keylightURL = $"http://{IP}:9123/elgato/lights";
141147

142-
HttpResponseMessage response = await httpClient.GetAsync(keylightURL, requestStateToken);
148+
HttpResponseMessage response = await httpClient.GetAsync(keylightURL, requestStateToken);
143149

144-
if (response.IsSuccessStatusCode)
145-
{
146-
bool success = false;
150+
if (response.IsSuccessStatusCode)
151+
{
152+
bool success = false;
147153

148-
JsonNode? responseRoot = JsonNode.Parse(await response.Content.ReadAsStringAsync(requestStateToken));
149-
if (responseRoot != null)
154+
JsonNode? responseRoot = JsonNode.Parse(await response.Content.ReadAsStringAsync(requestStateToken));
155+
if (responseRoot != null)
156+
{
157+
JsonNode? lightsArray = responseRoot.AsObject()["lights"];
158+
if (lightsArray != null)
150159
{
151-
JsonNode? lightsArray = responseRoot.AsObject()["lights"];
152-
if (lightsArray != null)
160+
JsonNode? lightObject = lightsArray.AsArray()[0];
161+
if (lightObject != null)
153162
{
154-
JsonNode? lightObject = lightsArray.AsArray()[0];
155-
if (lightObject != null)
163+
JsonNode? onProperty = lightObject.AsObject()["on"];
164+
if (onProperty != null)
156165
{
157-
JsonNode? onProperty = lightObject.AsObject()["on"];
158-
if (onProperty != null)
159-
{
160-
isCameraOn = onProperty.GetValue<int>() == 1;
161-
lblLightState.Text = isCameraOn ? "Lights are ON" : "Lights are OFF";
162-
lblLightState.ForeColor = isCameraOn ? Color.LawnGreen : Color.IndianRed;
163-
success = true;
164-
}
166+
isCameraOn = onProperty.GetValue<int>() == 1;
167+
lblLightState.Text = isCameraOn ? "Lights are ON" : "Lights are OFF";
168+
lblLightState.ForeColor = isCameraOn ? Color.LawnGreen : Color.IndianRed;
169+
success = true;
165170
}
166171
}
167172
}
168-
169-
if (!success)
170-
{
171-
lblError.Visible = true;
172-
lblError.Text = "Malformed JSON please\ncreate an Issue on GitHub";
173-
}
174173
}
175-
else
174+
175+
if (!success)
176176
{
177177
lblError.Visible = true;
178-
lblLightState.Text = $"Error, check IP: {IP}";
178+
lblError.Text = "Malformed JSON please\ncreate an Issue on GitHub";
179179
}
180180
}
181-
catch
181+
else
182182
{
183183
lblError.Visible = true;
184-
lblError.Text = $"Error, check IP: {IP}";
185-
186-
requestStateTokenSource = new CancellationTokenSource();
187-
requestStateToken = requestStateTokenSource.Token;
184+
lblLightState.Text = $"Error, check IP: {IP}";
188185
}
189186
}
187+
catch
188+
{
189+
lblError.Visible = true;
190+
lblError.Text = $"Error, check IP: {IP}";
191+
192+
requestStateTokenSource = new CancellationTokenSource();
193+
requestStateToken = requestStateTokenSource.Token;
194+
}
190195
}
191196

192197
private void CameraIsOn()
193198
{
194-
RefreshCameraState();
199+
RefreshCamerasState();
195200

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

215220
private void CameraIsOff()
216221
{
217-
RefreshCameraState();
222+
RefreshCamerasState();
218223
niTray.Icon = TrayIconUnlit;
219224

220225
lblCameraState.Text = "Camera is OFF";

0 commit comments

Comments
 (0)