Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 2 Support (Qt 5) › Infinite loop when doing multiple HttpRequest

Tagged: 

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #23355

    Pierre

    Hi,

    I am very new to QML and programming in general. I am trying to build a private app for my home automation system.

     

    I built an HTTP REST client to fetch data from an API. It works pefectly for one request, but when I use the client function twice on the same page, it keeps recalculating both functions endlessly…

     

    Here is my client code:

    QtObject {
            id: _
            readonly property string apiUrl: "XXX"
            readonly property string apiKey: "XXX"
            property string jsonData: "null"
            property string value: "null"
            //property string collectdate: "null"
    
            function post(method, cmdid) {
                HttpRequest.post(apiUrl)
                .timeout(maxRequestTimeout)
                .set('Content-Type', 'application/json')
                .send({
                          "jsonrpc": "2.0",
                          "method": method,
                          "params": {"id":[cmdid],"apikey": apiKey}
                     })
                .end(function(err, res) {
                    if(res.ok) {
    
                    jsonData = JSON.stringify(res.body.result)
    
                    var keys = Object.keys(res.body.result)
                    //console.log("keys= " + keys)
    
                    var keyid = keys [Object.keys(keys)[0]]
                    //console.log("keyid= " + keyid)
    
                    value = JSON.stringify(res.body.result[keyid].value)
                    //console.log("valeur= " + value);
    
                    var collectdate = JSON.stringify(res.body.result[keyid].collectDate)
                    //console.log("date de collecte= " + collectdate);
    
                    //return value
    
                    }
                    else {
                    console.log(err.message)
                    console.log(err.resonse)
                    }
                }
                )
                return value
             }
       }
    
        function execCmd(cmdid) {
            console.log(_.post("methodtype", cmdid))
            return _.post("methodtype", cmdid)
        }
    }

     

    And my page code:

     

    API{
            id: api
        }
    
    
        AppText {
            text: "Salon : " + api.execCmd(#CMDID_1#) + "°C"
            anchors.centerIn: parent
        }
    
        AppText {
            text: "Bureau : " + api.execCmd(#CMDID_2#) + "°C"
        }

     

    So if I have only one AppText (one CMD_ID), it works fine. But with two CMD_ID, I have an infinite loop on both console and display.

     

    I also tried with Promise, same result…

     

    I have the feeling that I am missing a very simple programming concept…

     

    Thanks a lot for your help
    Pierre

Viewing 1 post (of 1 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded