11using System ;
2- using System . Collections . Generic ;
3- using System . IO ;
42using System . Net ;
53using System . Net . Sockets ;
64using 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