Skip to content

Commit

Permalink
Merge pull request #17 from oroneta/feature_task_replace_flight_type
Browse files Browse the repository at this point in the history
Feature task replace flight type
  • Loading branch information
ZhengLinLei authored May 8, 2024
2 parents cc3dd97 + eab2f0d commit ad1c6f8
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 18 deletions.
9 changes: 9 additions & 0 deletions src/drone-api/controllers/Drones.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ export default class _DroneController {
return true;
}

static async updateAlarmStatus (dic, auth_code, alarm_status) {
await DroneModel.updateOne({
dic,
auth_code
}, {
alarm_status
});
return true;
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default async (db) => {
last_update: new Date().getTime()
},
status: true,
flight: Buffer.from([]),
flight: [[39.455109, -0.318429], [39.446593, -0.310454, 39.443292, -0.321233], [39.447712, -0.326340], [39.455544, -0.327053]],
flight_priority: 0,
flight_altitude: 0,
alarm_status: false,
Expand Down
4 changes: 2 additions & 2 deletions src/drone-api/models/Drones.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ const droneSchema = new mongoose.Schema({
default: false
},
flight: {
type: Buffer,
type: Object,
required: true,
default: 0
default: []
},
flight_priority: {
type: Number,
Expand Down
70 changes: 59 additions & 11 deletions src/drone-api/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ router.post('/routes/:dic', async (req, res) => {
if (jsonStr.status == 1) {
// Insrt drone with all data, and if already exist update the data
let drone = await _DroneController.findDroneExist(req.params.dic, auth);


route = JSON.stringify(route);
if (drone.length == 0) {
await _DroneController.insertOneWithFlight(req.params.dic, auth, route, 1, jsonStr.dangerous_level);
}
Expand Down Expand Up @@ -230,22 +231,25 @@ router.get('/alarm/:dic', _Middleware.checkAuth, async (req, res) => {
// Get the status of every drone
for (let i = 0; i < droneList.length; i++) {
drone = await _DroneController.findDroneExist(droneList[i], authList[i]);
alarm = await _ImageController.findImageData(drone[0].alarm_data);

if (drone.length == 0) {
// If not exist, continue to the next drone
continue;
}

result[droneList[i]] = (drone[0].alarm_status
?
{
status: 1,
expire_date: alarm[0].expire_date,
image_path: `/alarm/image/${drone[0].alarm_data}`
} : {
status: 0,
});
if (drone[0].alarm_status) {
alarm = await _ImageController.findImageData(drone[0].alarm_data);
result[droneList[i]] = {
status: 1,
expire_date: alarm[0].expire_date,
image_path: `/alarm/image/${drone[0].alarm_data}`
};
}
else {
result[droneList[i]] = {
status: 0
};
}
}

// If any drone exist, return 404
Expand All @@ -263,6 +267,50 @@ router.get('/alarm/:dic', _Middleware.checkAuth, async (req, res) => {
return _Common.r400(res);
});

/*
@example: /alarm/<dic>
@obligated: Add authorization header
@brief: disable alarm with DELETE method
*/
router.delete('/alarm/:dic', _Middleware.checkAuth, async (req, res) => {
if (req.headers.authorization.startsWith('Bearer ')) {
let drone;
try {
// Remove Bearer Clause
let auth = req.headers.authorization.split(' ')[1];
let droneList = req.params.dic.split(';');

// Get the status of every drone
for (let i = 0; i < droneList.length; i++) {
drone = await _DroneController.findDroneExist(droneList[i], auth);

if (drone.length == 0) {
// If not exist, continue to the next drone
continue;
}

if (drone[0].alarm_status) {
await _DroneController.updateAlarmStatus(droneList[i], auth, 0);
}
}

// If any drone exist, return 404
if (Object.keys(drone).length == 0) {
return _Common.r404(res);
}
} catch (error) {
logger.crit(error);
return _Common.r500(res);
}

return _Common.rJson(res, {});
}

return _Common.r400(res);
});


/*
@example: /alarm/image/<image>
*/
Expand Down
2 changes: 1 addition & 1 deletion src/drone-api/tests/alarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


if __name__ == "__main__":
url = 'http://127.0.0.1:60001/alarm/0'
url = 'http://localhost:60001/alarm/0'
headers = {
'Authorization': 'Bearer 0'
}
Expand Down
2 changes: 1 addition & 1 deletion src/drone-api/tests/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
print("Example of use: python {} <metadata>".format(sys.argv[0]))
exit(-1)

url = 'http://127.0.0.1:60001/metadata/'+sys.argv[1]+'/0'
url = 'http://localhost:60001/metadata/'+sys.argv[1]+'/0'
headers = {
'Authorization': 'Bearer 0'
}
Expand Down
2 changes: 1 addition & 1 deletion src/drone-api/tests/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


if __name__ == "__main__":
url = 'http://127.0.0.1:60001/route/0'
url = 'http://localhost:60001/route/0'
headers = {
'Authorization': 'Bearer 0'
}
Expand Down
2 changes: 1 addition & 1 deletion src/drone-api/tests/setRoute.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

if __name__ == "__main__":

url = 'http://127.0.0.1:60001/metadata/'+sys.argv[1]+'/0'
url = 'http://localhost:60001/metadata/'+sys.argv[1]+'/0'
headers = {
'Authorization': 'Bearer 0'
}
Expand Down
Binary file added test/fire.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions test/sendFirePic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
curl -v -X POST \
'http://localhost:60003/0' \
-H 'Authorization: Bearer 0' \
-H 'Content-Type: image/jpg' \
--data-binary '@./fire.jpg'

0 comments on commit ad1c6f8

Please # to comment.