Help: Webhook Results - Parsing a string with multiple data points and no delimiters?

Hi All,

I’m a newbie working on a little project - the goal is to visualize busses approaching a particular bus stop using a RGB LED strip.

So far I’ve managed to consult the MBTA API for arrival times, parse the times out of the JSON using mustache, and configure a webhook which provides this data through particle publish.

Example of a single result: http://pasteboard.co/1vdrUMSY.png

I was excited to make it this far, but now I’m a bit stuck. When there is more than one bus approaching, I will receive 1-10 epoch times all in a row.

Result ex with 3 busses: 145524366014552434201455243960

What would be the most effective way of pulling and parsing the webhook result in firmware?

Ideally I would like to light a LED in a specific position on a timeline for each of the 3 results above.

Much thanks from a newbie!
Rob


Example of the JSON result I’m working with:

{
  "stop_id": "2152",
  "stop_name": "Concord Ave opp Fawcett St",
  "mode": [
    {
      "route_type": "3",
      "mode_name": "Bus",
      "route": [
        {
          "route_id": "74",
          "route_name": "74",
          "direction": [
            {
              "direction_id": "1",
              "direction_name": "Inbound",
              "trip": [
                {
                  "trip_id": "29668971",
                  "trip_name": "9:15 pm from Alexander Ave @ Leonard St to Eliot St @ Bennett St",
                  "trip_headsign": "Harvard",
                  "sch_arr_dt": "1455243660",
                  "sch_dep_dt": "1455243660",
                  "pre_dt": "1455243628",
                  "pre_away": "1484"
                }
              ]
            }
          ]
        },
        {
          "route_id": "78",
          "route_name": "78",
          "direction": [
            {
              "direction_id": "1",
              "direction_name": "Inbound",
              "trip": [
                {
                  "trip_id": "29669357",
                  "trip_name": "9:05 pm from Wadsworth Rd @ Dow Ave to Eliot St @ Bennett St",
                  "trip_headsign": "Harvard",
                  "sch_arr_dt": "1455243420",
                  "sch_dep_dt": "1455243420",
                  "pre_dt": "1455243420",
                  "pre_away": "1276"
                }
              ]
            }
          ]
        }
      ]
    }
  ],
  "alert_headers": []
}

Example of the mustache code:


{{#mode}}
    {{#.}}
        {{#route}}
            {{#direction}}
                {{#trip}}
                    {{sch_arr_dt}}
                {{/trip}}
            {{/direction}}
        {{/route}}
    {{/.}}
{{/mode}}

Example of the webhook definition:

{
  "event": "op_fawcett_predict",
  "url": "http://realtime.mbta.com/developer/api/v2/predictionsbystop?api_key=5atXWikMk0q8P8RmTMp20Q&stop=2152&format=json",
  "requestType": "get",
  "headers": null,
  "query": null,
  "responseTemplate": "{{#mode}}{{#.}}{{#route}}{{#direction}}{{#trip}}{{sch_arr_dt}}{{/trip}}{{/direction}}{{/route}}{{/.}}{{/mode}}",
  "json": null,
  "auth": null,
  "mydevices": true
}

An alternate question would be, how can I get delimiters in my mustache result?

Does this mean you are getting multiple sch_arr_dt entries in one {#trip}..{/trip} block?
If so, try adding a separator/delimiter into your responseTemplate between the timestamps.
Or are you putting these timestamps back to back yourself on publish?
Can you show your code?

I am- and have not been able to insert delimiters using mustache.

http://stackoverflow.com/questions/8207730/c-how-do-i-split-a-string-into-evenly-sized-smaller-strings. I guess I’ll do something like this

How about just trying ot add a literal there like this?

 "responseTemplate": "{{#mode}}{{#.}}{{#route}}{{#direction}}{{#trip}}{{sch_arr_dt}};{{/trip}}{{/direction}}{{/route}}{{/.}}{{/mode}}",
1 Like

I think that would add one literal after all returns.

What I need is a method in c to split a string Into substrings every x characters and store them in an array.

I think strncpy will work for me.
String length / length of each string to get the number of loops. Then strncpy each string out into an integer.

http://www.tutorialspoint.com/c_standard_library/c_function_strncpy.htm

You mean you get all the timestamps in one sch_arr_dt?
I can’t quite believe that.
Have you tried it and got something back like this

145524366014552434201455243960;

instead of

1455243660;1455243420;1455243960;

What if we make this easier, and you give us the exact code you want to use, rather than ‘examples’? That way we can try things, rather than guesstimating what might work? Just an idea…

1 Like

Adding the delimiter as ScruffR suggested worked. Thank you.

3 Likes

Welcome :+1:

Sometimes trying it out is better than a “good” guess :wink:

1 Like