Skip to content

Commit

Permalink
Windows setup wizard created
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickpissurno committed Feb 18, 2019
1 parent d4c080f commit 1b5e880
Show file tree
Hide file tree
Showing 9 changed files with 1,241 additions and 90 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
*.exe
*.out
*.app
*.msi

bin/
.DS_Store
.pkg

.vs
obj/
obj/
packages
4 changes: 4 additions & 0 deletions daemon/Windows/Daemon.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.271
MinimumVisualStudioVersion = 10.0.40219.1
Project("{54435603-DBB4-11D2-8724-00A0C9A8B90C}") = "Setup", "Setup\Setup.vdproj", "{2F03DC7D-CBB7-4979-A7F3-4C9581F2EE95}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Daemon", "Daemon\Daemon.csproj", "{4BDF0DD1-1BA1-46DD-B14A-79551CAB96DA}"
EndProject
Global
Expand All @@ -11,6 +13,8 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2F03DC7D-CBB7-4979-A7F3-4C9581F2EE95}.Debug|Any CPU.ActiveCfg = Debug
{2F03DC7D-CBB7-4979-A7F3-4C9581F2EE95}.Release|Any CPU.ActiveCfg = Release
{4BDF0DD1-1BA1-46DD-B14A-79551CAB96DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BDF0DD1-1BA1-46DD-B14A-79551CAB96DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BDF0DD1-1BA1-46DD-B14A-79551CAB96DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
17 changes: 16 additions & 1 deletion daemon/Windows/Daemon/Daemon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4BDF0DD1-1BA1-46DD-B14A-79551CAB96DA}</ProjectGuid>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
<RootNamespace>Daemon</RootNamespace>
<AssemblyName>Daemon</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
Expand All @@ -31,9 +31,18 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Win32.TaskScheduler, Version=2.8.7.0, Culture=neutral, PublicKeyToken=c416bc1b32d97233, processorArchitecture=MSIL">
<HintPath>..\packages\TaskScheduler.2.8.7\lib\net40\Microsoft.Win32.TaskScheduler.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -43,7 +52,13 @@
<ItemGroup>
<Compile Include="Input.cs" />
<Compile Include="Program.cs" />
<Compile Include="ProjectInstaller.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
194 changes: 106 additions & 88 deletions daemon/Windows/Daemon/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.Win32.TaskScheduler;
using System;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;

namespace Daemon
Expand Down Expand Up @@ -56,119 +54,139 @@ static void Press(ScanCodeShort key, bool shift)
Thread.Sleep(1);
}

static void Exit(int status)
{
if(status != 0)
{
using (TaskService ts = new TaskService())
{
var task = ts.FindTask(ProjectInstaller.TASK_NAME);
if (task != null)
task.Run();
}
}
Environment.Exit(status);
}

static void Main(string[] args)
{
const int bufferMax = 256;
byte[] buf = new byte[bufferMax];
try
{
const int bufferMax = 256;
byte[] buf = new byte[bufferMax];

Console.WriteLine("Daemon Started");
Console.WriteLine("Daemon Started");

//wait a bit of time for all components to initialize before trying to connect
Thread.Sleep(10000);
//wait a bit of time for all components to initialize before trying to connect
Thread.Sleep(10000);

Console.WriteLine("Locating the Receiver device");
Console.WriteLine("Locating the Receiver device");

string[] ports;
int portsLength;
SerialPort port = null;
do
{
Thread.Sleep(1000);
ports = SerialPort.GetPortNames();
portsLength = ports.Length;
string[] ports;
int portsLength;
SerialPort port = null;
do
{
Thread.Sleep(1000);
ports = SerialPort.GetPortNames();
portsLength = ports.Length;

foreach (var name in ports) {
try
foreach (var name in ports)
{
port = new SerialPort(name, 9600);
port.ReadTimeout = 2100;
port.DtrEnable = true;
port.Open();
try
{
port = new SerialPort(name, 9600);
port.ReadTimeout = 2100;
port.DtrEnable = true;
port.Open();

if (port.ReadLine().Trim() == RECEIVER_HANDSHAKE_CODE.ToString())
break;
if (port.ReadLine().Trim() == RECEIVER_HANDSHAKE_CODE.ToString())
break;

throw new Exception();
}
catch
{
portsLength -= 1;
if (port != null)
throw new Exception();
}
catch
{
if(port.IsOpen)
port.Close();
port.Dispose();
portsLength -= 1;
if (port != null)
{
if (port.IsOpen)
port.Close();
port.Dispose();
}
port = null;
}
port = null;
}
}
}
while (portsLength < 1);
while (portsLength < 1);

if(port == null)
{
Console.WriteLine("Failed to find a Receiver device");
if (port == null)
{
Console.WriteLine("Failed to find a Receiver device");

Thread.Sleep(10);
Environment.Exit(1);
}
Thread.Sleep(10);
Exit(1);
}

Console.WriteLine("Connected to the Receiver device");
Console.WriteLine("Connected to the Receiver device");

port.ReadTimeout = 5000;
port.ReadTimeout = 5000;

int btn = 0;
while (true)
{
Thread.Sleep(500);
for (int i = 0; i < 5; i++)
int btn = 0;
while (true)
{
btn = 0;

try
{
btn = int.Parse(port.ReadLine().Trim());
}
catch
Thread.Sleep(500);
for (int i = 0; i < 5; i++)
{
btn = 0;
}

if (btn == RECEIVER_HANDSHAKE_CODE)
continue;
try
{
btn = int.Parse(port.ReadLine().Trim());
}
catch
{
btn = 0;
}

switch (btn)
{
case BUTTON_NEXT:
Press(ScanCodeShort.RIGHT, false);
break;
case BUTTON_PREV:
Press(ScanCodeShort.LEFT, false);
break;
case BUTTON_PAUSE:
Press(ScanCodeShort.SPACE, false);
break;
case BUTTON_NEXT_HOLD:
Press(ScanCodeShort.KEY_N, true);
break;
case BUTTON_PREV_HOLD:
Press(ScanCodeShort.KEY_P, true);
break;
}
if (btn == RECEIVER_HANDSHAKE_CODE)
continue;

Thread.Sleep(5);
}
switch (btn)
{
case BUTTON_NEXT:
Press(ScanCodeShort.RIGHT, false);
break;
case BUTTON_PREV:
Press(ScanCodeShort.LEFT, false);
break;
case BUTTON_PAUSE:
Press(ScanCodeShort.SPACE, false);
break;
case BUTTON_NEXT_HOLD:
Press(ScanCodeShort.KEY_N, true);
break;
case BUTTON_PREV_HOLD:
Press(ScanCodeShort.KEY_P, true);
break;
}

if (!port.IsOpen)
break;
}
Thread.Sleep(5);
}

port.Close();
port.Dispose();
if (!port.IsOpen)
break;
}

Console.WriteLine("Disconnected from the Receiver device");
port.Close();
port.Dispose();

Environment.Exit(1);
Console.WriteLine("Disconnected from the Receiver device");
}
finally
{
Exit(1);
}
}
}
}
87 changes: 87 additions & 0 deletions daemon/Windows/Daemon/ProjectInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Microsoft.Win32.TaskScheduler;
using System;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Management;
using System.Linq;

namespace Daemon
{
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
internal static readonly string TASK_NAME = "SK6-Remote";

public ProjectInstaller() : base()
{
this.AfterInstall += ProjectInstaller_AfterInstall;
this.AfterUninstall += ProjectInstaller_AfterUninstall;
this.BeforeUninstall += ProjectInstaller_BeforeUninstall;
}

private void ProjectInstaller_BeforeUninstall(object sender, InstallEventArgs e)
{
using (TaskService ts = new TaskService())
{
var task = ts.FindTask(TASK_NAME);
if(task != null)
task.Stop();
}
}

private void ProjectInstaller_AfterUninstall(object sender, InstallEventArgs e)
{
using (TaskService ts = new TaskService())
{
ts.RootFolder.DeleteTask(TASK_NAME, false);
}
}

public void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
string path = this.Context.Parameters["targetdir"];

// Get the service on the local machine
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Makes your LG SK6 soundbar remote controller work with your computer";

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
ManagementObjectCollection collection = searcher.Get();
string username = (string)collection.Cast<ManagementBaseObject>().First()["UserName"];

td.Principal.UserId = username;
td.Principal.LogonType = TaskLogonType.InteractiveToken;

// Create a trigger that will fire the task at this time every other day
td.Triggers.Add(new LogonTrigger());

// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction(Path.Combine(path, "Daemon.exe"), null));

td.Settings.Compatibility = TaskCompatibility.V2;
td.Settings.UseUnifiedSchedulingEngine = true;

td.Settings.AllowDemandStart = true;
td.Settings.DisallowStartIfOnBatteries = false;
td.Settings.Enabled = true;
td.Settings.RestartCount = 99999;
td.Settings.StopIfGoingOnBatteries = false;
td.Settings.RestartInterval = TimeSpan.FromMinutes(1);
td.Settings.Priority = System.Diagnostics.ProcessPriorityClass.BelowNormal;
td.Settings.RunOnlyIfIdle = false;
td.Settings.ExecutionTimeLimit = new TimeSpan();
td.Settings.MultipleInstances = TaskInstancesPolicy.StopExisting;

// Register the task in the root folder
var task = ts.RootFolder.RegisterTaskDefinition(TASK_NAME, td, TaskCreation.CreateOrUpdate, username, null, TaskLogonType.InteractiveToken);

//Start
task.Run();
}
}
}
}
Loading

0 comments on commit 1b5e880

Please sign in to comment.