HTTP Post , url variables

I have created an action, which fires correctly.
When I receive a GPS position, the goal is to send it to an endpoint on another platform.

We need call something like this:
http://mycustomendpoint/?id={0}&lat={1}&lon={2}

but when we put this url, the system does not correctly parse the variables …
“$ location.lat ” and “ location.lng $”

http://mycustomendpoint/?id=123456&lat=$location.lat$&lon=$location.lng$

Thanks in advances.

Hi @jordi,
I believe the actions do not parse the location parameter like the way you want. The only parameter available is LOCATION, which send the latitude, longitude concatenated.

When things like that is not available, the best way to go is to make your action trigger an analysis and do the post by yourself.
TagoIO has full support to Axios library, which you can use to do the HTTPs post.

Here is a very quick code that post the data in the way you expect:

const { Analysis } = require('@tago-io/sdk');
const axios = require('axios');

async function startAnalysis(context, scope) {
  if (!scope.length) return context.log('Analysis must be triggered by an action');

  // Get the "myvariable" containing location parameter. Change it for your correct variable;
  const { location } = scope.find(x => x.variable === 'myvariable');
  const end_point =  `http://mycustomendpoint/?id=123456&lat=${location.coordinates[1]}&lon=${location.coordinates[0]}`;

  await axios.post(end_point, scope)
  .then(() => {
    context.log('Sucesffully sent.');
  })
  .catch((error) => {
    context.log(`Post failed with status ${error.response.status}`);
    context.log(error.response.data || JSON.stringify(error));
  });
}
module.exports = new Analysis(startAnalysis);

Hi @vitor

Your answer has helped me a lot, I’m new to tagoio. :wink:

One question ‘myvariable’ -> in the bucket of my device, I can see all the variables, do I have to get the variable name?

LiveInspector

{
“variable”: “location”,
“location”: {
“lat”: XX.XXXXX,
“lng”: Y.YYYYY
},
“serie”: NNNNNNNNNNNN
},

Best Regards,
Jordi.

Hi @jordi,
The variable is exatcly field “variable”. It must be the same variable you selected for the action. As far as I can see, you must just change “myvariable” to “location”.

Thanks,
Vitor.

Hi @vitor

Using this:
const { location } = scope.find(x => x.variable === ‘location’);
context.log(‘location var:’ + location.coordinates);

result in Console:
[2020-09-18 15:17:33] location var: 2.494653,42.178698

now, i need to split ? true ? (nodjs)
const array = location.coordinates.split(",");

help ?? :wink: doesn’t works …

Hi @jordi,
Your result seems weird to me. Can you add this line to your code before the const { location }:

context.log(JSON.stringify(scope));

And send me the result of your console?

Thanks,
Vitor.

@vitor

[2020-09-18 17:16:32] [{“id”:“5f64cf507bc6730027de4765”,“created_at”:“2020-09-18T15:16:32.445Z”,“time”:“2020-09-18T15:16:32.445Z”,“bucket”:“5f5f174b835c6e0027073cb6”,“variable”:“decoded_payload”,“origin”:“5f5f174b835c6e0027073cb5”,“serie”:“1600442192412”,“value”:“0c1002a20f2600e8988302424b4fcf645f”},{“id”:“5f64cf507bc6730027de4766”,“created_at”:“2020-09-18T15:16:32.445Z”,“time”:“2020-09-18T15:16:32.445Z”,“bucket”:“5f5f174b835c6e0027073cb6”,“variable”:“gpsfix”,“origin”:“5f5f174b835c6e0027073cb5”,“serie”:“1600442192412”,“value”:“10”},{“id”:“5f64cf507bc6730027de4767”,“created_at”:“2020-09-18T15:16:32.445Z”,“time”:“2020-09-18T15:16:32.445Z”,“bucket”:“5f5f174b835c6e0027073cb6”,“variable”:“location”,“origin”:“5f5f174b835c6e0027073cb5”,“serie”:“1600442192412”,“location”:{“type”:“Point”,“coordinates”:[2.49437,42.178792]}},{“id”:“5f64cf507bc6730027de4768”,“created_at”:“2020-09-18T15:16:32.446Z”,“time”:“2020-09-18T15:16:32.446Z”,“bucket”:“5f5f174b835c6e0027073cb6”,“variable”:“reporttype”,“origin”:“5f5f174b835c6e0027073cb5”,“serie”:“1600442192412”,“value”:“10”},{“id”:“5f64cf507bc6730027de4769”,“created_at”:“2020-09-18T15:16:32.446Z”,“time”:“2020-09-18T15:16:32.446Z”,“bucket”:“5f5f174b835c6e0027073cb6”,“variable”:“battery”,“origin”:“5f5f174b835c6e0027073cb5”,“serie”:“1600442192412”,“value”:75},{“id”:“5f64cf507bc6730027de476a”,“created_at”:“2020-09-18T15:16:32.446Z”,“time”:“2020-09-18T15:16:32.446Z”,“bucket”:“5f5f174b835c6e0027073cb6”,“variable”:“date”,“origin”:“5f5f174b835c6e0027073cb5”,“serie”:“1600442192412”,“value”:“1/19/1970, 12:34:02 PM”}]

Hi Jordi,
So it seems that the value of location.coordinates is an array. That mean you can access this way:

context.log("Location lat: " + location.coordinates[1] + "lng: "+ location.coordinates[0]);

I’ve updated the code in the first post to work this way.

@vitor, perfect ! it works !

@vitor one more question…

In context, I have the device that generated the action?

****. updated ******
I don’t know if it is the best approach, but I have found this solution.

const device_id = scope[0].origin;
context.log(‘device id:’ + device_id);