Skip to content

feat: add IoT sensor fields (temperature, humidity, spoilage) to Batch model and backend controllers #454

@Pcmhacker-piro

Description

@Pcmhacker-piro

Description

The frontend extensively references batch-level IoT sensor fields — currentTemperature, currentHumidity, isSpoiled, and iotTimestamp — across three separate pages (track-batch, journey map, compare). However, none of these fields exist in the Batch Mongoose schema (backend/models/Batch.js). No backend controller or service ever sets them. As a result:

  1. The "IoT Sensor Data" section on the track-batch page (75+ lines) never renders — it has conditional checks like if (!batch.currentTemperature) which always evaluate as false
  2. The "Spoilage Breached" / "Oracle Secured" badges on the journey page never appear
  3. The "Spoiled" vs "Active" status comparison on the compare page always shows "Active"

Steps to Reproduce

  1. Navigate to any batch at /track-batch (e.g., BATCH000001)
  2. Observe the "IoT Sensor Data" section does not appear even for batches that have been in transport for days
  3. Open browser DevTools and inspect batch.currentTemperature — it is undefined
  4. Navigate to /batch/BATCH000001/journey — observe no spoilage/oracle badges appear
  5. Navigate to /compare?ids=BATCH000001,BATCH000002 — observe both always show "Active" never "Spoiled"

Expected Behavior

  • Backend model: Add currentTemperature, currentHumidity, isSpoiled, and iotTimestamp fields to the Batch schema
  • Backend route: Add a POST /api/batches/:batchId/iot endpoint for IoT sensor data ingestion
  • Backend route: Add a GET /api/batches/:batchId/iot/history endpoint for historical telemetry
  • Frontend: The existing IoT section on track-batch, journey spoilage badges, and compare status should function with real data
  • Validation: Temperature should be validated as a realistic range (-10°C to 60°C), humidity 0–100%

Implementation Hints

Update Batch model (backend/models/Batch.js):

iotData: {
  currentTemperature: { type: Number, min: -10, max: 60 },
  currentHumidity: { type: Number, min: 0, max: 100 },
  isSpoiled: { type: Boolean, default: false },
  lastUpdated: { type: Date },
  telemetryHistory: [{
    temperature: Number,
    humidity: Number,
    timestamp: { type: Date, default: Date.now }
  }]
}

Add IoT endpoint in backend/routes/index.js:

router.post(/batches/:batchId/iot, protect, recordIoTData);
router.get(/batches/:batchId/iot/history, protect, getIoTData);

Add a simple spoilage detection service that flags isSpoiled when temperature exceeds safe thresholds for the crop type:

const SPOILAGE_THRESHOLDS = {
  rice: { maxTemp: 25, maxHumidity: 70 },
  wheat: { maxTemp: 30, maxHumidity: 65 },
  tomato: { maxTemp: 10, maxHumidity: 85 },
  corn: { maxTemp: 28, maxHumidity: 75 },
};

Affected Files

  • backend/models/Batch.js
  • backend/controllers/batchController.js (new recordIoTData + getIoTData methods)
  • backend/routes/index.js
  • frontend/src/app/track-batch/page.tsx
  • frontend/src/app/batch/[batchId]/journey/page.tsx
  • frontend/src/app/compare/page.tsx

Labels

type:enhancement, level:intermediate, GSSoC-26

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions