Skip to content

Device Data

Overview

The device data endpoint serves two audiences (fields mentioned below follow the Field Reference):

  1. GET: Frontend dashboards that need the latest readings for a device and any chained sub-devices. Data is sourced from the real-time cache and stored with a 2-minute TTL.
  2. POST: Device gateways pushing uplink payloads into Yanji IoT Cloud. The API validates factor IDs, enriches missing fields, and forwards the data to the ingestion queue for downstream processing.
  • Base Path: /api/v2/projects/{project_id}/devices/{device_pk}/data/

Endpoint Summary

MethodPurposeNotes
GETReturn latest cached telemetry for the device and its child factorsSee GET Device Data for parameters and cache behavior.
POSTAccept telemetry payloads from device clientsSee POST Device Data for payload rules and enrichment.

GET Device Data

http
GET /api/v2/projects/{project_id}/devices/{device_pk}/data/
Authorization: Bearer <token>

Query Parameters

None. The endpoint automatically aggregates every factor whose agri_id starts with the device’s agri_id (covering sub-devices or modules).

Response

json
[
  {
    "agri_id": "d-1000-abcd-01",
    "temp": 23.5,
    "humi": 46.2,
    "t": 1708070400
  },
  {
    "agri_id": "d-1000-abcd-02",
    "switch": 1,
    "t": 1708070340
  }
]

Fields are flattened; each key represents a factor value stored in the real-time cache. t (if present) is a UNIX timestamp in UTC seconds. If the cache returns no data, the API responds with an empty list.

Behavior

  • Cache key: device_data_<device.agri_id> with 120-second TTL.
  • Data source: the telemetry cache exposes a map of agri_id -> field/value dictionaries.
  • Filtering: Only entries whose agri_id starts with the device’s agri_id are returned, covering the main controller plus sub-units.

POST Device Data

http
POST /api/v2/projects/{project_id}/devices/{device_pk}/data/
Authorization: Bearer <token>
Content-Type: application/json

[
  { "agri_id": "d-1000-abcd-01", "value": 24.3, "t": 1708070400 },
  { "agri_id": "d-1000-abcd-02", "value": 68.2 }
]

Payload Rules

FieldTypeRequiredNotes
agri_idstringYesMust start with the parent device agri_id (e.g., d-1000-abcd); otherwise the row is skipped
value / arbitrary keysnumber/stringYesKey/value pairs representing factor readings
tintegerNoUTC timestamp (seconds). If missing or invalid, server fills current UTC time

Server-side Enrichment

For each valid row:

  1. Looks up factor metadata by agri_id.
  2. Injects the_type from the factor definition.
  3. Adds project_id and org_id (organization slug) to the payload.
  4. Queues the transformed batch to the ingestion pipeline.

If the queue operation fails, the API raises APIException and returns HTTP 503 with the error message.

Sample Response

json
[
  {
    "agri_id": "d-1000-abcd-01",
    "value": 24.3,
    "t": 1708070400,
    "the_type": "temp",
    "project_id": "greenhouse-alpha",
    "org_id": "yanji-lab"
  }
]

Only rows that pass validation are returned. Invalid entries (missing agri_id, mismatched prefix, or unknown factor) are dropped silently to keep ingestion resilient.

Error Codes

StatusCodeDescription
400BAD_REQUESTMalformed JSON array or unsupported field types
401UNAUTHORIZEDMissing/invalid token
403FORBIDDENToken lacks access to the project/device
404DEVICE_NOT_FOUNDDevice PK not in project or disabled
503DATA_PIPELINE_FAILUREFailed to enqueue data to the ingestion pipeline (POST only)

Operational Notes

  1. Prefix Enforcement: The server rejects samples whose agri_id does not start with the parent device’s agri_id, preventing data leakage across devices.
  2. Silent Drops: Invalid rows are skipped without aborting the entire batch. Always inspect the response to know which entries were accepted.
  3. Idempotence: The API does not deduplicate payloads. Include timestamps (t) to help downstream services detect duplicates.
  4. Rate Limits: Reuse the same batching strategy as other POST APIs (60 rpm burst). Large deployments should aggregate readings before posting.
  5. Data Consistency: GET responses may lag POST payloads by up to the cache refresh interval. For real-time monitoring, pair GET /data/ with Factor Data endpoints when historical context is needed.