Skip to content

Commit 6df0870

Browse files
authored
Merge pull request #172 from topcoderinc/6151_postionUpdateViaSerial
6151 postion update via serial
2 parents 7ef0d02 + 339c227 commit 6df0870

File tree

4 files changed

+411
-302
lines changed

4 files changed

+411
-302
lines changed

controllers/DroneController.js

+19
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = {
2424
getSingle,
2525
currentLocations,
2626
updateLocation,
27+
updateLocationBySerialNumber,
2728
createEmpty,
2829
};
2930

@@ -110,6 +111,24 @@ function* updateLocation(req, res) {
110111
res.io.emit('dronepositionupdate', drone);
111112
}
112113

114+
/**
115+
* Update a drone location by serial number
116+
* @param req
117+
* @param res
118+
*/
119+
function* updateLocationBySerialNumber(req, res) {
120+
const nfzFields = helper.convertQueryFieldStringToArray(req.query.nfzFields);
121+
const nearDroneFields = helper.convertQueryFieldStringToArray(req.query.nearDroneFields);
122+
const returnNFZ = req.query.returnNFZ;
123+
const nfzLimit = req.query.nfzLimit;
124+
const nearDronesMaxDist = req.query.nearDronesMaxDist;
125+
const nearDronesLimit = req.query.nearDronesLimit;
126+
const drone = yield DroneService.updateLocationBySerialNumber(req.params.sn, req.body, returnNFZ,
127+
nfzFields, nfzLimit, nearDronesMaxDist, nearDroneFields, nearDronesLimit);
128+
res.json(drone);
129+
res.io.emit('dronepositionupdate', drone);
130+
}
131+
113132
/**
114133
* obsolete , post drone
115134
*/

routes.js

+7
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@ module.exports = {
302302
},
303303
},
304304

305+
'/drones/position/:sn': {
306+
put: {
307+
controller: 'DroneController',
308+
method: 'updateLocationBySerialNumber',
309+
},
310+
},
311+
305312
'/provider/drones': {
306313
get: {
307314
controller: 'DroneController',

services/DroneService.js

+62-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ const joi = require('joi');
1414

1515
const models = require('../models');
1616

17-
const ObjectId = require('../datasource').getMongoose().Types.ObjectId;
18-
1917
const Drone = models.Drone;
2018
const DronePosition = models.DronePosition;
2119
const helper = require('../common/helper');
@@ -36,6 +34,7 @@ module.exports = {
3634
deleteSingle,
3735
getSingle,
3836
updateLocation,
37+
updateLocationBySerialNumber,
3938
};
4039

4140

@@ -225,7 +224,6 @@ function *getSingle(id) {
225224
return drone.toObject();
226225
}
227226

228-
229227
updateLocation.schema = {
230228
id: joi.string().required(),
231229
entity: joi.object().keys({
@@ -259,12 +257,70 @@ updateLocation.schema = {
259257
* @param nearDronesLimit {Number} limit of Drone to be returned
260258
* @returns {*}
261259
*/
262-
function *updateLocation(id, entity, returnNFZ, nfzFields, nfzLimit, nearDronesMaxDist, nearDroneFields, nearDronesLimit) {
260+
function* updateLocation(id, entity, returnNFZ, nfzFields, nfzLimit, nearDronesMaxDist, nearDroneFields, nearDronesLimit) {
263261
const drone = yield Drone.findOne({_id: id});
264262
if (!drone) {
265263
throw new errors.NotFoundError(`Current logged in provider does not have this drone , id = ${id}`);
266264
}
267265

266+
return yield doUpdateLocation(entity, drone, returnNFZ, nfzFields, nfzLimit, nearDronesMaxDist, nearDronesLimit, nearDroneFields);
267+
}
268+
269+
updateLocation.schema = {
270+
serialNumber: joi.string().required(),
271+
entity: joi.object().keys({
272+
lat: joi.number(), // Made these not required, we need to turn this into a general drone update endpoint / service (so we can update just status, speed, ...etc)
273+
lng: joi.number(),
274+
status: joi.string(),
275+
altitude: joi.number(),
276+
heading: joi.number(),
277+
speed: joi.number(),
278+
lastSeen: joi.string(),
279+
}).required(),
280+
returnNFZ: joi.boolean(),
281+
nfzFields: joi.array().items(joi.string()),
282+
nfzLimit: joi.limit(),
283+
nearDronesMaxDist: joi.number().min(0),
284+
nearDroneFields: joi.array().items(joi.string()),
285+
nearDronesLimit: joi.limit().default(1),
286+
287+
};
288+
289+
/**
290+
* update a drone location by serial number
291+
*
292+
* @param serialNumber
293+
* @param entity
294+
* @param returnNFZ {Boolean} True to return the NFZ.
295+
* @param nfzFields {Array} Fields of NFZ to be projected
296+
* @param nfzLimit {Number} limit of NFZ to be returned
297+
* @param nearDronesMaxDist {Number} Max dist to search nearest drones
298+
* @param nearDroneFields {Array} Fields of Drone to be projected
299+
* @param nearDronesLimit {Number} limit of Drone to be returned
300+
* @returns {*}
301+
*/
302+
function* updateLocationBySerialNumber(serialNumber, entity, returnNFZ, nfzFields, nfzLimit, nearDronesMaxDist, nearDroneFields, nearDronesLimit) {
303+
const drone = yield Drone.findOne({ serialNumber });
304+
if (!drone) {
305+
throw new errors.NotFoundError(`Current logged in provider does not have this drone , serialNumber = ${serialNumber}`);
306+
}
307+
308+
return yield doUpdateLocation(entity, drone, returnNFZ, nfzFields, nfzLimit, nearDronesMaxDist, nearDronesLimit, nearDroneFields);
309+
}
310+
311+
/**
312+
* Do actual location update for a specific drone
313+
* @param entity
314+
* @param drone The specific drone
315+
* @param returnNFZ
316+
* @param nfzFields
317+
* @param nfzLimit
318+
* @param nearDronesMaxDist
319+
* @param nearDronesLimit
320+
* @param nearDroneFields
321+
* @returns {*}
322+
*/
323+
function* doUpdateLocation(entity, drone, returnNFZ, nfzFields, nfzLimit, nearDronesMaxDist, nearDronesLimit, nearDroneFields) {
268324
entity.lng = entity.lng || drone.currentLocation[0];
269325
entity.lat = entity.lat || drone.currentLocation[1];
270326
drone.currentLocation = [entity.lng, entity.lat];
@@ -275,7 +331,7 @@ function *updateLocation(id, entity, returnNFZ, nfzFields, nfzLimit, nearDronesM
275331
drone.lastSeen = new Date();
276332
yield drone.save();
277333

278-
entity.droneId = id;
334+
entity.droneId = drone._id;
279335
yield DronePosition.create(entity);
280336

281337
const ret = drone.toObject();
@@ -313,7 +369,7 @@ function *updateLocation(id, entity, returnNFZ, nfzFields, nfzLimit, nearDronesM
313369
maxDistance: nearDronesMaxDist,
314370
spherical: true,
315371
query: {
316-
_id: { $ne: ObjectId(id) },
372+
_id: {$ne: drone._id},
317373
},
318374
};
319375
if (nearDronesLimit) {

0 commit comments

Comments
 (0)