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 am using an httpclient request to get an address out of json array of google maps. It is working perfect but I would like to have the value out of the request scope/block.

 

I get a value at 1 but I do not get a value at 2. I hope someone can help me. Thank you!

 

longitude    = 52.568197
latitude    = 4.560753

 

        http = net. HTTPClient ()
        http : request ( 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' .. longitude ..',' .. latitude .. '&sensor=false',
            {
            options =  { method =  "POST" },
              success =     function (result)
                          output = json.decode(result.data);
                          address = (output.results[1].formatted_address); 
            
                         print("1:" .. address)
                          end,
          
            error   =  function (err) fibaro: debug ("Errortje:"  .. err); end
            })
   
  
print ("2: " .. address

13 answers to this question

Recommended Posts

  • 0
Posted
1 hour ago, mdejager said:

Hi,

I am using an httpclient request to get an address out of json array of google maps. It is working perfect but I would like to have the value out of the request scope/block.

 

I get a value at 1 but I do not get a value at 2. I hope someone can help me. Thank you!

 

longitude    = 52.568197
latitude    = 4.560753

 

        http = net. HTTPClient ()
        http : request ( 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' .. longitude ..',' .. latitude .. '&sensor=false',
            {
            options =  { method =  "POST" },
              success =     function (result)
                          output = json.decode(result.data);
                          address = (output.results[1].formatted_address); 
            
                         print("1:" .. address)
                          end,
          
            error   =  function (err) fibaro: debug ("Errortje:"  .. err); end
            })
   
  
print ("2: " .. address

 

Hi @mdejager

 

The issue here is that a scene with a http request doesn't execute as you would think.

It issues the http request and then moves onto and executes the rest of the code and doesn't wait for success or fail of the http request

This means that time it executes the print(2..address) it hasn't completed the request and assigned the address to the variable

 

The way I would solve this is to break it into functions and then use the setTimeout to delay the execution of the print(2..address) 

The following code is runable

 

Please login or register to see this code.

with output as follows

 

Please login or register to see this code.

 

hope this helps

-f

 

 

 

  • 0
  • Inquirer
  • Posted

    Cool thanks. I am going to try this

     

    i searched for synnchron/asynchron yesterday. Is there also a solution that the request waits? 

    • 0
    Posted
    17 minutes ago, mdejager said:

    Cool thanks. I am going to try this

     

    i searched for synnchron/asynchron yesterday. Is there also a solution that the request waits? 

     

    @mdejager

     

    No worries 

    I don't know of any sync/async with fibaro

    Let me know how you get on 

    • 0
    Posted

    Hi @mdejager

     

    The HTTP request will be processed asynchronously, and so as AutoFrank has pointed out, the only way to do what you want is to handle the address returned in the success function.  This is called after the HTTP request has completed as a callback function.  However, you could write the success function slightly differently so that all of the success code is moved out of the getAddress function, and this gets a little nearer to what I think you are trying to achieve, eg:

     

    Please login or register to see this code.

     

    • 0
    Posted
    11 hours ago, Dave Harrison said:

    Hi @mdejager

     

    The HTTP request will be processed asynchronously, and so as AutoFrank has pointed out, the only way to do what you want is to handle the address returned in the success function.  This is called after the HTTP request has completed as a callback function.  However, you could write the success function slightly differently so that all of the success code is moved out of the getAddress function, and this gets a little nearer to what I think you are trying to achieve, eg:

     

    Please login or register to see this code.

     

     

    Thanks @Dave Harrison

    I really like the compactness of the error and success handling and might borrow that in future :-)

     

    • 0
  • Inquirer
  • Posted

    Thanks!. I will try this.

     

    Is there also a wait /await function for the http request?

     

     

    • 0
    Posted
    16 minutes ago, mdejager said:

    Thanks!. I will try this.

     

    Is there also a wait /await function for the http request?

     

     

    No, but Virtual Devices have a different http interface, and that one is blocking. It's probably easier to work with.

    • 0
  • Inquirer
  • Posted
    On 12-3-2017 at 10:49 PM, Dave Harrison said:

    Hi @mdejager

     

    The HTTP request will be processed asynchronously, and so as AutoFrank has pointed out, the only way to do what you want is to handle the address returned in the success function.  This is called after the HTTP request has completed as a callback function.  However, you could write the success function slightly differently so that all of the success code is moved out of the getAddress function, and this gets a little nearer to what I think you are trying to achieve, eg:

     

    Please login or register to see this code.

     

    Thanks, It works perfect.

     

    Do i have to close this connection ? 

     

     

    • 0
    Posted
    3 hours ago, mdejager said:

    Do i have to close this connection ? 

     

    I've never seen an explicit close.  So, no, I believe the system closes the connection for you.

    • 0
  • Inquirer
  • Posted

    Ok thanks! 

     

    And do you know how to pass  longitude and latitude (or another var) from getAddress to processAddress? I cant get it to work on the normal function way.

     

     

    • 0
    Posted
    49 minutes ago, mdejager said:

    And do you know how to pass  longitude and latitude (or another var) from getAddress to processAddress?

     

    No, it doesn't work like that because the success function is a callback function and so the returned data is predetermined by the callback, and as far as I know this can't be changed.

     

    In processAddress, you still have available the variables which have been defined outside the functions at the top of the scene, so you could do something like this:

    Please login or register to see this code.

     

    However, due to the asynchronous nature of the HTTP calls, if you're processing multiple requests, then this won't work.  Take the following example:

    Please login or register to see this code.

     

    Then you get:

    Please login or register to see this code.

    The first address shows the requested latitude and longitude from the second address.

     

    You're best off using the location returned by the HTTP request, if you can.

     

    Dave

    • 0
  • Inquirer
  • Posted

    I fixed it like this. Seems to work.

     

            success =    function (result)
                                ProcessAddress(result,var2)
                                end,
            error    =    ProcessError  

    • 0
    Posted

    Hi @mdejager

     

    1 hour ago, mdejager said:

    success =    function (result)
                                ProcessAddress(result,var2)
                                end,

     

    Nice one!  Well done for figuring that one out.  It works well.

     

    Dave

    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...