-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
247 lines (196 loc) · 7.89 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System.Globalization;
using System.IO;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OfficeTimeOut
{
public partial class frmTimeOut : Form
{
CheckForWorkstationLocking workLock = new CheckForWorkstationLocking();
public frmTimeOut()
{
InitializeComponent();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
workLock.SysEventsCheck(null, new SessionSwitchEventArgs(SessionSwitchReason.SessionLock));
e.Cancel = false;
base.OnFormClosing(e);
if (e.CloseReason == CloseReason.WindowsShutDown) return;
}
private void Form1_Load(object sender, EventArgs e)
{
workLock.Run(this);
workLock.SysEventsCheck(null, new SessionSwitchEventArgs(SessionSwitchReason.SessionUnlock));
}
}
public class CheckForWorkstationLocking : IDisposable
{
private const int REQUIRED_WORK_TOTAL_MINUTES = 585; // 9 hrs 45 mins
DateTime unlock;
int currentDayMsg = 0;
Timer timer;
int currentWeek = 0;
private TimeSpan tsTargetWorkingTime;
private SessionSwitchEventHandler sseh;
private string ThisWeekFileName
{
get
{
return string.Format("{0}_WK{1}.txt", DateTime.Now.Year, GetWeekOfYear(DateTime.Now));
}
}
public void SysEventsCheck(object sender, SessionSwitchEventArgs e)
{
switch (e.Reason)
{
case SessionSwitchReason.SessionLock:
LogTimeOut();
break;
case SessionSwitchReason.SessionUnlock:
unlock = DateTime.Now;
if (!File.Exists(ThisWeekFileName))
{
string timeInEntry = string.Format("{0}|", DateTime.Now);
File.WriteAllText(ThisWeekFileName, timeInEntry);
}
else
{
string logContent = File.ReadAllText(ThisWeekFileName);
string[] lines = logContent.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
string lastTimeIn = lines[lines.Length - 1].Split("|".ToCharArray())[0];
DateTime d = DateTime.Parse(lastTimeIn);
if (d.Day != DateTime.Now.Day)
{
d = DateTime.Now;
string timeInEntry = Environment.NewLine + string.Format("{0}|", d);
File.AppendAllText(ThisWeekFileName, timeInEntry);
}
unlock = d;
}
form.lblTimeUnlock.Text = unlock.ToLongTimeString();
var calculatedTimeOut = unlock.Add(new TimeSpan(9, 45, 0));
form.lblCalculatedTimeout.Text = calculatedTimeOut.ToLongTimeString();
UpdateTimes();
timer.Start();
break;
}
}
void LogTimeOut()
{
string logContent1 = File.ReadAllText(ThisWeekFileName);
string[] lines1 = logContent1.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
string lastTimeIn1 = lines1[lines1.Length - 1].Split("|".ToCharArray())[0];
DateTime d1 = DateTime.Parse(lastTimeIn1);
if (d1.Day == DateTime.Now.Day)
{
lines1[lines1.Length - 1] = lastTimeIn1 + "|" + DateTime.Now;
string overwriteContent = string.Empty;
foreach (var line in lines1)
{
if (overwriteContent == string.Empty)
{
overwriteContent += line;
}
else
{
overwriteContent += Environment.NewLine + line;
}
}
File.WriteAllText(ThisWeekFileName, overwriteContent);
}
}
void UpdateForecast()
{
string logContent = File.ReadAllText(ThisWeekFileName);
string[] lines = logContent.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
// average
double sumDailyMinutes = 0;
int targetSum = (lines.Length) * REQUIRED_WORK_TOTAL_MINUTES;
for (int i = 0; i < lines.Length - 1; i++)
{
var lineTimes = lines[i].Split("|".ToCharArray());
DateTime timein = DateTime.Parse(lineTimes[0]);
DateTime timeout = DateTime.Parse(lineTimes[1]);
double timediff = (timeout - timein).TotalMinutes;
sumDailyMinutes += timediff;
}
tsTargetWorkingTime = TimeSpan.FromMinutes(targetSum - sumDailyMinutes);
form.lblTargetWorkingTime.Text = string.Format("{0} hour(s) & {1} minute(s)",
tsTargetWorkingTime.Hours,
tsTargetWorkingTime.Minutes);
var targetedTimeOut = unlock.Add(tsTargetWorkingTime);
form.lblTargetTimeout.Text = targetedTimeOut.ToLongTimeString();
}
void UpdateTodayWorkingTime()
{
var countdown = DateTime.Now - unlock;
string countdownMsg = string.Format("{0} hour(s) & {1} minute(s)", countdown.Hours, countdown.Minutes);
form.lblWorkingTime.Text = countdownMsg;
}
void UpdateTimeoutCountdown()
{
var countdown = unlock.AddMinutes(REQUIRED_WORK_TOTAL_MINUTES) - DateTime.Now;
string countdownMsg = string.Format("{0} hour(s) & {1} minute(s)", countdown.Hours, countdown.Minutes);
form.lblCountdown.Text = countdownMsg;
}
void UpdateTimes()
{
UpdateTimeoutCountdown();
UpdateForecast();
UpdateTodayWorkingTime();
}
private void Timer_Tick(object sender, EventArgs e)
{
UpdateTimes();
var totalMins = DateTime.Now.Subtract(unlock).TotalMinutes;
if (totalMins >= tsTargetWorkingTime.TotalMinutes)
{
if (currentDayMsg == DateTime.Now.Day)
{
if (form.WindowState == FormWindowState.Minimized)
{
form.WindowState = FormWindowState.Normal;
}
form.Activate();
currentDayMsg = DateTime.Now.Day;
timer.Stop();
//MessageBox.Show("You can now leave the office");
}
}
}
frmTimeOut form;
public void Run(frmTimeOut form)
{
timer = new Timer();
timer.Tick += Timer_Tick;
timer.Interval = 5000;
this.form = form;
sseh = new SessionSwitchEventHandler(SysEventsCheck);
SystemEvents.SessionSwitch += sseh;
}
#region IDisposable Members
public void Dispose()
{
SystemEvents.SessionSwitch -= sseh;
}
#endregion
public static int GetWeekOfYear(DateTime time)
{
DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
}
}