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

1 hour ago, tinman said:

so it's important to close the socket (to stop the binding made by the QA socket) after one received the data.

 

My need is continually receiving UDP broadcast messages sent to a specific IANA registered port (3639) - so that's just not possible yet in HC3 ?   

Link to comment
Share on other sites

send possible to ANY addresses

but you can rcvd only via port what provide @tinman

just bind HC3 IP and magic port from Tin

Link to comment
Share on other sites

I need to receive UDP messages that are being sent by other external devices to a local broadcast address

e.g. 192.255.255.255  on port 3639  I can't alter that 3639.

 

Fibaro need to include the capability of adding the required port to their firewall I think.

Edited by xAPPO
Link to comment
Share on other sites

7 minutes ago, xAPPO said:

I need to receive UDP messages that are being sent by other external devices to a local broadcast address

e.g. 192.255.255.255  on port 3639  I can't alter that 3639.

 

- solution 1 - wait till Fibaro allow that

- solution 2 - buy raspi and configure it as UDP forwarder

  • Like 1
Link to comment
Share on other sites

  • Topic Author
  • ... and meanwhile I can practice UDPSocket with

    Please login or register to see this link.

    . However, I think it works better in the emulator than on the HC3 at the moment :-) 

    Please login or register to see this code.

    Gives output:

    Please login or register to see this code.

     

     

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

    5 hours ago, jgab said:

    However, I think it works better in the emulator than on the HC3 at the moment :-) 

     

    Nice.... jealous

     

    i notice in your QA toolkit you have a currently commented out method to allow a QA device type to be changed after creation..  is this something that might be doable ?

    Link to comment
    Share on other sites

  • Topic Author
  • 1 hour ago, xAPPO said:

     

    Nice.... jealous

     

    i notice in your QA toolkit you have a currently commented out method to allow a QA device type to be changed after creation..  is this something that might be doable ?

    No, unfortunately I don’t think so.

    Link to comment
    Share on other sites

  • Topic Author
  • Interactive push works (sometimes,. I got some errors)

    Anyway, we can now do an interactive push in a QA that makes a callback to a function of our choice if the user press Yes. This was a bit more complex on the HC2.

    Please login or register to see this code.

     

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

    does anybody know how to delete local QA variable?

    Edited by petrkl12
    Link to comment
    Share on other sites

  • Topic Author
  • 49 minutes ago, petrkl12 said:

    does anybody know how to delete local QA variable?

    Something like this?

    Please login or register to see this code.

    However, the QA will restart when you do the api.put.

    Link to comment
    Share on other sites

  • Topic Author
  • It would be interesting if we could agree about a format for sharing tried and trusted code in this forum.

     

    We have the "files" concept for QAs and we can call QA functions from scenes.

    I see 2-3 kind of demands for sharable code.

    1. To be able to include code in your QA by importing/copying "files".

    1.1 To decide order of files loading

    1.2 To avoid polluting the global namespace with functions from included files - e.g. some kind of module concept.

    1.3. Some mechanism for versioning, like meta-data

    2. To be able to remotely call functions residing in "QA libraries", something necessary for scenes as they can not add/include "files".

    3. Some kind of audit/governance so that code could be deemed trusted.

     

    1. Here, it would be nice to control the load order of "files" - lets call them modules - because we can't guarantee the "load order" of files (except that .isMain seems to be loaded last).

    One approach is that each file keep all their functions inside a initialiser function and add the initialiser to a global

    Please login or register to see this code.

    Then the main function could load each module in the order it wants to load them, or load them on demand.

    Please login or register to see this code.

     

    We could also add more meta-data

    Please login or register to see this code.

    and loading like

    Please login or register to see this code.

    Then it would be easy to create add-ons that copy files from a library QA and keep files updates etc.

     

    2. Remote function library.

    It's easy with the possibility to call QA methods with fibaro.call(<QA id>, <method name>, <args>). This is the only way to get access to shared functions from a scene (without patching the code).

    It's a bit trickier to return a value.

    Please login or register to see this link.

    . Best would be if Fibaro allowed fibaro.call/getValue to return values. 

    The approach would mean to package the functions in QA that makes the functions in them available for remote calls (and they could use 1. to update their "files")

    It would be nice to be able to have a library that could be both used as a sharable file - and as host for remote functions (the same functions)

    In any case, remote function libraries are interesting because they allow share "services" with state - like loggers etc.

     

    3. If Libraries where shared as QAs in the download section, 'likes' could be a sign of trust. Would also be nice with some common repository with an "installer" QA that downloaded and installed shared code and helped with updates.. Kind of an apt-get for the fibaro community.

     

    ...anyway, some first thoughts.  All the mechanisms are there - just need to agree how to share code and best practices.

    Link to comment
    Share on other sites

    I am waiting while @A.Sochaand Fibaro dev will add return value(s) to fibaro.call 

    cuz it’s first and last right method to have and call shared code AKA libs

    IMHO

    No guess why json response from get/post what translated by fibaro.call method now routed to /dev/null 

    Link to comment
    Share on other sites

  • Topic Author
  • Some time back I coded a Lua interpreter using the limited environment we have on the HC2 (no parser libraries etc). It compiled to a "nested Lua functions" representation that is a relatively simple way to create a backend for a runtime. However, because it uses Lua's own stack for its nested calls it's very difficult to "pause " the execution to implement something like coroutines. So I have enjoyed myself to recode the backend for a home-made VM with a classic "flat" code + stack structure. This means that the new version have both coroutines and metatables. 

    The main file in my test QA looks like below and runs a couple of tests - coroutines, metatables, http etc. The QA UI has 3 buttons, to run the code in main() with different types of debug flags. (Note that everything within "function main() ... end" is compiled and run by the interpreter). In a way it's kind of a mini IDE inside the existing QA IDE... :-)  (and it's powered by the "

    Please login or register to see this link.

    " that makes the QA structure easy)

    Please login or register to see this code.

    Note how the HTTP example can wrap the asynchronous net.HTTPClient():request and present it as synchronous calls to the following two "local code,res = httpRequest("http://worldtimeapi.org/api/ip")". 

     

    An example of all bells turned on for a simple piece of code looks like:

    Please login or register to see this code.

    ..with trace:

    Please login or register to see this spoiler.

     

    If I run a speed comparison between some "real" lua code. A 'between' times function coded in "native" Lua and one version loaded into the VM.

    Please login or register to see this code.

    I get a speed diff around ~40x - it varies of course due to the type of code. Note, that the emulated version of 'between' takes ~1ms, which is still pretty fast. So I believe it's usable...

    Please login or register to see this code.

    Besides it being fun to implement Lua with all it quirks this could be used as a base for scripting rules - like ER. The advantage with coroutines is to be able to sleep/wait without blocking code - something I have in EventScript, but this becomes a much more general model. Ex. overlaying a fibaro.call that returns values etc. It would also be possible to implement a real debugger and allow to step through the Lua code...

    Anyway, it's fun :-) 

    Here is the code

    Please login or register to see this attachment.

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

    Thats when hardware developers is lazy.

    we want hook events in QA?! Building own bike

    we want shared library for all QAs? Yes building own bike

    We want .... sure!! Building own bike 

    that’s Fibaro, isn’t it , 

    @A.Socha?

     

    Link to comment
    Share on other sites

  • Topic Author
  • A small trick that can help in debugging.

    When your code crashes in json.encode/decode the error message is a bit lacking...

    Please login or register to see this code.

    or

    Please login or register to see this code.

    The problem is that we only get the information that json died somewhere inside its code... not very helpful. It would be better if we knew what line in our QuickApp that called json when it died.

    Please login or register to see this code.

    This will patch the json functions so that they report the line from where they were called. I have found it to be very helpful...

    (

    Please login or register to see this link.

    )

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

    I have some strange behavior of a QuickApp to retrieve information from the SENSOR.COMMUNITY. I don't know if it is the

    Please login or register to see this link.

    or the source code ??? The SENSOR.COMMUNITY site does now and then doesn't give an answer. 

     

    This is the result, with an interval of 5 seconds, a successful "beep" followed by an error but immediately followed by a successful beep: 

    Please login or register to see this code.

     

    I have removed the most from the source code, to reduce faults. This is the remaining code:

     

    Please login or register to see this code.

     

     

    Edited by SmartHomeEddy
    Link to comment
    Share on other sites

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

     

     

    Edited by Martin_N
    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...