Skip to content

Commit 9539cba

Browse files
committed
#173 Drone Series - Location Checker
1 parent 6df0870 commit 9539cba

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

controllers/DroneController.js

+21
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = {
2626
updateLocation,
2727
updateLocationBySerialNumber,
2828
createEmpty,
29+
checkLocation,
2930
};
3031

3132

@@ -129,6 +130,26 @@ function* updateLocationBySerialNumber(req, res) {
129130
res.io.emit('dronepositionupdate', drone);
130131
}
131132

133+
/**
134+
* check location
135+
* @param req
136+
* @param res
137+
*/
138+
function* checkLocation(req, res) {
139+
const lng = req.query.lng;
140+
const lat = req.query.lat;
141+
const nfzFields = helper.convertQueryFieldStringToArray(req.query.nfzFields);
142+
const nearDroneFields = helper.convertQueryFieldStringToArray(req.query.nearDroneFields);
143+
const returnNFZ = req.query.returnNFZ;
144+
const nfzLimit = req.query.nfzLimit;
145+
const nearDronesMaxDist = req.query.nearDronesMaxDist;
146+
const nearDronesLimit = req.query.nearDronesLimit;
147+
const ret = yield DroneService.checkLocation(lng, lat, returnNFZ, nfzFields,
148+
nfzLimit, nearDronesMaxDist, nearDroneFields, nearDronesLimit);
149+
res.json(ret);
150+
res.io.emit('checklocation', ret);
151+
}
152+
132153
/**
133154
* obsolete , post drone
134155
*/

routes.js

+6
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ module.exports = {
281281
method: 'currentLocations',
282282
},
283283
},
284+
'/drones/checklocation': {
285+
get: {
286+
controller: 'DroneController',
287+
method: 'checkLocation',
288+
},
289+
},
284290
'/drones': {
285291
get: {
286292
controller: 'DroneController',

services/DroneService.js

+83
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = {
3535
getSingle,
3636
updateLocation,
3737
updateLocationBySerialNumber,
38+
checkLocation,
3839
};
3940

4041

@@ -394,3 +395,85 @@ function* doUpdateLocation(entity, drone, returnNFZ, nfzFields, nfzLimit, nearDr
394395
}
395396
return ret;
396397
}
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

Comments
 (0)