-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkHours.js
More file actions
72 lines (59 loc) · 1.89 KB
/
workHours.js
File metadata and controls
72 lines (59 loc) · 1.89 KB
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
/*
* Script to track my work hours automatically.
* Set up a 500m radius border around my workplace, and measuring the time spent within the area.
*
* Testing on my way to work tomorrow :-)
*/
var myEmail = "mail@example.com";
var workplace = device.regions.createRegion({
name:"Rotvoll",
latitude: 63.43915607186877,
longitude: 10.480849756066846,
radius: 500
});
var dailyWorkplaceCounter = 0;
var workplaceEntry = new Date(0);
workplace.on("enter", function(signal) {
dailyWorkplaceCounter += 1;
workplaceEntry = new Date();
var notification = device.notifications.createNotification("Entering workplace for the " + dailyWorkplaceCounter + ". time today. Enjoy!");
notification.show();
});
workplace.on("exit", function(signal) {
var now = new Date();
var remainingSeconds = (now.getTime() - workplaceEntry.getTime())/1000;
var hours = remainingSeconds / (60*60) >> 0;
remainingSeconds %= 60*60;
var minutes = remainingSeconds / 60 >> 0;
remainingSeconds %= 60;
var seconds = remainingSeconds;
var durationString = "" + hours + ":" + minutes + ":" + seconds;
var notification = device.notifications.createNotification("Left work, you stayed for " + durationString);
notification.show();
// And send mail
device.messaging.sendMail(
{
to: myEmail,
subject: 'AUTO:WORKHOURS',
body: "Arrived work at " + workplaceEntry.toString() + ".\nLeft work at " + now.toString() + ".\n\nTime spent: " + durationString
},
function (err) {
console.log(err || 'mail was sent successfully');
}
);
});
var now = new Date();
var timerStart = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
var timerAction = function() {
dailyWorkplaceCounter = 0;
};
device.scheduler.setTimer(
{
name : "midnightResetTimer",
time : timerStart.getTime(),
interval : "day",
repeat : true,
exact : false
},
timerAction);
device.regions.startMonitoring(workplace);