Skip to content

Commit

Permalink
Release: 2023.11.1-ubeta (#13)
Browse files Browse the repository at this point in the history
Release fixing reported issues regarding CompatHelper and VirtualDesktop feature handling.
  • Loading branch information
amadeo-alex authored Nov 12, 2023
1 parent 0afb3f0 commit 7867123
Show file tree
Hide file tree
Showing 21 changed files with 304 additions and 101 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ There are two options to approach this:

Currently the recommended option is the "patch it over". HASS.Agent is capable of functioning without the installer but the installer takes care of some dependencies that might need to be installed manually otherwise.
<br>
1. Download and install HASS.Agent from [the official release](https://github.com/LAB02-Research/HASS.Agent/releases) (usually in **"C:\Users\<username>\AppData\Roaming\LAB02 Research\HASS.Agent\"**)
1. Download and install HASS.Agent from [the official release](https://github.com/LAB02-Research/HASS.Agent/releases) (usually in **"C:\Users\<username>\AppData\Roaming\LAB02 Research\HASS.Agent\"** and **"C:\Program Files (x86)\LAB02 Research\HASS.Agent Satellite Service"**)
2. Download and install WindowsAppSDK ([explanation why](https://github.com/LAB02-Research/HASS.Agent.Staging/pull/18))
for HASS.Agent 2023.10.0 - https://aka.ms/windowsappsdk/1.3/1.3.230724000/windowsappruntimeinstall-x64.exe
<br/>for HASS.Agent 2023.10.0, 2023.11.0 and 2023.11.1 - https://aka.ms/windowsappsdk/1.3/1.3.230724000/windowsappruntimeinstall-x64.exe
3. Make sure the HASS.Agent service is stopped
<br><img src="https://github.com/amadeo-alex/HASS.Agent/assets/68441479/38590ab0-7d42-4790-9629-73596725d75e" height="350px" />
4. Close/Exit out of the HASS.Agent
<br><img src="https://github.com/amadeo-alex/HASS.Agent/assets/68441479/38939e3d-6dff-447c-a497-78def5fa41ff" height="350px" />
5. Download the release package from this repository
6. Copy/Replace the downloaded files over the installed ones (again, usually in **"C:\Users\<username>\AppData\Roaming\LAB02 Research\HASS.Agent\"**)
5. Download the release package (HASS.Agent & HASS.Agent.Service) from this repository
6. Copy/Replace the downloaded files over the installed ones (again, usually in **"C:\Users\<username>\AppData\Roaming\LAB02 Research\HASS.Agent\"** and **"C:\Program Files (x86)\LAB02 Research\HASS.Agent Satellite Service\"**)
7. Launch HASS.Agent and verify that you're using the unofficial beta version by navigating to help window - "u" in the version postfix
<img src="https://github.com/amadeo-alex/HASS.Agent/assets/68441479/19edc4f6-e674-4238-8d11-d50c16feb8a9" height="350px" />
<img src="https://github.com/amadeo-alex/HASS.Agent/assets/68441479/05df8795-b8f6-4a9e-b666-a55b89196a3e" height="350px" />
Expand Down Expand Up @@ -77,7 +77,7 @@ This project contains the latest code of all three parts of the HASS.Agent platf
<br/>


Note: it's best to have `enable extended logging` enabled, which will also reflect on the satellite service (as long as it's started in console mode instead of service mode). But that'll also generate false positives, so primarily focus on the issue at hand.
Note: when reporting issues it's best to have `enable extended logging` enabled.

----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,15 @@ private void BtnStore_Click(object sender, EventArgs e)
Command.Command = selectedItem.Key;
}
break;

case CommandType.SwitchDesktopCommand:
if (!VirtualDesktopManager.Initialized)
{
MessageBoxAdv.Show(this, Languages.CommandsMod_BtnStore_VirtualDesktop_Unavailable, Variables.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
ActiveControl = TbSetting;
return;
}
break;
}

Command.RunAsLowIntegrity = CbRunAsLowIntegrity.CheckState == CheckState.Checked;
Expand Down
2 changes: 1 addition & 1 deletion src/HASS.Agent.Staging/HASS.Agent/Forms/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private void HotkeyListener_HotkeyPressed(object sender, HotkeyEventArgs e)
/// </summary>
private void InitializeVirtualDesktopManager()
{
VirtualDesktop.Configure();
VirtualDesktopManager.Initialize();
}

/// <summary>
Expand Down
12 changes: 11 additions & 1 deletion src/HASS.Agent.Staging/HASS.Agent/Forms/Sensors/SensorsMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using HASS.Agent.Shared.Functions;
using HASS.Agent.Shared.HomeAssistant.Sensors.GeneralSensors.SingleValue;
using HASS.Agent.Managers.DeviceSensors;
using HASS.Agent.Managers;

namespace HASS.Agent.Forms.Sensors
{
Expand Down Expand Up @@ -789,7 +790,16 @@ private void BtnStore_Click(object sender, EventArgs e)
}
Sensor.Query = windowprocess;
break;
}

case SensorType.ActiveDesktopSensor:
if (!VirtualDesktopManager.Initialized)
{
MessageBoxAdv.Show(this, Languages.CommandsMod_BtnStore_VirtualDesktop_Unavailable, Variables.MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
ActiveControl = TbSetting1;
return;
}
break;
}

// set values
Sensor.Type = sensorCard.SensorType;
Expand Down
38 changes: 32 additions & 6 deletions src/HASS.Agent.Staging/HASS.Agent/Functions/CompatHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,57 @@ namespace HASS.Agent.Functions
{
internal static class CompatHelper
{
internal static (int, int, int) SplitHAVerion(string haVersion)
private static (int, int, int) SplitHAVerion(string haVersion)
{
var splitVersion = haVersion.Split('.');

_ = int.TryParse(splitVersion[0], out int major);
_ = int.TryParse(splitVersion[1], out int minor);
var major = 0;
if (splitVersion.Length > 0)
{
_ = int.TryParse(splitVersion[0], out major);
}

int patch = 0;
var minor = 0;
if (splitVersion.Length > 1)
{
_ = int.TryParse(splitVersion[1], out minor);
}

var patch = 0;
if (splitVersion.Length > 2)
{
_ = int.TryParse(splitVersion[2], out patch);
}

return (major, minor, patch);
}

/// <summary>
/// Function checks if the Home Assistant connected to HASS.Agent is greater or equal to <paramref name="haVersion"/>.
/// </summary>
/// <param name="haVersion"></param>
/// <returns>
/// True if version is greater or equal from <paramref name="haVersion"/>.
/// False if version is lower of there is no connection to Home Assistant instance or it's version cannot be determined.
/// </returns>
internal static bool HassVersionEqualOrOver(string haVersion)
{
if (haVersion == null)
if (string.IsNullOrWhiteSpace(haVersion) || string.IsNullOrWhiteSpace(HassApiManager.HaVersion))
{
return false;
}

var (targetMajor, targetMinor, targetPatch) = SplitHAVerion(haVersion);
var (major, minor, patch) = SplitHAVerion(HassApiManager.HaVersion);

if(major == 0)
{
return false;
}

return major > targetMajor
|| major == targetMajor && minor > targetMinor
|| major == targetMajor && minor == targetMinor && patch > targetPatch;
|| major == targetMajor && minor == targetMinor && patch >= targetPatch;
}
}
}
8 changes: 4 additions & 4 deletions src/HASS.Agent.Staging/HASS.Agent/HASS.Agent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PlatformTarget>x64</PlatformTarget>
<Platforms>AnyCPU;x64</Platforms>
<DebugType>full</DebugType>
<Version>2023.11.0-ubeta1</Version>
<Version>2023.11.1-ubeta</Version>
<Company>LAB02 Research</Company>
<Authors>LAB02 Research</Authors>
<Description>Windows-based client for Home Assistant. Provides notifications, quick actions, commands, sensors and more. </Description>
Expand All @@ -22,8 +22,8 @@
<RepositoryUrl>https://github.com/LAB02-Research/HASS.Agent</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<ApplicationManifest>app.manifest</ApplicationManifest>
<AssemblyVersion>2022.15.0</AssemblyVersion>
<FileVersion>2022.15.0</FileVersion>
<AssemblyVersion>2023.11.1</AssemblyVersion>
<FileVersion>2023.11.1</FileVersion>
<RootNamespace>HASS.Agent</RootNamespace>
<WindowsPackageType>None</WindowsPackageType>
<RuntimeIdentifiers>win10-x64;win10-x86;win10-arm64</RuntimeIdentifiers>
Expand Down Expand Up @@ -65,12 +65,12 @@
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Slions.VirtualDesktop" Version="6.2.1" />
<PackageReference Include="Syncfusion.Core.WinForms" Version="20.3.0.50" />
<PackageReference Include="Syncfusion.Licensing" Version="20.3.0.50" />
<PackageReference Include="Syncfusion.Shared.Base" Version="20.3.0.50" />
<PackageReference Include="Syncfusion.Tools.Windows" Version="20.3.0.50" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
<PackageReference Include="VirtualDesktop" Version="5.0.5" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,92 +1,71 @@
using System;
using System.Diagnostics;
using System.IO;
using HASS.Agent.Managers;
using HASS.Agent.Shared.Enums;
using HASS.Agent.Shared.Managers;
using Serilog;
using WindowsDesktop;

namespace HASS.Agent.Shared.HomeAssistant.Commands.InternalCommands
namespace HASS.Agent.Shared.HomeAssistant.Commands.InternalCommands;

/// <summary>
/// Activates provided Virtual Desktop
/// </summary>
public class SwitchDesktopCommand : InternalCommand
{
/// <summary>
/// Activates provided Virtual Desktop
/// </summary>
public class SwitchDesktopCommand : InternalCommand
private const string DefaultName = "switchdesktop";

public SwitchDesktopCommand(string name = DefaultName, string friendlyName = DefaultName, string desktopId = "", CommandEntityType entityType = CommandEntityType.Switch, string id = default) : base(name ?? DefaultName, friendlyName ?? null, desktopId, entityType, id)
{
CommandConfig = desktopId;
State = "OFF";
}

public override void TurnOn()
{
private const string DefaultName = "switchdesktop";
State = "ON";

public SwitchDesktopCommand(string name = DefaultName, string friendlyName = DefaultName, string desktopId = "", CommandEntityType entityType = CommandEntityType.Switch, string id = default) : base(name ?? DefaultName, friendlyName ?? null, desktopId, entityType, id)
if (string.IsNullOrWhiteSpace(CommandConfig))
{
CommandConfig = desktopId;
Log.Warning("[SWITCHDESKTOP] [{name}] Unable to launch command, it's configured as action-only", Name);

State = "OFF";
return;
}

public override void TurnOn()
{
State = "ON";
ActivateVirtualDesktop(CommandConfig);

if (string.IsNullOrWhiteSpace(CommandConfig))
{
Log.Warning("[SWITCHDESKTOP] [{name}] Unable to launch command, it's configured as action-only", Name);
State = "OFF";
}

State = "OFF";
return;
}
public override void TurnOnWithAction(string action)
{
State = "ON";

ActivateVirtualDesktop(CommandConfig);
if (string.IsNullOrWhiteSpace(action))
{
Log.Warning("[SWITCHDESKTOP] [{name}] Unable to launch command, empty action provided", Name);

State = "OFF";
return;
}

public override void TurnOnWithAction(string action)
if (!string.IsNullOrWhiteSpace(CommandConfig))
{
State = "ON";

if (string.IsNullOrWhiteSpace(action))
{
Log.Warning("[SWITCHDESKTOP] [{name}] Unable to launch command, empty action provided", Name);

State = "OFF";
return;
}

if (!string.IsNullOrWhiteSpace(CommandConfig))
{
Log.Warning("[SWITCHDESKTOP] [{name}] Command launched by action, command-provided process will be ignored", Name);

State = "OFF";
return;
}

ActivateVirtualDesktop(action);
Log.Warning("[SWITCHDESKTOP] [{name}] Command launched by action, command-provided process will be ignored", Name);

State = "OFF";
return;
}

private void ActivateVirtualDesktop(string virtualDesktopId)
{
var targetDesktopGuid = Guid.Empty;
var parsed = Guid.TryParse(virtualDesktopId, out targetDesktopGuid);
if (!parsed)
{
Log.Warning("[SWITCHDESKTOP] [{name}] Unable to parse virtual desktop id: {virtualDesktopId}", Name, virtualDesktopId);
return;
}

var targetDesktop = VirtualDesktop.GetDesktops().FirstOrDefault(d => d.Id == targetDesktopGuid);
if (targetDesktop == null)
{
Log.Warning("[SWITCHDESKTOP] [{name}] Unable to find virtual desktop with id: {virtualDesktopId}", Name, virtualDesktopId);
return;
}

if (VirtualDesktop.Current == targetDesktop)
{
Log.Information("[SWITCHDESKTOP] [{name}] Target virtual desktop '{virtualDesktopId}' is already active", Name, virtualDesktopId);
return;
}

targetDesktop.Switch();
}
ActivateVirtualDesktop(action);

State = "OFF";
}

private void ActivateVirtualDesktop(string virtualDesktopId)
{
VirtualDesktopManager.ActivateDesktop(virtualDesktopId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using System.Windows.Forms;
using System.Windows.Threading;
using HASS.Agent.Managers;
using HASS.Agent.Resources.Localization;
using HASS.Agent.Shared.Models.HomeAssistant;
using Microsoft.Win32;
Expand All @@ -20,8 +21,9 @@ namespace HASS.Agent.Shared.HomeAssistant.Sensors.GeneralSensors.SingleValue
public class ActiveDesktopSensor : AbstractSingleValueSensor
{
private const string _defaultName = "activedesktop";

private string _desktopId = string.Empty;
private string _desktopName = string.Empty;
private string _attributes = string.Empty;

public ActiveDesktopSensor(int? updateInterval = null, string name = _defaultName, string friendlyName = _defaultName, string id = default) : base(name ?? _defaultName, friendlyName ?? null, updateInterval ?? 15, id)
Expand All @@ -31,10 +33,16 @@ public ActiveDesktopSensor(int? updateInterval = null, string name = _defaultNam

public override DiscoveryConfigModel GetAutoDiscoveryConfig()
{
if (Variables.MqttManager == null) return null;
if (Variables.MqttManager == null)
{
return null;
}

var deviceConfig = Variables.MqttManager.GetDeviceConfigModel();
if (deviceConfig == null) return null;
if (deviceConfig == null)
{
return null;
}

return AutoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel()
{
Expand All @@ -51,31 +59,19 @@ public override DiscoveryConfigModel GetAutoDiscoveryConfig()

public override string GetState()
{
var currentDesktop = VirtualDesktop.Current;
_desktopId = currentDesktop.Id.ToString();

var desktops = new Dictionary<string, string>();
foreach (var desktop in VirtualDesktop.GetDesktops())
{
var id = desktop.Id.ToString();
desktops[id] = string.IsNullOrWhiteSpace(desktop.Name) ? GetDesktopNameFromRegistry(id) : desktop.Name;
}
var currentDesktop = VirtualDesktopManager.GetCurrentDesktop();
_desktopId = currentDesktop == null ? string.Empty : currentDesktop.Id.ToString();
_desktopName = currentDesktop == null ? string.Empty : currentDesktop.Name;

_attributes = JsonConvert.SerializeObject(new
{
desktopName = currentDesktop.Name,
availableDesktops = desktops
desktopName = _desktopName,
availableDesktops = VirtualDesktopManager.GetAllDesktopsInfo()
}, Formatting.Indented);

return _desktopId;
}

public override string GetAttributes() => _attributes;

private string GetDesktopNameFromRegistry(string id)
{
var registryPath = $"HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops\\Desktops\\{{{id}}}";
return (Registry.GetValue(registryPath, "Name", string.Empty) as string) ?? string.Empty;
}
}
}
Loading

0 comments on commit 7867123

Please sign in to comment.