Zzz – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Threading;
5 using InTheHand.Net.Bluetooth;
6 using Newtonsoft.Json.Linq;
7 using Serilog;
8 using Zzz.Properties;
9  
10 namespace Zzz.Idle
11 {
12 public class Idler : EventArgs, IDisposable
13 {
14 #region Public Properties & Fields
15  
16 public TimeSpan IdleTimeout {
17 get {
18 return _timeout;
19 }
20 set {
21 _timeout = value;
22  
23 Log.Information("[{Identifier}] Set timeout to {Timeout}.", _identifier, _timeout);
24 }
25 }
26  
27 public bool IsRunning => _running;
28  
29 #endregion
30  
31 #region Static Fields and Constants
32  
33 private Timer _idleTimer;
34  
35 private MouseInput _mouseInput;
36  
37 private KeyboardInput _keyboardInput;
38  
39 private BluetoothScan _bluetoothScan;
40  
41 private bool _announced;
42  
43 #endregion
44  
45 #region Public Events & Delegates
46  
47 public event EventHandler<IdleEventArgs> Idle;
48  
49 public event EventHandler<IdleImminentEventArgs> IdleImminent;
50  
51 #endregion
52  
53 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
54  
55 private bool _running;
56  
57 private WindowPresence _windowPresence;
58  
59 private TimeSpan _timeout;
60  
61 private Configuration.Configuration _configuration;
62  
63 private string _identifier;
64  
65 #endregion
66  
67 #region Constructors, Destructors and Finalizers
68  
69 public Idler(Configuration.Configuration configuration, string identifier)
70 {
71 _configuration = configuration;
72  
73 _identifier = identifier;
74 }
75  
76 public void Dispose()
77 {
78 if (_windowPresence != null)
79 {
80 _windowPresence.WindowDetected -= WindowPresence_WindowDetected;
81 _windowPresence.Dispose();
82 _windowPresence = null;
83 }
84  
85 if (BluetoothRadio.IsSupported)
86 {
87 if (_bluetoothScan != null)
88 {
89 _bluetoothScan.BluetoothDeviceDetected -= BluetoothScan_BluetoothDeviceDetected;
90 _bluetoothScan.Dispose();
91 _bluetoothScan = null;
92 }
93 }
94  
95 if (_mouseInput != null)
96 {
97 _mouseInput.MouseMoved -= MouseInput_MouseMoved;
98 _mouseInput.MouseClicked -= MouseInput_MouseClicked;
99 _mouseInput.MouseWheelScrolled -= MouseInput_MouseWheelScrolled;
100 _mouseInput.Dispose();
101 _mouseInput = null;
102 }
103  
104 if (_keyboardInput != null)
105 {
106 _keyboardInput.KeyboardKeyPressed -= KeyboardInput_KeyboardKeyPressed;
107 _keyboardInput.Dispose();
108 _keyboardInput = null;
109 }
110 }
111  
112 #endregion
113  
114 #region Event Handlers
115  
116 private void BluetoothScan_BluetoothDeviceDetected(object sender, EventArgs e)
117 {
118 Log.Information("[{Identifier}] Idler reset by bluetooth device.", _identifier);
119  
120 _idleTimer?.Change(_timeout, Timeout.InfiniteTimeSpan);
121 }
122  
123 private void MouseInput_MouseMoved(object sender, MouseMovedEventArgs e)
124 {
125 if (e.Distance < (int)_configuration.MouseMoveTolerance)
126 {
127 Log.Information("[{Identifier}] Mouse movement too small to reset idler.", _identifier);
128  
129 return;
130 }
131  
132 Log.Information("[{Identifier}] Idler reset by mouse movement.", _identifier);
133  
134 _idleTimer?.Change(_timeout, Timeout.InfiniteTimeSpan);
135 }
136  
137 private void MouseInput_MouseClicked(object sender, EventArgs e)
138 {
139 Log.Information("[{Identifier}] Idler reset by mouse click.", _identifier);
140  
141 _idleTimer?.Change(_timeout, Timeout.InfiniteTimeSpan);
142 }
143  
144 private void MouseInput_MouseWheelScrolled(object sender, EventArgs e)
145 {
146 Log.Information("[{Identifier}] Idler reset by mouse scroll.", _identifier);
147  
148 _idleTimer?.Change(_timeout, Timeout.InfiniteTimeSpan);
149 }
150  
151 private void KeyboardInput_KeyboardKeyPressed(object sender, EventArgs e)
152 {
153 Log.Information("[{Identifier}] Idler reset by key press.", _identifier);
154  
155 _idleTimer?.Change(_timeout, Timeout.InfiniteTimeSpan);
156 }
157  
158 private void WindowPresence_WindowDetected(object sender, EventArgs e)
159 {
160 Log.Information("[{Identifier}] Idler reset by detected window.", _identifier);
161  
162 _idleTimer?.Change(_timeout, Timeout.InfiniteTimeSpan);
163 }
164  
165 #endregion
166  
167 #region Public Methods
168  
169 public void Start(TimeSpan timeout)
170 {
171 if (_running)
172 {
173 return;
174 }
175  
176 _timeout = timeout;
177  
178 // Bind input events.
179 if (_configuration.MonitorMouse)
180 {
181 if (_mouseInput == null)
182 {
183 _mouseInput = new MouseInput();
184 _mouseInput.MouseClicked += MouseInput_MouseClicked;
185 _mouseInput.MouseMoved += MouseInput_MouseMoved;
186 _mouseInput.MouseWheelScrolled += MouseInput_MouseWheelScrolled;
187 }
188 }
189  
190 if (_configuration.MonitorKeyboard)
191 {
192 if (_keyboardInput == null)
193 {
194 _keyboardInput = new KeyboardInput();
195 _keyboardInput.KeyboardKeyPressed += KeyboardInput_KeyboardKeyPressed;
196 }
197 }
198  
199 // Bind bluetooth events.
200 if (BluetoothRadio.IsSupported)
201 {
202 if (_configuration.MonitorBluetooth)
203 {
204 if (_bluetoothScan == null)
205 {
206 _bluetoothScan = new BluetoothScan(_configuration);
207 _bluetoothScan.BluetoothDeviceDetected += BluetoothScan_BluetoothDeviceDetected;
208 }
209 }
210 }
211  
212 if (_configuration.MonitorWindows)
213 {
214 if (_windowPresence == null)
215 {
216 _windowPresence = new WindowPresence(_configuration);
217 _windowPresence.WindowDetected += WindowPresence_WindowDetected;
218 }
219 }
220  
221 // Reset the announcement flag.
222 _announced = false;
223  
224 // Reset the idle timer.
225 _idleTimer = new Timer(IdleTimeCallback, null,
226 _timeout.Subtract(TimeSpan.FromMinutes(1)),
227 Timeout.InfiniteTimeSpan);
228  
229 _running = true;
230  
231 Log.Information("[{Identifier}] Idler enabled.", _identifier);
232 }
233  
234 public void Stop()
235 {
236 if (!_running)
237 {
238 return;
239 }
240  
241 if (_windowPresence != null)
242 {
243 _windowPresence.WindowDetected -= WindowPresence_WindowDetected;
244 _windowPresence.Dispose();
245 _windowPresence = null;
246 }
247  
248 if (BluetoothRadio.IsSupported)
249 {
250 if (_bluetoothScan != null)
251 {
252 _bluetoothScan.BluetoothDeviceDetected -= BluetoothScan_BluetoothDeviceDetected;
253 _bluetoothScan.Dispose();
254 _bluetoothScan = null;
255 }
256 }
257  
258 if (_mouseInput != null)
259 {
260 _mouseInput.MouseMoved -= MouseInput_MouseMoved;
261 _mouseInput.MouseClicked -= MouseInput_MouseClicked;
262 _mouseInput.MouseWheelScrolled -= MouseInput_MouseWheelScrolled;
263 _mouseInput.Dispose();
264 _mouseInput = null;
265 }
266  
267 if (_keyboardInput != null)
268 {
269 _keyboardInput.KeyboardKeyPressed -= KeyboardInput_KeyboardKeyPressed;
270 _keyboardInput.Dispose();
271 _keyboardInput = null;
272 }
273  
274 if (_idleTimer != null)
275 {
276 _idleTimer.Change(Timeout.Infinite, Timeout.Infinite);
277 _idleTimer.Dispose();
278 _idleTimer = null;
279 }
280  
281 _running = false;
282  
283 Log.Information("[{Identifier}] Idler disabled.", _identifier);
284 }
285  
286 #endregion
287  
288 #region Private Methods
289  
290 private void IdleTimeCallback(object state)
291 {
292 if (!_announced)
293 {
294 Log.Information("[{Identifier}] Announcing that idle timeout has been reached.", _identifier);
295  
296 // Invoke the idle imminent event on the parent task scheduler.
297 IdleImminent?.Invoke(this, new IdleImminentEventArgs());
298  
299 // Reschedule one minute.
300 _idleTimer.Change(TimeSpan.FromMinutes(1), Timeout.InfiniteTimeSpan);
301  
302 _announced = true;
303  
304 return;
305 }
306  
307 Log.Information("[{Identifier}] Idling state reached.", _identifier);
308  
309 // Invoke the idle event on the parent task scheduler.
310 Idle?.Invoke(this, new IdleEventArgs());
311 }
312  
313 #endregion
314 }
315 }