105 lines
3.2 KiB
JavaScript
105 lines
3.2 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const multer = require('multer');
|
|
const database_manager = require('../src/database/database_manager');
|
|
const storage_manager = require('../src/data/storage_manager');
|
|
const serverError = require('../utils/serverError');
|
|
|
|
const upload = multer({ storage: multer.memoryStorage() });
|
|
|
|
/**
|
|
* @swagger
|
|
* /camera/upload:
|
|
* post:
|
|
* summary: Upload a new measurement with image
|
|
* tags:
|
|
* - Camera
|
|
* consumes:
|
|
* - multipart/form-data
|
|
* parameters:
|
|
* - in: formData
|
|
* name: image
|
|
* type: file
|
|
* required: true
|
|
* description: The image file to upload
|
|
* - in: formData
|
|
* name: projectId
|
|
* type: string
|
|
* required: true
|
|
* description: ID of the project
|
|
* - in: formData
|
|
* name: timestamp
|
|
* type: string
|
|
* required: true
|
|
* description: Timestamp of the measurement
|
|
* - in: formData
|
|
* name: temperature
|
|
* type: string
|
|
* required: true
|
|
* description: Temperature value
|
|
* - in: formData
|
|
* name: humidity
|
|
* type: string
|
|
* required: true
|
|
* description: Humidity value
|
|
* responses:
|
|
* 200:
|
|
* description: Measurement uploaded successfully
|
|
* content:
|
|
* application/json:
|
|
* schema:
|
|
* type: object
|
|
* properties:
|
|
* message:
|
|
* type: string
|
|
* path:
|
|
* type: string
|
|
* id:
|
|
* type: string
|
|
* 400:
|
|
* description: Missing required fields
|
|
* 404:
|
|
* description: Project not found
|
|
* 500:
|
|
* description: Server error
|
|
*/
|
|
router.post('/camera/upload', upload.single('image'), async (req, res) => {
|
|
//afficher le body de la requête
|
|
console.log(req.body);
|
|
const { projectId, timestamp, temperature, humidity } = req.body;
|
|
const image = req.file;
|
|
|
|
if (!image || !projectId || !timestamp || !temperature || !humidity) {
|
|
return res.status(400).json({ error: 'All fields are required' });
|
|
}
|
|
|
|
try {
|
|
const nextOrderId = await database_manager.measurement.get_next_order_id(projectId);
|
|
if (nextOrderId === null) {
|
|
return res.status(404).json({ error: 'Project not found' });
|
|
}
|
|
|
|
// Log types for debugging
|
|
console.log('Types:', {
|
|
image: typeof image,
|
|
projectId: typeof projectId,
|
|
nextOrderId: typeof nextOrderId
|
|
});
|
|
|
|
const imagePath = await storage_manager.measurement.upload_measurement_image(image, projectId, nextOrderId);
|
|
if (!imagePath) {
|
|
return res.status(500).json({ error: 'Failed to upload image' });
|
|
}
|
|
const measurement = await database_manager.measurement.create_measurement(projectId, timestamp, imagePath, temperature, humidity, nextOrderId);
|
|
if (!measurement) {
|
|
return res.status(500).json({ error: 'Failed to create measurement' });
|
|
}
|
|
res.json({ message: 'Measurement uploaded successfully', path: imagePath, id: measurement.id });
|
|
} catch (error) {
|
|
serverError.sendError('Error uploading measurement:', res, error, 500);
|
|
}
|
|
});
|
|
|
|
|
|
module.exports = router;
|