From bab1ae5f041bba13901599f496ff8fd387294204 Mon Sep 17 00:00:00 2001 From: bogap <58374268+bogap@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:26:24 +0300 Subject: [PATCH 1/4] ecliptica --- server/routers/epja-2024-1/ecliptica/index.js | 6 + .../epja-2024-1/ecliptica/plants/config.js | 6 + .../epja-2024-1/ecliptica/plants/getPlants.js | 133 ++++++++++++++++++ .../ecliptica/plants_calendar/index.js | 48 +++++++ 4 files changed, 193 insertions(+) create mode 100644 server/routers/epja-2024-1/ecliptica/index.js create mode 100644 server/routers/epja-2024-1/ecliptica/plants/config.js create mode 100644 server/routers/epja-2024-1/ecliptica/plants/getPlants.js create mode 100644 server/routers/epja-2024-1/ecliptica/plants_calendar/index.js diff --git a/server/routers/epja-2024-1/ecliptica/index.js b/server/routers/epja-2024-1/ecliptica/index.js new file mode 100644 index 0000000..9db8617 --- /dev/null +++ b/server/routers/epja-2024-1/ecliptica/index.js @@ -0,0 +1,6 @@ +const router = require('express').Router(); +const plantsRouter = require('express').Router(); +module.exports = router; + + +router.use('/plants',plantsRouter) \ No newline at end of file diff --git a/server/routers/epja-2024-1/ecliptica/plants/config.js b/server/routers/epja-2024-1/ecliptica/plants/config.js new file mode 100644 index 0000000..8b9b0c3 --- /dev/null +++ b/server/routers/epja-2024-1/ecliptica/plants/config.js @@ -0,0 +1,6 @@ +const CONFIG = { + CLIENT_ID: 'kD2JXj1faSCYAWdT4B069wQAx89CZAkXmzTinRvH', + CLIENT_SECRET: 'bJq7Uiwua52tHiLP80N60hALNtQX2wcE4Mj6yNA9OzG2iZbgHuqyeAs6WSWX6MNJdfv0Nqzb7OHta8qPZr4zxWBLTauleaMfraln3xFEvbXLDpi1Lcrwe7DxfgsQQ1E4', +}; + +module.exports = CONFIG; \ No newline at end of file diff --git a/server/routers/epja-2024-1/ecliptica/plants/getPlants.js b/server/routers/epja-2024-1/ecliptica/plants/getPlants.js new file mode 100644 index 0000000..aeb89e4 --- /dev/null +++ b/server/routers/epja-2024-1/ecliptica/plants/getPlants.js @@ -0,0 +1,133 @@ +const express = require('express'); +const axios = require('axios'); +const FormData = require('form-data'); +const plantsRouter = express.Router(); +const app = express(); +const port = process.env.PORT || 3000; +const cors = require('cors'); +const CONFIG = require('./config'); +const {config} = require("../../../bro.config"); +app.use(cors()); + +app.listen(port, () => { + console.log(`Server is running on port ${port}`); +}); + +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) { + // 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); diff --git a/server/routers/epja-2024-1/ecliptica/plants_calendar/index.js b/server/routers/epja-2024-1/ecliptica/plants_calendar/index.js new file mode 100644 index 0000000..ec5b4c8 --- /dev/null +++ b/server/routers/epja-2024-1/ecliptica/plants_calendar/index.js @@ -0,0 +1,48 @@ +const express = require('express'); +const cors = require('cors'); + +const app = express(); +const port = 5000; + +app.use(cors()); + +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), +})); + +app.get('/api/plants', (req, res) => { + res.json(plantsWithDates); +}); + +app.listen(port, () => { + console.log(`Server running on http://localhost:${port}`); +}); \ No newline at end of file -- 2.45.2 From 86f00de29820eab96bc41efcf28ed0683a38d58a Mon Sep 17 00:00:00 2001 From: bogap <58374268+bogap@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:30:27 +0300 Subject: [PATCH 2/4] Ecliptica routes --- server/routers/epja-2024-1/ecliptica/index.js | 1 - server/routers/epja-2024-1/ecliptica/plants/config.js | 1 + server/routers/epja-2024-1/ecliptica/plants/getPlants.js | 1 + server/routers/epja-2024-1/ecliptica/plants_calendar/index.js | 1 - 4 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/routers/epja-2024-1/ecliptica/index.js b/server/routers/epja-2024-1/ecliptica/index.js index 9db8617..f937ffa 100644 --- a/server/routers/epja-2024-1/ecliptica/index.js +++ b/server/routers/epja-2024-1/ecliptica/index.js @@ -2,5 +2,4 @@ const router = require('express').Router(); const plantsRouter = require('express').Router(); module.exports = router; - router.use('/plants',plantsRouter) \ No newline at end of file diff --git a/server/routers/epja-2024-1/ecliptica/plants/config.js b/server/routers/epja-2024-1/ecliptica/plants/config.js index 8b9b0c3..4cc00ac 100644 --- a/server/routers/epja-2024-1/ecliptica/plants/config.js +++ b/server/routers/epja-2024-1/ecliptica/plants/config.js @@ -3,4 +3,5 @@ const CONFIG = { CLIENT_SECRET: 'bJq7Uiwua52tHiLP80N60hALNtQX2wcE4Mj6yNA9OzG2iZbgHuqyeAs6WSWX6MNJdfv0Nqzb7OHta8qPZr4zxWBLTauleaMfraln3xFEvbXLDpi1Lcrwe7DxfgsQQ1E4', }; + module.exports = CONFIG; \ No newline at end of file diff --git a/server/routers/epja-2024-1/ecliptica/plants/getPlants.js b/server/routers/epja-2024-1/ecliptica/plants/getPlants.js index aeb89e4..506f29d 100644 --- a/server/routers/epja-2024-1/ecliptica/plants/getPlants.js +++ b/server/routers/epja-2024-1/ecliptica/plants/getPlants.js @@ -13,6 +13,7 @@ app.listen(port, () => { console.log(`Server is running on port ${port}`); }); + async function getAccessToken() { const formData = new FormData(); formData.append('grant_type', 'client_credentials'); diff --git a/server/routers/epja-2024-1/ecliptica/plants_calendar/index.js b/server/routers/epja-2024-1/ecliptica/plants_calendar/index.js index ec5b4c8..f0b73db 100644 --- a/server/routers/epja-2024-1/ecliptica/plants_calendar/index.js +++ b/server/routers/epja-2024-1/ecliptica/plants_calendar/index.js @@ -21,7 +21,6 @@ const plants = [ }, ]; - const calculateWateringDates = (startDate, frequency) => { const dates = []; const start = new Date(startDate); -- 2.45.2 From fd293711a22848942e7f5f1fbb9edf7426b525c4 Mon Sep 17 00:00:00 2001 From: bogap <58374268+bogap@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:37:10 +0300 Subject: [PATCH 3/4] index route --- server/routers/epja-2024-1/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/routers/epja-2024-1/index.js b/server/routers/epja-2024-1/index.js index 2097192..146b465 100644 --- a/server/routers/epja-2024-1/index.js +++ b/server/routers/epja-2024-1/index.js @@ -4,4 +4,6 @@ const router = express.Router() router.use('/enterfront', require('./enterfront/index')) +router.use('/ecliptica', require('./ecliptica/index')) + module.exports = router -- 2.45.2 From c7a3c06f609fbd195f14ee838e733b804a9fe4de Mon Sep 17 00:00:00 2001 From: bogap <58374268+bogap@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:39:15 +0300 Subject: [PATCH 4/4] ecliptica routes --- server/routers/epja-2024-1/ecliptica/index.js | 1 + server/routers/epja-2024-1/ecliptica/plants/config.js | 1 - server/routers/epja-2024-1/ecliptica/plants/getPlants.js | 1 - server/routers/epja-2024-1/ecliptica/plants_calendar/index.js | 1 + server/routers/epja-2024-1/index.js | 1 - 5 files changed, 2 insertions(+), 3 deletions(-) diff --git a/server/routers/epja-2024-1/ecliptica/index.js b/server/routers/epja-2024-1/ecliptica/index.js index f937ffa..9db8617 100644 --- a/server/routers/epja-2024-1/ecliptica/index.js +++ b/server/routers/epja-2024-1/ecliptica/index.js @@ -2,4 +2,5 @@ const router = require('express').Router(); const plantsRouter = require('express').Router(); module.exports = router; + router.use('/plants',plantsRouter) \ No newline at end of file diff --git a/server/routers/epja-2024-1/ecliptica/plants/config.js b/server/routers/epja-2024-1/ecliptica/plants/config.js index 4cc00ac..8b9b0c3 100644 --- a/server/routers/epja-2024-1/ecliptica/plants/config.js +++ b/server/routers/epja-2024-1/ecliptica/plants/config.js @@ -3,5 +3,4 @@ const CONFIG = { CLIENT_SECRET: 'bJq7Uiwua52tHiLP80N60hALNtQX2wcE4Mj6yNA9OzG2iZbgHuqyeAs6WSWX6MNJdfv0Nqzb7OHta8qPZr4zxWBLTauleaMfraln3xFEvbXLDpi1Lcrwe7DxfgsQQ1E4', }; - module.exports = CONFIG; \ No newline at end of file diff --git a/server/routers/epja-2024-1/ecliptica/plants/getPlants.js b/server/routers/epja-2024-1/ecliptica/plants/getPlants.js index 506f29d..aeb89e4 100644 --- a/server/routers/epja-2024-1/ecliptica/plants/getPlants.js +++ b/server/routers/epja-2024-1/ecliptica/plants/getPlants.js @@ -13,7 +13,6 @@ app.listen(port, () => { console.log(`Server is running on port ${port}`); }); - async function getAccessToken() { const formData = new FormData(); formData.append('grant_type', 'client_credentials'); diff --git a/server/routers/epja-2024-1/ecliptica/plants_calendar/index.js b/server/routers/epja-2024-1/ecliptica/plants_calendar/index.js index f0b73db..20cdeb1 100644 --- a/server/routers/epja-2024-1/ecliptica/plants_calendar/index.js +++ b/server/routers/epja-2024-1/ecliptica/plants_calendar/index.js @@ -13,6 +13,7 @@ const plants = [ frequency: 3, startDate: "2024-10-09", }, + { name: "Sunflower", image: "https://avatars.mds.yandex.net/i?id=31da587c9aabc83ad3615023f91d7284781be06c-10701700-images-thumbs&n=13", diff --git a/server/routers/epja-2024-1/index.js b/server/routers/epja-2024-1/index.js index 146b465..e29e690 100644 --- a/server/routers/epja-2024-1/index.js +++ b/server/routers/epja-2024-1/index.js @@ -3,7 +3,6 @@ const router = express.Router() router.use('/enterfront', require('./enterfront/index')) - router.use('/ecliptica', require('./ecliptica/index')) module.exports = router -- 2.45.2