Jump to content









Photo

What and how is the format of header, parameters and data when using a trigger?

Trigger

  • Please log in to reply
11 replies to this topic

#1 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 21 April 2013 - 11:03 AM

I've got a trigger setup in my Control Panel.  The trigger, based on server logs, it hitting my external server. However, I can't tell what the headers are or what data is passed.  Can you post an example of what headers are set and what the body of the triggers is for each of the different verbs?  In my case I am using POST because I thought that the body of the post may contain the body of the initial post to Carriots that caused the trigger.



#2 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 21 April 2013 - 11:10 PM

To add some more information to this request.  Below is what data I could harvest from my server logs.  

 

POST
Content-Length: 0
User-Agent: python-requests/1.1.0 CPython/2.7.3 Linux/3.2.0-23-generic
Accept: */*
Content-Type: 
Accept-Encoding: gzip, deflate, compress

 

The Content-Length is 0, and the data or json of the incoming request also had no data.  I thought there should be some of some sort.  Seeing the user agent as Python Requests makes me curious, maybe you have python running in your system?  I'd love to write my triggers and alarms in Python instead of Groovy.  Though learning Groovy has not been difficult.

 

Key information I am looking for is what to expect when using a trigger to post data to an external web server because right now I am not seeing anything.



#3 alvaro

alvaro

    Carriots CTO

  • Administrators
  • 72 posts

Posted 22 April 2013 - 07:43 AM

Hello Zambiot.

 

There is a post thread that talks about that here: http://forum.carriot...les/?hl=payload

 

A simple tutorial illustrating the use of triggers can be helpful: https://www.carriots...APIs/ducksboard

 

Basically, you can configure your HTTP request as you need and you have to fill your payload with the data you want to send using %%% wrapper. For example, if your data contains the key "temp" and you want its value to be sent in your payload you will need to use %%%temp%%% in the payload.

 

Hope it helps!


Alvaro Everlet

Carriots CTO


#4 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 23 April 2013 - 06:36 AM

Thank you Alvaro.  The two links you provided have shown me how to do this.  I'll try them out when I get a chance.

 

I recommend reviewing the "tutorial" on pushing data, as it would benefit from added information that you have in the posts and Ducksboard tutorial.  https://www.carriots...ls/publish_data



#5 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 25 April 2013 - 04:02 AM

Alvaro - 

 

I just tried having a trigger post data to an external server.  I got it to work, but there are a couple questions/comments that I have.

 

  1. The %%% wrapper worked well - that is exactly what I needed to know.
  2. The wrapper seems to only work on data.  I'd like for my external server to also know the "at", "device", and "id_developer", but those items didn't appear to parse when I used %%%at%%%, %%%device%%% or %%%id_developer%%%.
  3. What should the external server return to Carriots on success or fail?
  4. I used Carriots' "Wizard Send Stream" to do two posts to Carriots to test the trigger.  However when I look in my server log it looks like the same stream was posted multiple times to my server.  In my Carriots data stream I only see it posted twice.  Should my server reply with something to acknowledge a successful post?
  5. In the "Wizard Send Stream", I recommend having a button at the end of the process to resend the same stream, with updated time.  I setup a stream as a test stream, so I'd like to send it, test, code, test, send the exact same stream.  But to send a second or third stream, I have to reenter all details in the wizard again.

Thank you for your help.



#6 jpastor

jpastor

    Development leader

  • Administrators
  • 159 posts

Posted 26 April 2013 - 04:52 PM

Hello Zambiot.

I answer point by point

1. ;-)
2. If you use a Trigger, the wrapper only work for properties in the stream data. If you want to send "at", "device" and "id_developer", the best way is to use a Listener.
In a Listener, you have all the device's context fields available and can send them using our "com.carriots.sdk.utils.BasicHttp" library.

In the next example, I send a new stream to another Carriots device, via listener:

import com.carriots.sdk.utils.BasicHttp;
import com.carriots.sdk.Device;

def basicHttp = new BasicHttp( );
basicHttp.url ="https://api.carriots.com/streams/";  
def map = ["carriots.apikey":"MY_APIKEY"];
basicHttp.headers = map;
def device = Device.find("DEVICE@USERNAME");
basicHttp.payload='{"protocol": "v2", "device": "ANOTHER_DEVICE@USERNAME", "at": "now", "data": {"measure" :' +device.device_properties.sum+  '}}';
basicHttp.verb ="POST";
basicHttp.send();

 

3 and 4. You have to send a 200 OK http response if all is all right. Different responses (different from 200) causes Carriots retries up to max retries defined in your trigger property.

 

5. I'm sure that this is a new feature that we'll implement. Thanks for the suggestion.

 

 

Hope it helps!



#7 Zambiot

Zambiot

    Advanced Member

  • Members
  • PipPipPip
  • 94 posts
  • LocationAsia

Posted 16 May 2013 - 11:57 AM

jpastor - Thank you for the response.  I've started to get into BasicHttp and I see how it works.  You mentioned that I need to use a Listener to get access to "at" and "id_developer", but I am not seeing it.  I started another thread on this topic, perhaps you can comment on it?

 

Thank you again for your help.



#8 mayginne

mayginne

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 08 December 2013 - 04:41 AM

Does %%%datakey%%% still work. I have a context.data.Temp variable but when I try

 
import com.carriots.sdk.utils.BasicHttp;
import com.carriots.sdk.Device;
 
basicHttp.http_auth=map;
basicHttp.url ="http://requestb.in/1joy64f1";
basicHttp.verb ="POST";
 
basicHttp.payload='{"value":"%%%Temp%%%"}';
basicHttp.send();
 
 
RequestBin sees this: 
RAW BODY
{"value":"%%%Temp%%%"}

 

Do I have something wrong?



#9 alvaro

alvaro

    Carriots CTO

  • Administrators
  • 72 posts

Posted 08 December 2013 - 03:20 PM

Hi maygine.

 

The %%%Temp%%% notation is only used in triggers forms for custom data push formatting.

When writing scripts or expressions in listeners you have to use context based notation like context.data.Temp

 

Your code would look like the following

 

basicHttp.payload='{"value":"'+context.data.Temp+'"}';

 

 

You can find more information in the SDK context documentation page.

 

Hope it helps.

 

Enjoy Carriots!


Alvaro Everlet

Carriots CTO


#10 mayginne

mayginne

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 09 December 2013 - 12:56 AM

I see, I didn't realize the difference in listener and trigger before. Your answer is perfect though, none of the examples I saw in the documentation try and pass a variable so I had tried payload='{value":context.data.Temp"}'; which of course just passed the string "context.data.Temp". Your answer above is a better example. :-)



#11 mayginne

mayginne

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 08 January 2014 - 08:55 PM

I've been using 

basicHttp.payload='{"value":"'+context.data.Temp+'"}'; for a few weeks, but suddenly I'm getting an Alarm 

:java lang Integer cannot be cast to java lang String. What changed.? I'm assuming a work around would be to explicitly recast context.data.Temp as a string first. 



#12 alvaro

alvaro

    Carriots CTO

  • Administrators
  • 72 posts

Posted 09 January 2014 - 05:18 PM

Hi maygine.

 

The exception message shows that the casting to be performed is from Integer to String. We found that Groovy performs a casting over the second element of a comparison expression so, for example context.data.Temp > 20 will try to cast 20 to String. You can try it in the console writing "30">20 which will throw java.lang.Integer cannot be cast to java.lang.String and 20<"30" which will throw java.lang.String cannot be cast to java.lang.Integer

 

What is your listener if expression? Have you changed your JSON data payload? I mean, was it a Number and now a String?

 

The workaround you mentioned should work, or use a float cast if you need it. For example context.data.Temp.toFloat()>=20

 

Hope it helps.

 

Enjoy Carriots!


Alvaro Everlet

Carriots CTO






Also tagged with one or more of these keywords: Trigger

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users