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


  • 0

Bailing out of a function after another function call.


Rikbast

Question

Hi,

 

I have a function which calls another function to obtain a new token, GetHuaweiToken() when error 305 appears.

After that it should bail out the existing function and not follow through the current function , is that possible and if yes, how?

 

I enclose the code:

 

function QuickApp:getStationRealKpi()
self.getStationRealKpiAPI = ('https://eu5.fusionsolar.huawei.com/thirdData/getStationRealKpi')
self:debug("Path Huawei to getStationRealKpi: ", self.getStationRealKpiAPI)
self:debug("xsrf-token: ",self.xsrftoken)
print("Getting Huawei Station Real KPI data")
local requestBody = {
             
            ["stationCodes"] = "CODE"
 
        }
          --}
            self.http:request(self.getStationRealKpiAPI, {
            options = {
                --checkCertificate = false,
            data = json.encode(requestBody),
            method = "POST",
            headers = {
                ["Content-Type"] = "application/json",
                ["XSRF-TOKEN"] = self.xsrftoken
            }
        },         
            success = function(response) 
            self:debug(response.status)
            self.status = response.status
            --self:debug(response.data)
            jsontablereadable = json.encode(response.data)
            self:debug("Station KPI output :",jsontablereadable)
            -- continue if HTTP status code is 200
            if (tonumber(self.status) == 200then
            --self.xsrftoken = response.headers["xsrf-token"]
            local jsontable = json.decode(response.data)
            self.failCode = jsontable.failCode
            if (tonumber(self.failCode) == 305then self:debug("FailCode GetStationRealKPI= " .. tostring(self.failCode)) self:GetHuaweiToken() end
           
          --if above line is true, it should bail out of this function, because it first need to run the function self:GetHuaweiToken()
           
            self:debug("FailCode GetStationRealKPI OK = " .. tostring(self.failCode))
            -- Daily Yield
            data.day_power = jsontable.data[1].dataItemMap.day_power or 0
            data.day_power = tostring(data.day_power or 0)
 
            self:debug("Yield today Station KPI = " .. data.day_power .. " kWh");
            self:debug("GELUKT!")
             -- Update QA
                self:updateChildDevices() -- Update the Child Devices
            local timeout = 60000 - (os.date("%S") * 1000)
    hub.setTimeout(timeout, function() -- wait 1 minute
        self:getStationRealKpi()
    end)
            --else
            end
         end,
        error = function(message)
            self:debug("error:", message)
        end
 
    })   
end
Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

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