Skip to content

Commit

Permalink
fix: failover interval
Browse files Browse the repository at this point in the history
switches interval to 5 min on failing API to avoid ban
  • Loading branch information
ikubicki committed Nov 13, 2023
1 parent 37a79f6 commit 900535d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 6 additions & 3 deletions Salus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function Salus:new(config)
return self
end

function Salus:getProperties(callback)
function Salus:getProperties(callback, failCallback)
local properties = {}
local batteryLevelCallback = function(response)
properties["battery"] = response.value
Expand Down Expand Up @@ -45,7 +45,7 @@ function Salus:getProperties(callback)
local authCallback = function(response)
Salus:temperature(temperatureCallback)
end
Salus:auth(authCallback)
Salus:auth(authCallback, failCallback)
end

function Salus:searchDevices(callback)
Expand Down Expand Up @@ -346,7 +346,7 @@ function Salus:listDevices(callback, fail, attempt)
self.http:get(url, success, fail, headers)
end

function Salus:auth(callback)
function Salus:auth(callback, failCallback)
if string.len(self.token) > 1 then
-- QuickApp:debug('Already authenticated')
if callback ~= nil then
Expand All @@ -355,6 +355,9 @@ function Salus:auth(callback)
return
end
local fail = function(response)
if failCallback then
failCallback(response.status)
end
QuickApp:error('Unable to authenticate')
Salus:setToken('')
end
Expand Down
10 changes: 9 additions & 1 deletion main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Salus IT600 thermostats integration v 1.1.1

function QuickApp:onInit()
self.config = Config:new(self)
self.failover = false
self.salus = Salus:new(self.config)
self.i18n = i18n:new(api.get("/settings/info").defaultLanguage)
self:trace('')
Expand Down Expand Up @@ -88,13 +89,20 @@ end
function QuickApp:run()
self:pullDataFromCloud()
local interval = self.config:getInterval()
if self.failover then
interval = 300000
end
if (interval > 0) then
fibaro.setTimeout(interval, function() self:run() end)
end
end

function QuickApp:pullDataFromCloud()
local getFailCallback = function()
self.failover = true
end
local getPropertiesCallback = function(properties)
self.failover = false
-- QuickApp:debug(json.encode(properties))
self:updateView("button2_2", "text", self.i18n:get('refresh'))
if self.childrenIds["com.fibaro.temperatureSensor"] ~= nil then
Expand Down Expand Up @@ -137,7 +145,7 @@ function QuickApp:pullDataFromCloud()
end
end
self:updateView("button2_2", "text", self.i18n:get('refreshing'))
self.salus:getProperties(getPropertiesCallback)
self.salus:getProperties(getPropertiesCallback, getFailCallback)
end

function QuickApp:searchEvent(param)
Expand Down

0 comments on commit 900535d

Please sign in to comment.