Skip to content

Commit e781b57

Browse files
committed
Enable IR support for linux, Add ON IR cmd for fans that need it, allow multiple instances of the app to run (for advanced use cases)
1 parent adde710 commit e781b57

14 files changed

Lines changed: 269 additions & 238 deletions

File tree

FanTrayIcon/TrayIcon.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace FanTrayIcon
1212
public class TrayIcon
1313
{
1414
string _IP;
15+
string _port;
16+
string _instanceName;
1517

1618
private NotifyIcon trayIcon;
1719
private ToolStripMenuItem itemConsole;
@@ -26,15 +28,17 @@ public class TrayIcon
2628
const int SW_HIDE = 0;
2729
const int SW_SHOW = 5;
2830

29-
public TrayIcon(string IP)
31+
public TrayIcon(string IP, string port, string instanceName)
3032
{
3133
_IP = IP;
34+
_port = port;
35+
_instanceName = instanceName;
3236

3337
IntPtr handle = GetConsoleWindow();
3438
ShowWindow(handle, SW_HIDE);
3539

3640
trayIcon = new NotifyIcon();
37-
trayIcon.Text = "HTFanControl (Right Click For Menu)";
41+
trayIcon.Text = $"{_instanceName} (Right Click For Menu)";
3842
trayIcon.Icon = new Icon(GetType(), "htfancontrol.ico");
3943
trayIcon.MouseClick += new MouseEventHandler(trayIcon_MouseClick);
4044
trayIcon.DoubleClick += new EventHandler(trayIcon_DoubleClick);
@@ -49,19 +53,19 @@ public TrayIcon(string IP)
4953
itemConsole.Click += new EventHandler(itemConsole_Click);
5054

5155
itemAutostart = new ToolStripMenuItem();
52-
itemAutostart.Text = "Start HTFanControl Automatically";
56+
itemAutostart.Text = $"Start {_instanceName} Automatically";
5357
itemAutostart.CheckOnClick = true;
5458
itemAutostart.CheckedChanged += new EventHandler(itemAutostart_CheckedChanged);
5559

56-
if (CheckRegKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "HTFanControl"))
60+
if (CheckRegKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", _instanceName))
5761
{
5862
itemAutostart.Checked = true;
5963
}
6064

6165
ToolStripSeparator sep1 = new ToolStripSeparator();
6266

6367
ToolStripMenuItem itemClose = new ToolStripMenuItem();
64-
itemClose.Text = "Shutdown HTFanControl";
68+
itemClose.Text = $"Shutdown {_instanceName}";
6569
itemClose.Click += new EventHandler(itemClose_Click);
6670

6771
ContextMenuStrip trayMenu = new ContextMenuStrip();
@@ -97,7 +101,7 @@ private void trayIcon_DoubleClick(object sender, EventArgs e)
97101

98102
private void itemWebUI_Click(object sender, EventArgs e)
99103
{
100-
string url = $"http://{_IP}:5500";
104+
string url = $"http://{_IP}:{_port}";
101105

102106
try
103107
{
@@ -136,12 +140,19 @@ private void itemAutostart_CheckedChanged(object sender, EventArgs e)
136140
if (itemAutostart.Checked)
137141
{
138142
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
139-
key.SetValue("HTFanControl", Process.GetCurrentProcess().MainModule.FileName);
143+
if (_port == "5500")
144+
{
145+
key.SetValue("HTFanControl", Process.GetCurrentProcess().MainModule.FileName);
146+
}
147+
else
148+
{
149+
key.SetValue(_instanceName, $@"{Process.GetCurrentProcess().MainModule.FileName} ""{_port}"" ""{_instanceName}""");
150+
}
140151
}
141152
else
142153
{
143154
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
144-
key.DeleteValue("HTFanControl", false);
155+
key.DeleteValue(_instanceName, false);
145156
}
146157
}
147158

HTFanControl/Controllers/LIRCController.cs

Lines changed: 64 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
42
using System.Net;
53
using System.Net.Sockets;
64
using System.Text;
@@ -12,94 +10,57 @@ namespace HTFanControl.Controllers
1210
class LIRCController : IController
1311
{
1412
private Socket _lircSocket;
15-
private Dictionary<string, string> _lircMapping;
16-
1713
private Settings _settings;
1814

15+
private bool _isOFF = true;
16+
private bool _ONcmd = false;
17+
1918
public string ErrorStatus { get; private set; }
2019

2120
public LIRCController(Settings settings)
2221
{
2322
_settings = settings;
24-
25-
LoadLIRCMapping();
23+
_ONcmd = _settings.LIRC_ON_Delay > 0;
2624
}
2725

28-
public bool Connect()
26+
public bool SendCMD(string cmd)
2927
{
30-
Disconnect();
28+
bool send = true;
29+
bool goodResult = true;
3130

32-
try
31+
//case when fan needs to be turned ON before a command can be sent
32+
if (_ONcmd && _isOFF)
3333
{
34-
IPAddress ipAddress = IPAddress.Parse(_settings.LIRC_IP);
35-
IPEndPoint remoteEP = new IPEndPoint(ipAddress, _settings.LIRC_Port);
36-
_lircSocket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
37-
38-
IAsyncResult result = _lircSocket.BeginConnect(remoteEP, null, null);
39-
result.AsyncWaitHandle.WaitOne(3000);
34+
if (cmd != "OFF")
35+
{
36+
string lircOnCMD = $"SEND_ONCE {_settings.LIRC_Remote} ON\n";
37+
SendLIRCBytes(Encoding.ASCII.GetBytes(lircOnCMD));
4038

41-
if (!_lircSocket.Connected)
39+
Thread.Sleep(_settings.MQTT_ON_Delay);
40+
}
41+
//fan is already OFF, but it's being asked to turn OFF, so don't send a command, because that would cause it to turn ON again if ON/OFF are the same IR command
42+
else
4243
{
43-
throw new Exception();
44+
send = false;
4445
}
45-
46-
_lircSocket.EndConnect(result);
47-
48-
Thread.Sleep(25);
49-
}
50-
catch
51-
{
52-
ErrorStatus = $"({DateTime.Now:h:mm:ss tt}) Cannot connect to LIRC at: {_settings.LIRC_IP}:{_settings.LIRC_Port}";
53-
return false;
5446
}
5547

56-
Thread.Sleep(25);
57-
58-
return true;
59-
}
60-
61-
public void Disconnect()
62-
{
63-
if (_lircSocket != null)
48+
if (send)
6449
{
65-
try
66-
{
67-
_lircSocket.Shutdown(SocketShutdown.Both);
68-
_lircSocket.Close();
69-
}
70-
catch { }
50+
string lircCMD = $"SEND_ONCE {_settings.LIRC_Remote} {cmd}\n";
51+
goodResult = SendLIRCBytes(Encoding.ASCII.GetBytes(lircCMD));
7152
}
72-
}
7353

74-
public bool SendCMD(string cmd)
75-
{
76-
if (cmd == "STOP")
54+
if (cmd == "OFF")
7755
{
78-
if (_lircMapping != null && _lircMapping.TryGetValue("STOP", out string stopCMD))
79-
{
80-
cmd = stopCMD;
81-
}
82-
else
83-
{
84-
cmd = "OFF";
85-
}
56+
_isOFF = true;
8657
}
87-
88-
string[] cmds = cmd.Split(',');
89-
bool goodResult = true;
90-
91-
foreach (string c in cmds)
58+
else
9259
{
93-
if (_lircMapping != null && _lircMapping.TryGetValue(c, out string remote))
94-
{
95-
_settings.LIRC_Remote = remote;
96-
}
97-
98-
string lircCMD = $"SEND_ONCE {_settings.LIRC_Remote} {c}\n";
99-
goodResult = SendLIRCBytes(Encoding.ASCII.GetBytes(lircCMD));
60+
_isOFF = false;
10061
}
10162

102-
if(!goodResult)
63+
if (!goodResult)
10364
{
10465
return false;
10566
}
@@ -150,23 +111,49 @@ private bool SendLIRCBytes(byte[] cmd)
150111
return true;
151112
}
152113

153-
private void LoadLIRCMapping()
114+
public bool Connect()
154115
{
155-
_lircMapping = null;
156-
if (File.Exists(Path.Combine(ConfigHelper._rootPath, "lircmapping.txt")))
116+
Disconnect();
117+
118+
try
157119
{
158-
_lircMapping = new Dictionary<string, string>();
159-
string[] mappingFile = File.ReadAllLines(Path.Combine(ConfigHelper._rootPath, "lircmapping.txt"));
120+
IPAddress ipAddress = IPAddress.Parse(_settings.LIRC_IP);
121+
IPEndPoint remoteEP = new IPEndPoint(ipAddress, _settings.LIRC_Port);
122+
_lircSocket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
123+
124+
IAsyncResult result = _lircSocket.BeginConnect(remoteEP, null, null);
125+
result.AsyncWaitHandle.WaitOne(3000);
160126

161-
foreach (string s in mappingFile)
127+
if (!_lircSocket.Connected)
162128
{
163-
try
164-
{
165-
string[] vals = s.Split('=');
166-
_lircMapping.Add(vals[1], vals[0]);
167-
}
168-
catch { }
129+
throw new Exception();
169130
}
131+
132+
_lircSocket.EndConnect(result);
133+
134+
Thread.Sleep(25);
135+
}
136+
catch
137+
{
138+
ErrorStatus = $"({DateTime.Now:h:mm:ss tt}) Cannot connect to LIRC at: {_settings.LIRC_IP}:{_settings.LIRC_Port}";
139+
return false;
140+
}
141+
142+
Thread.Sleep(25);
143+
144+
return true;
145+
}
146+
147+
public void Disconnect()
148+
{
149+
if (_lircSocket != null)
150+
{
151+
try
152+
{
153+
_lircSocket.Shutdown(SocketShutdown.Both);
154+
_lircSocket.Close();
155+
}
156+
catch { }
170157
}
171158
}
172159
}

0 commit comments

Comments
 (0)