Jump to content









Photo

Adding calculated values to a stream


  • Please log in to reply
6 replies to this topic

#1 xenonauto

xenonauto

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 09 February 2015 - 11:17 AM

Hi,

 

Is there a way to add a calculated value to a stream, based on the values of the other fields?

e.g. one record in the stream is:

{a: 5,

b:10,

c:30

}

 

I would like to inject a value d to the stream as follows:

d = a^2 + 20* (c + d) /2

 

so that the stream is persisted as follows:

{a: 5,

b:10,

c:30

d: 425

}

 
Is it possible:
a. To modify the stream an inject the value to it before it is persisted i.e. real-time calculation?, or
b. To modify the stream after it is persisted and update it to include the new data value i.e. batch mode calculation?
 
Thanks!
 


#2 amendoza

amendoza

    Master of Puppets

  • Members
  • PipPip
  • 10 posts

Posted 09 February 2015 - 02:48 PM

Hi xenonauto

 

I'm afraid you can't do what your asking for. The closest thing you can do is to create a listener that sends another stream with the calculation done. It's recommendable that the listener send the stream with another device so it won't be in a loop



#3 xenonauto

xenonauto

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 09 February 2015 - 02:51 PM

Thanks amendoza.

How do I create a listener that sends another stream to the carriots platform itself? When I tried creating using api.carriots.com as the URL, it gave me an error that is is not allowed.



#4 amendoza

amendoza

    Master of Puppets

  • Members
  • PipPip
  • 10 posts

Posted 09 February 2015 - 03:00 PM

You can use something like that.

 

import com.carriots.sdk.utils.BasicHttp;

def basicHttp = new BasicHttp( );
basicHttp.url ="https://api.carriots.com/streams/";  
def map = ["carriots.apikey":"YOUR_APIKEY"];
basicHttp.headers = map;
def a = context.data.a;
def b = context.data.b;
def c = context.data.c;
def d = a**2 + 20*(b+c)/2;

basicHttp.payload='{"protocol": "v2", "device": "yourDevice@xenonauto.xenonauto", "at": "now", "data": {"a" :"' + a +  '","b" :"' + b +  '","c" :"' + c +  '","d" :"' + d +  '"}}';
basicHttp.verb ="POST";
basicHttp.send();

 

Remember to set your APIKEY and your DEVICE in the code above



#5 xenonauto

xenonauto

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 09 February 2015 - 04:19 PM

I tried creating a listener with the above code, for "event data received" condition but it did not work. Changed it to "event stream data received", but it still did not create any stream.

I created a new device since you mentioned earlier to do that to prevent looping. Does the new device to which the stream is to be sent need to have some specific properties for the above to work?



#6 amendoza

amendoza

    Master of Puppets

  • Members
  • PipPip
  • 10 posts

Posted 12 February 2015 - 09:32 AM

Do you have any alarm?



#7 xenonauto

xenonauto

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 12 February 2015 - 09:34 AM

No. I have not created any alarm.

No alarms showed up when I was sending data streams either.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users