Skip to content

Commit

Permalink
2022.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
LAB02 Research committed Dec 21, 2022
1 parent 4751d01 commit 83e159e
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 31 deletions.
25 changes: 25 additions & 0 deletions src/HASS.Agent.AutoImport/Functions/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HASS.Agent.AutoImport.Functions
{
public static class Extensions
{
/// <summary>
/// Replaces all double backslashes into singles
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string RemoveBackslashEscaping(this string text) => text.Replace(@"\\", @"\");

/// <summary>
/// Replaces all single backslashes into doubles
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string AddBackslashEscaping(this string text) => text.Replace(@"\", @"\\");
}
}
41 changes: 39 additions & 2 deletions src/HASS.Agent.AutoImport/Functions/HelperFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal static bool IsProcessActiveUnderCurrentUser(string process)
}
catch (Exception ex)
{
Log.Fatal(ex, "[PROCESS] [{process}] Error while determining if process is running: {msg}", process, ex.Message);
Log.Fatal(ex, "[PROCESS] [{process}] Error while determining if process is running: {err}", process, ex.Message);
return false;
}
}
Expand Down Expand Up @@ -110,9 +110,46 @@ internal static bool CloseProcess(string process)
}
catch (Exception ex)
{
Log.Fatal(ex, "[PROCESS] [{process}] Error while closing process: {msg}", process, ex.Message);
Log.Fatal(ex, "[PROCESS] [{process}] Error while closing process: {err}", process, ex.Message);
return false;
}
}

/// <summary>
/// Attempts to parse the URL section of an .url file
/// </summary>
/// <param name="urlFile"></param>
/// <returns></returns>
internal static (bool parsed, string url) ParseUrl(string urlFile)
{
try
{
if (!File.Exists(urlFile))
{
Log.Error("[SHORTCUTS] Unable to parse, file not found");
return (false, string.Empty);
}

var lnkFile = File.ReadAllLines(urlFile);

var parsed = false;
var url = string.Empty;

foreach (var line in lnkFile)
{
if (!line.StartsWith("URL=")) continue;

url = line.Split('=')[1].Trim();
parsed = true;
}

return (parsed, url);
}
catch (Exception ex)
{
Log.Fatal(ex, "[SHORTCUTS] Error while parsing: {err}", ex.Message);
return (false, string.Empty);
}
}
}
}
2 changes: 1 addition & 1 deletion src/HASS.Agent.AutoImport/HASS.Agent.AutoImport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<Version>2022.1.0</Version>
<Version>2022.2.0</Version>
<Authors>LAB02 Research</Authors>
<Company>LAB02 Research</Company>
<ApplicationIcon>hassagent.ico</ApplicationIcon>
Expand Down
1 change: 1 addition & 0 deletions src/HASS.Agent.AutoImport/Managers/HASSAgentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ internal static void Restart()
// wait a bit
Thread.Sleep(TimeSpan.FromSeconds(2));
}
else Log.Information("[HASS.Agent] Not currently running, no need to close");

// relaunch
Log.Information("[HASS.Agent] Starting new instance ..");
Expand Down
9 changes: 6 additions & 3 deletions src/HASS.Agent.AutoImport/Managers/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal static (bool success, string error) LoadSettings()
}

// attempt to parse
var settings = JsonConvert.DeserializeObject<AppSettings>(settingsStr);
var settings = JsonConvert.DeserializeObject<AppSettings>(settingsStr.AddBackslashEscaping());
if (settings == null) return (false, "unable to parse settings file");

// check if the ShortcutSourceFolder exists
Expand Down Expand Up @@ -114,7 +114,7 @@ internal static (bool success, string error) LoadProcessedShortcuts()
}

// attempt to parse
var shortcuts = JsonConvert.DeserializeObject<List<ProcessedShortcut>>(shortcutsStr);
var shortcuts = JsonConvert.DeserializeObject<List<ProcessedShortcut>>(shortcutsStr.AddBackslashEscaping());
if (shortcuts == null) return (false, "unable to parse processedshortcuts file");

// done
Expand All @@ -136,8 +136,11 @@ private static void SaveDefaultSettings()
// create the settings dir
if (!Directory.Exists(Variables.SettingsPath)) Directory.CreateDirectory(Variables.SettingsPath);

// serialize the settings
var serializedSettings = JsonConvert.SerializeObject(Variables.Settings, Formatting.Indented).RemoveBackslashEscaping();

// write the config
File.WriteAllText(Variables.AppSettingsFile, JsonConvert.SerializeObject(Variables.Settings, Formatting.Indented));
File.WriteAllText(Variables.AppSettingsFile, serializedSettings);
}
}
}
149 changes: 125 additions & 24 deletions src/HASS.Agent.AutoImport/Managers/ShortcutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,78 @@ internal static bool ProcessImport()

// get the shortcuts
var searchOption = Variables.Settings.ShortcutSearchRecusively ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
var shortcuts = Directory.EnumerateFiles(Variables.Settings.ShortcutSourceFolder, "*.lnk", searchOption)?.ToArray();
var (lnkSuccesful, lnkNewFiles) = ProcessLnks(searchOption);
var (urlSuccesful, urlNewFiles) = ProcessUrls(searchOption);

if (!shortcuts.Any())
// new shortcuts?
if (lnkNewFiles || urlNewFiles)
{
Log.Information("[SHORTCUTS] No shortcuts found, nothing to process");
return true;
// yep, store them
var serializedShortcuts = JsonConvert.SerializeObject(Variables.ProcessedShortcuts, Formatting.Indented).RemoveBackslashEscaping();
File.WriteAllText(Variables.ProcessedShortcutsFile, serializedShortcuts);

// restart hass.agent
if (Variables.Settings.RestartHASSAgentOnNewItems) HASSAgentManager.Restart();
}
else Log.Information("[SHORTCUTS] No shortcuts found, nothing to process");

// done
return lnkSuccesful && urlSuccesful;
}
catch (Exception ex)
{
Log.Fatal(ex, "[SHORTCUTS] Importing failed: {err}", ex.Message);
return false;
}
}

private static (bool succesful, bool newFiles) ProcessLnks(SearchOption searchOption)
{
var newFiles = 0;

try
{
var shortcuts = Directory.EnumerateFiles(Variables.Settings.ShortcutSourceFolder, "*.lnk", searchOption)?.ToArray();
if (!shortcuts.Any()) return (true, false);

// iterate them
var newFiles = 0;
foreach (var shortcutFile in shortcuts)
{
var fileName = Path.GetFileName(shortcutFile);

var shortcut = Shortcut.ReadFromFile(shortcutFile);
if (shortcut == null)
{
Log.Warning("[SHORTCUTS] Shortcut parsing failed for: {file}", Path.GetFileName(shortcutFile));
Log.Warning("[SHORTCUTS] Shortcut parsing failed for: {file}", fileName);
continue;
}

if (Variables.ProcessedShortcuts.Any(x => x.LinkFile == shortcutFile))
{
// already know it, skip
continue;
}

var name = Path.GetFileNameWithoutExtension(shortcutFile);
var target = shortcut.LinkTargetIDList.Path;
var args = shortcut.StringData.CommandLineArguments;

Log.Information("[SHORTCUTS] New shortcut found, importing into HASS.Agent:");
var target = shortcut.LinkTargetIDList?.Path ?? string.Empty;
var args = shortcut.StringData?.CommandLineArguments ?? string.Empty;

if (string.IsNullOrWhiteSpace(target))
{
Log.Warning("[SHORTCUTS] Shortcut contained empty or no target: {file}", fileName);
continue;
}

Log.Information("[SHORTCUTS] New shortcut found, importing into HASS.Agent:\r\n");
Log.Information("[SHORTCUTS] Type: {type}", ".lnk");
Log.Information("[SHORTCUTS] Name: {name}", name);
Log.Information("[SHORTCUTS] Target: {target}", target);
if (!string.IsNullOrWhiteSpace(args)) Log.Information("[SHORTCUTS] Arguments: {args}", args);
if (string.IsNullOrWhiteSpace(args)) Log.Information("[SHORTCUTS] Target: {target}\r\n", target);
else
{
Log.Information("[SHORTCUTS] Target: {target}", target);
Log.Information("[SHORTCUTS] Arguments: {args}\r\n", args);

}

var processedShortcut = new ProcessedShortcut
{
Expand All @@ -74,7 +113,7 @@ internal static bool ProcessImport()
CommandGuid = Guid.NewGuid().ToString(),
SensorGuid = Guid.NewGuid().ToString()
};

var imported = HASSAgentManager.ProcessShortcut(processedShortcut);
if (!imported)
{
Expand All @@ -88,23 +127,85 @@ internal static bool ProcessImport()
Log.Information("[SHORTCUTS] Succesfully imported\r\n");
}

// new shortcuts?
if (newFiles > 0)
return (true, newFiles > 0);
}
catch (Exception ex)
{
return (false, newFiles > 0);
}
}

private static (bool succesful, bool newFiles) ProcessUrls(SearchOption searchOption)
{
var newFiles = 0;

try
{
var shortcuts = Directory.EnumerateFiles(Variables.Settings.ShortcutSourceFolder, "*.url", searchOption)?.ToArray();
if (!shortcuts.Any()) return (true, false);

// iterate them
foreach (var shortcutFile in shortcuts)
{
// yep, store them
File.WriteAllText(Variables.ProcessedShortcutsFile, JsonConvert.SerializeObject(Variables.ProcessedShortcuts, Formatting.Indented));
var fileName = Path.GetFileName(shortcutFile);

// restart hass.agent
if (Variables.Settings.RestartHASSAgentOnNewItems) HASSAgentManager.Restart();
var (parsed, target) = HelperFunctions.ParseUrl(shortcutFile);
if (!parsed)
{
Log.Warning("[SHORTCUTS] Shortcut parsing failed for: {file}", fileName);
continue;
}

if (string.IsNullOrEmpty(target))
{
Log.Warning("[SHORTCUTS] Shortcut contained empty or no URL: {file}", fileName);
continue;
}

if (Variables.ProcessedShortcuts.Any(x => x.LinkFile == shortcutFile))
{
// already know it, skip
continue;
}

var name = Path.GetFileNameWithoutExtension(shortcutFile);

Log.Information("[SHORTCUTS] New shortcut found, importing into HASS.Agent:\r\n");
Log.Information("[SHORTCUTS] Type: {type}", ".url");
Log.Information("[SHORTCUTS] Name: {name}", name);
Log.Information("[SHORTCUTS] Target: {target}\r\n", target);

// create the shortcut with the right command
var processedShortcut = new ProcessedShortcut
{
LinkFile = shortcutFile,
Name = name,
Target = $"start {target}",
CommandGuid = Guid.NewGuid().ToString(),
SensorGuid = Guid.NewGuid().ToString()
};

var imported = HASSAgentManager.ProcessShortcut(processedShortcut);
if (!imported)
{
Log.Error("[SHORTCUTS] Import failed\r\n");
continue;
}

// restore the target
processedShortcut.Target = target;

newFiles++;
Variables.ProcessedShortcuts.Add(processedShortcut);

Log.Information("[SHORTCUTS] Succesfully imported\r\n");
}

// done
return true;
return (true, newFiles > 0);
}
catch (Exception ex)
{
Log.Fatal(ex, "[SHORTCUTS] Importing failed: {err}", ex.Message);
return false;
return (false, newFiles > 0);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/HASS.Agent.AutoImport/Models/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public AppSettings()
public string ShortcutSourceFolder { get; set; } = string.Empty;
public bool ShortcutSearchRecusively { get; set; } = false;
public bool CreateCustomCommands { get; set; } = true;
public bool CreateProcessActiveSensors { get; set; } = true;
public bool CreateProcessActiveSensors { get; set; } = false;
public bool RestartHASSAgentOnNewItems { get; set; } = true;
}
}

0 comments on commit 83e159e

Please sign in to comment.