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


SDK for remote and offline HC3 development


jgab

Recommended Posts

  • Topic Author
  • 10 minutes ago, petrkl12 said:

    OK, now I understand

    I need "only" test of pressing buttons and start correct function in ZBS for debuging 

    btw. in HC2 you created http page with some buttons in ZBS and it could be also possibility how to emulate press buttons 

    Ok, it's been on my todo list to make a fake web UI for QAs - I will have a look at it.

    • Thanks 1
    Link to comment
    Share on other sites

  • Topic Author
  • New version v 0.100

    - New format for credentials file.

     A file "credentials.lua" will be loaded at startup and the table return assigned to hc3_emulator.credentials

    Ex. a file that looks like

    Please login or register to see this code.

    will set hc3_emulator.credentials = { ip = "192.168.1.66", user = "admin",  pwd = "admin" }

    ...which are the necessary credentials to connect to the HC3.

    You can also add other credentials to the table that you need to access other services. To set quickVars to the values do

    Please login or register to see this code.

    this will initialise  the quickVar "HueIP" to hc3_emulator.credentials.HueIP when running QA.

     

    - Web UI support. When running a QA, open http://127.0.0.1:6872/web/main (If you have the fibaroapiHC3plugin you have the short cut option-cmd-e to open browser)

      UI needs to be defined in header.  Ex.

    Please login or register to see this code.

    If you also have a proxy on the HC3 the sliders will be synched...

     

    Here is a little demo

     

    ...and no, I don't think I can emulate UIs for childDevices as they use custom js controllers.

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

    Hello.

    I am very grateful for your great work (sdk)

     

     

    I have problem with new version 0.100.

     

    Please login or register to see this attachment.

     

    Please login or register to see this attachment.

     

    Please login or register to see this image.

    /monthly_2020_07/K-20200717-001071.png.81563af9d68d343e93a429f599304031.png" />

     

     

     

    and...with version 0.99

    It starts when typing   hc3_emulator.start{}    eof.

    if not type "hc3_emulator.start{}", it does not start.

     

    I call fibaroapiHC3 with this code.

     

    Please login or register to see this code.

     

     

    Can you help me?

     

    Link to comment
    Share on other sites

  • Topic Author
  • 12 minutes ago, rangee said:

    Hello.

    I am very grateful for your great work (sdk)

    I have problem with new version 0.100.

    and...with version 0.99

    It starts when typing   hc3_emulator.start{}    eof.

    if not type "hc3_emulator.start{}", it does not start.

     

    I call fibaroapiHC3 with this code.

     

    Can you help me?

    Yes, since .99 the way to include the emulator changed.

    You should do

    Please login or register to see this code.

    ...no .start() at the end (everything moved to to header) and add "not hc3_emulator" to the initial condition.

    and more that the credentials.lua format changed. See

     

    Link to comment
    Share on other sites

    @jgab Okay. THX   :)

    It works but .. something wrong.

     

    Please login or register to see this code.


    Then error occurs. lien fibaroapiHC3.lua's  line 112x and error below.

    Please login or register to see this code.

     

    So I changed...

     

    Please login or register to see this code.

     

    with code,  my code init successfully.

     

     

    ++

    Did you unabled api.call(/refreshStates) ?? then why?

    Can you roll-back it or give options to use or not
     

    Edited by rangee
    Link to comment
    Share on other sites

  • Topic Author
  • 2 hours ago, rangee said:

    @jgab Okay. THX   :)

    It works but .. something wrong.

     

    Please login or register to see this code.

    Then error occurs. lien fibaroapiHC3.lua's  line 112x and error below.

     

    Where does _QVAR come from? You include it yourselves? But if _QVAR is like in your example nothing wrong so far....

     

    Quote

    Please login or register to see this code.

     

    So this doesn't make sense. It seems like you call self:setVariable with number 4 as the first argument. Where did that come from?

    The only way I can repeat that is if I add a '4' key to the _QVAR struct, like

    Please login or register to see this code.

     

    Quote

    So I changed...

     

    Please login or register to see this code.

     

    with code,  my code init successfully.

     

    No, no, no, this will break the code if you do self:setVariable("myVar",{a=9}) which is allowed. The value to setVariable can be anything.

    The bug you need to hunt down is why a number has sneaked into being a key in your quickVars.

     

    Quote

    ++

    Did you unabled api.call(/refreshStates) ?? then why?

    Can you roll-back it or give options to use or not

     

    api.get("/refreshStates?....") is supported when you set hc3_emulator.poll=<ms to poll>.

    What kind of problems do you have? How do you use it?

     

    P.S. I see that the line numbering doesn't match in fibaroapiHC3.lua. Have you added/removed code in the file? If so what code and why? There should be no reason to modify the file and if so let's make it a configurable option...

    Edited by jgab
    Link to comment
    Share on other sites

  • Topic Author
  • 17 minutes ago, petrkl12 said:

    @jgab

    In new version - is there possible to create web UI directly from existing QA?

    No, it must be declared in the code headers as hc3_emulator.UI = ....

    ...and on the HC3 it must be a proxy QA - that's the only way I can get the UI interaction on the HC3 back to ZBS (and the web GUI)

    However, I will provide a function to read the UI of an existing QA and print it out as an "UI" table, so it can be pasted into the code, it that is of any help.

     

    Link to comment
    Share on other sites

    @jgab 

    13 hours ago, jgab said:

    . Have you added/removed code in the file? If so what code and why?

    Oh. I change <function rawCall(method,call,data,cType,hs,to)> to connect hc3 at outside from my home.

    (Because.. I use https to connect at outside)

    I set a boolean value   "hc3_emulator.credentials.https" and add lines...

    Please login or register to see this code.

     

    Anyway.. I'll explain with you orginal fibaroapiHC3.lua  sdk file.

    This is  my init setting and _QVAR and  line 1126's code(self:setVariable  with function Quickapp:__init(Device))

    Please login or register to see this code.

     This is original code from yours.. and this occurs error.

     

    Please login or register to see this code.

     

    So I tried to find out what's wrong.

     

    Please login or register to see this attachment.

     

    There's something strance key  3B, 60, 2C, 48, 0E, 36, 44

    I didn't add that. Where they came from?

    Need more information..

    Please login or register to see this code.

     and finaly...

     

    Please login or register to see this image.

    /monthly_2020_07/K-20200717-630631.png.47e6704b299c304c4878ffb7ea110ae7.png" />

     

    what?   What's going on?

    I just  set   hc3_emulator.quickVars = _QVAR or {}   and _QVAR has no problem.

     

    Link to comment
    Share on other sites

  • Topic Author
  • 40 minutes ago, rangee said:

    @jgab 

    Oh. I change <function rawCall(method,call,data,cType,hs,to)> to connect hc3 at outside from my home.

    (Because.. I use https to connect at outside)

    I set a boolean value   "hc3_emulator.credentials.https" and add lines...

    Please login or register to see this code.

     

    Ok, I'll add a flag for https in the next version. I think it's better to call https.request than hope that http.request will deal with it. (It may change in the future)

     

    40 minutes ago, rangee said:

    Anyway.. I'll explain with you orginal fibaroapiHC3.lua  sdk file.

    This is  my init setting and _QVAR and  line 1126's code(self:setVariable  with function Quickapp:__init(Device))

    Please login or register to see this code.

     

     

    You declare _QVAR before "if dofile" ? ok.

    I assume the loop should be 

    Please login or register to see this code.

    ... but it doesn't change anything. 

     

    Well, setVariable will always asserts that the first arg should be a string otherwise it will throw an error.

    Somehow, someone corrupts your quickVars table. ...and I don't think it's me. But let's continue to look.

    When you have a number key - can you print out the json representation av the value? It seems to be a table,  maybe we can recognise the values in the table?

     

    Can you also give me the whole initial ZBS log when you run the code until it crash?

    Link to comment
    Share on other sites

  • Topic Author
  • @rangee I have uploaded v 0.101 on my gitHub with some extra checks. Try it and let me know.

    Oh, and hc3_emulator.apiHTTPS=true will use https for api calls.    Ok, now it's fixed. just pushed a new version (still v 0.101 though)

    Please login or register to see this link.

     

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

    22 minutes ago, jgab said:

    @rangee I have uploaded v 0.101 on my gitHub with some extra checks. Try it and let me know.

    Oh, and hc3_emulator.apiHTTPS=true will use https for api calls.    Ok, now it's fixed. just pushed a new version (still v 0.101 though)

    Please login or register to see this link.

     

     

    v 0.101  works with no error.

     

     

    And here's error when run with v 0.100

    Please login or register to see this code.

     

     

     

    16 hours ago, jgab said:

    api.get("/refreshStates?....") is supported when you set hc3_emulator.poll=<ms to poll>.

    What kind of problems do you have? How do you use it?


    I use api.get("/refreshStates" ..)  in some of my QA to trigger the device like your ER4.

    some QA(A) not uses api.get("/ref...")  some QA(B) uses.

    Before v 0.98.. it was not problem.

    But after v 0.100,  I need to change   hc3_emulator.poll=ms  when I test QA(B) and hc3_emulator.poll=nil when I test QA(A).

    I do not need polling when I work with QA(A)

     

    But.. if you need to do it this way..  remain it.  I will adapt it.

     

    I found _debugFlags and  set _debugFlags.trigger = false...  no more trigger message.

     

    Link to comment
    Share on other sites

  • Topic Author
  • 8 minutes ago, rangee said:

     

    v 0.101  works with no error.

     

     

    And here's error when run with v 0.100

    Please login or register to see this code.

     

    Good that it solved itself.

     

    8 minutes ago, rangee said:

    I use api.get("/refreshStates" ..)  in some of my QA to trigger the device like your ER4.

    some QA(A) not uses api.get("/ref...")  some QA(B) uses.

    Before v 0.98.. it was not problem.

    But after v 0.100,  I need to change   hc3_emulator.poll=ms  when I test QA(B) and hc3_emulator.poll=nil when I test QA(A).

    I do not need polling when I work with QA(A)

     

    But.. if you need to do it this way..  remain it.  I will adapt it.

     

    I found _debugFlags and  set _debugFlags.trigger = false...  no more trigger message.

     

     

    Yes, fibaroapiHC3 will poll and store events/triggers in an internal queue (polling every ms given in  hc3_emulator.poll).

    All user calls to api.get("/refreshStates...) will get events/triggers from that queue.

    If hc3_emulator.poll=nil it will not add triggers to the internal queue and you will not receive any triggers with api.get("/refreshStates...).  However, if you run in offline mode (hc3_emulator.offline=true) calling simulated devices will push triggers to that queue....

    So there is no way to get /refreshStates without going through the fibaroapiHC3 internal queue now.

    The reason is that the emulator wants to call the HC3's /refreshStates to get current states and if user code also do it it will put too much load on the HC3. Instead user code will get /refreshStates values from the values the the emulator has fetched. It also makes it easier handle "offline" mode to have an internal queue...

    If you don't need polling set hc3_emulator.poll=false

     

    Yes, hc3_emulator._debugFlags have some flags that turn on/off various debugging messages.

    • Thanks 1
    Link to comment
    Share on other sites

  • Topic Author
  • Workflow with fibaroapiHC3

    I thought I'm going to share my workflow when I develop QuickApps these days.

    1. Make sure that you have downloaded the latest fibaroapiHC3plugin.lua from my GitHub and installed it. It gives, besides tooltips and completion for fibaro api commands, also keyboard shortcuts for some practical things.
    2. Make sure that you have a credentials.lua file with the HC3 credentials
    3. Open new empty window in ZBS and choose Edit-HC3 SDK templates...->QuickApp
      This will insert skeleton code in the buffer for a QuickApp with the right headers to include the emulator etc.
    4. Edit hc3_emulator properties, names etc.
    5. Add hc3_emulator.UI table for buttons sliders etc.
    6. No hc3_emulator.proxy yet...
    7. Add code. In your code you can do a  'require("<Lua file>")' to include a file with common functions that you often use in your QuickApps. It should be the filename without .lua. It's best if the file resides in the working directory.
    8. Run
    9. Open the web QA interface with View->HC3 Emulator (shortcut option-cmd-E)
      Check that buttons sliders labels works
    10. If everything looks ok. add  hc3_emulator.proxy=true
    11. Run again
    12. Check that the proxy app on the HC3 works with UI interaction etc. Look at the HC3 log for the QA to see if there is something the emulator missed.
    13. Everything works?
    14. add hc3_emulator.deploy=true
    15. Run
    16. The emulator will create a QuickApp with the real code on the HC3. It will substitute any 'require' in the code with the required file's code, because the HC3 doesn't support 'require'
    17. Voila, you have developed and debugged your QuickApp offline and then deployed the working version to the HC3.

    (video may come...)

    Link to comment
    Share on other sites

    On 4/18/2020 at 10:51 AM, jgab said:

    You need to have installed 

    Please login or register to see this link.

     so that require("mqtt") works from fibaroapiHC3.lua

     

    Would it be possible for you to add a quick post showing how to install luamqtt ? I am running ZeroBrane on Windows 10.    Sorry - I tried but got in a mess...

     

    Please login or register to see this link.

     

    Update 

    I've played some more and it's a lot of work it seems just to add the mqtt library - I have memory errors being thrown from luarock during the install of luasocket and apparently that's a 64 bit vs 32 bit issue so I'm going to give up on the mqtt library install and just use your great sdk for other things.  The installed luasocket version was too old to satisfy the dependency.

     

    If anyone has a step by step or easy install I'll give it a go but for now I'll debug mqtt directly on HC3.

     

    Edited by xAPPO
    more info
    Link to comment
    Share on other sites

  • Topic Author
  • 3 hours ago, xAPPO said:

     

    Would it be possible for you to add a quick post showing how to install luamqtt ? I am running ZeroBrane on Windows 10.    Sorry - I tried but got in a mess...

     

    Please login or register to see this link.

     

    Update 

    I've played some more and it's a lot of work it seems just to add the mqtt library - I have memory errors being thrown from luarock during the install of luasocket and apparently that's a 64 bit vs 32 bit issue so I'm going to give up on the mqtt library install and just use your great sdk for other things.  The installed luasocket version was too old to satisfy the dependency.

     

    If anyone has a step by step or easy install I'll give it a go but for now I'll debug mqtt directly on HC3.

     

    Unfortunately, Windows and I parted ways many years ago...

    Do you run the latest version of ZBS? I think that there were some updates of the Luasocket version, but maybe that's what causes the issue. ZBS has it's own version of luasocket that's come with the installation (and own versions of Lua) so you shouldn't need to install it

    The 

    Please login or register to see this link.

     is an ancient article (2013) and everything (Lua, luasocket) comes with the ZBS installation these days (parts are written in Lua).

    Please login or register to see this link.

     

    .

    Please login or register to see this link.

      is a pure lua library so I pulled it down as a directory named 'mqtt' in my ZBS working directory. require("mqtt") will look in wd.

    Please login or register to see this code.

    But I'm all MacOS.

    @10der, have you tried fibaroapiHC3 with MQTT with VS? Need to install 

    Please login or register to see this link.

     so that require("mqtt") works.

     

    Anyway, I have only played a little with the MQTT functionality so I'm not sure I have got all the messages right (the way they do it on the HC3). So, if someone does something serious with fibaroapiHC3 and MQTT it would be great as we could iron out the differences...

    Edited by jgab
    Link to comment
    Share on other sites

    2 hours ago, jgab said:

    have you tried fibaroapiHC3 with MQTT with VS

    Done. Installed. 
    trying to understand what is next ;) 

    Jan can you give a example what I should test.

    thank you

    Link to comment
    Share on other sites

  • Topic Author
  • 1 hour ago, 10der said:

    Done. Installed. 
    trying to understand what is next ;) 

    Jan can you give a example what I should test.

    thank you

    Well, I manage to run code like this

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    @jgab updated module now req lfs what not installed in my env

    well.  i have fixed. copy from ZBS.

    continue testing

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