-
Notifications
You must be signed in to change notification settings - Fork 1
/
last_update.js
172 lines (145 loc) · 6.19 KB
/
last_update.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
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
// this following things are also successful
var $jsValue_aster= document.querySelector('.jsValue_aster');
var public_aster='w5nEnw974mswgrx0ALOE';
var $jsValue_bougainvillea= document.querySelector('.jsValue_bougainvillea');
var public_bougainvillea='J4g8nMZrV9TDyqX31x5L';
var $jsValue_camellia= document.querySelector('.jsValue_camellia');
var public_camellia='q5YnK9A9qMCqoNqv78XD';
var $jsValue_daisy= document.querySelector('.jsValue_daisy');
var public_daisy='RMxqjA6nRXfbm01raooM';
var $jsValue_area51_redmud= document.querySelector('.jsValue_area51_redmud');
var public_area51_redmud='21mmonra90TYXlx9KL4J';
var $jsValue_concrete_curing= document.querySelector('.jsValue_concrete_curing');
var public_concrete_curing='jwDYgo7DnmtdRDboA0nQ';
var $jsValue_compacted_redmud= document.querySelector('.jsValue_compacted_redmud');
var public_compacted_redmud='Vpr5RDVV3VTagYD9L196';
var $jsValue_coal_tailings= document.querySelector('.jsValue_coal_tailings');
var public_coal_tailings='9J2rX3QZ94s5RJ9LjrbN';
var data_sensor;
function millisecondsToStr (milliseconds) {
// TIP: to find current time in milliseconds, use:
// var current_time_milliseconds = new Date().getTime();
function numberEnding (number) {
return (number > 1) ? 's' : '';
}
var temp = Math.floor(milliseconds / 1000);
var years = Math.floor(temp / 31536000);
if (years) {
return years + ' year' + numberEnding(years);
}
//TODO: Months! Maybe weeks?
var days = Math.floor((temp %= 31536000) / 86400);
if (days) {
return days + ' day' + numberEnding(days);
}
var hours = Math.floor((temp %= 86400) / 3600);
if (hours) {
return hours + ' hour' + numberEnding(hours);
}
var minutes = Math.floor((temp %= 3600) / 60);
if (minutes) {
return minutes + ' minute' + numberEnding(minutes);
}
var seconds = temp % 60;
if (seconds) {
return seconds + ' second' + numberEnding(seconds);
}
return 'less than a second'; //'just now' //or other string you like;
}
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
var format = d3.timeParse('%Y-%m-%dT%H:%M:%S.%LZ')
var time_now= new Date()
function get_the_latest_json(input,public_key)
{
var offset = new Date().getTimezoneOffset();
$.ajax({
url:'https://data.sparkfun.com/output/'+public_key+'.json',
//data:{page:1,sample:1,limit:1}, // working, getting the latest one! 2017-06-05 11:03
data:{limit:1},
//async: false, // https://stackoverflow.com/questions/1478295/what-does-async-false-do-in-jquery-ajax
async: false, // https://stackoverflow.com/questions/1478295/what-does-async-false-do-in-jquery-ajax
dataType:'jsonp',
tryCount : 0,
retryLimit :3,
timeout: 1000 ,
success : function (json) {
json.forEach(function(d) {
d.timestamp = d3.timeHour.offset(format(d.timestamp),-offset/60); // http://stackoverflow.com/questions/187
});
data_sensor=json;
console.log(data_sensor)
//$jsValue4.innerHTML =data_sensor[0]['evap1']
//$jsValue4.innerHTML =Math.round((new Date() -data_sensor[0]['timestamp'])/60000)+' min ago'
//$jsValue4.innerHTML =millisecondsToStr(new Date() -data_sensor[0]['timestamp'])+' ago'
input.innerHTML=millisecondsToStr(new Date() -data_sensor[0]['timestamp'])+' ago'
//input.innerHTML='bad'
return data_sensor
},
error : function(xhr, textStatus, errorThrown ) {
wait(300)
//alert(xhr.responseText)
if (textStatus == 'timeout') {
//input.innerHTML='bad gateway'
//return
this.tryCount++;
if (this.tryCount <= this.retryLimit) {
//try again
//https://stackoverflow.com/questions/10024469/whats-the-best-way-to-retry-an-ajax-request-on-failure-using-jquery
setTimeout ( function(){ get_the_latest_json(input,public_key) }, $.ajaxSetup().retryAfter );
//$.ajax(this);
return;
}
return;
}
if (xhr.status == 500) {
input.innerHTML='bad gateway1'
this.tryCount++;
if (this.tryCount <= this.retryLimit) {
//try again
$.ajax(this);
return;
}
return;
//handle error
//return
} else {
this.tryCount++;
if (this.tryCount <= this.retryLimit) {
//try again
//https://stackoverflow.com/questions/10024469/whats-the-best-way-to-retry-an-ajax-request-on-failure-using-jquery
setTimeout ( function(){ get_the_latest_json(input,public_key) }, $.ajaxSetup().retryAfter );
//$.ajax(this);
//return;
}
//return;
//handle error
//input.innerHTML='bad gateway2'
//return
}//else
} //error
});//ajax
}; //function
//function function2(b,data_sensor) {
// b =millisecondsToStr(new Date() -data_sensor[0]['timestamp'])+' ago'
//}
////
//function2($jsValue4.innerHTML,get_the_latest_json())
get_the_latest_json($jsValue_aster,public_aster)
get_the_latest_json($jsValue_bougainvillea,public_bougainvillea)
get_the_latest_json($jsValue_camellia,public_camellia)
get_the_latest_json($jsValue_daisy,public_daisy)
get_the_latest_json($jsValue_area51_redmud,public_area51_redmud)
get_the_latest_json($jsValue_concrete_curing,public_concrete_curing)
get_the_latest_json($jsValue_coal_tailings,public_coal_tailings)
get_the_latest_json($jsValue_compacted_redmud,public_compacted_redmud)
//get_the_latest_json($jsValue5,'RMxqjA6nRXfbm01raooM')
// $jsValue4.innerHTML =50
//get_the_latest_json().done(function (d){
//$jsValue4.innerHTML =millisecondsToStr(new Date() -data_sensor[0]['timestamp'])+' ago'
//})