-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson.js
644 lines (562 loc) · 19.4 KB
/
json.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
(function () {
var jsonDatasource = function (settings, updateCallback) {
var self = this;
var updateTimer = null;
var currentSettings = settings;
var errorStage = 0; // 0 = try standard request
// 1 = try JSONP
// 2 = try thingproxy.freeboard.io
var lockErrorStage = false;
function updateRefresh(refreshTime) {
if (updateTimer) {
clearInterval(updateTimer);
}
updateTimer = setInterval(function () {
self.updateNow();
}, refreshTime);
}
updateRefresh(currentSettings.refresh * 1000);
this.updateNow = function () {
if ((errorStage > 1 && !currentSettings.use_thingproxy) || errorStage > 2) // We've tried everything, let's quit
{
return; // TODO: Report an error
}
var requestURL = currentSettings.url;
if (errorStage == 2 && currentSettings.use_thingproxy) {
requestURL = (location.protocol == "https:" ? "https:" : "http:") + "//thingproxy.freeboard.io/fetch/" + encodeURI(currentSettings.url);
}
var body = currentSettings.body;
// Can the body be converted to JSON?
if (body) {
try {
body = JSON.parse(body);
}
catch (e) {
}
}
$.ajax({
url: requestURL,
dataType: (errorStage == 1) ? "JSONP" : "JSON",
type: currentSettings.method || "GET",
data: body,
beforeSend: function (xhr) {
try {
_.each(currentSettings.headers, function (header) {
var name = header.name;
var value = header.value;
if (!_.isUndefined(name) && !_.isUndefined(value)) {
xhr.setRequestHeader(name, value);
}
});
}
catch (e) {
}
},
success: function (data) {
lockErrorStage = true;
updateCallback(data);
},
error: function (xhr, status, error) {
if (!lockErrorStage) {
// TODO: Figure out a way to intercept CORS errors only. The error message for CORS errors seems to be a standard 404.
errorStage++;
self.updateNow();
}
}
});
}
this.onDispose = function () {
clearInterval(updateTimer);
updateTimer = null;
}
this.onSettingsChanged = function (newSettings) {
lockErrorStage = false;
errorStage = 0;
currentSettings = newSettings;
updateRefresh(currentSettings.refresh * 1000);
self.updateNow();
}
};
freeboard.loadDatasourcePlugin({
type_name: "JSON",
settings: [
{
name: "url",
display_name: "URL",
type: "text"
},
{
name: "use_thingproxy",
display_name: "Try thingproxy",
description: 'A direct JSON connection will be tried first, if that fails, a JSONP connection will be tried. If that fails, you can use thingproxy, which can solve many connection problems to APIs. <a href="https://github.com/Freeboard/thingproxy" target="_blank">More information</a>.',
type: "boolean",
default_value: true
},
{
name: "refresh",
display_name: "Refresh Every",
type: "number",
suffix: "seconds",
default_value: 5
},
{
name: "method",
display_name: "Method",
type: "option",
options: [
{
name: "GET",
value: "GET"
},
{
name: "POST",
value: "POST"
},
{
name: "PUT",
value: "PUT"
},
{
name: "DELETE",
value: "DELETE"
}
]
},
{
name: "body",
display_name: "Body",
type: "text",
description: "The body of the request. Normally only used if method is POST"
},
{
name: "headers",
display_name: "Headers",
type: "array",
settings: [
{
name: "name",
display_name: "Name",
type: "text"
},
{
name: "value",
display_name: "Value",
type: "text"
}
]
}
],
newInstance: function (settings, newInstanceCallback, updateCallback) {
newInstanceCallback(new jsonDatasource(settings, updateCallback));
}
});
var openWeatherMapDatasource = function (settings, updateCallback) {
var self = this;
var updateTimer = null;
var currentSettings = settings;
function updateRefresh(refreshTime) {
if (updateTimer) {
clearInterval(updateTimer);
}
updateTimer = setInterval(function () {
self.updateNow();
}, refreshTime);
}
function toTitleCase(str) {
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
updateRefresh(currentSettings.refresh * 1000);
this.updateNow = function () {
$.ajax({
url: "http://api.openweathermap.org/data/2.5/weather?APPID="+currentSettings.api_key+"&q=" + encodeURIComponent(currentSettings.location) + "&units=" + currentSettings.units,
dataType: "JSONP",
success: function (data) {
// Rejigger our data into something easier to understand
var newData = {
place_name: data.name,
sunrise: (new Date(data.sys.sunrise * 1000)).toLocaleTimeString(),
sunset: (new Date(data.sys.sunset * 1000)).toLocaleTimeString(),
conditions: toTitleCase(data.weather[0].description),
current_temp: data.main.temp,
high_temp: data.main.temp_max,
low_temp: data.main.temp_min,
pressure: data.main.pressure,
humidity: data.main.humidity,
wind_speed: data.wind.speed,
wind_direction: data.wind.deg
};
updateCallback(newData);
},
error: function (xhr, status, error) {
}
});
}
this.onDispose = function () {
clearInterval(updateTimer);
updateTimer = null;
}
this.onSettingsChanged = function (newSettings) {
currentSettings = newSettings;
self.updateNow();
updateRefresh(currentSettings.refresh * 1000);
}
};
freeboard.loadDatasourcePlugin({
type_name: "openweathermap",
display_name: "Open Weather Map API",
settings: [
{
name: "api_key",
display_name: "API Key",
type: "text",
description: "Your personal API Key from Open Weather Map"
},
{
name: "location",
display_name: "Location",
type: "text",
description: "Example: London, UK"
},
{
name: "units",
display_name: "Units",
type: "option",
default: "imperial",
options: [
{
name: "Imperial",
value: "imperial"
},
{
name: "Metric",
value: "metric"
}
]
},
{
name: "refresh",
display_name: "Refresh Every",
type: "number",
suffix: "seconds",
default_value: 5
}
],
newInstance: function (settings, newInstanceCallback, updateCallback) {
newInstanceCallback(new openWeatherMapDatasource(settings, updateCallback));
}
});
var dweetioDatasource = function (settings, updateCallback) {
var self = this;
var currentSettings = settings;
function onNewDweet(dweet) {
updateCallback(dweet);
}
this.updateNow = function () {
dweetio.get_latest_dweet_for(currentSettings.thing_id, function (err, dweet) {
if (err) {
//onNewDweet({});
}
else {
onNewDweet(dweet[0].content);
}
});
}
this.onDispose = function () {
}
this.onSettingsChanged = function (newSettings) {
dweetio.stop_listening_for(currentSettings.thing_id);
currentSettings = newSettings;
dweetio.listen_for(currentSettings.thing_id, function (dweet) {
onNewDweet(dweet.content);
});
}
self.onSettingsChanged(settings);
};
freeboard.loadDatasourcePlugin({
"type_name": "dweet_io",
"display_name": "Dweet.io",
"external_scripts": [
"http://dweet.io/client/dweet.io.min.js"
],
"settings": [
{
name: "thing_id",
display_name: "Thing Name",
"description": "Example: salty-dog-1",
type: "text"
}
],
newInstance: function (settings, newInstanceCallback, updateCallback) {
newInstanceCallback(new dweetioDatasource(settings, updateCallback));
}
});
var playbackDatasource = function (settings, updateCallback) {
var self = this;
var currentSettings = settings;
var currentDataset = [];
var currentIndex = 0;
var currentTimeout;
function moveNext() {
if (currentDataset.length > 0) {
if (currentIndex < currentDataset.length) {
updateCallback(currentDataset[currentIndex]);
currentIndex++;
}
if (currentIndex >= currentDataset.length && currentSettings.loop) {
currentIndex = 0;
}
if (currentIndex < currentDataset.length) {
currentTimeout = setTimeout(moveNext, currentSettings.refresh * 1000);
}
}
else {
updateCallback({});
}
}
function stopTimeout() {
currentDataset = [];
currentIndex = 0;
if (currentTimeout) {
clearTimeout(currentTimeout);
currentTimeout = null;
}
}
this.updateNow = function () {
stopTimeout();
$.ajax({
url: currentSettings.datafile,
dataType: (currentSettings.is_jsonp) ? "JSONP" : "JSON",
success: function (data) {
if (_.isArray(data)) {
currentDataset = data;
}
else {
currentDataset = [];
}
currentIndex = 0;
moveNext();
},
error: function (xhr, status, error) {
}
});
}
this.onDispose = function () {
stopTimeout();
}
this.onSettingsChanged = function (newSettings) {
currentSettings = newSettings;
self.updateNow();
}
};
freeboard.loadDatasourcePlugin({
"type_name": "playback",
"display_name": "Playback",
"settings": [
{
"name": "datafile",
"display_name": "Data File URL",
"type": "text",
"description": "A link to a JSON array of data."
},
{
name: "is_jsonp",
display_name: "Is JSONP",
type: "boolean"
},
{
"name": "loop",
"display_name": "Loop",
"type": "boolean",
"description": "Rewind and loop when finished"
},
{
"name": "refresh",
"display_name": "Refresh Every",
"type": "number",
"suffix": "seconds",
"default_value": 5
}
],
newInstance: function (settings, newInstanceCallback, updateCallback) {
newInstanceCallback(new playbackDatasource(settings, updateCallback));
}
});
var clockDatasource = function (settings, updateCallback) {
var self = this;
var currentSettings = settings;
var timer;
function stopTimer() {
if (timer) {
clearTimeout(timer);
timer = null;
}
}
function updateTimer() {
stopTimer();
timer = setInterval(self.updateNow, currentSettings.refresh * 1000);
}
this.updateNow = function () {
var date = new Date();
var data = {
numeric_value: date.getTime(),
full_string_value: date.toLocaleString(),
date_string_value: date.toLocaleDateString(),
time_string_value: date.toLocaleTimeString(),
date_object: date
};
updateCallback(data);
}
this.onDispose = function () {
stopTimer();
}
this.onSettingsChanged = function (newSettings) {
currentSettings = newSettings;
updateTimer();
}
updateTimer();
};
freeboard.loadDatasourcePlugin({
"type_name": "clock",
"display_name": "Clock",
"settings": [
{
"name": "refresh",
"display_name": "Refresh Every",
"type": "number",
"suffix": "seconds",
"default_value": 1
}
],
newInstance: function (settings, newInstanceCallback, updateCallback) {
newInstanceCallback(new clockDatasource(settings, updateCallback));
}
});
freeboard.loadDatasourcePlugin({
// **type_name** (required) : A unique name for this plugin. This name should be as unique as possible to avoid collisions with other plugins, and should follow naming conventions for javascript variable and function declarations.
"type_name" : "meshblu",
// **display_name** : The pretty name that will be used for display purposes for this plugin. If the name is not defined, type_name will be used instead.
"display_name": "Octoblu",
// **description** : A description of the plugin. This description will be displayed when the plugin is selected or within search results (in the future). The description may contain HTML if needed.
"description" : "app.octoblu.com",
// **external_scripts** : Any external scripts that should be loaded before the plugin instance is created.
"external_scripts" : [
"http://meshblu.octoblu.com/js/meshblu.js"
],
// **settings** : An array of settings that will be displayed for this plugin when the user adds it.
"settings" : [
{
// **name** (required) : The name of the setting. This value will be used in your code to retrieve the value specified by the user. This should follow naming conventions for javascript variable and function declarations.
"name" : "uuid",
// **display_name** : The pretty name that will be shown to the user when they adjust this setting.
"display_name" : "UUID",
// **type** (required) : The type of input expected for this setting. "text" will display a single text box input. Examples of other types will follow in this documentation.
"type" : "text",
// **default_value** : A default value for this setting.
"default_value": "device uuid",
// **description** : Text that will be displayed below the setting to give the user any extra information.
"description" : "your device UUID",
// **required** : Set to true if this setting is required for the datasource to be created.
"required" : true
},
{
// **name** (required) : The name of the setting. This value will be used in your code to retrieve the value specified by the user. This should follow naming conventions for javascript variable and function declarations.
"name" : "token",
// **display_name** : The pretty name that will be shown to the user when they adjust this setting.
"display_name" : "Token",
// **type** (required) : The type of input expected for this setting. "text" will display a single text box input. Examples of other types will follow in this documentation.
"type" : "text",
// **default_value** : A default value for this setting.
"default_value": "device token",
// **description** : Text that will be displayed below the setting to give the user any extra information.
"description" : "your device TOKEN",
// **required** : Set to true if this setting is required for the datasource to be created.
"required" : true
},
{
// **name** (required) : The name of the setting. This value will be used in your code to retrieve the value specified by the user. This should follow naming conventions for javascript variable and function declarations.
"name" : "server",
// **display_name** : The pretty name that will be shown to the user when they adjust this setting.
"display_name" : "Server",
// **type** (required) : The type of input expected for this setting. "text" will display a single text box input. Examples of other types will follow in this documentation.
"type" : "text",
// **default_value** : A default value for this setting.
"default_value": "meshblu.octoblu.com",
// **description** : Text that will be displayed below the setting to give the user any extra information.
"description" : "your server",
// **required** : Set to true if this setting is required for the datasource to be created.
"required" : true
},
{
// **name** (required) : The name of the setting. This value will be used in your code to retrieve the value specified by the user. This should follow naming conventions for javascript variable and function declarations.
"name" : "port",
// **display_name** : The pretty name that will be shown to the user when they adjust this setting.
"display_name" : "Port",
// **type** (required) : The type of input expected for this setting. "text" will display a single text box input. Examples of other types will follow in this documentation.
"type" : "number",
// **default_value** : A default value for this setting.
"default_value": 80,
// **description** : Text that will be displayed below the setting to give the user any extra information.
"description" : "server port",
// **required** : Set to true if this setting is required for the datasource to be created.
"required" : true
}
],
// **newInstance(settings, newInstanceCallback, updateCallback)** (required) : A function that will be called when a new instance of this plugin is requested.
// * **settings** : A javascript object with the initial settings set by the user. The names of the properties in the object will correspond to the setting names defined above.
// * **newInstanceCallback** : A callback function that you'll call when the new instance of the plugin is ready. This function expects a single argument, which is the new instance of your plugin object.
// * **updateCallback** : A callback function that you'll call if and when your datasource has an update for freeboard to recalculate. This function expects a single parameter which is a javascript object with the new, updated data. You should hold on to this reference and call it when needed.
newInstance : function(settings, newInstanceCallback, updateCallback)
{
// myDatasourcePlugin is defined below.
newInstanceCallback(new meshbluSource(settings, updateCallback));
}
});
// ### Datasource Implementation
//
// -------------------
// Here we implement the actual datasource plugin. We pass in the settings and updateCallback.
var meshbluSource = function(settings, updateCallback)
{
// Always a good idea...
var self = this;
// Good idea to create a variable to hold on to our settings, because they might change in the future. See below.
var currentSettings = settings;
/* This is some function where I'll get my data from somewhere */
function getData()
{
var conn = skynet.createConnection({
"uuid": currentSettings.uuid,
"token": currentSettings.token,
"server": currentSettings.server,
"port": currentSettings.port
});
conn.on('ready', function(data){
conn.on('message', function(message){
var newData = message;
updateCallback(newData);
});
});
}
// **onSettingsChanged(newSettings)** (required) : A public function we must implement that will be called when a user makes a change to the settings.
self.onSettingsChanged = function(newSettings)
{
// Here we update our current settings with the variable that is passed in.
currentSettings = newSettings;
}
// **updateNow()** (required) : A public function we must implement that will be called when the user wants to manually refresh the datasource
self.updateNow = function()
{
// Most likely I'll just call getData() here.
getData();
}
// **onDispose()** (required) : A public function we must implement that will be called when this instance of this plugin is no longer needed. Do anything you need to cleanup after yourself here.
self.onDispose = function()
{
//conn.close();
}
// Here we call createRefreshTimer with our current settings, to kick things off, initially. Notice how we make use of one of the user defined settings that we setup earlier.
// createRefreshTimer(currentSettings.refresh_time);
}
}());