Most if not all of those scripts use code similar to this:
Code: Select all
local file=assert(io.popen('curl http://someserver.json'))
local raw = file:read('*all')
file:close() So, all of a sudden when you least expect it your Domoticz system freezes and you have no clue why. The whole Domoticz system seems unstable and unpredictable. So my advise is to get rid of all those scripts immediately. We all know that there are delays on the Internet ...You can use such scripts in your LAN if you are 100% sure that your LAN servers are up and running 24/7 and always replies very fast.
What can you do about it then?
For a linux system it's not difficult at all to write a script that first calls the url without waiting for the server to respond and then returns one minute later to check the result. Below is a fully working dzVents skeleton script that does exactly that. It calls an URL every 5 minutes. The server is expected to respond with a json and the result is redirected to a temporary file. Then the next minute it checks for the result file and encodes it. Please feel free to use my script.
Code: Select all
-- This example dzVents Lua script shows how to retrieve data from the Internet
-- without risking to lock the Domoticz event system.
local jsonResultFile = '/tmp/tmpJson.json' -- Temporary file to store the servers response
local fetchIntervalMins = 5 -- (Integer) (Minutes, Range 5-60) How often the data shall be retrieved
local scriptVersion = '1.0.2'
return {
active = true,
logging = {
level = domoticz.LOG_INFO, -- Uncomment to override the dzVents global logging setting
marker = 'callNoWait '..scriptVersion
},
on = {
timer = {'every minute'} -- Don't change! Verander niet! Ne changez pas!
},
execute = function(domoticz, device)
-- Every 5 minutes, call the URL and exit.
-- The following minute, return to read the result file
local callUrl = false
if (os.date('*t').min % fetchIntervalMins) == 0 then
callUrl = true
elseif ((os.date('*t').min -1) % fetchIntervalMins) ~= 0 then
return
end
if callUrl then
local url = 'https://jsonplaceholder.typicode.com/posts/1'
domoticz.log('Requesting data from the jsonplaceholder server...', domoticz.LOG_INFO)
domoticz.log('URL used: '..url, domoticz.LOG_DEBUG)
os.execute('curl -s "'..url..'" > '..jsonResultFile..'&')
return -- Nothing more to do for now, we'll be back in a minute to read the data!
end
---loads a json file with the provided name and returns it as a table (if it exists)
local function readLuaFromJsonFile(fileName)
local file = io.open(fileName, 'r')
if file then
package.path = './scripts/lua/?.lua;'..package.path
local jsonParser = require('JSON')
local _json = file:read('*a')
local json = jsonParser:decode(_json)
io.close(file)
return json
end
return nil
end
local demoData = readLuaFromJsonFile(jsonResultFile)
if not demoData then
domoticz.log('Could not read demoData from file: '.. jsonResultFile, domoticz.LOG_ERROR)
return
end
domoticz.log('json data file has been read. "title" = "'..demoData.title..'"', domoticz.LOG_INFO)
end
}
Cheers!