Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
112 lines (93 loc) · 4.1 KB
/
Copy pathserver.js
File metadata and controls
112 lines (93 loc) · 4.1 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
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
var nconf = require("nconf"),
async = require("async"),
http = require("http"),
etemp = require("./etemp.js"),
time = require("./time.js");
function loadConfig() {
o_conf = {};
// retrieve home dir
var home = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
nconf.file(home+'/.dontmakehours.json');
var t_conf_k = ['user', 'password', 'url', 'server', 'port', 'max_day', 'max_month', 'max_week', 'ref_day'];
for(var i in t_conf_k) {
var param = nconf.get(t_conf_k[i]);
if(param == undefined) {
return new Error('please define param '+t_conf_k[i]+' in config file (~/.dontmakehours.json)');
}
o_conf[t_conf_k[i]] = param;
}
return o_conf;
}
function green(str) {
return '<span style="color:green">'+str+'</span>';
}
function red(str) {
return '<span style="color:red">'+str+'</span>';
}
// 1 - init config
var o_conf = loadConfig();
if ( o_conf instanceof Error ) {
// handle the error
console.log(o_conf);
process.exit();
}
console.log('init conf done');
et = etemp.get(o_conf.url, o_conf.server, o_conf.port);
et.setRefreshRate(60);
// Create an HTTP server
http.createServer(function (req, response) {
console.log("***** Hit received *****");
async.series(
{
log: function(callback) {
et.login(o_conf.user, o_conf.password, callback);
},
badging_current: function(callback) {
et.getWeekHours(new Date(), callback);
},
badging_previous: function(callback) {
var d = new Date();
d.setDate(d.getDate() - 7); // lastweek
et.getWeekHours(d, callback);
},
today: function(callback) {
et.getTodayWorkTime(callback);
}
},
function(err, res) {
var worked = time.minToTime(res.today.total);
//console.log("Today work time (max = 10) : " + worked);
worked = res.today.total > time.timeToMin(o_conf['max_day']) ? red(worked) : green(worked);
var html = "Today work time (max = "+o_conf['max_day']+") : " + worked + "<br/>";
var week_min = res.today.wip + time.timeToMin(res.badging_current['today']);
var week = time.minToTime(week_min);
week = week_min > time.timeToMin(o_conf['max_month']) ? red(week) : green(week);
//console.log("Instant week cmpt (abs, max = 64) : " + week);
html += "Instant week cmpt (abs, max = "+ o_conf['max_month'] +") : " + week + "<br/>";
var extra_week_min = week_min - time.timeToMin(res.badging_previous[7]);
var extra_week = time.minToTime(extra_week_min);
extra_week = extra_week_min > time.timeToMin(o_conf['max_week']) ? red(extra_week) : green(extra_week);
//console.log("Week extra hours (rel, max = 5) : " + extra_week +" --- "+ res.badging_previous[7] +"=>"+week);
html += "Week extra hours (rel, max = "+ o_conf['max_week'] +") : " + extra_week +" --- "+ res.badging_previous[7] +"=>"+week + "<br/>";
var rtt = Math.floor(week_min / (time.timeToMin(o_conf['ref_day'])));
//console.log("Rtt possible : "+rtt);
rtt = rtt > 0 ? green(rtt) : red(rtt);
html += "Rtt possible : "+rtt + "<br/>";
if(res.today.total > time.timeToMin(o_conf['max_day']) || extra_week_min > time.timeToMin(o_conf['max_week']) || week_min > time.timeToMin(o_conf['max_month']) ) {
html += "<b style='color:green;'>GO GO GO GO !!</b><br/>";
} else {
// 8 hours by day
var now = new Date();
var mins = time.timeToMin(o_conf['ref_day'])-res.today.total;
var ttl_date = time.minToTime(mins);
now.setMinutes(now.getMinutes()+mins);
var ttl = now.getFullYear()+'_'+etemp.zeroLeftPad(now.getMonth()+1)+'_'+etemp.zeroLeftPad(now.getDate())+'_'+etemp.zeroLeftPad(now.getHours())+'_'+etemp.zeroLeftPad(now.getMinutes());
ttl_date = mins > 8*60 ? red(ttl_date) : green(ttl_date);
html += "Today remaining time ("+o_conf['ref_day']+"): <span>"+ttl_date+"</span><br />";
html += '<iframe src="http://www.countdownr.com/external.html?logo=clock.png&alert=gong.mp3&time='+ttl+'&title=Go%20Home&repeat=0&url=&background=transparent" frameborder="0" width="320" height="130" scrolling="no"><a href="http://www.countdownr.com">Countdownr</a></iframe>';
}
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(html);
}
);
}).listen(9999);