Waziup / api-server

3 stars 6 forks source link

subscription endpoint #40

Open cdupont opened 6 years ago

cdupont commented 6 years ago

Currently, a user can only retrieve sensor data through GET (pooling). Let's create a new endpoint to subscribe to sensor data.

cdupont commented 6 years ago

New endpoint: /domains/{domain}/subscriptions

POST /domains/{domain}/subscriptions
{
  "id": "string",
  "description": "subscribe to sensor data",
  "subject": {
    "sensor_id": "Sensor1",
    "measurement_id": "TC1"
    }
  "http": {
    "url": "http://example.com/input"
  },
  "expires": "2017-10-13T14:51:22.12Z",
  "throttling": 3600
}

The data sent to http://example.com/input will be:


POST http://example.com/input
{
  "sensor_id": "Sensor1",
  "measurement_id": "TC1",
  "datapoint": {
    "value": "25.6",
    "timestamp": "2016-06-08T18:20:27.873Z"
  }
}
malishahi commented 6 years ago

I think, Orion will direclty inform the subscribers, so Waziup api might not need to do anything.

cdupont commented 6 years ago

As this subscription is only for sensor values, it makes more sense to put subscription at sensor level. New endpoint: /domains/{domain}/sensors/subscriptions

POST /domains/{domain}/sensors/subscriptions
{
  "sensor_id_pattern": "Sensor1",
  "url": "http://example.com/input",
}

By default, subscribing on one sensor will get you the update on all measurements of that sensor. The pattern is a regular expression. There is no expiration date. The throttling is set to minimum (no minimum time between two messages).

malishahi commented 6 years ago

I put here the code is from Feeder for subscription. It does not have httpCustom, etc. Just for reference.

 subscribe(description, cid, endpointUrl) {
       const entities = [];

       entities.push({
           "idPattern": ".*"
       });

       console.info(`Subscribing to entities: ${this.orionConfig.service} 
           ${this.orionConfig.servicePath} `, JSON.stringify(entities));

       const sub = {
           description: description,
           subject: {
               entities
           },
           notification: {
               http: {
                   url: `${endpointUrl}/orion/sensors/update/${cid}`
               }
           }
       };

       if (this.orionConfig.throttling) {
           sub.throttling = this.orionConfig.throttling;
       }

       return new Promise(resolve => {
           rp({
               method: 'POST',
               uri: `${this.orionConfig.uri}/v2/subscriptions`,
               headers: {
                   'Fiware-Service': this.orionConfig.service,
                   'Fiware-ServicePath': this.orionConfig.servicePath
               },
               body: sub,
               json: true
           }, (err, msg, body) => {
               if (err) {
                   log.error(err);
               } else {
                   if (!msg.headers.location) {
                       log.error('Subscription failed.')
                   } else {
                       this.subscriptionId = msg.headers.location.replace(/.*v2\/subscriptions\/(.*)/, '$1');
                   }
               }
               resolve();
           });
       });
   }