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


  • 0

Best Practice for API use on Fibaro?


ipsofacto
 Share

Question

All

 

I tend to be a little lazy when using API calls from LUA in VD and Scenes.   I also recently spotted "api.get" in some posts but have seen any information on that in ip/docs or the various fibaro websites.  For those that are considerably more professional in the LUA and Fibaro development than me, could you comment on your best practice e.g. error handling, debugging, parsing JSON etc.  I will try to curate and consolidate in the first entry of this topic for everyone's benefit.

 

Please login or register to see this link.

 

by the prolific 

Please login or register to see this link.

 

Also,  I should of thought Wikipedia for HTTP error codes. 

Please login or register to see this link.

 

So perhaps comment away on the extract from my announcer VD that uses jishi SONOS API bridge below and lets make that a good example of best practice for those learning the ropes (me included)?   

 

thanks in advance

 

Please login or register to see this code.

 

Edited by ipsofacto
update with the tutorial
Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • Please login or register to see this code.

    A bit belt and braces.  Perhaps not worth it once everything is humming along reliably.   In this case,  I have inconsistent response from the Sonos Bridge API on SAYALL commands ... 500 is all I get back in any case.

     

    Also check the following which I will add to as I discover other helpful existing materials.

     

    1. Please login or register to see this link.

    2. Please login or register to see this link.

    3. tbc

     

    I would note that (2) seems to suggest that api.post api.get etc.  are scene and VD compatible (check Sankotronic's responses)

     

    it is nice that the documentation is so up to date ... not helpful for the amateur non-reselling community here...

    Edited by ipsofacto
    Revising for added existing materials.
    Link to comment
    Share on other sites

    • 0

    Regarding your topic title. My best practices when I want to interface to some device, using http and related protocols:

    • Definitely check if the response from a web server is 200. Always. Display at least numerical return code if it is not. Without this, you will always wonder "is it my code, or is it the web server / web interface". Watch out for timeouts and special encoding, like chunked encoding.
    • Do not attempt to construct JSON, XML, HTTP yourself. JSON is easy: convert from (and to) a table with json.decode. Remember: HTTP is sensitive to special characters like 1 and > and <. This is a contrived example: Debug(true, "Blue", "A <DIV> B").
    • Sometimes you need urlencode to construct a valid url.
    • If you want to post to a form, you might want to have a look at my post here: 

      Please login or register to see this link.

    • Use pcall to catch errors from JSON. pcall is standard Lua. Regarding pcall, you can find examples here if you use "search".
    Link to comment
    Share on other sites

    • 0

    Bear in mind that api.get, api.put etc are only for making an API call to the HC2, for situations where there is no equivalent fibaro: command (such as creating global variables).  You can't use them for connecting to external devices as you're attempting to do.  However, as you mention, they're scene and VD agnostic so will run on both, unlike the Net.FHttp (VD only) and net.HTTPClient (Scene only) calls which aren't interchangeable.

     

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • thanks. Api.xxx is for internal HC use only ... been offline for some months and it was new to me this week - a classic chance Fibaro discovery from the forums!

    Link to comment
    Share on other sites

    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.

     Share

    ×
    ×
    • Create New...