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


Visual Studio Code (vscode) for QuickApp development


jgab

Recommended Posts

  • Topic Author
  • The advantage with an emulator is that we have access to more functions than what's offered on the HC3. With io, and listDir we can easily make a backup script

    Please login or register to see this code.

    This "script" will fetch all the QAs on the HC3 and back them up to the subdirectory ./dev/backup

    Each file will be stored as

    Please login or register to see this code.

    where date and time is the QA's modified time.

    It will prune the directory so only the 3 (configurable)  last version of each QA is kept.

     

    The file is available in the fibemu directory ./examples/backup.lua

    Edited by jgab
    Link to comment
    Share on other sites

  • Topic Author
  • Here is another script example.

    It takes a QA lua file and reads it in and package it as an .fqa file with all extra QA files

    • It initialises some quickAppVariables to pre-defined values.
    • It extract the version of the QA (looks for the code "local version = "xxx"" in the main file)
    • It collects all QuickApp functions tagged as --EXPORT

    Ex.

    Please login or register to see this code.

    It will collect the string

    "function QuickApp:secretFun(x,y) -- takes 2 arguments, try if you dare"

    • It creates an documentation file names <QA name>.doc
    • It creates the fqa file names <QA name>:fqa
    • It makes an zip archive out of the 2 files names <QA name><version>.zip
      (It requires that you have the zip command installed on your system...)
    • It deletes the fqa and doc file, leaving the zip archive.

     

    If you develop QA files for distribution this types of automation scripts comes in very handy....

     

    Please login or register to see this code.

     

    This script is available in the fibemu directory examples/package.lua

    Edited by jgab
    Link to comment
    Share on other sites

  • Topic Author
  • The emulator allows us to easily test interaction between different QAs.

    We can let one QA load another and then call it. The example below is a "Ping" QA that loads a "Pong" QA and then exchanges call between them.

    The magic function is fibaro.fibemu.install that takes the path to a Lua file implementing the QA we want to load. If we have the file open in vscode we can set breakpoints in both QAs and debug both at the same time...

     

    The Ping QA:

    Please login or register to see this code.

    and the Pong QA:

    Please login or register to see this code.

    ...and the output

    Please login or register to see this code.


     

    Edited by jgab
    Link to comment
    Share on other sites

  • Topic Author
  • Another trick.

    Sometimes when debugging the QA in vscode it can be nice to have an eye on the HC3 log to see what's going on there. One could of course have a separate window open for that, but it's also easy to add a function to the QA that loops and fetches the HC3 log and displays it in the vscode debug console. Then one can also use the good filtering mechanism available for the vscode debug console.

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

  • Topic Author
  • 21 hours ago, jgab said:

    Another trick.

    Sometimes when debugging the QA in vscode it can be nice to have an eye on the HC3 log to see what's going on there. One could of course have a separate window open for that, but it's also easy to add a function to the QA that loops and fetches the HC3 log and displays it in the vscode debug console. Then one can also use the good filtering mechanism available for the vscode debug console.

     

    Ok, this turned out to be so handy it comes built in to the emulator now as

    Please login or register to see this code.

    Link to comment
    Share on other sites

    Please login or register to see this code.

    @jgab, I took the example HTTP tester and modified it to check for communication with my ceiling fan device. I am unable to see why I am not getting the results I expect. I have used Postman to POST to the device and found that I need to provide the Content-Length header to avoid the 404 error return. However, when I put the Content-Length header in the emulator I get that error. What am I not seeing?

     

     

    Link to comment
    Share on other sites

  • Topic Author
  • 2 hours ago, PeterV959 said:

    Please login or register to see this code.

    @jgab, I took the example HTTP tester and modified it to check for communication with my ceiling fan device. I am unable to see why I am not getting the results I expect. I have used Postman to POST to the device and found that I need to provide the Content-Length header to avoid the 404 error return. However, when I put the Content-Length header in the emulator I get that error. What am I not seeing?

    A post needs a content-length header so that is provided by the underlying http stack and should not be included. In fibemu it's the standard python library which I trust calculates the content-length header correctly.

    I checked and it sends the correct 'Content-Length' header - so you should not need to set it. I expect that if you provide it it will be overwritten anyway.

    If you try and make the request to "https://httpbin.org/anything" you will get back the data with all the headers received, and you can check. Alternatively use Wireshark to capture and see.

     

    I updated the examples/QA_http_test.lua with the https://httpbin.org/anything POST call.

     

    I would recommend that you create the json from a lua table so there is no issue with encoding, like

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

  • Topic Author
  • 29 minutes ago, jgab said:

    A post needs a content-length header so that is provided by the underlying http stack and should not be included. In fibemu it's the standard python library which I trust calculates the content-length header correctly.

    I checked and it sends the correct 'Content-Length' header - so you should not need to set it. I expect that if you provide it it will be overwritten anyway.

    If you try and make the request to "https://httpbin.org/anything" you will get back the data with all the headers received, and you can check. Alternatively use Wireshark to capture and see.

     

    I updated the examples/QA_http_test.lua with the https://httpbin.org/anything POST call.

     

    I would recommend that you create the json from a lua table so there is no issue with encoding, like

    Please login or register to see this code.

     

     

    Ok, you should not set content-length, net.HTTPClient does that if you provide the data string.

    However, I had introduced a bug where the data became json encoded twice and probably upset the server side.

    It should work now.

    Thanks for pointing me to this bug :-) 

    Edited by jgab
    Link to comment
    Share on other sites

    Thank you @jgab! I saw your first reply and thought I'd lost my mind. I normally use json.encode but wanted to get things down to the fewest moving parts. Contrary to popular opinion, I do try to get things running without resorting to seeking help (I'm a guy, after all, and it violates my code to ask for directions 😊). I do like VS Code a lot. With it I can spend 75% of my time exploring features. But, in all honesty, I'm having fun doing it.

     

    Peter 

    • Like 1
    Link to comment
    Share on other sites

    • jgab featured this topic
  • Topic Author
  • We can call the emulated QA from the HC3 - from a QA or a Scene.

    The trick is that the emulator updates a global variable in the HC3 with the IP address and port pointing back to the emulator we run on or PC or Mac.

    We can then "intercept" fibaro.call and send the request to the emulator

     

    Please login or register to see this code.

     

    Of course, the emulator must be running with our QA 5000 for this to work.

    Now we can set breakpoints in our emulated QA and make sure that it does the right thing when called to do "turnOn"...

     

    To enable this feature you need to set 

    Please login or register to see this code.

    in your config.json file.

    Just because that in some cases it may be annoying to have a global variable updated frequently on the HC3, especially if you log/monitor refreshStates.

    Edited by jgab
    Link to comment
    Share on other sites

  • Topic Author
  • There is some experimental support to run scenes now.

    This QA loads a scene and then changes a global variable to trigger the scene.

    Please login or register to see this code.

     

    The Scene_test.lua file looks like

    Please login or register to see this code.

     

    The scene conditions supported are 'device', 'global-variable', and 'date' (cron, sunset/sunrise and interval).

    I may add more in the future, on request.

    Note, the 'durations not yet supported - need to make some tests to understand the semantic...

    Link to comment
    Share on other sites

    @jgab, I had some stack overflows the past two days. I have been spending my time setting up the VS Code environment the way you recommend so the fault is probably mine. I think it came from the Python runtime but I couldn't fathom how the Lua code could overflow the stack since there isn't any recursion and it is all simple execution.

     

    I also notice that when I add my own UI, the stock UI for the device doesn't appear in the emulator. I haven't dropped it into my hub to observe the behavior but I hope to soon.

     

    Thank you for this excellent development tool.

     

    Peter 

    Link to comment
    Share on other sites

  • Topic Author
  • 6 hours ago, PeterV959 said:

    @jgab, I had some stack overflows the past two days. I have been spending my time setting up the VS Code environment the way you recommend so the fault is probably mine. I think it came from the Python runtime but I couldn't fathom how the Lua code could overflow the stack since there isn't any recursion and it is all simple execution.

     

    I also notice that when I add my own UI, the stock UI for the device doesn't appear in the emulator. I haven't dropped it into my hub to observe the behavior but I hope to soon.

     

    Thank you for this excellent development tool.

     

    Peter 

    Yes, I seen that too - but the last days I haven't seen it. Did you find the new frontend? It had some issues during the weekend (now it's starting to look good,

    Please login or register to see this link.

     )

     

    Yes, the stock ui... I will probably add a flag to force it to be included. 

    For fibaro it's something they "add on the side". I need to add it to the viewLayout. That works. However, I typically don't want it added when I exported it as a .fqa to the HC3 as then the QA will get double "stock" ui. But yes, it something you want in the emulator. Also now with the new frontend I can more easily add some nice UIs....

    Link to comment
    Share on other sites

  • Topic Author
  • To be more specific, the stack overflow error I have is endless recursion of 

    Please login or register to see this code.

    is that the same you have seen? Unfortunately it seems to be deep in the Lua debugger which I have no control over.

    In any case, I made some changes (i.e. added http timeouts so requests don't pile up) . Another thing to try if it persists is to turn off config option fibemuvar=true, if you have it turned on. My error originated from a http call to update the HC3 variable.

    I have run now for 8 hours both on MacOS and WIn11 without seeing the error.

    Link to comment
    Share on other sites

    13 hours ago, jgab said:

    To be more specific, the stack overflow error I have is endless recursion of 

    Please login or register to see this code.

    is that the same you have seen? Unfortunately it seems to be deep in the Lua debugger which I have no control over.

    In any case, I made some changes (i.e. added http timeouts so requests don't pile up) . Another thing to try if it persists is to turn off config option fibemuvar=true, if you have it turned on. My error originated from a http call to update the HC3 variable.

    I have run now for 8 hours both on MacOS and WIn11 without seeing the error.

    while I can't say for sure, I do think that's what I saw. I have not seen it in 3-4 days so I figure you've found it. Because I do boneheaded things, sometimes, I always hesitate to relate what's happening until I have a huge lump on my head from banging it repeatedly against the wall. When I post something, I do so to help myself, and to help you, snuff out any weird bugs I may think I found. Thank you for your efforts in developing this emulator. I can tell it's a labor of love and you do so for yourself but you have shared your work with others. That is appreciated.

     

    Peter

    Link to comment
    Share on other sites

  • Topic Author
  • Ok, now I'll add the stock ui elements (if I have defined them) on top with an divider bar before user elements.

    If you set 

    --%%noStock=true

    It will not be added

    Link to comment
    Share on other sites

  • Topic Author
  • Please login or register to see this code.

    Ok, I bumped the version to fibemu v0.36.

    Beside many bug fixes there is a new frontend ui based on vue.js so it's "reactive"

    Please login or register to see this code.

    it renders the standards buttons of a device type (the "stock ui") on top of user defined ui elements, with a diver below.

    If you don't want that you can specify the QA directive

    --%%noStock=true

    Ex. a QA of type com.fibaro.binarySwitch. Here we get the turnOn and turnOff buttons at the top of the ui with a grey divider before the user defined ui elements.

    Please login or register to see this attachment.

     

    Edited by jgab
    Link to comment
    Share on other sites

    @jgab, I am having fits with my reinstall. I'm trying to get my git source control set up the way you suggest in the docs. I cloned the fibemu repository as a workspace and entered my config info correctly. When I try to run a remote session with qa-ping.lua I get the following debug output:

    Please login or register to see this attachment.

     

    but when I run it locally it all works as expected. I ran a repair on python to no avail. What obvious thing am I overlooking?

     

    Thanks

     

    Peter

    Link to comment
    Share on other sites

  • Topic Author
  • 6 hours ago, PeterV959 said:

    @jgab, I am having fits with my reinstall. I'm trying to get my git source control set up the way you suggest in the docs. I cloned the fibemu repository as a workspace and entered my config info correctly. When I try to run a remote session with qa-ping.lua I get the following debug output:

     

    but when I run it locally it all works as expected. I ran a repair on python to no avail. What obvious thing am I overlooking?

     

    Thanks

     

    Peter

     

    Hi, I was away on "vacation" and did a lot of refactoring of the code without being able to test against the HC3. So, it was an error I fixed now and pushed. 

     

    I'm about to start to tag releases in the repo so it should be possible to checkout an earlier version. I'm announcing that when I start with it.
    I'm a bit reluctant to develop in a separate branch as the code is evolving so much at the moment. Maybe when it reaches v1.0 ;-) 

    Edited by jgab
    Link to comment
    Share on other sites

    "Vacation"? 😯 You don't have time for a vacation. 😎

     

    Ok, I hope you're well rested. Thank you for ALL your efforts.

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