-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathServiceObject.js
More file actions
1528 lines (1259 loc) · 49.4 KB
/
Copy pathServiceObject.js
File metadata and controls
1528 lines (1259 loc) · 49.4 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
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
Copyright 2015 CREATE-NET
Developed for COMPOSE project (compose-project.eu)
@author Luca Capra <luca.capra@create-net.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
(function(){
var DEBUG = false;
var d = function(m) { DEBUG === true || DEBUG > 10 && console.log(m); };
var solib = {};
solib.setup = function(compose) {
var Promise = compose.lib.Promise;
var ComposeError = compose.error.ComposeError;
var ValidationError = compose.error.ValidationError;
var Emitter = compose.lib.Client.Emitter;
/**
*
* @constructor
* */
var Subscription = function() {
if(this instanceof Subscription) {
var args = arguments[0] && typeof arguments[0] === 'object' ? arguments[0] : {};
this.initialize(args);
}
};
Subscription.prototype.__$container;
Subscription.prototype.container = function(o) {
this.__$container = o || this.__$container;
return this.__$container;
};
Subscription.prototype.initialize = function(object) {
for(var i in object) {
this[i] = object[i];
}
};
/*
*
* @param {boolean} asString Return as string if true, object otherwise
* @returns {Object|String}
*/
Subscription.prototype.toJson = function(asString) {
var json = compose.util.copyVal(this);
return asString ? JSON.stringify(json) : json;
};
Subscription.prototype.toString = function() {
return this.toJson(true);
};
/**
* Create a ServiceObject subscription
*
* @return {Promise} Promise callback with result
*/
Subscription.prototype.create = function() {
var me = this;
var so = me.container().container();
return new Promise(function(resolve, reject) {
var url = '/'+so.id+'/streams/'+ me.container().name
+'/subscriptions'+ (me.id ? '/'+me.id : '');
so.getClient().post(url, me.toJson(), function(data) {
me.id = data.id;
me.created = data.id;
resolve && resolve(me, me.container());
}, reject);
}).bind(so);
};
/**
* Update a ServiceObject subscription
*
* @return {Promise} Promise callback with result
*/
Subscription.prototype.update = function() {
var me = this;
var so = me.container().container();
return new Promise(function(resolve, reject) {
if(!me.id) {
throw new ComposeError("Subscription must have an id");
}
var url = '/subscriptions/'+ me.id;
so.getClient().put(url, me.toJson(), function(data) {
resolve(data);
}, reject);
}).bind(so);
};
/**
* Delete a ServiceObject subscription
*
* @return {Promise} Promise callback with result
*/
Subscription.prototype.delete = function() {
var me = this;
var so = me.container().container();
return new Promise(function(resolve, reject) {
if(!me.id) {
throw new ComposeError("Subscription must have an id");
}
var url = '/subscriptions/'+ me.id;
so.getClient().delete(url, null, function() {
var stream = me.container();
stream.getSubscriptions().remove(me);
resolve();
}, reject);
}).bind(so);
};
/**
*
* List of Subscriptions
*
* @constructor
* @augments WebObject.StreamList
*/
var SubscriptionList = function() {
compose.util.List.ArrayList.apply(this, arguments);
};
compose.util.extend(SubscriptionList, compose.util.List.ArrayList);
SubscriptionList.prototype.validate = function(obj) {
var sub = new Subscription(obj);
sub.container(this.container());
return sub;
};
/**
* Load all the ServiceObject subscriptions
*
* @return {Promise} Promise callback with result
*/
SubscriptionList.prototype.refresh = function() {
var me = this;
var so = me.container().container();
return new Promise(function(resolve, reject) {
var url = '/'+so.id+'/streams/'+ me.container().name +'/subscriptions/';
so.getClient().get(url, null, function(data) {
me.initialize(data.subscriptions);
resolve(me, me.container());
}, reject);
}).bind(so);
};
/**
* @constructor
* */
var Actuation = function() {
if(this instanceof Actuation) {
var args = arguments[0] ? arguments[0] : {};
this.initialize(args);
}
};
Actuation.prototype.__$container;
/**
*
* @param {Stream} Optional, a Stream object
* @returns {Stream} The parent object
*/
Actuation.prototype.container = function(o) {
this.__$container = o || this.__$container;
return this.__$container;
};
/**
* Set the values of the object passed as argument
*
* @param {Object} object A plain object with actuations properties
*/
Actuation.prototype.initialize = function(object) {
for(var i in object) {
this[i] = object[i];
}
};
/*
*
* @param {boolean} asString Return as string if true, object otherwise
* @returns {Object|String}
*/
Actuation.prototype.toJson = function(asString) {
var json = compose.util.copyVal(this);
return asString ? JSON.stringify(json) : json;
};
Actuation.prototype.toString = function() {
return this.toJson(true);
};
/**
* Invoke the ServiceObject action
* @param {mixed} body The body of the request
* @return {Promise} Promise callback with result
*/
Actuation.prototype.invoke = function(body) {
var me = this;
return new Promise(function(resolve, reject) {
var url = '/'+ me.container().id +'/actuations/'+ me.name;
me.container().getClient().post(url, body, function(data) {
me.id = data.id;
me.createdAt = data.createdAt;
resolve && resolve(me);
}, reject);
});
};
/**
* Reset the status of an actuation
* */
Actuation.prototype.reset = function() {
this.id = null;
this.createdAt = null;
};
/**
* Get the status of an actuation
*
* @return {Promise} Promise callback with result
*/
Actuation.prototype.status = function() {
var me = this;
return new Promise(function(resolve, reject) {
if(!me.id) {
throw new ComposeError("Actuation must have an id, have you invoked it first?");
}
var url = '/actuations/'+ me.id;
me.getClient().get(url, null, function(data) {
if(data.status === 'completed') {
me.reset();
}
resolve(data.status, data);
}, reject);
});
};
/**
* Cancel a launched actuation
*
* @return {Promise} Promise callback with result
*/
Actuation.prototype.cancel = function() {
var me = this;
return new Promise(function(resolve, reject) {
if(!me.id) {
throw new ComposeError("Actuation must have an id, have you invoked it first?");
}
var url = '/actuations/'+ me.id;
me.getClient().delete(url, null, function(data) {
if(data.status === 'cancelled') {
me.reset();
}
resolve(data.status, data);
}, reject);
});
};
/**
*
* List of Actuations
*
* @constructor
* @augments compose.util.List.ArrayList
*/
var ActuationList = function() {
compose.util.List.ArrayList.apply(this, arguments);
};
compose.util.extend(ActuationList, compose.util.List.ArrayList);
ActuationList.prototype.validate = function(obj) {
var action = new Actuation(obj);
action.container(this.container());
return action;
};
/**
* Load all the ServiceObject actuations
*
* @return {Promise} Promise callback with result
*/
ActuationList.prototype.refresh = function() {
var me = this;
return new Promise(function(resolve, reject) {
var url = '/'+me.container().id+'/actuations';
me.container().getClient().get(url, null, function(data) {
me.container().setActions(data.actions);
resolve(data.actions);
}, reject).bind(me.container());
});
};
/**
*
* @param {Array} data A list of values
* @returns {DataBag} An object containing the data
*/
var DataBag = function(data) {
this.__$list = (data && data.length) ? data : [];
this.__$container = null;
};
compose.util.extend(DataBag, compose.util.List.Enumerable);
/**
* @return {Stream} A reference to the source stream
* */
DataBag.prototype.container = function($__c) {
if($__c) this.__$container = $__c;
return this.__$container;
};
/**
* Return an object at a specific index
* */
DataBag.prototype.at = function(i) {
return this.get(i);
};
/**
* Return an object in the list. If index is not provided, the current cursor position will be used
*
* @param {Number} index Optional, index in the list
* @param {String} channel The channel name
* @param {mixed} defaultValue A default value if the requested channel is not available
*
* @returns {Object|mixed} A value set if index is provided, if channel is provided, its value
*/
DataBag.prototype.get = function(index, channel, defaultValue) {
if(arguments[0]*1 !== arguments[0]) {
return this.get(this.index(), arguments[0], arguments[1]);
}
defaultValue = (typeof defaultValue === 'undefined') ? null : defaultValue;
var list = this.getList();
var data = list[index];
if(data) {
var channels = data.channels;
if(!channels) return null;
if(channel && typeof channels[channel] !== 'undefined') {
return channels[channel]['current-value'];
}
// add a get function to retrieve a single value without the full json path
data.get = function(_channel, _defaultValue) {
_defaultValue = (typeof _defaultValue === 'undefined') ? null : _defaultValue;
if(_channel && data.channels[_channel] && typeof data.channels[_channel] !== 'undefined') {
return data.channels[_channel]['current-value'];
}
return _defaultValue;
};
// returns a simple js object with key-value pairs of data
data.asObject = function() {
var res = {};
for(var i in data.channels) {
(function(_i) {
res[_i] = data.channels[_i]['current-value'];
})(i);
}
return res;
};
return data;
}
return null;
};
/**
*
* A Stream object
*
* @constructor
* @param {Object} obj An object with the Stream properties
* @augments WebObject.Stream
*/
var Stream = function(obj) {
compose.lib.WebObject.Stream.apply(this, arguments);
this.initialize(obj);
};
compose.util.extend(Stream, compose.lib.WebObject.Stream);
Stream.prototype.__$subscriptions;
Stream.prototype.__$pubsub = null;
Stream.prototype.initialize = function(obj) {
obj = obj || {};
this.__$parent.initialize.call(this, obj);
var subscriptions = new SubscriptionList(obj.subscriptions || {});
subscriptions.container(this);
this.__$subscriptions = subscriptions;
this.__$emitter = new Emitter;
return this;
};
Stream.prototype.emitter = function() {
return this.__$emitter;
};
Stream.prototype.getSubscriptions = function() {
return this.__$subscriptions;
};
Stream.prototype.setSubscriptions = function(list) {
for(var i in list) {
this.getSubscriptions().add(list[i]);
}
return this;
};
/**
* Get a subscriptions by id
*
* @param {mixed} value The id value
* @param {mixed} key The key of the subscription object to match with `value`
*
* @return {Subscription} A subscription if found
*/
Stream.prototype.getSubscription = function(value, key) {
key = key || 'id';
return this.getSubscriptions().get(value, key);
};
/**
* Add a subscriptions
*
* @param {mixed} object An object with the Subscription properties
*
* @return {Subscription} A subscription object
*/
Stream.prototype.addSubscription = function(object) {
object = object || {};
return this.getSubscriptions().add(object);
};
/**
* Create a pubsub subscription for the stream
*
* @return {Promise} A promise for the subscription object creation
*/
Stream.prototype.subscribe = function(fn) {
var me = this;
if(!me.__$pubsub) {
me.__$pubsub = {
callback: 'pubsub',
destination: compose.config.apiKey
};
}
var listener = function(subscription) {
return new Promise(function(success, failure) {
try {
me.container().getClient().subscribe({
uuid: me.container().id + '.stream.' + me.name,
topic: 'stream',
stream: me,
emitter: me.emitter(),
onQueueData: function() {}
});
}
catch(e) {
failure(e);
return;
}
if(fn && typeof fn === 'function') {
me.on('data', fn);
}
success(subscription);
});
};
return this.getSubscriptions().refresh().then(function() {
var subscription = me.getSubscription(me.__$pubsub.callback, "callback");
if(!subscription) {
subscription = me.addSubscription(me.__$pubsub);
return subscription.create().then(listener);
}
else {
return listener(subscription);
}
});
};
/**
* Remove a pubsub subscription for the stream
*
* @param {Function} fn Callback to be called when data is received
* @return {Stream} The current stream
*/
Stream.prototype.unsubscribe = function(fn) {
var me = this;
this.getSubscriptions().refresh().then(function() {
var subscription = this.getSubscription(me.__$pubsub.callback, "callback");
var _clean = function() {
me.off('data');
me.__$pubsub = null;
};
if(!subscription) {
_clean();
}
else {
subscription.delete().then(_clean);
}
});
return this;
};
Stream.prototype.on = function(event, callback) {
if(event === 'data') {
compose.util.receiver.bind(this, this.container().id + '.stream.' + this.name);
}
this.emitter().on(event, callback);
return this;
};
Stream.prototype.off = function(event, callback) {
if(event === 'data') {
compose.util.receiver.unbind(this, this.container().id + '.stream.' + this.name);
}
this.emitter().off(event, callback);
};
/**
* Prepare a list of data values formatted to be sent to the backend
*
* @see Stream.push
*
* @param {Object} values A list of channels name and their values
* @param {Number|Date|String} lastUpdate A value rapresenting the lastUpdate for the data values
*
* @return {Stream} The current stream
*/
Stream.prototype.prepareData = function(values, lastUpdate) {
var me = this;
// default value
if(typeof lastUpdate === 'undefined') {
lastUpdate = new Date();
}
if(typeof lastUpdate === 'string' || typeof lastUpdate === 'number') {
lastUpdate = new Date(lastUpdate);
}
if(lastUpdate instanceof Date) {
lastUpdate = lastUpdate.getTime();
}
if(!lastUpdate) {
throw new compose.error.ValidationError("prepareData expect");
}
// convert from milliseconds to seconds
if(lastUpdate.toString().length === 13) {
lastUpdate = Math.floor(lastUpdate / 1000);
}
var data = {
channels: {},
lastUpdate: lastUpdate
};
if(typeof values === "object") {
for(var name in values) {
var channel = this.getChannel(name);
if (channel) {
data.channels[ name ] = data.channels[ name ] || {};
data.channels[ name ]['current-value'] = values[name];
}
else {
if(console && console.log)
console.log("Channel " + name + " is not available in stream " + me.name);
}
}
}
else {
var type = typeof values;
throw new compose.error.ValidationError("prepareData expect an `object` as first parameter but `" + type + "` has been provided");
}
return data;
};
/**
* Send data to a ServiceObject stream
*
* @return {Promise} Promise callback with result
*/
Stream.prototype.push = function(data, lastUpdate) {
var me = this;
return new Promise(function(resolve, reject) {
if(!me.container().id) {
throw new ComposeError("Missing ServiceObject id.");
}
if(!data) {
throw new ComposeError("Data for push has to be provided as first argument");
}
var values = me.prepareData(data, lastUpdate);
var url = '/' + me.container().id + '/streams/' + me.name;
me.container().getClient().put(url, values, resolve, reject);
});
};
/**
* Retieve data from a ServiceObject stream
*
* @param {String} timeModifier text, optional Possible values: lastUpdate, 1199192940 (time ago as timestamp)
* @return {Promise} Promise callback with result
*/
Stream.prototype.pull = function(timeModifier) {
var me = this;
timeModifier = timeModifier ? timeModifier : "";
return new Promise(function(resolve, reject) {
if(!me.container().id) {
throw new ComposeError("Missing ServiceObject id.");
}
var url = '/' + me.container().id + '/streams/' + me.name + '/' + timeModifier;
me.container().getClient().get(url, null, function(res) {
var data = [];
if(res && res.data) {
data = res.data;
}
var dataset = new DataBag(data);
dataset.container(me);
resolve && resolve(dataset, data);
}, reject);
});
};
/**
* Search data of a ServiceObject stream
*
* @param {Object} options
* @return {Promise} Promise callback with result
*/
Stream.prototype.search = function(options) {
var me = this;
return new Promise(function(resolve, reject) {
if(!me.container().id) {
throw new ComposeError("Missing ServiceObject id.");
}
if(!options) {
throw new ComposeError("No params provided for search");
}
var getFieldName = function(opts) {
var hasField = (typeof opts.field !== 'undefined' && opts.field),
hasChannel = (typeof opts.channel !== 'undefined'
&& opts.channel && me.getChannel(opts.channel));
if(!hasChannel && !hasField) {
throw new ComposeError("At least a valid `channel` or `field` properties has to be provided for numeric search");
}
if(hasField) {
return opts.field;
}
else if(hasChannel) {
return "channels." + opts.channel + ".current-value";
}
};
var hasProp = function(data, name) {
return 'undefined' !== data[name];
};
var params = {};
/**
{
"numericrange": true,
"rangefrom": 13,
"rangeto": 17,
"numericrangefield": "channels.age.current-value",
}
{
numeric: {
channel: 'name'
from: 1
to: 10
}
}
*/
var queryParams = options.numeric;
if(queryParams) {
params.numericrange = true;
params.numericrangefield = getFieldName(queryParams);
var hasFrom = hasProp(queryParams, "from"),
hasTo = hasProp(queryParams, "to");
if(!hasFrom && !hasTo) {
throw new ComposeError("At least one of `from` or `to` properties has to be provided for numeric range search");
}
if(hasFrom) {
params.rangefrom = queryParams.from;
}
if(hasTo) {
params.rangeto = queryParams.to;
}
}
/**
{
"timerange": true,
"rangefrom": 1396859660,
}
{
time: {
from: time
to: time
}
}
*/
var queryParams = options.time;
if(queryParams) {
params.timerange = true;
var hasFrom = hasProp(queryParams, "from"),
hasTo = hasProp(queryParams, "to");
if(!hasFrom && !hasTo) {
throw new ComposeError("At least one of `from` or `to` properties has to be provided for time range search");
}
// set defaults
// if from is not set, set to epoch
queryParams.from = queryParams.from || (new Date(0));
// if to is not set, set to now
queryParams.to = queryParams.to || (new Date());
// a timestamp is expected but try parsing other values too
var getTimeVal = function(val, label) {
var type = typeof val;
var date;
var err = false;
if(type === 'number') {
var d = new Date(val);
if(d.getTime() !== val) {
d = new Date(val * 1000);
if(d.getTime() !== val) {
err = true;
}
}
if(!err) {
date = d;
}
}
else if(type === "string") {
var d = new Date(val);
if(!d) {
err = true;
}
else{
date = d;
}
}
else if(val instanceof Date) {
date = val;
}
if(err || !date) {
throw new ComposeError("The value " + val + " for `" + label
+ "` cannot be parsed as a valid date");
}
return date.getTime();
};
if(hasFrom) {
params.rangefrom = getTimeVal(queryParams.from, 'timeRange.from');
}
if(hasTo) {
params.rangeto = getTimeVal(queryParams.to, 'timeRange.to');
}
}
/**
{
"match": true,
"matchfield": "channels.name.current-value",
"matchstring": "Peter John",
options.match : {
channel: '',
string: ''
}
}
*/
var queryParams = options.match;
if(queryParams) {
params.match = true;
params.matchfield = getFieldName(queryParams);
var hasString = hasProp(queryParams, "string");
if(!hasString) {
throw new ComposeError("A value for `string` property has to be provided for text based search");
}
params.string = queryParams.string;
}
var checkForLocationChannel = function() {
if(!me.getChannel('location')) {
throw new ComposeError("To use geospatial based search a `location` channel is required");
}
};
/**
{
"geoboundingbox": true,
"geoboxupperleftlon": 15.43,
"geoboxupperleftlat": 43.15,
"geoboxbottomrightlat": 47.15,
"geoboxbottomrightlon": 15.47
bbox: {
coords: [
{ latitude: '', longitude: ''}, // top position
{ latitude: '', longitude: ''} // bottom position
]
}
}
*/
var queryParams = options.bbox;
if(queryParams) {
checkForLocationChannel();
params.geoboundingbox = true;
var hasBbox = false;
if(queryParams.coords) {
// [toplat, toplon, bottomlat, bottomlon]
if(queryParams.coords instanceof Array && queryParams.coords.length === 4) {
params.geoboxupperleftlat = queryParams.coords[0];
params.geoboxupperleftlon = queryParams.coords[1];
params.geoboxbottomrightlat = queryParams.coords[2];
params.geoboxbottomrightlon = queryParams.coords[3];
hasBbox = true;
}
//[{lat, lon}, {lat, lon}]
if(queryParams.coords instanceof Array && queryParams.coords.length === 2) {
params.geoboxupperleftlat = queryParams.coords[0].lat || queryParams.coords[0].latitude;
params.geoboxupperleftlon = queryParams.coords[0].lon || queryParams.coords[0].longitude;
params.geoboxbottomrightlat = queryParams.coords[1].lat || queryParams.coords[1].latitude;
params.geoboxbottomrightlon = queryParams.coords[1].lon || queryParams.coords[1].longitude;
hasBbox = true;
}
}
if(!hasBbox) {
throw new ComposeError("The values provided for `coords` option are not valid");
}
}
else {
if(options.bbox) {
(console && console.warn) && console.warn("`bbox` and `distance` search are not compatible, `bbox` will be used");
}
/*
{
"geodistance": true,
"geodistancevalue": 300,
"pointlat": 43.15,
"pointlon": 15.43,
"geodistanceunit": "km"
}
{
distance: {
position: {latitude: '', longitude: ''}
// or
// position: [lat, lon]
value: 'val',
unit: 'km'
}
}
*/
var queryParams = options.distance;
if(queryParams) {
checkForLocationChannel();
params.geodistance = true;
if(queryParams.position) {
var position = queryParams.position;
var isArray = (position instanceof Array);
queryParams.lat = isArray ? position[0] : (position.latitude || position.lat);
queryParams.lon = isArray ? position[1] : (position.longitude || position.lon);
}