diff --git a/AutoKeyLight.sln b/AutoKeyLight.sln index c512f0d..26560cd 100644 --- a/AutoKeyLight.sln +++ b/AutoKeyLight.sln @@ -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 diff --git a/AutoKeyLight/AutoKeyLight.csproj b/AutoKeyLight/AutoKeyLight.csproj index 1870aa0..55222e1 100644 --- a/AutoKeyLight/AutoKeyLight.csproj +++ b/AutoKeyLight/AutoKeyLight.csproj @@ -1,8 +1,7 @@  - WinExe - net6.0-windows + net8.0-windows enable true enable @@ -12,11 +11,9 @@ TrayIconUnlit.png Gabriel Garcia - - True @@ -29,14 +26,12 @@ Settings.settings - ResXFileCodeGenerator Resources.Designer.cs - True @@ -47,5 +42,4 @@ Settings.Designer.cs - \ No newline at end of file diff --git a/AutoKeyLight/MainForm.cs b/AutoKeyLight/MainForm.cs index 5c6628f..04270a6 100644 --- a/AutoKeyLight/MainForm.cs +++ b/AutoKeyLight/MainForm.cs @@ -74,7 +74,7 @@ void ReloadLights() private void MainForm_Load(object sender, EventArgs e) { - RefreshCameraState(); + RefreshCamerasState(); tmrCameraCheck_Tick(sender, e); ReloadLights(); @@ -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() == 1; - lblLightState.Text = isCameraOn ? "Lights are ON" : "Lights are OFF"; - lblLightState.ForeColor = isCameraOn ? Color.LawnGreen : Color.IndianRed; - success = true; - } + isCameraOn = onProperty.GetValue() == 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; @@ -214,7 +219,7 @@ private void CameraIsOn() private void CameraIsOff() { - RefreshCameraState(); + RefreshCamerasState(); niTray.Icon = TrayIconUnlit; lblCameraState.Text = "Camera is OFF";