corrected
This commit is contained in:
parent
6015bce32f
commit
02891fe301
12
server/routers/epja-2024-1/ecliptica/index.js
Normal file
12
server/routers/epja-2024-1/ecliptica/index.js
Normal file
@ -0,0 +1,12 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
|
||||
const plantsRouter = require('./plants/getPlants');
|
||||
const calendarRouter = require('./plants_calendar/index');
|
||||
|
||||
const router = require('express').Router();
|
||||
|
||||
module.exports = router;
|
||||
|
||||
router.use('/plants',plantsRouter)
|
||||
router.use('/plants_calendar',calendarRouter)
|
6
server/routers/epja-2024-1/ecliptica/plants/config.js
Normal file
6
server/routers/epja-2024-1/ecliptica/plants/config.js
Normal file
@ -0,0 +1,6 @@
|
||||
const CONFIG = {
|
||||
CLIENT_ID: 'kD2JXj1faSCYAWdT4B069wQAx89CZAkXmzTinRvH',
|
||||
CLIENT_SECRET: 'bJq7Uiwua52tHiLP80N60hALNtQX2wcE4Mj6yNA9OzG2iZbgHuqyeAs6WSWX6MNJdfv0Nqzb7OHta8qPZr4zxWBLTauleaMfraln3xFEvbXLDpi1Lcrwe7DxfgsQQ1E4',
|
||||
};
|
||||
|
||||
module.exports = CONFIG;
|
145
server/routers/epja-2024-1/ecliptica/plants/getPlants.js
Normal file
145
server/routers/epja-2024-1/ecliptica/plants/getPlants.js
Normal file
@ -0,0 +1,145 @@
|
||||
const { exec } = require('child_process');
|
||||
exec('npm list axios', (err, stdout, stderr) => {
|
||||
if (stderr.includes('empty')) {
|
||||
console.log('Installing axios...');
|
||||
exec('npm install axios', (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.error('Error installing axios:', error);
|
||||
} else {
|
||||
console.log('Axios installed successfully');
|
||||
startApp();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
startApp();
|
||||
}
|
||||
});
|
||||
|
||||
function startApp() {
|
||||
const express = require('express');
|
||||
const axios = require('axios');
|
||||
const FormData = require('form-data');
|
||||
const plantsRouter = express.Router();
|
||||
const cors = require('cors');
|
||||
const CONFIG = require('./config');
|
||||
|
||||
plantsRouter.use(cors());
|
||||
|
||||
async function getAccessToken() {
|
||||
const formData = new FormData();
|
||||
formData.append('grant_type', 'client_credentials');
|
||||
formData.append('client_id', CONFIG.CLIENT_ID);
|
||||
formData.append('client_secret', CONFIG.CLIENT_SECRET);
|
||||
|
||||
try {
|
||||
const response = await axios.post('https://open.plantbook.io/api/v1/token/', formData, {
|
||||
headers: {'Content-Type': 'multipart/form-data'}
|
||||
});
|
||||
if (response.data && response.data.access_token) {
|
||||
console.log('Access token retrieved:', response.data.access_token);
|
||||
return response.data.access_token;
|
||||
} else {
|
||||
console.error('Error: access_token not found in response');
|
||||
return null;
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error fetching access token:', error.response ? error.response.data : error.message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchPlantData(plantId) {
|
||||
const accessToken = await getAccessToken();
|
||||
if (!accessToken) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const response = await axios.get(`https://open.plantbook.io/api/v1/plant/detail/${encodeURIComponent(plantId)}/`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${accessToken}`
|
||||
}
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Error fetching plant data:', error.response ? error.response.data : error.message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
plantsRouter.get('/list', async (req, res) => {
|
||||
const accessToken = await getAccessToken();
|
||||
if (!accessToken) {
|
||||
res.status(500).send({message: 'Error obtaining access token'});
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const alias = req.query.alias || 'monstera';
|
||||
const response = await axios.get('https://open.plantbook.io/api/v1/plant/search', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${accessToken}`
|
||||
},
|
||||
params: {
|
||||
alias: alias
|
||||
}
|
||||
});
|
||||
|
||||
const plantsList = response.data.results;
|
||||
|
||||
const plants = await Promise.all(plantsList.map(async (plant) => {
|
||||
const plantDetails = await fetchPlantData(plant.pid);
|
||||
return {
|
||||
id: plant.pid,
|
||||
alias: plant.alias,
|
||||
display_name: plant.display_name,
|
||||
image_url: plantDetails ? plantDetails.image_url : null,
|
||||
max_light: plantDetails ? plantDetails.max_light : null,
|
||||
min_light: plantDetails ? plantDetails.min_light : null,
|
||||
max_temp: plantDetails ? plantDetails.max_temp : null,
|
||||
min_temp: plantDetails ? plantDetails.min_temp : null,
|
||||
max_env_humid: plantDetails ? plantDetails.max_env_humid : null,
|
||||
min_env_humid: plantDetails ? plantDetails.min_env_humid : null,
|
||||
max_soil_moist: plantDetails ? plantDetails.max_soil_moist : null,
|
||||
min_soil_moist: plantDetails ? plantDetails.min_soil_moist : null,
|
||||
max_soil_ec: plantDetails ? plantDetails.max_soil_ec : null,
|
||||
min_soil_ec: plantDetails ? plantDetails.min_soil_ec : null,
|
||||
};
|
||||
}));
|
||||
|
||||
res.send({results: plants});
|
||||
} catch (error) {
|
||||
console.error('Error fetching plant list:', error.response ? error.response.data : error.message);
|
||||
res.status(500).send({message: 'Error fetching plant list'});
|
||||
}
|
||||
});
|
||||
|
||||
plantsRouter.get('/:id', async (req, res) => {
|
||||
const plantId = req.params.id;
|
||||
const plantData = await fetchPlantData(plantId);
|
||||
|
||||
if (plantData) {
|
||||
const detailedPlantData = {
|
||||
id: plantData.pid,
|
||||
display_name: plantData.display_name,
|
||||
alias: plantData.alias,
|
||||
max_light: plantData.max_light,
|
||||
min_light: plantData.min_light,
|
||||
max_temperature: plantData.max_temp,
|
||||
min_temperature: plantData.min_temp,
|
||||
max_humidity: plantData.max_env_humid,
|
||||
min_humidity: plantData.min_env_humid,
|
||||
max_soil_moisture: plantData.max_soil_moist,
|
||||
min_soil_moisture: plantData.min_soil_moist,
|
||||
max_soil_ec: plantData.max_soil_ec,
|
||||
min_soil_ec: plantData.min_soil_ec,
|
||||
image_url: plantData.image_url
|
||||
};
|
||||
|
||||
res.send(detailedPlantData);
|
||||
} else {
|
||||
res.status(404).send({message: 'Plant not found'});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = plantsRouter;
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
|
||||
const plantsRouter = express.Router()
|
||||
|
||||
const plants = [
|
||||
{
|
||||
name: "Rose",
|
||||
image: "https://ervanarium.com.br/wp-content/uploads/2019/03/cactus-3142589_1920.jpg",
|
||||
frequency: 3,
|
||||
startDate: "2024-10-09",
|
||||
},
|
||||
{
|
||||
name: "Sunflower",
|
||||
image: "https://avatars.mds.yandex.net/i?id=31da587c9aabc83ad3615023f91d7284781be06c-10701700-images-thumbs&n=13",
|
||||
frequency: 3,
|
||||
startDate: "2024-10-05",
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
const calculateWateringDates = (startDate, frequency) => {
|
||||
const dates = [];
|
||||
const start = new Date(startDate);
|
||||
|
||||
for (let i = 0; i < 30; i += frequency) {
|
||||
const nextWateringDate = new Date(start);
|
||||
nextWateringDate.setDate(start.getDate() + i);
|
||||
dates.push(nextWateringDate.toISOString().split('T')[0]);
|
||||
}
|
||||
return dates;
|
||||
};
|
||||
|
||||
const plantsWithDates = plants.map(plant => ({
|
||||
...plant,
|
||||
wateringDates: calculateWateringDates(plant.startDate, plant.frequency),
|
||||
}));
|
||||
|
||||
plantsWithDates.get('/api/plants', (req, res) => {
|
||||
res.json(plantsWithDates);
|
||||
});
|
||||
|
||||
module.exports = plantsRouter;
|
@ -6,4 +6,6 @@ router.use('/enterfront', require('./enterfront/index'))
|
||||
|
||||
router.use('/cats', require('./cats/index'))
|
||||
|
||||
router.use('/ecliptica', require('./ecliptica/index'))
|
||||
|
||||
module.exports = router
|
||||
|
Loading…
Reference in New Issue
Block a user