corrected routes
This commit is contained in:
parent
c7a3c06f60
commit
176ec8ce5a
@ -1,6 +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();
|
const router = require('express').Router();
|
||||||
const plantsRouter = require('express').Router();
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
||||||
|
|
||||||
router.use('/plants',plantsRouter)
|
router.use('/plants',plantsRouter)
|
||||||
|
router.use('/plants_calendar',calendarRouter)
|
||||||
|
@ -1,133 +1,145 @@
|
|||||||
const express = require('express');
|
const { exec } = require('child_process');
|
||||||
const axios = require('axios');
|
exec('npm list axios', (err, stdout, stderr) => {
|
||||||
const FormData = require('form-data');
|
if (stderr.includes('empty')) {
|
||||||
const plantsRouter = express.Router();
|
console.log('Installing axios...');
|
||||||
const app = express();
|
exec('npm install axios', (error, stdout, stderr) => {
|
||||||
const port = process.env.PORT || 3000;
|
if (error) {
|
||||||
const cors = require('cors');
|
console.error('Error installing axios:', error);
|
||||||
const CONFIG = require('./config');
|
} else {
|
||||||
const {config} = require("../../../bro.config");
|
console.log('Axios installed successfully');
|
||||||
app.use(cors());
|
startApp();
|
||||||
|
}
|
||||||
app.listen(port, () => {
|
});
|
||||||
console.log(`Server is running on port ${port}`);
|
} else {
|
||||||
|
startApp();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
async function getAccessToken() {
|
function startApp() {
|
||||||
const formData = new FormData();
|
const express = require('express');
|
||||||
formData.append('grant_type', 'client_credentials');
|
const axios = require('axios');
|
||||||
formData.append('client_id', CONFIG.CLIENT_ID);
|
const FormData = require('form-data');
|
||||||
formData.append('client_secret', CONFIG.CLIENT_SECRET);
|
const plantsRouter = express.Router();
|
||||||
|
const cors = require('cors');
|
||||||
|
const CONFIG = require('./config');
|
||||||
|
|
||||||
try {
|
plantsRouter.use(cors());
|
||||||
const response = await axios.post('https://open.plantbook.io/api/v1/token/', formData, {
|
|
||||||
headers: { 'Content-Type': 'multipart/form-data' }
|
async function getAccessToken() {
|
||||||
});
|
const formData = new FormData();
|
||||||
if (response.data && response.data.access_token) {
|
formData.append('grant_type', 'client_credentials');
|
||||||
console.log('Access token retrieved:', response.data.access_token);
|
formData.append('client_id', CONFIG.CLIENT_ID);
|
||||||
return response.data.access_token;
|
formData.append('client_secret', CONFIG.CLIENT_SECRET);
|
||||||
} else {
|
|
||||||
console.error('Error: access_token not found in response');
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error fetching access token:', error.response ? error.response.data : error.message);
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async function fetchPlantData(plantId) {
|
async function fetchPlantData(plantId) {
|
||||||
const accessToken = await getAccessToken();
|
const accessToken = await getAccessToken();
|
||||||
if (!accessToken) {
|
if (!accessToken) {
|
||||||
return null;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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) => {
|
plantsRouter.get('/list', async (req, res) => {
|
||||||
const accessToken = await getAccessToken();
|
const accessToken = await getAccessToken();
|
||||||
if (!accessToken) {
|
if (!accessToken) {
|
||||||
res.status(500).send({ message: 'Error obtaining access token' });
|
res.status(500).send({message: 'Error obtaining access token'});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const alias = req.query.alias || 'monstera';
|
const alias = req.query.alias || 'monstera';
|
||||||
const response = await axios.get('https://open.plantbook.io/api/v1/plant/search', {
|
const response = await axios.get('https://open.plantbook.io/api/v1/plant/search', {
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Bearer ${accessToken}`
|
'Authorization': `Bearer ${accessToken}`
|
||||||
},
|
},
|
||||||
params: {
|
params: {
|
||||||
alias: alias
|
alias: alias
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const plantsList = response.data.results;
|
const plantsList = response.data.results;
|
||||||
|
|
||||||
const plants = await Promise.all(plantsList.map(async (plant) => {
|
const plants = await Promise.all(plantsList.map(async (plant) => {
|
||||||
const plantDetails = await fetchPlantData(plant.pid);
|
const plantDetails = await fetchPlantData(plant.pid);
|
||||||
return {
|
return {
|
||||||
id: plant.pid,
|
id: plant.pid,
|
||||||
alias: plant.alias,
|
alias: plant.alias,
|
||||||
display_name: plant.display_name,
|
display_name: plant.display_name,
|
||||||
image_url: plantDetails ? plantDetails.image_url : null,
|
image_url: plantDetails ? plantDetails.image_url : null,
|
||||||
max_light: plantDetails ? plantDetails.max_light : null,
|
max_light: plantDetails ? plantDetails.max_light : null,
|
||||||
min_light: plantDetails ? plantDetails.min_light : null,
|
min_light: plantDetails ? plantDetails.min_light : null,
|
||||||
max_temp: plantDetails ? plantDetails.max_temp : null,
|
max_temp: plantDetails ? plantDetails.max_temp : null,
|
||||||
min_temp: plantDetails ? plantDetails.min_temp : null,
|
min_temp: plantDetails ? plantDetails.min_temp : null,
|
||||||
max_env_humid: plantDetails ? plantDetails.max_env_humid : null,
|
max_env_humid: plantDetails ? plantDetails.max_env_humid : null,
|
||||||
min_env_humid: plantDetails ? plantDetails.min_env_humid : null,
|
min_env_humid: plantDetails ? plantDetails.min_env_humid : null,
|
||||||
max_soil_moist: plantDetails ? plantDetails.max_soil_moist : null,
|
max_soil_moist: plantDetails ? plantDetails.max_soil_moist : null,
|
||||||
min_soil_moist: plantDetails ? plantDetails.min_soil_moist : null,
|
min_soil_moist: plantDetails ? plantDetails.min_soil_moist : null,
|
||||||
max_soil_ec: plantDetails ? plantDetails.max_soil_ec : null,
|
max_soil_ec: plantDetails ? plantDetails.max_soil_ec : null,
|
||||||
min_soil_ec: plantDetails ? plantDetails.min_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({ results: plants });
|
res.send(detailedPlantData);
|
||||||
} catch (error) {
|
} else {
|
||||||
console.error('Error fetching plant list:', error.response ? error.response.data : error.message);
|
res.status(404).send({message: 'Plant not found'});
|
||||||
res.status(500).send({ message: 'Error fetching plant list' });
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
|
|
||||||
plantsRouter.get('/:id', async (req, res) => {
|
module.exports = plantsRouter;
|
||||||
const plantId = req.params.id;
|
}
|
||||||
const plantData = await fetchPlantData(plantId);
|
|
||||||
|
|
||||||
if (plantData) {
|
|
||||||
// Construct a response based on the plantData structure
|
|
||||||
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' });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.use('/plants', plantsRouter);
|
|
@ -1,10 +1,7 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
|
|
||||||
const app = express();
|
const plantsRouter = express.Router()
|
||||||
const port = 5000;
|
|
||||||
|
|
||||||
app.use(cors());
|
|
||||||
|
|
||||||
const plants = [
|
const plants = [
|
||||||
{
|
{
|
||||||
@ -13,7 +10,6 @@ const plants = [
|
|||||||
frequency: 3,
|
frequency: 3,
|
||||||
startDate: "2024-10-09",
|
startDate: "2024-10-09",
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
name: "Sunflower",
|
name: "Sunflower",
|
||||||
image: "https://avatars.mds.yandex.net/i?id=31da587c9aabc83ad3615023f91d7284781be06c-10701700-images-thumbs&n=13",
|
image: "https://avatars.mds.yandex.net/i?id=31da587c9aabc83ad3615023f91d7284781be06c-10701700-images-thumbs&n=13",
|
||||||
@ -22,6 +18,7 @@ const plants = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
const calculateWateringDates = (startDate, frequency) => {
|
const calculateWateringDates = (startDate, frequency) => {
|
||||||
const dates = [];
|
const dates = [];
|
||||||
const start = new Date(startDate);
|
const start = new Date(startDate);
|
||||||
@ -39,10 +36,8 @@ const plantsWithDates = plants.map(plant => ({
|
|||||||
wateringDates: calculateWateringDates(plant.startDate, plant.frequency),
|
wateringDates: calculateWateringDates(plant.startDate, plant.frequency),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
app.get('/api/plants', (req, res) => {
|
plantsWithDates.get('/api/plants', (req, res) => {
|
||||||
res.json(plantsWithDates);
|
res.json(plantsWithDates);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(port, () => {
|
module.exports = plantsRouter;
|
||||||
console.log(`Server running on http://localhost:${port}`);
|
|
||||||
});
|
|
@ -3,6 +3,7 @@ const router = express.Router()
|
|||||||
|
|
||||||
|
|
||||||
router.use('/enterfront', require('./enterfront/index'))
|
router.use('/enterfront', require('./enterfront/index'))
|
||||||
|
|
||||||
router.use('/ecliptica', require('./ecliptica/index'))
|
router.use('/ecliptica', require('./ecliptica/index'))
|
||||||
|
|
||||||
module.exports = router
|
module.exports = router
|
||||||
|
Loading…
Reference in New Issue
Block a user