forked from xively/channel-viz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
follows.js
454 lines (390 loc) · 15.9 KB
/
follows.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
(function ( $ ){
/*
EXAMPLE CONFIGURATION
var defaultKey = 'fje329iun52ngtuijo2f4jeun432A', // Unique master Xively API key to be used as a default
defaultFeeds = [61916,12425,94322], // Comma separated array of Xively Feed ID numbers
applicationName = 'My Company\'s Application', // Replaces Xively logo in the header
dataDuration = '90days', // Default duration of data to be displayed // ref: https://xively.com/dev/docs/api/data/read/historical_data/
dataInterval = 10800, // Default interval for data to be displayed (in seconds)
dataColor = '0A1922', // CSS HEX value of color to represent data (omit leading #)
hideForm = 0;
*/
var defaultKey = '', // Unique master Xively API key to be used as a default
defaultFeeds = [], // Comma separated array of Xively Feed ID numbers
applicationName = '', // Replaces Xively logo in the header
dataDuration = '', // Default duration of data to be displayed // ref: https://xively.com/dev/docs/api/data/read/historical_data/
dataInterval = 0, // Default interval for data to be displayed (in seconds)
dataColor = '', // CSS HEX value of color to represent data (omit leading #)
hideForm = 0; // To hide input form use value of 1, otherwise set to 0
// Function Declarations
// URL Parameters
function getParam(key) {
var value = location.hash.match(new RegExp(key+'=([^&]*)'));
if(value) {
return value[1];
} else {
return "";
}
}
// Graph Annotations
function addAnnotation(force) {
if (messages.length > 0 && (force || Math.random() >= 0.95)) {
annotator.add(seriesData[2][seriesData[2].length-1].x, messages.shift());
}
}
// Add One (1) Day to Date Object
Date.prototype.addDays = function (d) {
if (d) {
var t = this.getTime();
t = t + (d * 86400000);
this.setTime(t);
}
};
// Subtract One (1) Day to Date Object
Date.prototype.subtractDays = function (d) {
if (d) {
var t = this.getTime();
t = t - (d * 86400000);
this.setTime(t);
}
};
// Parse Xively ISO Date Format to Date Object
Date.prototype.parseISO = function(iso){
var stamp= Date.parse(iso);
if(!stamp) throw iso +' Unknown date format';
return new Date(stamp);
}
// Set xively API Key
function setApiKey(key) {
xively.setKey(key);
}
function updateFeeds(feedId, datastreamIds, duration, interval) {
xively.feed.get(feedId, function(feedData) {
if(feedData.datastreams) {
if(datastreamIds == '' || !datastreamIds) {
feedData.datastreams.forEach(function(datastream) {
datastreamIds += datastream.id + " ";
});
}
feedData.datastreams.forEach(function(datastream) {
var now = new Date();
var then = new Date();
var updated = new Date;
updated = updated.parseISO(datastream.at);
var diff = null;
if(duration == '6hours') diff = 21600000;
if(duration == '1day') diff = 86400000;
if(duration == '1week') diff = 604800000;
if(duration == '1month') diff = 2628000000;
if(duration == '90days') diff = 7884000000;
then.setTime(now.getTime() - diff);
if(updated.getTime() > then.getTime()) {
if(datastreamIds && datastreamIds != '' && datastreamIds.indexOf(datastream.id) >= 0) {
xively.datastream.history(feedId, datastream.id, {duration: duration, interval: interval, limit: 1000}, function(datastreamData) {
var series = [];
var points = [];
// Create Datastream UI
$('.datastream-' + datastream.id).empty();
$('.datastream-' + datastream.id).remove();
$('#feed-' + feedId + ' .datastream.hidden').clone().appendTo('#feed-' + feedId + ' .datastreams').addClass('datastream-' + datastream.id).removeClass('hidden');
// Check for Datastream Tags
var tagsHtml = '';
if(datastreamData.tags) {
tagsHtml = '<div style="font-size: 14px;"><span class="radius secondary label">' + datastreamData.tags.join('</span> <span class="radius secondary label">') + '</span></div>';
} else {
tagsHtml = '';
}
// Fill Datastream UI with Data
$('#feed-' + feedId + ' .datastreams .datastream-' + datastream.id + ' .datastream-name').html(datastream.id);
$('#feed-' + feedId + ' .datastreams .datastream-' + datastream.id + ' .datastream-value').html(datastream.current_value);
// Include Datastream Unit (If Available)
if(datastream.unit) {
if(datastream.unit.symbol) {
$('#feed-' + feedId + ' .datastreams .datastream-' + datastream.id + ' .datastream-value').html(datastream.current_value + datastream.unit.symbol);
} else {
$('#feed-' + feedId + ' .datastreams .datastream-' + datastream.id + ' .datastream-value').html(datastream.current_value);
}
} else {
$('#feed-' + feedId + ' .datastreams .datastream-' + datastream.id + ' .datastream-value').html(datastream.current_value);
}
$('.datastream-' + datastream.id).removeClass('hidden');
// Historical Datapoints
if(datastreamData.datapoints) {
// Add Each Datapoint to Array
datastreamData.datapoints.forEach(function(datapoint) {
points.push({x: new Date(datapoint.at).getTime()/1000.0, y: parseFloat(datapoint.value)});
});
// Add Datapoints Array to Graph Series Array
series.push({
name: datastream.id,
data: points,
color: '#' + dataColor
});
// Initialize Graph DOM Element
$('#feed-' + feedId + ' .datastreams .datastream-' + datastream.id + ' .graph').attr('id', 'graph-' + feedId + '-' + datastream.id);
// Build Graph
var graph = new Rickshaw.Graph( {
element: document.querySelector('#graph-' + feedId + '-' + datastream.id),
width: 600,
height: 200,
renderer: 'line',
min: parseFloat(datastream.min_value) - .25*(parseFloat(datastream.max_value) - parseFloat(datastream.min_value)),
max: parseFloat(datastream.max_value) + .25*(parseFloat(datastream.max_value) - parseFloat(datastream.min_value)),
padding: {
top: 0.02,
right: 0.02,
bottom: 0.02,
left: 0.02
},
series: series
});
graph.render();
var ticksTreatment = 'glow';
// Define and Render X Axis (Time Values)
var xAxis = new Rickshaw.Graph.Axis.Time( {
graph: graph,
ticksTreatment: ticksTreatment
});
xAxis.render();
// Define and Render Y Axis (Datastream Values)
var yAxis = new Rickshaw.Graph.Axis.Y( {
graph: graph,
tickFormat: Rickshaw.Fixtures.Number.formatKMBT,
ticksTreatment: ticksTreatment
});
yAxis.render();
// Enable Datapoint Hover Values
var hoverDetail = new Rickshaw.Graph.HoverDetail({
graph: graph,
formatter: function(series, x, y) {
var swatch = '<span class="detail_swatch" style="background-color: ' + series.color + ' padding: 4px;"></span>';
var content = swatch + " " + parseFloat(y) + ' <br>';
return content;
}
});
$('#feed-' + feedId + ' .datastreams .datastream-' + datastream.id + ' .slider').prop('id', 'slider-' + feedId + '-' + datastream.id);
var slider = new Rickshaw.Graph.RangeSlider({
graph: graph,
element: $('#slider-' + feedId + '-' + datastream.id)
});
} else {
$('#feed-' + feedId + ' .datastreams .datastream-' + datastream.id + ' .graphWrapper').addClass('hidden');
}
});
} else {
console.log('Datastream not requested! (' + datastream.id + ')');
}
} else {
$('#feed-' + feedId + ' .datastreams .datastream-' + datastream.id + ' .graphWrapper').html('<div class="alert alert-box no-info">Sorry, this datastream does not have any associated data.</div>');
}
});
}
$('#loadingData').foundation('reveal', 'close');
});
}
function setFeeds(feeds) {
$('#welcome').addClass('hidden');
feeds.forEach(function(id) {
var thisFeedId, thisFeedDatastreams;
if(id.indexOf('!') > 0) {
thisFeedId = id.substring(0, id.indexOf('!'));
thisFeedDatastreams = id.substring(id.indexOf('!')+1).split('!');
} else {
thisFeedId = id;
}
id = thisFeedId;
if($('#feed-' + id)) {
$('#feed-' + id).remove();
}
xively.feed.history(id, { duration: "6hours", interval: 30 }, function (data) {
if(data.id == id) {
// Duplicate Example to Build Feed UI
$('#exampleFeed').clone().appendTo('#feeds').attr('id', 'feed-' + id).removeClass('hidden');
// ID
$('#feed-' + data.id + ' .title .value').html(data.title);
// Title
$('#feed-' + data.id + ' .id .value').html(data.id);
// Description
if(data.description) {
$('#feed-' + data.id + ' .description .value').html(data.description);
} else {
$('#feed-' + data.id + ' .description').addClass('hidden');
}
// Link
$('#feed-' + data.id + ' .link .value').html('<a href="https://xively.com/feeds/' + data.id + '/">View on Xively »</a>');
// Creator
var creator = /[^/]*$/.exec(data.creator)[0];
$('#feed-' + data.id + ' .creator .value').html('<a href="' + data.creator + '">' + creator + '</a>');
// Date Updated
$('#feed-' + data.id + ' .updated .value').html(data.updated);
// Tags
if(data.tags) {
$('#feed-' + data.id + ' .tags .value').html('<span class="radius secondary label">' + data.tags.join('</span> <span class="radius secondary label">') + '</span>');
} else {
$('#feed-' + data.id + ' .tags').addClass('hidden');
}
// Location
if(data.location) {
if(data.location.name || data.location.lat || data.location.ele || data.location.disposition) {
// Location Name
if(data.location.name) {
$('#feed-' + data.id + ' .location-name .value').html(data.location.name);
} else {
$('#feed-' + data.id + ' .location-name').addClass('hidden');
}
// Location Coordinates
if(data.location.lat && data.location.lon) {
$('#feed-' + data.id + ' .latitude .value').html(data.location.lat);
$('#feed-' + data.id + ' .longitude .value').html(data.location.lon);
} else {
$('#feed-' + data.id + ' .latitude').addClass('hidden');
$('#feed-' + data.id + ' .longitude').addClass('hidden');
}
// Location Elevation
if(data.location.ele) {
$('#feed-' + data.id + ' .elevation .value').html(data.location.ele);
} else {
$('#feed-' + data.id + ' .elevation').addClass('hidden');
}
// Location Disposition
if(data.location.disposition) {
$('#feed-' + data.id + ' .disposition .value').html(data.location.disposition);
} else {
$('#feed-' + data.id + ' .disposition').addClass('hidden');
}
// Location Map
if(data.location.lat && data.location.lon) {
$('#feed-' + data.id + ' .map .value').html('<a href="http://maps.google.com/maps?z=14&t=m&q=loc:' + data.location.lat + '+' + data.location.lon + '">View on Google Maps »</a>');
} else {
$('#feed-' + data.id + ' .map').addClass('hidden');
}
} else {
// Location Information Unavailable
$('#feed-' + data.id + ' .no-location').removeClass('hidden');
$('#feed-' + data.id + ' .location-name').addClass('hidden');
$('#feed-' + data.id + ' .latitude').addClass('hidden');
$('#feed-' + data.id + ' .longitude').addClass('hidden');
$('#feed-' + data.id + ' .elevation').addClass('hidden');
$('#feed-' + data.id + ' .disposition').addClass('hidden');
$('#feed-' + data.id + ' .map').addClass('hidden');
}
} else {
// Location Information Unavailable
$('#feed-' + data.id + ' .no-location').removeClass('hidden');
$('#feed-' + data.id + ' .location-name').addClass('hidden');
$('#feed-' + data.id + ' .latitude').addClass('hidden');
$('#feed-' + data.id + ' .longitude').addClass('hidden');
$('#feed-' + data.id + ' .elevation').addClass('hidden');
$('#feed-' + data.id + ' .disposition').addClass('hidden');
$('#feed-' + data.id + ' .map').addClass('hidden');
}
$('#feed-' + data.id + ' .duration-hour').click(function() {
$('#loadingData').foundation('reveal', 'open');
updateFeeds(data.id, thisFeedDatastreams, '6hours', 30);
return false;
});
$('#feed-' + data.id + ' .duration-day').click(function() {
$('#loadingData').foundation('reveal', 'open');
updateFeeds(data.id, thisFeedDatastreams, '1day', 60);
return false;
});
$('#feed-' + data.id + ' .duration-week').click(function() {
$('#loadingData').foundation('reveal', 'open');
updateFeeds(data.id, thisFeedDatastreams, '1week', 900);
return false;
});
$('#feed-' + data.id + ' .duration-month').click(function() {
$('#loadingData').foundation('reveal', 'open');
updateFeeds(data.id, thisFeedDatastreams, '1month', 1800);
return false;
});
$('#feed-' + data.id + ' .duration-90').click(function() {
$('#loadingData').foundation('reveal', 'open');
updateFeeds(data.id, thisFeedDatastreams, '90days', 10800);
return false;
});
// Handle Datastreams
if(dataDuration != '' && dataInterval != 0) {
updateFeeds(data.id, thisFeedDatastreams, dataDuration, dataInterval);
} else {
updateFeeds(data.id, thisFeedDatastreams, '6hours', 30);
}
} else {
// Duplicate Example to Build Feed UI
$('#exampleFeedNotFound').clone().appendTo('#feeds').attr('id', 'feed-' + id).removeClass('hidden');
$('#feed-' + id + ' h2').html(id);
}
});
});
}
// END Function Declarations
// BEGIN Initialization
if(hideForm == 1) {
$('#form').hide();
}
var today = new Date();
var yesterday = new Date(today.getTime()-1000*60*60*24*1);
var lastWeek = new Date(today.getTime()-1000*60*60*24*7);
var key = getParam('key');
var feedString = getParam('feeds');
// Check for Default Values
if(key == '' && defaultKey != '') {
key = defaultKey;
}
if(feedString == '' && defaultFeeds.toString(',') != '') {
feedString = defaultFeeds.toString(',');
}
if(applicationName != '') {
$('h1').html(applicationName).css('color', 'white');
document.title = applicationName + ' - Powered by Xively';
}
if(dataColor == '') {
dataColor = '0A1922';
}
var feeds = feedString.split(',');
$('#apiKeyInput').val(key);
$('#feedsInput').val(feedString);
$("#apiKeyInput").mouseover(function() {
console.log($("#apiKeyInput").prop('disabled'));
if($("#apiKeyInput").prop('disabled')) {
$("#apiKeyInput").prop('disabled', false);
}
});
if(key != '' && feedString != '') {
setApiKey($('#apiKeyInput').val());
feeds = $('#feedsInput').val().replace(/\s+/g, '').split(',');
setFeeds(feeds);
}
if(key != '') {
$("#apiKeyInput").prop('disabled', true);
}
$('#apiKeyInput').change(function() {
if($('#apiKeyInput').val() == '') {
$('#welcome').addClass('hidden');
$('#invalidApiKey').removeClass('hidden');
$('#validApiKey').addClass('hidden');
} else {
xively.setKey($('#apiKeyInput').val());
xively.feed.get(61916, function(data) {
if(data.id == 61916) {
$("#apiKeyInput").prop('disabled', true);
$('#welcome').addClass('hidden');
$('#validApiKey').removeClass('hidden');
$('#invalidApiKey').addClass('hidden');
} else {
$('#welcome').addClass('hidden');
$('#validApiKey').addClass('hidden');
$('#invalidApiKey').removeClass('hidden');
}
});
}
return false;
});
$('#setFeeds').click(function() {
setApiKey($('#apiKeyInput').val());
feeds = $('#feedsInput').val().replace(/\s+/g, '').split(',');
window.location = './index.html#key=' + $('#apiKeyInput').val() + '&feeds=' + $('#feedsInput').val();
return false;
});
// END Initialization
})( jQuery );