@@ -35,6 +35,7 @@ module.exports = {
35
35
getSingle,
36
36
updateLocation,
37
37
updateLocationBySerialNumber,
38
+ checkLocation,
38
39
} ;
39
40
40
41
@@ -394,3 +395,85 @@ function* doUpdateLocation(entity, drone, returnNFZ, nfzFields, nfzLimit, nearDr
394
395
}
395
396
return ret ;
396
397
}
398
+
399
+ checkLocation . schema = {
400
+ lng : joi . number ( ) . required ( ) ,
401
+ lat : joi . number ( ) . required ( ) ,
402
+ returnNFZ : joi . boolean ( ) ,
403
+ nfzFields : joi . array ( ) . items ( joi . string ( ) ) ,
404
+ nfzLimit : joi . limit ( ) ,
405
+ nearDronesMaxDist : joi . number ( ) . min ( 0 ) ,
406
+ nearDroneFields : joi . array ( ) . items ( joi . string ( ) ) ,
407
+ nearDronesLimit : joi . limit ( ) . default ( 1 ) ,
408
+ } ;
409
+
410
+ /**
411
+ * Check location
412
+ * @param entity should include lng and lat
413
+ * @param returnNFZ
414
+ * @param nfzFields
415
+ * @param nfzLimit
416
+ * @param nearDronesMaxDist
417
+ * @param nearDroneFields
418
+ * @param nearDronesLimit
419
+ * @returns {* }
420
+ */
421
+ function * checkLocation ( lng , lat , returnNFZ , nfzFields , nfzLimit , nearDronesMaxDist , nearDroneFields , nearDronesLimit ) {
422
+ const currentLocation = [ lng , lat ] ;
423
+ const ret = { } ;
424
+ // Check whether we need to return NFZ
425
+ if ( returnNFZ ) {
426
+ // We need to find active and match the time of NFZ
427
+ const criteria = {
428
+ isActive : true ,
429
+ matchTime : true ,
430
+ geometry : {
431
+ type : 'Point' ,
432
+ coordinates : currentLocation ,
433
+ } ,
434
+ projFields : [ 'circle' , 'description' , 'startTime' , 'endTime' , 'isPermanent' , 'mission' ] ,
435
+ } ;
436
+ // Add all fields except the polygon of NFZ.
437
+ if ( nfzFields && nfzFields . length > 0 ) {
438
+ criteria . projFields = nfzFields ;
439
+ }
440
+ // Add limit
441
+ if ( nfzLimit ) {
442
+ criteria . limit = nfzLimit ;
443
+ }
444
+ const searchedNFZs = yield NoFlyZoneService . search ( criteria ) ;
445
+ ret . noFlyZones = searchedNFZs . items ;
446
+ }
447
+ // Search the near drones within the nearDronesMaxDist
448
+ if ( nearDronesMaxDist ) {
449
+ const geoNearOption = {
450
+ near : {
451
+ type : 'Point' ,
452
+ coordinates : currentLocation ,
453
+ } ,
454
+ distanceField : 'distance' ,
455
+ maxDistance : nearDronesMaxDist ,
456
+ spherical : true ,
457
+ } ;
458
+ if ( nearDronesLimit ) {
459
+ geoNearOption . limit = nearDronesLimit ;
460
+ }
461
+ const aggregateOption = [
462
+ {
463
+ $geoNear : geoNearOption ,
464
+ } ,
465
+ ] ;
466
+ const projection = helper . convertArrayToProjectionObject ( nearDroneFields ) ;
467
+ if ( projection ) {
468
+ aggregateOption . push ( {
469
+ $project : projection ,
470
+ } ) ;
471
+ }
472
+ const nearestDrones = yield Drone . aggregate ( aggregateOption ) ;
473
+ ret . nearestDrones = _ . map ( nearestDrones , ( d ) => {
474
+ const transformFunc = Drone . schema . options . toObject . transform ;
475
+ return transformFunc ( d , d ) ;
476
+ } ) ;
477
+ }
478
+ return ret ;
479
+ }
0 commit comments