forked from fukudasjp/iota-starter-server-fm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudantHelper.js
770 lines (700 loc) · 21.5 KB
/
cloudantHelper.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
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
/**
* Copyright 2016 IBM Corp. All Rights Reserved.
*
* Licensed under the IBM License, a copy of which may be obtained at:
*
* http://www14.software.ibm.com/cgi-bin/weblap/lap.pl?li_formnum=L-DDIN-AEGGZJ&popup=y&title=IBM%20IoT%20for%20Automotive%20Sample%20Starter%20Apps%20%28Android-Mobile%20and%20Server-all%29
*
* You may not use this file except in compliance with the license.
*/
/*
* This cloudantHelper provides two capabilities:
* - A functions for operating with car-sharing application -- cloudantHelper
* - A deferred version of cloudant client -- CloudantDeferred
* - You can get the client by `cloudantHelper.getDeferredClient(dbname, [designDoc], [indices])`
*/
var _ = require('underscore');
var Q = require("q");
//var connectedDevicesCache = require('./workbenchLib').connectedDevicesCache;
var cloudantHelper = exports;
var cloudantCreds = VCAP_SERVICES.cloudantNoSQLDB[0].credentials;
var Cloudant = require('cloudant');
/*
* Cloudant library initialization option
*/
var CLOUDANT_OPTS = {url: cloudantCreds.url, plugin: 'retry', retryAttempts: 5, retryTimeout: 500 };
/*
* Settings for the device and reservation database
*/
var CLOUDANT_DB_NAME = 'mobilitystarterappdb';
var DB_UPDATE_INTERVAL = 6000; // period to update the DB from the memory image (mSec)
//
// ========= DEFERRED CLOUDANT CLIENT =========
//
/**
* Create a deferred Cloudant client
*/
cloudantHelper.getDeferredDB = function(dbname, designDoc, indexes){
var baseWrapper = new CloudantDeferred(dbname);
return new CloudantDeferred(dbname,
baseWrapper.updateDesignDoc(designDoc)
.then(function(){
return baseWrapper.updateIndexes(indexes);
}).then(function(){
return baseWrapper.db; // resolved as the base db at last
}));
};
function CloudantDeferred(dbname, db_promise){
this.dbname = dbname;
this.db = db_promise || this.ensureDB(dbname);
}
CloudantDeferred.prototype.ensureDB = function(dbName){
//connect to the database or create it if needed
var deferred = Q.defer();
Cloudant(CLOUDANT_OPTS, function(err, cloudant){
console.log('Connected to Cloudant');
cloudant.db.list(function(err, all_dbs){
if(err)
return deferred.reject(err);
if(all_dbs.indexOf(dbName) < 0){
console.log('Missing Cloudant DB ' + dbName + '. Creating...');
cloudant.db.create(dbName, function(err, body){
if(err)
return deferred.reject(err);
console.log(' created ' + dbName + '.');
return deferred.resolve(cloudant.use(dbName));
});
}else{
console.log('Found existing DB ' + dbName + '.');
return deferred.resolve(cloudant.use(dbName));
}
});
});
return deferred.promise;
}
CloudantDeferred.prototype.updateDesignDoc = function(designDoc){
if(!designDoc) return Q();
if(!designDoc._id) return Q.reject(new Error('Missing _id property in the design doc'));
return this.db.then(function(db){
var deferred = Q.defer();
db.get(designDoc._id, null, function(err, body){
if(!err){
designDoc._rev = body._rev;
db.insert(designDoc, null, function(err, body){
if (err){
console.error("!!!!!!!!error updating design doc: " + designDoc._id + " err = " + err + " body = " + body);
deferred.reject(err);
}else
deferred.resolve(body);
});
}else if(err.error == 'not_found'){
db.insert(designDoc, designDoc._id, function(err, body){
if (err){
console.error("!!!!!!!!error updating design doc: " + designDoc._id + " err = " + err + " body = " + body);
deferred.reject(err);
}else
deferred.resolve(db);
});
}else{
console.error('error on get design doc ' + err);
deferred.resolve(err);
}
});
return deferred.promise;
});
};
CloudantDeferred.prototype.updateIndexes = function(indexes){
if(!indexes || indexes.length == 0) return Q();
var self = this;
return this.db.then(function(db){
var deferred = Q.defer();
db.index(function(er, result){
if(er) return deferred.reject(er);
var existingIndexes = _.indexBy(result.indexes, 'name');
var ops = indexes.filter(function(index){
var existing = existingIndexes[index.name];
return !_.isEqual(index, existing);
}).map(function(index){
var deferredIndex = Q.defer();
console.log(' creating db index %s in DB %s...', index.name, self.dbname);
db.index(index, function(er, result){
if(er) return deferredIndex.reject(er);
deferredIndex.resolve(true);
})
return deferredIndex.promise;
});
(ops.length == 0 ? Q() : Q.all(ops)).then(function(){
deferred.resolve(db);
}, function(er){
deferred.reject(db);
}).done();
});
return deferred.promise;
});
};
CloudantDeferred.prototype.insert = function(doc, params){
return Q(this.db).then(function(db){
var deferred = Q.defer();
db.insert(doc, params, function(er, result){
if(er) return deferred.reject(er);
deferred.resolve(result);
});
return deferred.promise;
});
};
CloudantDeferred.prototype.destroy = function(docname, rev){
return this.db.then(function(db){
var deferred = Q.defer();
db.destroy(docname, rev, function(er, result){
if(er) return deferred.reject(er);
deferred.resolve(result);
});
return deferred.promise;
});
};
CloudantDeferred.prototype.get = function(docname, params){
return this.db.then(function(db){
var deferred = Q.defer();
db.insert(docname, params, function(er, result){
if(er) return deferred.reject(er);
deferred.resolve(result);
});
return deferred.promise;
});
};
CloudantDeferred.prototype.bulk = function(docs, params){
return this.db.then(function(db){
var deferred = Q.defer();
db.bulk(docs, params, function(er, result){
if(er) return deferred.reject(er);
deferred.resolve(result);
});
return deferred.promise;
});
};
CloudantDeferred.prototype.list = function(params){
return this.db.then(function(db){
var deferred = Q.defer();
db.list(params, function(er, result){
if(er) return deferred.reject(er);
deferred.resolve(result);
});
return deferred.promise;
});
};
CloudantDeferred.prototype.fetch = function(docnames, params){
return this.db.then(function(db){
var deferred = Q.defer();
params = params || {}
db.fetch(docnames, params, function(er, result){
if(er) return deferred.reject(er);
deferred.resolve(result);
});
return deferred.promise;
});
};
/*
* Cloudant Query
*/
CloudantDeferred.prototype.index = function(arg1){
return this.db.then(function(db){
var deferred = Q.defer();
if(arg1){
db.index(arg1, function(er, result){
if(er) return deferred.reject(er);
deferred.resolve(result);
});
}else{
db.index(function(er, result){
if(er) return deferred.reject(er);
deferred.resolve(result);
});
}
return deferred.promise;
});
};
CloudantDeferred.prototype.find = function(params){
return this.db.then(function(db){
var deferred = Q.defer();
db.find(params, function(er, result){
if(er) return deferred.reject(er);
deferred.resolve(result);
});
return deferred.promise;
});
};
/*
* Cloudant Search function
*
* Result:
* {
* rows: [
* {id: "_id", fields: ["field_value", ...], order: ['v1', ...]}, ...
* ],
* ranges?: {
* "faceted field name": {
* "facet name": "value", ...
* }, ...
* }
* }
*/
CloudantDeferred.prototype.search = function(designname, searchname, params){
designname = designname || this.dbname || null;
return this.db.then(function(db){
var deferred = Q.defer();
db.search(designname, searchname, params, function(er, result){
if(er) return deferred.reject(er);
deferred.resolve(result);
});
return deferred.promise;
});
};
/*
* Cloudant View function
*
* result: {
* rows: [
* {key: key, value: value},
* ..
* ]
* }
*/
CloudantDeferred.prototype.view = function(designname, viewname, params){
designname = designname || this.dbname || null;
return Q(this.db).then(function(db){
var deferred = Q.defer();
db.view(designname, viewname, params, function(er, result){
if(er) return deferred.reject(er);
deferred.resolve(result);
});
return deferred.promise;
});
}
//
//========= CAR-SHARING APPLICATION DB-RELATED IMPLEMENTATION =========
//
//init cloudant DB connection
var cloudant = Cloudant(CLOUDANT_OPTS);
var db = null;
cloudantHelper.getDBClient = function(){
var deferred = Q.defer();
Cloudant(CLOUDANT_OPTS, function(err,cloudant) {
if(!err)
deferred.resolve(cloudant.db.use(CLOUDANT_DB_NAME));
else
deferred.reject(err);
});
return deferred.promise;
};
//called periodically - scan devices and store data in DB
//a document per device
cloudantHelper.storeDeviceData = function(){
// var connectedDevices = connectedDevicesCache.getConnectedDevices();
// connectedDevices.forEach(function(device){
// cloudantHelper.createOrUpdateDevice(device);
// });
};
//update device in a DB document - if the device exists update data,
//if not, create a new document
cloudantHelper.createOrUpdateDevice = function(device){
if (!device) return; // no device....
var devID = device.deviceID;
var updateTime = device.lastUpdateTime;
if (updateTime > 0){
var db = cloudant.db.use(CLOUDANT_DB_NAME);
db.get(devID,null,function(err,body) {
if (!err){
if(!_.isEqual(body[updateTime], device)){
body[updateTime] = device;
db.insert(body,null,function(err, body){
if (err){
console.error("!!!!!!!!error inserting " + devID + " err = " + err + " body = " + body);
}
});
}
}else if (err.error == 'not_found'){
var deviceDetails = cloudantHelper.onGettingNewDeviceDetails(device);
if (deviceDetails){
cloudantHelper.createDevice(device, deviceDetails);
} else {
console.error('unregistered device: ' + devID);
}
}else{
console.error('error on get document ' + devID + " "+ err + ' body ' + body);
}
});
}
};
//insert a device into a DB document
cloudantHelper.createDevice = function(device, deviceDetails){
var deferred = Q.defer();
var devID = device.deviceID;
var updateTime = device.lastUpdateTime;
console.log('A new device ' + devID + ' is detected. Creating a device document...');
var doc = {};
doc.deviceDetails = deviceDetails;
if (updateTime > 0) doc[updateTime] = device;
var db = cloudant.db.use(CLOUDANT_DB_NAME);
db.insert(doc,devID,function(err, body) {
if (err) {
console.error("error inserting" + devID + " err = " + err + " body = " + body);
return deferred.reject(err);
}
deferred.resolve(body);
});
return deferred.promise;
};
//user call back to get the device details for an unknown device
//- when this return non-false object, the device will be registered
//- otherwise, the device won't be handled
//- this is overridden in _app.js for demo
cloudantHelper.onGettingNewDeviceDetails = function(device) {
return null;
};
// Get device additional info stored in the cloudant DB
cloudantHelper.getDeviceDetails = function(device){
// handle array
if (Array.isArray(device)){
return Q.all(device.map(cloudantHelper.getDeviceDetails));
}
// handle single device
if (!device) return Q(); // no device....
var devID = device.deviceID;
function cloneAndExtendDevice(body){
var r = _.clone(device);
_.extend(r, _.pick(body, function(value, key, object) {
return !key.startsWith('_');
}));
return r;
}
var deferred = Q.defer();
var db = cloudant.db.use(CLOUDANT_DB_NAME);
db.find({selector:{_id:devID}, fields:["deviceDetails"]}, function(er, result) {
if (er)
return deferred.reject(er);
if(!er && result.docs.length > 0 && !_.isEmpty(result.docs[0].deviceDetails)){
// found a deviceDetails document
deferred.resolve(cloneAndExtendDevice(result.docs[0].deviceDetails));
return;
}
// FALLBACK
// try to find a deviceDetails for the given device
var deviceDetails = cloudantHelper.onGettingNewDeviceDetails(device);
if (!deviceDetails){
console.log(' can\'t get device details for ' + devID + '. skipping...');
return deferred.reject();
}
// in case the device is missing, create the document
if (result.docs.length == 0){
cloudantHelper.createDevice(device, deviceDetails);
return deferred.resolve(cloneAndExtendDevice(deviceDetails));
}
// add new device details to existing document
console.log('Device details for ' + devID + ' is not in the device document. Migrating by adding it...');
db.get(devID,null,function(err,body){
if(!err){
body.deviceDetails = deviceDetails;
db.insert(body,null,function(err,body){
if(err){
console.error("!!!!!!!!error inserting" + devID + " err = " + err + " body = " + body);
deferred.reject();
}
else
deferred.resolve(cloneAndExtendDevice(deviceDetails));
});
}
else{
console.error('!!!!!!!device document ' + devID + 'is missing.');
deferred.reject();
}
});
});
return deferred.promise;
};
//Get device additional info stored in the cloudant DB
cloudantHelper.getExistingDeviceDetails = function(device){
// handle array
if (device && Array.isArray(device)){
return Q.all(device.map(cloudantHelper.getExistingDeviceDetails));
}
// handle single device
var deferred = Q.defer();
var devID = device ? device.deviceID : null;
if (devID) {
function cloneAndExtendDevice(body){
var r = _.clone(device);
_.extend(r, _.pick(body, function(value, key, object) { return !key.startsWith('_'); }));
return r;
}
var db = cloudant.db.use(CLOUDANT_DB_NAME);
db.get(devID, null, function(err, body) {
if (err) {
console.error("Error: " + err);
return deferred.reject(err);
}
var deviceDetails = body.deviceDetails || {};
deferred.resolve(cloneAndExtendDevice(deviceDetails));
});
} else {
function cloneAndExtendDevice(body){
var r = _.clone(device);
_.extend(r, _.pick(body, function(value, key, object) { return !key.startsWith('_'); }));
return r;
}
cloudantHelper.searchView('allDeviceDetials', {}).then(function(result) {
var deviceDetailsList = result.rows.map(function(row) {
var deviceDetails = _.clone(row.value);
deviceDetails.deviceID = row.id;
return deviceDetails;
});
deferred.resolve(deviceDetailsList);
}, function(err) {
console.error(err);
deferred.reject(err);
});
}
return deferred.promise;
};
//update device details in a DB document
cloudantHelper.updateDeviceDetails = function(device, deviceDetails){
var deferred = Q.defer();
if (!device) {
console.error("invalid parameter: device is not specified.");
return deferred.reject();
}
var devID = device.deviceID;
var db = cloudant.db.use(CLOUDANT_DB_NAME);
// get the current document for the device
db.get(devID, null, function(err, body) {
if (err) {
if (err.statusCode === 404) {
body = {_id: device.deviceID};
} else {
console.error("Error: " + err);
return deferred.reject(err);
}
}
// replace deviceDetails with given values and push it back to db
body.deviceDetails = deviceDetails;
db.insert(body, null, function(err, body) {
if (err) {
console.error("device cannot be updated: " + devID + " err = " + err + " body = " + body);
return deferred.reject(err);
}
return deferred.resolve(body);
});
});
return deferred.promise;
};
//update device details in a DB document
cloudantHelper.removeDeviceDetails = function(device){
var deferred = Q.defer();
if (!device) {
console.error("invalid parameter: device is not specified.");
return deferred.reject();
}
var db = cloudant.db.use(CLOUDANT_DB_NAME);
db.get(device.deviceID, null, function(err, body) {
if (err) {
console.error(err);
return deferred.reject(err);
}
db.destroy(body._id, body._rev, function(err, data) {
if (err) {
console.error(err);
return deferred.reject(err);
}
return deferred.resolve(data);
});
});
return deferred.promise;
}
//cleanup stale reservations
//- one still in 'active' whose dropOffTime is past
//- one still in 'driving' whose dropOffTime is more then 5 minutes before
cloudantHelper.cleanupStaleReservations = function(){
return cloudantHelper.searchView('activeReservations', {})
.then(function(result){
var now = Math.floor(Date.now()/1000); // in seconds
function isStale(res){
var pickupTime = parseInt(res.pickupTime);
var dropOffTime = parseInt(res.dropOffTime);
return (res.status == 'active' && now > Math.max(pickupTime, dropOffTime)) || // passed reservation
(res.status == 'driving' && now + 5*60 > Math.max(pickupTime, dropOffTime)); // passed reservation
}
var db = cloudant.db.use(CLOUDANT_DB_NAME);
var cancelActions = _.pluck(result.rows, 'value')
.filter(function(v){ return v.type == 'reservation';})
.filter(isStale)
.map(function(res){
var deferred = Q.defer();
console.log('Canceling stale reservation...: ' + JSON.stringify({
status: res.status,
dropOffTime: new Date(parseInt(res.dropOffTime)*1000).toISOString(),
}));
res.status = 'canceled'; // update the reservation status to canceled
db.insert(res,null,function(err, body){
if (err)
console.error("!!!!!!!!error canceling reservation " + res._id + " err = " + err + " body = " + body);
deferred.resolve(); // resolve anyway. will be retried again on failure
});
return deferred.promise;
});
if(cancelActions.length > 0){
return Q.all(cancelActions);
}
return Q(); // return resolved promise
})['catch'](function(err){
console.error('Got error on canceling stale reservations');
console.error(err);
});
}
//search Cloudant view async
cloudantHelper.searchView = function(viewName, opts){
var deferred = Q.defer();
var db = cloudant.db.use(CLOUDANT_DB_NAME);
db.view(CLOUDANT_DB_NAME, viewName, opts, function(err, body){
if(!err)
deferred.resolve(body);
else
deferred.reject(err);
});
return deferred.promise;
}
//search Cloudant index async
cloudantHelper.searchIndex = function(db, ddocName, indexName, opts){
var deferred = Q.defer();
ddocName = ddocName || CLOUDANT_DB_NAME;
db.search(ddocName, indexName, opts, function(err, result){
if (!err)
return deferred.resolve(result);
else
deferred.resolve(err);
})
return deferred.promise;
}
/*
* The following characters require escaping if you want to search on them: + - && || ! ( ) { } [ ] ^ " ~ * ? : \ /
* Escape these with a preceding backslash character.
*/
cloudantHelper.escapeKey = function(key) {
return key.replace(/\\/g, '\\\\')
.replace(/\+/g, '\\+')
.replace(/\-/g, '\\-')
.replace(/&&/g, '\\&&')
.replace(/\|\|/g, '\\||')
.replace(/!/g, '\\!')
.replace(/\(/g, '\\(')
.replace(/\)/g, '\\)')
.replace(/\{/g, '\\{')
.replace(/\}/g, '\\}')
.replace(/\[/g, '\\[')
.replace(/\]/g, '\\]')
.replace(/\^/g, '\\^')
.replace(/"/g, '\\"')
.replace(/~/g, '\\~')
.replace(/\*/g, '\\*')
.replace(/\?/g, '\\?')
.replace(/:/g, '\\:')
.replace(/\//g, '\\/')
;
}
//create a new Cloudant connection and returns a deferred DB
cloudantHelper.getDB = function(dbName, designDoc, indices){
return cloudantHelper.getDeferredDB(dbName, designDoc, indices).db;
}
//
// INTIIALIZE THE MAIN `mobilitystarterappdb`
//
var locationMap = function (doc) {
var lastUpdate = doc[Object.keys(doc)[Object.keys(doc).length - 1]];
if(lastUpdate && lastUpdate.deviceID){
var key = [lastUpdate.lat, lastUpdate.lng];
emit(key, lastUpdate);
}
};
var carLocationQueryIndex = function(doc){
var lastUpdate = doc[Object.keys(doc)[Object.keys(doc).length - 1]];
if(lastUpdate && lastUpdate.deviceID){
index('lat', parseFloat(lastUpdate.lat), {"store": true});
index('lng', parseFloat(lastUpdate.lng), {"store": true});
index('name', (doc.deviceDetails && doc.deviceDetails.name), {"store": true});
}
};
var carLocationGeoIndex = function(doc) {
var lastUpdate = doc[Object.keys(doc)[Object.keys(doc).length - 1]];
if(lastUpdate && lastUpdate.deviceID){
var geometry = {
type: "Point",
coordinates: [parseFloat(lastUpdate.lng), parseFloat(lastUpdate.lat)]
};
st_index(geometry);
}
};
var allDeviceDetailsMap = function(doc) {
if(doc.deviceDetails){
emit("deviceDetails", doc.deviceDetails);
}
};
var privateDevicesMap = function(doc) {
if(doc.deviceDetails && doc.deviceDetails.ownerId){
emit(doc._id, {deviceId: doc._id, ownerId: doc.deviceDetails.ownerId});
}
};
var activeReservationMap = function(doc) {
if(doc.type == "reservation" && (doc.status == "active" || doc.status == "driving")){
emit(doc.carId, {user: doc.userId, status: doc.status, pickupTime: parseInt(doc.pickupTime), dropOffTime: parseInt(doc.dropOffTime)});
emit(doc.userId, doc);
}
};
var closedReservationMap = function(doc) {
if(doc.type == "reservation" && doc.status == "closed"){
emit(doc.carId, {user: doc.userId, status: doc.status});
emit(doc.userId, doc);
}
};
var allReservationMap = function(doc) {
if(doc.type == "reservation"){
emit(doc.carId, {user: doc.userId, status: doc.status});
emit(doc.userId, doc);
}
};
var designDoc = {
_id: '_design/' + CLOUDANT_DB_NAME,
views: {
location: {
map: locationMap.toString()
},
allDeviceDetials: {
map: allDeviceDetailsMap.toString()
},
privateDevices: {
map: privateDevicesMap.toString()
},
activeReservations: {
map: activeReservationMap.toString()
},
closedReservations: {
map: closedReservationMap.toString()
},
allReservations: {
map: allReservationMap.toString()
}
},
indexes: {
location: {
index: carLocationQueryIndex.toString()
},
},
st_indexes: {
geoindex: {
index: carLocationGeoIndex.toString()
}
}
};
//init DB and start timer session
(function(){
cloudantHelper.ddb = cloudantHelper.getDeferredDB(CLOUDANT_DB_NAME, designDoc);
cloudantHelper.db = db = cloudantHelper.ddb.db;
setInterval(cloudantHelper.storeDeviceData, DB_UPDATE_INTERVAL);
}());