Jump to content

HC3 QuickApps coding - tips and tricks


Recommended Posts

2 hours ago, jgab said:

The "interesting" feature is that the file formats for the files are "flat" - meaning that they can be downloaded and run in the offline IDE - no encoded json format.

Hehehe... For HC2 I wrote a "de-VFIB-rilator", it "flattens" a bit like unzip, but I never finished it due to me not spending much time with VDs. For FQA, I was thinking about JSON to YAML conversion, that would play nice (or nicer) with diff tools. Do you have a tool too flatten/deflatten FQA?

Link to post
Share on other sites
  • Replies 876
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

This thread is most about QuickApp tricks and usually requires some deeper understanding of Lua. I'm going to make an attempt to do something more tutorial wise that could work for newcomers to Lua. I

The anatomy of QuickApps – Part 2 (Part 1)   Disclaimer1: We are now venturing into undocumented land. That means that Fibaro is free to change how things work at any time. Well, Fibaro

A thread to share some coding techniques for QuickApps?  Because QAs are "long running scenes" (they don't have to be loaded and restarted for every event) - it is actually worthwhile to build up

Posted Images

2 minutes ago, petergebruers said:

Hehehe... For HC2 I wrote a "de-VFIB-rilator", it "flattens" a bit like unzip, but I never finished it due to me not spending much time with VDs. For FQA, I was thinking about JSON to YAML conversion, that would play nice (or nicer) with diff tools. Do you have a tool too flatten/deflatten FQA?

 

No I don't have any tool, as I code directly in my "flat format" :-) 

json is pretty ok as there is json.decode readily available on the HC3.

The thing is that I want a file that looks like a Lua file so it fits in my workflow. I can run and debug the QuickApp offline and when finished push it to my repository and download it to the HC3.

The offline environment (fibaroapiHC3.lua) can also on the fly create a proxy using the info in the file (header comments with type and UI definition)  and install it on the HC3 so I can debug interactions with buttons and fibaro.call methods.

 

It's pretty smooth now and far from trying to develop on the HC3 alone .... ;-) 

Link to post
Share on other sites

Have tried to find away to get response from QuickApp in scene without success.

Is anyone knows if there is away to access QuickApp variables from scene or to get any response? Without using system's global variable.

Nothing found useful in documents

Edited by cag014
Link to post
Share on other sites
24 minutes ago, cag014 said:

Have tried to find away to get response from QuickApp in scene without success.

Is anyone knows if there is away to access QuickApp variables from scene or to get any response? Without using system's global variable.

Nothing found useful in documents

api.get("/devices/"..id).properties.quickAppVariables

 

In general it would be nice if the call fibaro.getValue(deviceID,<property>) would call the function QuickApp:getValue(<property>) .... end

and a value could be returned...

Link to post
Share on other sites
6 minutes ago, jgab said:

api.get("/devices/"..id).properties.quickAppVariables

I know, but it force you to deal with jSON that includes all global variables... I thought finally fibaro did something that we can receive value and not a table.

Edited by cag014
Link to post
Share on other sites

Here is my small contribution (compered to @jgab efforts and tremendous output)

Attached QuickApp as a template for HTTP GET requests.

For use in scene use follow

fibaro.call(<QD-ID>, "getHttp",url)

Currently no response received. To get response, please define global variable and read it in your scene.

Addition features:

1. by pressing "Test HTTP" button you should receive HC3 service status

2. by clicking on QD icon (like turn ON/OFF) you should receive HC3 LEDs status.

3. Any above action will set icon to "ON" state during HTTP execution and back to "OFF" after...

 

Since icons are not transferred here custom icons for QuickApp

router_off.png.5dc06b168945350cc1984e072ca5dc51.pngrouter_on.png.a50a064e4e884bd62092629e9f2fb1fe.png

 

Currently QuickApp label is not resizable.  Should be fixed in next release

 

 

 

getMyHttp.fqa

Edited by cag014
  • Like 2
Link to post
Share on other sites

Fibaro must to give back HTTP ability to scenes... now I have scenes in my device list... this is an absurd.

Another disturbing issue, by creating QuickApp device you cannot press buttons in block scenes (on/off/toggle only). This is outrages....Same true for sliders.

Now all users with no Lua knowledge, needs to learn it just to have an option to activate buttons.... very bad business wise decision.

Edited by cag014
Link to post
Share on other sites
16 hours ago, cag014 said:

Fibaro must to give back HTTP ability to scenes... now I have scenes in my device list... this is an absurd.

Another disturbing issue, by creating QuickApp device you cannot press buttons in block scenes (on/off/toggle only). This is outrages....Same true for sliders.

Now all users with no Lua knowledge, needs to learn it just to have an option to activate buttons.... very bad business wise decision.

Here is a replacement.

Make a QuickApp:

function QuickApp:RPC(tag,url,options)
    local function result(res,timeout)
        api.post("/globalVariables",{value=(json.encode(res)),name=tag})
        setTimeout(function() api.delete("/globalVariables/"..tag) end,timeout)
    end
    --self:debug(tag," ",url," ",type(options))
    net.HTTPClient():request(url,{
       options = options,
       error = function(res) result({error=res},options.timeout or 5000) end,
       success = function(res) result({success=res},options.timeout or 5000) end})
end

function QuickApp:onInit()
    self:debug("RPC")
end

and then from a scene (assume the previous QuickApp has deviceID 51)

local function _remoteHTTTPreq(_,url,options)
    local tag,last,count = "RPC"..(tostring({})):match("0x(.*)"),0,0
    local TIMEOUT = options.timeout or 5000
    fibaro.call(51,"RPC",tag,url,json.encode(options.options))
    local function check()
        local value = fibaro.getGlobalVariable(tag)
        if value and value ~="" then 
            value = json.decode(value)
            if value.error and options.error then options.error(value.error)
            elseif options.success then options.success(value.success) end
            api.delete("/globalVariables/"..tag)
            return
        end
        if count >= TIMEOUT then if options.error then options.error("Timeout") end
        else count=count+250; fibaro.setTimeout(250,check) end
    end
    check()
end
net = {HTTPClient = function() return { request = _remoteHTTTPreq } end}


--- Old style net.HTTPClient():request() in scenes ---------------

net.HTTPClient():request("http://worldtimeapi.org/api/timezone/Europe/Stockholm",{
    options = { 
        method = 'GET' ,
        headers={['Accept']='application/json'}
    },
    success = function(s) fibaro.debug("",json.encode(s.data)) end,
    error = function(s) fibaro.debug("",json.encode(s)) end
})

 

Maybe time to do a compatibility library for old scenes? :-) 

  • Like 1
Link to post
Share on other sites

What Fibaro didn't implement is forcing the users to reinvent the wheel.

Thanks to @jgab and his ingenious solutions we are able to move a bit with HC3. Otherwise we woul'd suffer under the following algorithm:

 

if CEO==salesman and (product== horrible or immature) then
   release = asap
end
fibaro:debug("Just wait for eternity")
fibaro:sleep(forever)

In fact, it's not funny at all

  • Like 2
Link to post
Share on other sites
34 minutes ago, jgab said:

Maybe time to do a compatibility library for old scenes? :-) 

 

please don't … i think it might be better idea to rethink how the old scenes has been used and how HC3 can be used to do the Job better.

  • Like 1
Link to post
Share on other sites
1 hour ago, jgab said:

Maybe time to do a compatibility library for old scenes? :-) 

As I said several time, everything could has an workaround even entire HC3 could have an workaround by other brand...😁

Edited by cag014
Link to post
Share on other sites

Jan,

Slendid idea! I will be happy with a kind of translation HC2>>HC3.

Laters we can smouth it out a bit but as far as I see what you are doing the smotth is already in!

Its a pitty Fibaro didt hire you for a few months to make the HC3 more users friendly (and easier)

Your service is excellence!!

Perhaps some Fibaro developpers should read the forum.

Thanks Jan

//Sjakie 

Link to post
Share on other sites

@jgab

Any workaround for pressing buttons and slider to support block scene? Regular users  have been abandoned.

Currently I need your advise on how to write a scene to support fibaro key fob (CentralSceneEvent) .

Since fibaro:getSourceTrigger() not supported in HC3 it looks like for each combination of keyId and attribute you need to write a scene !!?? This is an absurd.

Any idea?

I don't have Fibaro key fob, so don't have a clue if last action could be retrieved from property by api.get()

Edited by cag014
Link to post
Share on other sites
44 minutes ago, tinman said:

just wait till new fw

Are you sure they will give back this function?

May HTTP also... keep my finger crossed

Link to post
Share on other sites
2 hours ago, tinman said:

 

please don't … i think it might be better idea to rethink how the old scenes has been used and how HC3 can be used to do the Job better.

On second thought you're absolutely correct.

👍

Link to post
Share on other sites
9 minutes ago, cag014 said:

Are you sure they will give back this function?

May HTTP also... keep my finger crossed

sourceTrigger seems to be a variable in the next release - it's documented in the Lua scene docs for the HC3.

With this condition (that matches all keypresses from key fob 53)

{
  conditions = { {
      id = 53,
      isTrigger = true,
      operator = "match",
      property = "centralSceneEvent",
      type = "device",
      value = {
       keyAttribute = "Pressed", 
        keyId = '*'
      }
    } },
  operator = "all"
}

Below works for me to pick up a keyfob event in a future compatible way in a scene

for _,e in ipairs(api.get("/refreshStates?last=1").events or {}) do
    if e.type=='CentralSceneEvent' then 
     sourceTrigger={
         type='device', 
         value={keyId=e.data.keyId,keyAttribute=e.data.keyAttribute},
         property='centralSceneEvent', 
         id=e.data.deviceId} 
     break
  end
end


fibaro.debug("",json.encode(sourceTrigger or false))

 

 

Edited by jgab
Link to post
Share on other sites
2 minutes ago, jgab said:

Below works for me to pick up a keyfob event in a future compatible way in a scene

No, This is a guessing when it happened. You need exact number of last refreshStates otherwise  It could be an old event or few events could be in same refreshState.

 

 

Link to post
Share on other sites
1 minute ago, cag014 said:

No, This is a guessing when it happened. You need exact number of last refreshStates otherwise  It could be an old event or few events could be in same refreshState.

What is an old event? If the scene has a CentralSceneEvent trigger it will eat all centralscene events....

I never managed to get more that one centralsceneevent in the refreshState, no matter how fast I press a button... but hey, it just a hack waiting for the real sourceTrigger to arrive.

Link to post
Share on other sites
30 minutes ago, jgab said:

What is an old event? If the scene has a CentralSceneEvent trigger it will eat all centralscene events....

I never managed to get more that one centralsceneevent in the refreshState, no matter how fast I press a button... but hey, it just a hack waiting for the real sourceTrigger to arrive.

I'm curious how do you use refreshState?

Is  it always

/refreshStates?last=1

You know that you need to get next correct last number?

Link to post
Share on other sites
  • jgab changed the title to SDK for remote and offline HC3 development.

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