Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


Question

Posted

Hi

I´m trying to connect to Entur, the central Norwegian travel information hub, to get subway data to my HC2 for in turn på present this on a small display at my entrance door.

I think I have contact with the server but I need to send a json tabel/structure to get some information in return. This structure is as follows:

 

{
  quay(id: "NSR:Quay:10825") {
    estimatedCalls(timeRange: 72100, numberOfDepartures: 1) {
      aimedArrivalTime
      destinationDisplay {
        frontText
      }
    }
  }
}

 

How must I format this table/structure for in turn to use the json.encode command? I´ve tried in several ways but get error messages...

 

(This in the "generator" of the datastructure I need to send to 

Please login or register to see this link.

 along with some headers )

9 answers to this question

Recommended Posts

  • 0
Posted
2 hours ago, tnesheim said:

Hi

I´m trying to connect to Entur, the central Norwegian travel information hub, to get subway data to my HC2 for in turn på present this on a small display at my entrance door.

I think I have contact with the server but I need to send a json tabel/structure to get some information in return. This structure is as follows:

 

{
  quay(id: "NSR:Quay:10825") {
    estimatedCalls(timeRange: 72100, numberOfDepartures: 1) {
      aimedArrivalTime
      destinationDisplay {
        frontText
      }
    }
  }
}

 

How must I format this table/structure for in turn to use the json.encode command? I´ve tried in several ways but get error messages...

 

(This in the "generator" of the datastructure I need to send to 

Please login or register to see this link.

 along with some headers )

 

I think you are out of luck as the api requires https that is not supported by the HC2.

You could try to proxy it over another computer like a raspberry,,,

  • 0
  • Inquirer
  • Posted

    Hi. Thanks, that explains a whole lot!

    • 0
  • Inquirer
  • Posted

    @jgab HTTPS is indeed possible. My problem was to send the payload as a string...which I now managed. I got a proper response body from the API.

    • 0
    Posted
    5 hours ago, tnesheim said:

    @jgab HTTPS is indeed possible. My problem was to send the payload as a string...which I now managed. I got a proper response body from the API.

    @tnesheim

    could you publish you solution..

     

    thanks

     

    • 0
  • Inquirer
  • Posted

    Hi

    I managed to get the https-request to work...the missing piece was the header "checkCertificate = false". My next problem btw is to read out data from the json structure... it returns a nil-error whatever I do...

     

     

    --[[
    %% properties
    %% events
    %% globals
    --]]


    --local payload = tostring('{quay(id:"NSR:Quay:10825"){estimatedCalls(timeRange: 72100, numberOfDepartures: 1){aimedArrivalTime}}}')
    local payload=tostring('{quay(id:"NSR:Quay:10825"){estimatedCalls(timeRange: 72100, numberOfDepartures: 4){destinationDisplay{frontText}aimedArrivalTime}}}')
    --local inndata

    local Url = 'https://api.entur.io/journey-planner/v2/graphql'
    local httpClient = net.HTTPClient();
    httpClient:request(Url, { 
           options = {
           checkCertificate = false,
           method="POST",
           headers={
                   ['content-type'] = 'application/graphql', 
                   ['ET-Client-Name'] = 'TNsmart-tavle', 
                   ['User-Agent'] = 'TNscript for Entur'
                   },
           data= payload
           },
               success = function (response) 
    --         fibaro:debug(response.data) 
               jvar=json.encode(response.data)
               fibaro:setGlobal("Entur", jvar)    
        
               end,
               error  =  function (err) fibaro:debug ("Error: "  .. err) end
       })


    local var=fibaro:getGlobalValue("Entur")
    local jW=json.decode(var)

    test= jW.data.quay.estimatedCalls[2].aimedArrivalTime
    fibaro:debug(test)

    • 0
    Posted (edited)

    Good news, then the service is not that strict on SSL...

    So with asynchronous calls like http:request you have to structure your code a bit different.

     

    Please login or register to see this code.

     

    Edited by jgab
    • Thanks 1
    • 0
  • Inquirer
  • Posted

    Aha, this makes sense, asynchronous! Thanks for the code, will try it tonight. 

    It probably answers why I observered some things in reverse order?

    • 0
    Posted
    35 minutes ago, tnesheim said:

    Aha, this makes sense, asynchronous! Thanks for the code, will try it tonight. 

    It probably answers why I observered some things in reverse order?

    So, if you need to request data with intervals I would recommend a scene structure type below; you 'main' function is driven by your own "events" that tells what to do next; first request, then response, then next request etc... Also use setTimeout to send ourselves "events" in the future (5sec here). setTimeout is also "asynchronous"...

    Please login or register to see this code.

     

    • Thanks 1
    • 0
  • Inquirer
  • Posted

    Thanks a lot! It works, and even better, since I´m not a programmer by profession I rely on looking at examples made by experts, like piece of code. When I need to connect to another API in the future I know where to look!

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...