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

Hi,

I'm trying to convert a HC2 code to HC3. I managed to make all the changes and the code is running in HC3 without any issues (at least as far as I can see).

But when I'm using emulator I'm getting this error:

Please login or register to see this attachment.

 

Any idea why?

Link to comment
Share on other sites

  • Topic Author
  •  

    Well, you stumbled on a limitation of the emulator.

    It comes down to that we can't make fibaro.x calls that calls the HC3 from within callbacks of some of the builtin lua functions.

    table.sort and string.gsub are typical such functions.

    The reason is that these functions are implemented in C and when the emulator call the HC3 (to get the value) it will try to suspend the current running thread/coroutine and that's not allowed across C-boundaries.

    The way around it is that I provide pure Lua version of these functions - and I may do that eventually.

     

    However, your code is not that efficient.

    Please login or register to see this code.

     

    The sorting function will be called multiple times to sort the array and it will result in multiple calls to getValue, multiple times returning the same value for the same devices when it call fibaro.getValue.

     

    I would recommend to get all the values before sorting - which is faster (and works with the emulator :-) )

    Please login or register to see this code.

     

    Edited by jgab
    Link to comment
    Share on other sites

    @jgab

    very strange issue with json.encode() (same problem on HC3 platform)

    in example defining array like

     k={test="test",{test1="test1"}}

    and trying to use

        print(json.encode(k))

    I receive an error:

    (fibaroapiHC3.lua:6190: json.encode, fibaroapiHC3.lua:6124: invalid table: mixed or invalid key types......

     

    While running the same on HC2 it works finds and the output is:

    [DEBUG] 04:31:23: {"1":{"test1":"test1"},"test":"test"}

     

    The problem that some  refreshstate responses have same format!!

    Link to comment
    Share on other sites

  • Topic Author
  • Yes, the json encoder doesn't allow for "mixed" keys. 

    Either a table should be a key-value table, that is encoded to json with curly brackets "{" ... "}"

    or it should be an array that is encoded with square brackets "[" ... "]"

    Your array is a mix with one key-value and one array entry (on top-level, then if the array entry in itself is a key-value table doesn't matter). It is a valid Lua table but not a valid json table.

    The HC2 json encoder is not a correct encoder and "invents" missing keys. You end up with the string key "1" for your array entry. It's not a encoder in my eyes...

     

    I have never seen the mixed table response from refreshStates - and then the problem should have been json.decode ? The HC3 send the data in the REST request, json encoded and that should not have worked then?

    You have an example?

     

    Link to comment
    Share on other sites

    • 2 weeks later...
    On 3/15/2020 at 11:34 AM, jgab said:

    Please login or register to see this link.

     Here is a plugin for ZeroBrane studio. 

    Put it in the zerobrane folder, in ZBS/packages/ or HOME/.zbstudio/packages folder (where ZBS is the path to ZeroBrane Studio location and HOME is the path specified by the HOME environment variable)

    Plugin: 

    It's not fully developed yet but has a few features.

    • In ZBS 'Help' menu there are links to this thread, the Fibaro QuickApp manual, and Fibaro Lua Scene manual etc.
    • Under 'File' menu there is a "Create HC3sdk database" command that copies some of the most common resources from the HC3 and stores it in a file in the current working directory. File is named "HC3sdk.db". That file can then be loaded into the emulator when you run a scene/QA by adding the option hc3_emulator.start{loadDB=true}. This only works when running offline, but is a convenient way to have access to "existing" devices and globals in offline testing
    • Under 'File' there is a "Create HC3 backup" that downloads QuickApps, Scenes, Global variables, Locations, and Custom Events to a backup directory - more below.
    • Under 'File' there is a "Download files..." that downloads the selected resource from GitHub  - more below.
    • Under "Edit" there is a menu that inserts in the current window templates for a QuickApps and a Scene with the added hc3_emulator commands needed.
    • There is also under "View" a "HC3 Emulator" command that opens a web page available while a QA/Scene is running.

    Future add-on that would be nice...

    • Upload of Scene to the HC3
    • ...

     

    Please login or register to see this attachment.

    "Copy HC3 data..." copies resource info from the HC3 and stores it in "HC3sdk.db". Can be used in offline-offline mode.

     

    Please login or register to see this attachment.

    "Download files from my Github into the current directory. Requires HC3 credentials to have been setup. By default I try to load "credentials.lua" from current directory to get the credentials.

     

    Please login or register to see this attachment.

    "HC3 SDK templates..." insert a Scene or QuickApp code skeleton in the current buffer to be used with the emulator.

     

    Please login or register to see this attachment.

    Deploy QuickApp will build and deploy the complete QuickApp in the current editor buffer to the HC3. If multiple files (like the toolbox) is included it will add this files to the QA.

     

    Please login or register to see this link.

    Practical browser links to this thread and Fibaro's Scene and QuickApp documentation.

     

    Please login or register to see this link.

    Autocompletion for HC3 Lua commands, fibaro.* etc.

     

    Please login or register to see this link.

    ..with arguments

    Please login or register to see this attachment.

    ...and tooltip

     

     

    200331 - auto completion and tooltips added for HC3 functions.

    200405 - download option for latest fibaroapiHC3.lua

    200512 - resource management

     

    Please login or register to see this attachment.

     

    Any ide why i dont see anything under download files.

    fibaroapiHC3.lua should be placed correct..

    I rebooted ZBS

     

     

    Please login or register to see this attachment.

     

    Link to comment
    Share on other sites

  • Topic Author
  • 51 minutes ago, ChristianSogaard said:

     

    Any ide why i dont see anything under download files.

    fibaroapiHC3.lua should be placed correct..

    I rebooted ZBS

     

     

    Actually in the first post of the emulator:

     

    To get going download the  

    Please login or register to see this link.

     and put in in the working directory where you code your QA(s)

    If you run ZerobraneStudio (recommended) paste this and run

    Please login or register to see this code.

    It's the downloadAsset you are missing - it downloads the templates.

    Link to comment
    Share on other sites

    12 hours ago, jgab said:

     

    Actually in the first post of the emulator:

     

     

    To get going download the  

    Please login or register to see this link.

     and put in in the working directory where you code your QA(s)

    If you run ZerobraneStudio (recommended) paste this and run

    Please login or register to see this code.

    It's the downloadAsset you are missing - it downloads the templates.

    I pretty sure i have done as described.

    I have placed fibaroapiHC3.lua and fibaroapiHC3plug.lua and credentials.lua in described libraies.

     

    and I see an error when running the 

    if dofile and not hc3_emulator then
      dofile("fibaroapiHC3.lua")
    end--hc3

    hc3_emulator.downloadPlugin()
    hc3_emulator.downloadAssets()

     

    Program 'lua' started in '/Applications/ZeroBraneStudio.app/Contents/ZeroBraneStudio/myprograms' (pid: 22323).
    /Applications/ZeroBraneStudio.app/Contents/ZeroBraneStudio/bin/lua.app/Contents/MacOS/lua: fibaroapiHC3.lua:5934: unexpected symbol near '<'
    stack traceback:
        [C]: in function 'dofile'
        ...dio.app/Contents/ZeroBraneStudio/myprograms/download.lua:2: in main chunk
        [C]: at 0x010fa8adc0
    Program completed in 0.07 seconds (pid: 22323).

     

     

    fibaroapiHC3.lua:5934 shows this line

          crc = ((crc<<8) & 0xffff) ~ CRC16Lookup[(((crc>>8)~b) & 0xff) + 1]

     

     

    Please login or register to see this attachment.

    Please login or register to see this attachment.

     

     

    Edited by ChristianSogaard
    P
    Link to comment
    Share on other sites

  • Topic Author
  • You need to set Lua interpreter to 5.3.  (under Project -> Lua Interpreter)

     QuickApps on the HC3 uses Lua 5.3

     

    (I have just added it to the first post, thanks)

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

    8 minutes ago, jgab said:

    You need to set Lua interpreter to 5.3.  (under Project -> Lua Interpreter)

     QuickApps on the HC3 uses Lua 5.3

    That is missed :-)

    thank you Jan

     

    i´m up running

    Link to comment
    Share on other sites

    @jgab

    I am trying to use fibaro.sleep() commands, but the code crushes...  ??? What could be wrong?

    Please login or register to see this attachment.

    Link to comment
    Share on other sites

  • Topic Author
  • It could depend on from where you are calling fibaro.sleep....

    Is it from a callback function given to some built-in function like table.sort, or string.gsub ?

     

    Some Lua builtin functions take functions as arguments - however these Lua functions are coded in 'C' and Lua's coroutines don't allow yielding from a lua function cross a C function ....

    I use Lua's yield when a QA sleeps to give time to other QAs to run so I need to do that.

     

    In my new emulator I have started to code the most obvious functions like table.sort and string.gsub as pure Lua functions to get around this when needed.

    • Thanks 1
    Link to comment
    Share on other sites

    • 3 weeks later...

    Hi,

     

    after moving to a new machine I get

     

    Please login or register to see this code.

     

    with any scene or quickApp.

     

    I looked at line 5934 but there is nothing obvious (the line starting "crc="):

     

    Please login or register to see this code.

     

    For what it's worth, here is some example code that fails, but as mentioned, any code fails:

     

    Please login or register to see this code.

     

    I should mention that I downloaded the latest version 0.315 of fibaroapiHC3.lua; on the previous machine I had an older version, I guess, but I cannot say which one (HDD dead).

     

    Thanks,

     

    jayrock

     

     

    Link to comment
    Share on other sites

  • Topic Author
  • 6 minutes ago, jayrock said:

    Hi,

     

    after moving to a new machine I get

     

    Please login or register to see this code.

     

    with any scene or quickApp.

     

    I looked at line 5934 but there is nothing obvious (the line starting "crc="):

     

    Please login or register to see this code.

     

    For what it's worth, here is some example code that fails, but as mentioned, any code fails:

     

    Please login or register to see this code.

     

    I should mention that I downloaded the latest version 0.315 of fibaroapiHC3.lua; on the previous machine I had an older version, I guess, but I cannot say which one (HDD dead).

     

    Thanks,

     

    jayrock

     

     

    Set Lua interpreter to 5.3 - needed for QAs. 5.3 introduced bit operators like << and >>.

    There is an option in ZBS in the Project menu,.

     

    • Like 1
    Link to comment
    Share on other sites

     

    3 minutes ago, jgab said:

    Set Lua interpreter to 5.3 - needed for QAs. 5.3 introduced bit operators like << and >>.

    There is an option in ZBS in the Project menu,.

     

    That's it, thanks!

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