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

2 hours ago, jgab said:

Thanks, fixed in v0.200.2

thanks, now it's OK

Link to comment
Share on other sites

@jgab

Using latest v0.200.2 in VS code it crashes, here is the error

 

Please login or register to see this spoiler.

 

Edited by cag014
Link to comment
Share on other sites

  • Topic Author
  • Ok, fixed. You are the only one that seems to have tested the new version with VS...

     

    The emulator now have "multithreaded" I/O, meaning it's heavily structured around coroutines. Each QA, Scene and setTimeout function run in a coroutine. If they do I/O (ex. api.get or net.HTTPClient, the coroutine gets "outscheduled" and another coroutine gets a chans to run, then it will resume when the IO is done.

    This means that the debugging experience will rely on the Lua debugger used - mobdebug in ZBS is reasonable good at it - at least predictable - and provides some features that I leverage in ZBS.

    In VS mobdebug is disabled and I don't know how the experience with debugging coroutines is in that environment...

     

    Btw, there is a new "major" version about to be released real soon now...

     

     

    Edited by jgab
    Link to comment
    Share on other sites

  • Topic Author
  • New version 0.299 released. I will give it a week or 2 to kill bugs and release it as 0.300.

    It's feature rich enough to run EventRunner, GEA QA @lazer, Sonos @Krikroff, and Hue-all-in-one@petrkl12 - that is my system verification staging pipe... :-) 

     

    There is a new plugin for ZBS with custom code templates, and menu option to deploy scene/QA from IDE to HC3 in one click.

    Many changes and new features. I will start to update the first post with the current features and remove old stuff  not relevant anymore.

    ...will also create a series post on specific features.

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

  • Topic Author
  • The emulator can be used in different ways depending on what we develop and how our workflow look like.

    Below is a couple of different ways - and they can be combined & mixed.

     

    Please login or register to see this attachment.

    • Offline. We can run it completely "offline", meaning that the emulator doesn't communicate with the HC3. Http requests to other systems work of course.  In this model we can, besides the QA or Scene we develop,  create local device, globals, events etc. that our QA and Scene can interact with. Devices and globals can be automatically created when first called upon. A fibaro.getValue(88,"value") can create the local device 88 on the fly. This can be very convenient when testing out stuff. 
      In the picture below it is the left part with our QA and 2 local green resources. This allows our QA to turn on/off a local light device or set global variables and retrieve them. 
      We can open a web browser interface to the emulator where we can observe the state of the devices and see if our commands worked. In the same web interface we can also interact with our QA if it has an interface(buttons, sliders, labels). It's not a perfect rendition of the interface on the HC3 but close enough so we can debug our UI.

      This is my preferred way when quickly developing a small piece or when testing some code fragment posted on the forum that some forum member has issues with.
       
    • Mix offline. We can still run with connection to the HC3 but create local devices that we also interact with. A local created device or global will shadow a device or global with the same name.

      This is also a common way to work when testing shorter scripts but I still want to interact with real devices.
       
    • Proxy. We can automatically create a proxy QA on the HC3 when we debug our local QA. This allows other QAs on the HC3 to interact with the proxy. The proxy then sends all  commands it receives (or keypresses in the UI), back to our QA we develop offline. This way our code that we debug has a "presence" on the HC3 and we can debug interactions between our QA and other parts of the live system on the HC3.  If we update a property in our offline QA, the proxy's property is also updated allowing Scenes on the HC3 to trigger on it etc. QiuckAppVariables are also updated in the same way. When we send UI interaction back to the QA it allow us to use the "full power" of UI elements on the HC3.

      I use the proxy model when it's important that my QA under development should interact with QAs and devices on the HC3 when I debug the code.
       
    • QA interception. (I don't have a better name for this). In this case there is already a "real" QA on. the HC3 the twe want to use as "proxy". When the emulators starts it will add a file to the QA on the HC3 that takes over all calls and UI interactions of the QA (onAction() and UIEvent()). It's harmless for the QA and when the emulator stops it gives the control back to the real QA again. We can also tell our QA code to get the quickAppVariables and viewLayout from the  real QA when we start the emulator.
      This way of working can have side-effects if not carefully carried out as the real QA is still technically "running".
       
    • We can download devices, global variables etc into a file that we load at startup as local devices. This way we can "interact" with local versions of the data that is we have on the HC3. Devices with correct deviceIDs etc..

      Great way to work when you are at the beach and can't access the HC3...
       
    • We can run code that is neither a QA or Scene, but still have full access to HC3 APIs, json.encode etc but also the full Lua. As an example the ZBS plugin has a code template that is a script that does a QA and Scene backup from the HC3 and store them in a date named subdirectory locally.

      Works well for developing management and housekeeping scripts. But also to test code using a HC3 Lua. environment with json encoding,  setTimeout and fibaro.*. functions.
       
    • We can run plain code as above but load QAs and Scenes dynamically with hc3_emulator.loadQA(...) and hc3_emulator.loadScene(...) - and we can load multiple QAs and Scenes and run and debug them in parallel.

      Best way to test complex QA to QA or Scene interactions
       
    • We can run the code faster than realtime. os.speed(true) makes the clock speed. This useful for offline mode, but also for testing schedulers that run over weeks or month. Now it takes seconds. Running online with speed may make your lamps flicker... Oh, we can also set an arbitrary start date. Make the code start the day before DST or other dates that the code should treat special and see if it really works.

      I use this all the time when debugging EventRunner as it takes too long time waiting for timed rules to trigger.
       

    So, the task at hand and the choice of workflow decides what tools from the toolbox to use. When the QA/Scene works in the emulator it can be deployed with a click to the HC3 as the real thing - or saved to file as a .fqa to upload to a forum...

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

    @jgab

    Still have an error v0.299

    Please login or register to see this spoiler.

     

    Link to comment
    Share on other sites

  • Topic Author
  • 3 hours ago, cag014 said:

    @jgab

    Still have an error v0.299

    Please login or register to see this spoiler.

    Sorry, I made it a requirement to end the header with a 

    Please login or register to see this code.

    So, the structure of the header becomes

    Please login or register to see this code.

    The reason is that I need to parse out the header in a safe way. In the past I assumed that the last end was an end that started a line. However, that doesn't necessarily work as the header can contain arbitrary Lua code including if-then-end etc. It was also easy that a space ended up before the end making my regexp missing the pattern.

    So, it now requires that you mark the last end of the header in this way. 

    The error message tries to hint at it

    "Malformed emulator header, forgot 'end--hc3'?" and it was discussed in some of the last posts.

    I will highlight it better in the first post.

    Link to comment
    Share on other sites

    @jgab

     

    In newest version in child devices - there is after click in child this event:

    [11.04.2021] [10:13:03] |[34;1mSDBG [0m|: onAction: {"deviceId":906,"args":[],"actionName":"btnChValFromDevClicked"}
    [11.04.2021] [10:13:03] |[33mWARN [0m|: Class does not have btnChValFromDevClicked function defined - action ignored

     

    but in HC3 it calls function btnChValFromDev (not btnChValFromDevClicked)

     

    Link to comment
    Share on other sites

    3 hours ago, jgab said:

    "Malformed emulator header, forgot 'end--hc3'?" and it was discussed in some of the last posts.

    I will highlight it better in the first post.

    OK,

    in credentials I use variables to set ip, user and pwd. in this version it shows an error (attempt to index a nil value

    Just for debugging have set the fixed credentials and ran into a new error

    Please login or register to see this spoiler.

    I'm trying to run it totally offline as I need the fibaro library only to simulate Quick Application and fibaro commands

    Link to comment
    Share on other sites

  • Topic Author
  • 1 hour ago, cag014 said:

    OK,

    in credentials I use variables to set ip, user and pwd. in this version it shows an error (attempt to index a nil value

    Just for debugging have set the fixed credentials and ran into a new error

     

    I always run with credentials set (even when running offline). However, I used the. credentials to pre-configure stuff not need. when. offline. I have hopefully fixed that now.

     

    1 hour ago, cag014 said:

    Please login or register to see this spoiler.

    Is it possible to PM me the QA or is it too complex? :-)

    I guess you are using custom UI for the child?

    Link to comment
    Share on other sites

    51 minutes ago, jgab said:

    Is it possible to PM me the QA or is it too complex? :-)

    I guess you are using custom UI for the child?

    as usually too complex :)

    and yes I'm using custom UI for the child. My QA will be sent via PM

     

    Link to comment
    Share on other sites

    @jgab

    Looks like you're trying to validate the entire header and fails on Lua commands (like print) and still credentials don't accept parameters.

    after workaround the header the follow error appears. (Lua command require not recognized?)

    Please login or register to see this spoiler.

     

    Link to comment
    Share on other sites

  • Topic Author
  • 2 hours ago, cag014 said:

    @jgab

    Looks like you're trying to validate the entire header and fails on Lua commands (like print) and still credentials don't accept parameters.

    after workaround the header the follow error appears. (Lua command require not recognized?)

    Please login or register to see this spoiler.

    Yes,  I'm very restrictive on lua functions used in the header (actually none).

    The reason is that the header is evaluated twice - I need to do that to bootstrap the emulator. If you do a print it will print twice... Actually, the first time the header runs (to get to the dofile) the whole environment is available,  the second round no functions are available, and that is the important round as it is when I expect the hc3_emulator to be setup for real.

    There is a mechanism with a credentials.lua file and $CREDS macro to fetch parameters from that file that is the recommended way to include credentials. I would recommend that you look into that model - is described in first post.

    You can try to add functions (like require) to line 4339

    Please login or register to see this code.

    ...and see if it works. Be aware to not have side-effects as it runs twice.

    I add "dofile=function() end" to null the dofile the second time to not include the emulator twice.

    Link to comment
    Share on other sites

  • Topic Author
  • Ok, v0.299.4 

    require() is allowed in the header.

    It works well because require only load once even if called many times. 

    Note that only stuff inside the hc3_emulator table will be carried over to the actual code. But you can use the required file to setup the hc3_emulator table if you want...  The required file run with full Lua context, so ex. print is available.

     

    Secondly

    Please login or register to see this code.

    will create a context for Scenes and QA with access to all standard Lua functions. Normally I restrict it to what's available in the HC3's Scene or QA context.

    Edited by jgab
    Link to comment
    Share on other sites

    @jgab

     

    I have some code for auto updating of QA but there is problem that main file or other files in QA are not updated in calling 

        local QAF = api.get("/quickApp/"..id.."/files/"..f.name)
     

    how to setup for testing these function?

     

    I have

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

  • Topic Author
  • 41 minutes ago, petrkl12 said:

    @jgab

     

    I have some code for auto updating of QA but there is problem that main file or other files in QA are not updated in calling 

        local QAF = api.get("/quickApp/"..id.."/files/"..f.name)
     

    how to setup for testing these function?

     

    I have

    Please login or register to see this code.

     

    Well,

    the code is there to get and update files but it only do it on the local QA, not the QA on the HC3

    I see now that I should also restart the local QA so the new code is actually used. I will fix that.

    I think that if you want to reflect the change to the QA on the HC3 also, it should be an option as it only makes sense in some specific cases...

     

    Link to comment
    Share on other sites

    8 minutes ago, jgab said:

    Well,

    the code is there to get and update files but it only do it on the local QA, not the QA on the HC3

    I see now that I should also restart the local QA so the new code is actually used. I will fix that.

    I think that if you want to reflect the change to the QA on the HC3 also, it should be an option as it only makes sense in some specific cases...

     

    Thanks. 

    I need that after update of QA I do the check if new files are same as in source QA

    Link to comment
    Share on other sites

  • Topic Author
  • 1 hour ago, petrkl12 said:

    Thanks. 

    I need that after update of QA I do the check if new files are same as in source QA

    Ok, v0.299.5

    Set hc3_emulator.HC3quickAppFile=true

    and it only works for QAs where hc3_emulator.proxy is a number (eg reusing an existing QA)

    It will then reflect the /quickApp/... commands to the QA on the HC3

    Link to comment
    Share on other sites

    3 hours ago, jgab said:

    Ok, v0.299.5

    Set hc3_emulator.HC3quickAppFile=true

    and it only works for QAs where hc3_emulator.proxy is a number (eg reusing an existing QA)

    It will then reflect the /quickApp/... commands to the QA on the HC3

    Thanks but in newest version click on QA button in HC3 is not sent to ZBS with this setting:

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

  • Topic Author
  • 46 minutes ago, petrkl12 said:

    Thanks but in newest version click on QA button in HC3 is not sent to ZBS with this setting:

    Please login or register to see this code.

     

    OK, try 0.299.6

    I have still not had time to look at the child buttons (...Clicked) problem. Will try to find time tonight,

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