Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Contains functioning application, needs documentation.
  • Loading branch information
LAB02 Research committed Oct 22, 2021
1 parent b640654 commit b0483dd
Show file tree
Hide file tree
Showing 137 changed files with 13,139 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

.vs/

HASSAgent/bin/

HASSAgent/obj/
32 changes: 32 additions & 0 deletions HASSAgent.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31402.337
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HASSAgent", "HASSAgent\HASSAgent.csproj", "{407FFF5A-97EA-429E-BD5E-81D93240A1BD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{407FFF5A-97EA-429E-BD5E-81D93240A1BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{407FFF5A-97EA-429E-BD5E-81D93240A1BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{407FFF5A-97EA-429E-BD5E-81D93240A1BD}.Debug|x64.ActiveCfg = Debug|x64
{407FFF5A-97EA-429E-BD5E-81D93240A1BD}.Debug|x64.Build.0 = Debug|x64
{407FFF5A-97EA-429E-BD5E-81D93240A1BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{407FFF5A-97EA-429E-BD5E-81D93240A1BD}.Release|Any CPU.Build.0 = Release|Any CPU
{407FFF5A-97EA-429E-BD5E-81D93240A1BD}.Release|x64.ActiveCfg = Release|x64
{407FFF5A-97EA-429E-BD5E-81D93240A1BD}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7C2BA89F-434B-40F9-93F5-DD62C005ABFE}
VisualSVNWorkingCopyRoot = .
EndGlobalSection
EndGlobal
8 changes: 8 additions & 0 deletions HASSAgent/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
</configuration>
129 changes: 129 additions & 0 deletions HASSAgent/Commands/CommandsManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System;
using System.Threading.Tasks;
using HASSAgent.Enums;
using HASSAgent.Models.Mqtt.Commands;
using HASSAgent.Models.Mqtt.Commands.CustomCommands;
using HASSAgent.Models.Mqtt.Commands.KeyCommands;
using HASSAgent.Mqtt;
using Serilog;

namespace HASSAgent.Commands
{
/// <summary>
/// Continuously performs command autodiscovery and state publishing
/// </summary>
internal static class CommandsManager
{
private static bool _active = true;
private static bool _pause;

/// <summary>
/// Initializes the command manager
/// </summary>
internal static async void Intialise()
{
// wait while mqtt's connecting
while (MqttManager.GetStatus() == MqttStatus.Connecting) await Task.Delay(250);

// start background processing
_ = Task.Run(Process);
}

/// <summary>
/// Stop processing commands
/// </summary>
internal static void Stop()
{
_active = false;
}

/// <summary>
/// Pause processing commands
/// </summary>
internal static void Pause()
{
_pause = true;
}

/// <summary>
/// Resume processing commands
/// </summary>
internal static void Unpause()
{
_pause = false;
}

/// <summary>
/// Continuously processes commands (autodiscovery, state)
/// </summary>
private static async void Process()
{
while (_active)
{
try
{
await Task.Delay(TimeSpan.FromSeconds(30));

// are we paused?
if (_pause) continue;

// is mqtt available?
if (MqttManager.GetStatus() != MqttStatus.Connected)
{
// nothing to do
continue;
}

// let hass know we're still here
await MqttManager.AnnounceAvailabilityAsync("sensor");

// publish command autodisco's
foreach (var command in Variables.Commands) await command.PublishAutoDiscoveryConfigAsync();

// publish command states
foreach (var command in Variables.Commands) await command.PublishStateAsync();
}
catch (Exception ex)
{
Log.Fatal(ex, "[COMMANDSMANAGER] Eroor while publishing: {err}", ex.Message);
}
}
}

/// <summary>
/// Returns default information for the specified command type
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
internal static string GetCommandDefaultInfo(CommandType type)
{
switch (type)
{
case CommandType.ShutdownCommand:
return new ShutdownCommand().GetInfo();
case CommandType.RestartCommand:
return new RestartCommand().GetInfo();
case CommandType.LogOffCommand:
return new LogOffCommand().GetInfo();
case CommandType.CustomCommand:
return new CustomCommand("").GetInfo();
case CommandType.MediaPlayPauseCommand:
return new MediaPlayPauseCommand().GetInfo();
case CommandType.MediaNextCommand:
return new MediaNextCommand().GetInfo();
case CommandType.MediaPreviousCommand:
return new MediaPreviousCommand().GetInfo();
case CommandType.MediaVolumeUpCommand:
return new MediaVolumeUpCommand().GetInfo();
case CommandType.MediaVolumeDownCommand:
return new MediaVolumeDownCommand().GetInfo();
case CommandType.MediaMuteCommand:
return new MediaMuteCommand().GetInfo();
case CommandType.KeyCommand:
return new KeyCommand(byte.MinValue).GetInfo();
default:
return "";
}
}
}
}
105 changes: 105 additions & 0 deletions HASSAgent/Controls/QuickActionControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b0483dd

Please sign in to comment.