localgreenchain/pages/api/environment/compare.ts
Claude 85591fc348
Add comprehensive environmental tracking for plants
Implements detailed tracking of soil composition, nutrients, climate,
lighting, and surrounding environment to help understand plant success
and optimize growing conditions.

Environmental Data Types (lib/environment/types.ts):
- Soil composition (type, pH, texture, drainage, organic matter)
- Soil amendments (compost, perlite, amendments tracking)
- Nutrient profiles (NPK, secondary nutrients, micronutrients, EC, TDS)
- Fertilizer applications (type, schedule, NPK values)
- Lighting conditions (natural/artificial, hours, spectrum, PPFD/DLI)
- Climate tracking (temperature, humidity, airflow, CO2, USDA zones)
- Growing location (indoor/outdoor/greenhouse with details)
- Container information (type, material, size, drainage)
- Watering schedule (method, frequency, water quality/pH)
- Surrounding environment (companion plants, pests, diseases, ecosystem)
- Growth metrics (measurements, health scores, vigor tracking)

Environmental Analysis (lib/environment/analysis.ts):
- Compare environments with similarity scoring (0-100)
- Generate personalized growing recommendations
- Calculate environmental health scores
- Find plants with similar conditions
- Analyze growth correlations across network
- Identify optimal growing conditions

API Endpoints:
- /api/environment/recommendations - Get plant-specific advice
- /api/environment/similar - Find plants with similar conditions
- /api/environment/compare - Compare two plants environments
- /api/environment/analysis - Network-wide growth correlation

Features:
- Comprehensive soil tracking (pH, texture, drainage, amendments)
- Full NPK and micronutrient monitoring
- Sunlight exposure and artificial light tracking
- Temperature and humidity ranges
- Water quality monitoring (pH, TDS, chlorine)
- Pest and disease tracking
- Companion planting recommendations
- Environmental health scoring with priority-based recommendations
- Growth success analysis across similar environments

Integration:
- Updated PlantData type to include environment and growthMetrics
- Compatible with existing blockchain structure
- Optional environmental data (backward compatible)

Use Cases:
- Track what works for your plants
- Learn from successful growers with similar conditions
- Get personalized recommendations based on your setup
- Compare your environment with thriving plants
- Optimize conditions for better yield/health
- Share growing knowledge with the community
- Research optimal conditions by species

This enables growers to:
- Understand why plants thrive or struggle
- Replicate successful growing conditions
- Make data-driven decisions
- Learn from the collective experience
- Improve propagation success rates
2025-11-16 16:24:38 +00:00

80 lines
2.6 KiB
TypeScript

/**
* API Route: Compare growing environments of two plants
* GET /api/environment/compare?plant1=xyz&plant2=abc
*/
import type { NextApiRequest, NextApiResponse } from 'next';
import { getBlockchain } from '../../../lib/blockchain/manager';
import { compareEnvironments } from '../../../lib/environment/analysis';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== 'GET') {
return res.status(405).json({ error: 'Method not allowed' });
}
try {
const { plant1, plant2 } = req.query;
if (!plant1 || !plant2 || typeof plant1 !== 'string' || typeof plant2 !== 'string') {
return res.status(400).json({ error: 'Missing plant1 and plant2 parameters' });
}
const blockchain = getBlockchain();
const plantData1 = blockchain.getPlant(plant1);
const plantData2 = blockchain.getPlant(plant2);
if (!plantData1 || !plantData2) {
return res.status(404).json({ error: 'One or both plants not found' });
}
if (!plantData1.environment || !plantData2.environment) {
return res.status(400).json({
error: 'Both plants must have environmental data',
plant1HasData: !!plantData1.environment,
plant2HasData: !!plantData2.environment,
});
}
const comparison = compareEnvironments(plantData1.environment, plantData2.environment);
comparison.plant1 = plant1;
comparison.plant2 = plant2;
res.status(200).json({
success: true,
plants: {
plant1: {
id: plantData1.id,
commonName: plantData1.commonName,
scientificName: plantData1.scientificName,
owner: plantData1.owner.name,
},
plant2: {
id: plantData2.id,
commonName: plantData2.commonName,
scientificName: plantData2.scientificName,
owner: plantData2.owner.name,
},
},
comparison: {
similarityScore: comparison.score,
similarities: comparison.similarities,
differences: comparison.differences,
},
interpretation: getScoreInterpretation(comparison.score),
});
} catch (error: any) {
console.error('Error comparing environments:', error);
res.status(500).json({ error: error.message || 'Internal server error' });
}
}
function getScoreInterpretation(score: number): string {
if (score >= 90) return 'Nearly identical growing conditions';
if (score >= 75) return 'Very similar environments - likely to have similar results';
if (score >= 60) return 'Moderately similar conditions';
if (score >= 40) return 'Some similarities but notable differences';
return 'Very different growing environments';
}