fablabbcn / smartcitizen-api

The Smart Citizen Engine
https://developer.smartcitizen.me
GNU Affero General Public License v3.0
10 stars 4 forks source link

Measurements in device endpoint #287

Closed oscgonfer closed 4 months ago

oscgonfer commented 7 months ago

Currently, in the /devices/<device-id> endpoint we return sensor information on the /data/sensors part json:

"sensors": [
 {
    "id":133,
    "ancestry":"132",
    "name":"ADC_48_0",
    "description":"16-bit Analog to Digital Converter - ADS1X15 addr. 48 ch. 0",
    "unit":"V",
    "created_at":"2020-12-11T15:35:48Z",
    "updated_at":"2020-12-11T15:48:02Z",
    "measurement_id":11,
    "uuid":"37e8843d-47e1-401e-ac85-1c77d5ce4cd5",
    "value":0.251125,
    "raw_value":0.251125,
    "prev_value":0.250719,
    "prev_raw_value":0.250719
  },
  ...
 ]

We are working on an improvement of this endpoint on the #246 PR:

"sensors":[
  {
    "id":133,
    "ancestry":"132",
    "name":"ADC_48_0",
    "description":"16-bit Analog to Digital Converter - ADS1X15 addr. 48 ch. 0",
    "unit":"V",
    "created_at":"2020-12-11T15:35:48Z",
    "updated_at":"2020-12-11T15:48:02Z",
    "measurement_id":11,
    "uuid":"37e8843d-47e1-401e-ac85-1c77d5ce4cd5",
    "value":0.246937,
    "prev_value":0.246937,
    "last_reading_at":"2023-09-18T13:00:25Z"
 },
  ...
]

Now, I suggest we do an additional improvement by reusing the sensor representation a bit more and nest the measurement in the sensor json:

"sensors":[
  {
    "id":133,
    "ancestry":"132",
    "name":"ADC_48_0",
    "description":"16-bit Analog to Digital Converter - ADS1X15 addr. 48 ch. 0",
    "unit":"V",
    "created_at":"2020-12-11T15:35:48Z",
    "updated_at":"2020-12-11T15:48:02Z",
     "measurement": {
       "id":11,
       "uuid":"49a89988-2385-4a92-8bde-dc48de240aab",
       "name":"Voltage",
       "description":"Voltage, also known as electric pressure, electric tension, or (electric) potential difference, is the difference in electric potential between two points."
     },
    "uuid":"37e8843d-47e1-401e-ac85-1c77d5ce4cd5",
    "value":0.246937,
    "prev_value":0.246937,
    "last_reading_at":"2023-09-18T13:00:25Z"
 },
  ...
]

I think this would improve two things (but please, provide input):

  1. Front-end view of the device, to avoid a separate request to /measurements and reduce the local storage (plus we would only have measurements of the device, not all measurements)
  2. Requests from other platforms that want to have us forward them data, and simplify the request by having measurements already in device

It's true that we would have to query the measurements table internally for every device request, but seems that they are doing that already as we need that data, only that we query it via endpoint.

@pral2a @timcowlishaw Do you think there would be anything against this change?

oscgonfer commented 4 months ago

Confirmed we go for this one @timcowlishaw

timcowlishaw commented 4 months ago

Done in a9dafce!