-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdooris.js
114 lines (107 loc) · 3.3 KB
/
dooris.js
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
$(document).ready(function() {
var checkJSON = function(){
};
checkJSON();
/**
* Checks wether you are developing on localhost or run this JS in a production environment to determine wich JSON to
* @uses [window.location]
* @return string relative path to JSON.
*/
var checkEnv = function () {
var pathname = window.location['host'];
if (pathname.toLowerCase().indexOf("localhost") >= 0) {
pathname = "json.php";
console.log('Localhost!');
} else {
pathname = "dooris.json";
}
return pathname;
};
var pathname = checkEnv();
// Returns human readable string for input in minutes, hours or days
/**
* Returns time in minutes/hours instead of seconds
* @param {int} time in seconds
* @return {string} Date in human redable form
* pre: time >= 0
* post: returns string
* Int -> String
*/
var parseTime = function(time) {
var strTime = time;
if(strTime < 60) {
if(strTime === 1) {
strTime += ' minute';
}
strTime += ' minutes';
} else if(strTime > 60 || strTime < 1440) {
strTime = Math.round(strTime/60);
if(strTime === 1){
strTime += ' hour';
} else {
strTime += ' hours';
}
} else if (strTime > 1440) {
strTime = Math.round(strTime/60/24);
if(strTime === 1) {
strTime += ' day';
} else {
strTime += ' days';
}
}
return strTime;
};
/**
* Returns the diffrence of time between input time and the current time.
* @param {int} time timestamp
* @return {int} timedifference in minutes
*/
var parseTimeDiffrence = function (time) {
var timeRec = time * 1000;
var timeNow = $.now();
return Math.round(((timeNow - timeRec)/1000)/60);
};
/**
* Removes all classes from jQuery Object
* @param {jQuery Object} obj
* @return {nil}
*/
var clearClasses = function (obj) {
obj.removeClass('open');
obj.removeClass('dhcp');
obj.removeClass('closed');
};
this.loadData = function() {
$.getJSON(pathname, function(data) {
var jsonData = data;
var statusDiv = $('#status');
var status = jsonData['door']['status'];
var dhcp = jsonData['router']['dhcp'];
dhcp = dhcp - 2; //Need to remove the Freifunk Router.
// Status Refresh all 5 minutes. If the status refresh is older than 5 minutes, there is a connection problem.
if(parseTimeDiffrence(jsonData['door']['last_update']) > 6) {
status = '-1';
}
if(status === '0') {
clearClasses(statusDiv);
statusDiv.html("Please come in, we're open!").addClass('open');
} else if (status ==='1') {
if (dhcp > 0) {
clearClasses(statusDiv);
statusDiv.html("There are " + dhcp + " DHCP Clients online, but the door is closed.").addClass('dhcp');
} else {
clearClasses(statusDiv);
statusDiv.html("Sorry, we're closed.").addClass('closed');
}
} else {
clearClasses(statusDiv);
statusDiv.html("There is a connection Problem").addClass('closed');
}
var time = parseTimeDiffrence(jsonData['door']['last_change']);
time = parseTime(time);
$('#time').html("Last status change " + time + " ago.");
});
};
this.loadData();
setInterval(this.loadData, 30*1000);
});