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


HC3 QuickApps coding - tips and tricks


jgab

Recommended Posts

  • Topic Author
  • 14 minutes ago, Martin_N said:

    @SmartHomeEddy

    The reason will probably be server overload. Because you have a timeout (HTTPclient) of 5000ms, you will get an error message. If I tested it on a question, in several cases I received an answer after 30 seconds.

    Maybe try to replace  your self:debug("beep") to self:debug("response status:", response.status). See if you get any codes other than 200.

     

     

    I get beeps every ~120s, lower than that and I get timeouts.

    Link to comment
    Share on other sites

    Thanks @Martin_N

     

    I only get 200

     

    Please login or register to see this code.

     

    Yes @jgab

     

    If I put the http = net.HTTPClient({timeout=120000}) indeed, no errors. 

     

    So the conclusion is, the site takes more time. 

     

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    Running some test with the http = net.HTTPClient({timeout=N}) setting. 60*1000 give some errors. 90*1000 minor amount error. 10*1000 no errors. But I think it will depend on the time if day en behavour of the site. 
     

    Now I know it isn’t the code. 
     

    Thanks ? !

    Link to comment
    Share on other sites

  • Topic Author
  • In my QA_Toolbox (v0.17) there is a new module for publish/subscribe.

    Publish/subscribe makes it possible for QAs to communicate with each other without having to know each others' names or deviceIDs.

    QAs  subscribe to the messages they want to receive.

    ...and QAs publish events - and the events are automatically picked up by the QAs that subscribe to them.

    Events will not be sent to QAs that doesn't subscribe to them and the subscribe filter can be quite advanced.

     

    It consist of two function 

    Please login or register to see this code.

    events are Lua tables with at least a type file. Ex. {type='myEvent', value=42}

    (It's the same structure as sourceTriggers have - and the Toolbox module 'triggers' fetches refreshStates events and repost them as sourceTriggers for the QA.)

    event-patterns that are subscribed to can be partial. That means that

    event.pattern {type='myEvent'} matches {type='myEvent', value=42}

    but event.pattern {type='myEvent', value=42} does not matches {type='myEvent'}

    If module "events" is loaded the patterns can also have constraints.

    event.pattern {type='myEvent', value='$>41'} matches {type='myEvent', value=42} but not  {type='myEvent', value=40}, and it also states the an event needs a 'value field.

     

    Ex.

    Please login or register to see this code.

    You will never receive your own 'posts' even if you subscribe to the events you post.

     

    To be able to do something with incoming events you need to register an eventHandler.

    If you load the Toolbox module 'events' it automatically register a handler for it's rules and you can write

    Please login or register to see this code.

    but you can also add your own custom handler

    Please login or register to see this code.

     

    It plays well together with the 'triggers' and 'events' module allowing you to have a common event stream for HC3 triggers (sourceTriggers from devices etc), local events used by the QA internally, and events from other QAs. It's also the same base I use for

    Please login or register to see this link.

    - so I do eat my own dog food ;-) 

    • Like 1
    • Thanks 1
    Link to comment
    Share on other sites

    another one lib ported to HC3:  sha2.lua from Egor Skriptunoff

     

    Support for: MD5, HMAC, SHA-1, SHA-224, SHA-256, SHA-512/224, SHA-512/256, SHA-384, SHA-512, SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128, SHAKE256

     

    I removed everything not necessary on HC3, leaved only INT64 (works best), LIB32 (slower but less CPU load) and bit lib emulation (for testing).
    Examples, FAQ and Benchmark included.
     

    Hashing speed (Bytes per Second) on Fibaro HC3, so there is definitely (bit) difference ^^
     

    Please login or register to see this code.

     

     

     

    Please login or register to see this attachment.

     

     

     

    Edited by tinman
    • Like 5
    • Thanks 1
    Link to comment
    Share on other sites

    Inspired by the code in the sha2 lib, i updated the aeelib (which i posted before somewhere in this topic)

     

    Support for: AES128 / AES192 / AES256 ->  ECB, CBC, OFB, CFB, CTR modes

     

    Now it works something like 77 times faster than before, so i'm happy now.

     

    Changed bit syntax and examples as well.

     

    function aeslib.decryptString(key, data, modeFunction, iv)
    function aeslib.encryptString(key, data, modeFunction, iv)

     

     

     

    Please login or register to see this attachment.

    Edited by tinman
    • Like 4
    • Thanks 1
    Link to comment
    Share on other sites

    Is anyone have tried to change QA global variables using api.put() or api.post() methods?

    Looks like it's blocked by system!?

    Have tried to post the data, no success.

    qData={value={{name="Test",value="value"},{name="myVar",value="10"}},modified=1597468789}
    api.post("/devices/78/properties/quickAppVariables",qData)
     
    Any idea?
     
    Link to comment
    Share on other sites

    3 minutes ago, cag014 said:

     

    qData={value={{name="Test",value="value"},{name="myVar",value="10"}},modified=1597468789}
    api.post("/devices/78/properties/quickAppVariables",qData)
     
    Any idea?
     

    accordint to sorces from Jan

    self:updateProperty('quickAppVariables'self.properties.quickAppVariables)
    Link to comment
    Share on other sites

    22 minutes ago, 10der said:

    accordint to sorces from Jan

    self:updateProperty('quickAppVariables'self.properties.quickAppVariables)

    I'm looking to update variables from other QA or scene.

    Looks like that should work, but still no success

    qData={properties= {quickAppVariables= {{name="Test", value= "900"}, {name= "myVar", value= "10"}}}}
    api.post("/devices/78",qData)
    Link to comment
    Share on other sites

    OK, it works... my mistake should use PUT method

    qData={properties= {quickAppVariables= {{name="Test", value= "900"}, {name= "myVar", value= "10"}}}}
    api.put("/devices/78",qData)
    Link to comment
    Share on other sites

    I am struggling with updating and deleting global variables. This is the function to put json data in some global variables:

     

    This is how I call the function (one example, more than one Global Variable):

    Please login or register to see this code.

     

    This is how the function looks like:

    Please login or register to see this code.

     

    If they don't exist I get:

    Please login or register to see this code.

    What looks OK

     

     

    But if I try to update them, I get

    Please login or register to see this code.

    (HTTP Status Code 400: The server could not understand the request due to invalid syntax.)

     

    The Global Variables does get updated. 

     

     

    And if I try to delete them, I get:

    Please login or register to see this code.

    (HTTP Status Code 404: The server could not understand the request due to invalid syntax.)

     

    And the Global Variables don't get deleted. 

     

     

    Apparently I am struggling when to put and when to post. 

    I am doing it all wrong, but what?

     

     

     

     

     

     

     

     

    Edited by SmartHomeEddy
    Link to comment
    Share on other sites

  • Topic Author
  • To get a variable do

    api.get("/globalVariables/"..name)

    if it doesn't exists you get nil otherwise you get a table {name=..., value=..., }

    To create a variable do

    api.post("/globalVariables",{name=..., value..., ...})

    'value' must be string. There are more keys like readOnly etc... but they default to false.

    To modify an existing variable

    api.put("/globalVariables/"...name,{value=...})

    value must be string

    Edited by jgab
    • Like 1
    Link to comment
    Share on other sites

  • Topic Author
  • Here is an "enhanced" fibaro.setGlobalVariable with an optional flag to create the variable if it doesn't exist

    Please login or register to see this code.

     

    Edited by jgab
    Bug - and check if global is deleted
    • Like 1
    • Thanks 1
    Link to comment
    Share on other sites

    38 minutes ago, jgab said:

    To get a variable do

    api.get("/globalVariables/"..name)

    if it doesn't exists you get nil otherwise you get a table {name=..., value=..., }

    To create a variable do

    api.post("/globalVariables",{name=..., value..., ...})

    'value' must be string. There are more keys like readOnly etc... but they default to false.

    To modify an existing variable

    api.put("/globalVariables/"...name,{value=...})

    value must be string

     

    When do I put a / behind the globalVariables ?

     

    Always for get, post, put and delete, or in case of a post not?

     

     

    Link to comment
    Share on other sites

    1 minute ago, SmartHomeEddy said:

    Always for get, post, put and delete, or in case of a post not?

    The REST API basic knowledges ;) 

    Link to comment
    Share on other sites

    4 minutes ago, 10der said:

    The REST API basic knowledges ;) 

     

    I am confused, according to Swagger alway with get, put, delete. But according to @jgab not in a post. 

     

    And I am doing something wrong 

     

     

    Edited by SmartHomeEddy
    Link to comment
    Share on other sites

    Before

    Please login or register to see this code.

     

     

    After

    Please login or register to see this code.

     

     

     

    Edited by SmartHomeEddy
    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
    Reply to this topic...

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