1001z
Junior Member
Posts: 51
|
Post by 1001z on Jan 28, 2021 17:16:06 GMT
I trying to make a script for listen events from outsideAs I can think we can do something like:
{"action": "doHelloWorld" }
Lua:
while (true) do local result = httpGet("https://example.com/my-path") if (result) then -- or conditions -- doHelloWorld end wait(3) end
But any better way? like socket or something to trigger Ankulua?
|
|
1001z
Junior Member
Posts: 51
|
Post by 1001z on Feb 1, 2021 17:25:52 GMT
Help pleaseeeeeeee
|
|
|
Post by Mercobots on Feb 2, 2021 0:05:44 GMT
I trying to make a script for listen events from outsideAs I can think we can do something like:
{"action": "doHelloWorld" }
Lua:
while (true) do local result = httpGet("https://example.com/my-path") if (result) then -- or conditions -- doHelloWorld end wait(3) end
But any better way? like socket or something to trigger Ankulua?
i don't know if is the best way to do it , but i will show you what i do! We already know pure lua don't have any synchronized functions , sokets or listeners, so the only way is to check always any data change from outside! so far the example logic you put here is good, but you can speed up and improve catch error, sothing like try/catch! so first we gonna secure our http request local http_get_status, http_get_result = pcall(httpGet, "https://example.com/my-path") if http_get_status then print("Host data\n"..http_get_result) else print("Host connection error\nERROR\n\n" .. http_get_result) end so far is easy , you get data from host if success or error on fail Now we need to check the returned data and see if is valid! For what i saw in your example you use json data structure, for that you need a json lib to read/convert the data to table! i recommend this lib, is pure lua https://gist.github.com/tylerneylon/59f4bcf316be525b30ab-- http try/catch local http_get_status, http_data = pcall(httpGet, "https://example.com/my-path") if http_get_status then print("Host data\n" .. http_data)
-- json try/catch local json_status, json_data = pcall(function() return Json.parse(http_data) end)
if json_status then -- json_data at this stage is a lua table print(json_data.action) else print("Data bad format\n" .. json_data) end else print("Host connection error\nERROR\n\n" .. http_data) end now from here you can apply any new condition from returned data ... if json_data.action == "stop" then scriptExit("Script stopped from outside") end ... The end , The basics is this, checks if we can connect to given url and validate parsed data, but in your case i would create a module just for this, this way we can re-use or modify data received, from multiples sources, in different ways instead of creating new functions for different scenarios! Something like this httpRequest.lualocal Json = require "Json" local M = {} M.__index = M
-- private function local _sanitizeUrl; local _getHttpData; local _jsonToLua;
-- --------------------------------------------------- -- init function M:new(url, init_http_timeout, max_http_timeout) local _self = {} setmetatable(_self, M) _self.url = _sanitizeUrl(url) _self.data = {} _self.init_http_timeout = init_http_timeout or 15 _self.max_http_timeout = max_http_timeout or 30 return _self end
-- --------------------------------------------------- -- you can put any conditions to sanitize the url -- for now we only see if is not empty _sanitizeUrl = function(url) if string.len(url) > 0 then return url end scriptExit("url is empty") end
-- --------------------------------------------------- -- Private -- try to connect until timeout -- returns data from http request or stop script if fail _getHttpData = function(self) local http_status, http_data, http_timeout = nil, nil, self.init_http_timeout
repeat setHttpTimeout(http_timeout) http_status, http_data = pcall(httpGet, self.url) if http_timeout > self.max_http_timeout then scriptExit("Host Connection\nRetrieving data - Timeout") end -- increase 5 sec http_timeout = http_timeout + 5 until http_status
return http_data end
-- --------------------------------------------------- -- Private -- try to convert json to lua table -- return data table from json data on success or a empty table on fail _jsonToLua = function(http_data) local json_status, json_data = pcall(function() return Json.parse(http_data) end)
if json_status then if typeOf(json_data) == "table" then return json_data else print("Json Data bad format, should be a table instead of " .. typeOf(json_data)) end else print("Json to table fail \n status = " .. json_status .. "\n data = " .. json_data) end return {} end
-- --------------------------------------------------- -- Public -- get http data -- resturn data function M:getData() self.data = _jsonToLua(_getHttpData(self)) return self.data end
-- --------------------------------------------------- -- Public -- print data for simple tables function M:showData() for k, v in pairs(self.data) do print("Key - " .. k .. " | Value - " .. v) end end
-- --------------------------------------------------- -- Public -- example to get company field from json data function M:getCompany() if #self.data > 0 then return self.data[1].company end
return false end
return M
main.lua-- load luaLib -- we only need PRINT function from this lib for data show dofile(scriptPath() .. "luaLib.lua")
-- updates modules path package.path = scriptPath() .. '?.lua;' .. package.path
-- if you use a folder for your modules/class add this line too -- package.path = scriptPath() .. 'YOUR_FOLDER_CLASS/?.lua;' .. package.path
-- load module local httpRequest = require("httpRequest")
-- New request -- httpRequest:new(url, init_http_timeout, max_http_timeout) local http = httpRequest:new("https://raw.githubusercontent.com/mercobots/ankulua_http_test/main/json_data") local http2 = httpRequest:new("SOURCE2")
http:getData()
print(http:getCompany()) print(http.data)
-- in a loop Example while true do http:getData() if http:getCompany() == 'exit' then break end print(http:getCompany())
-- ALTERNATIVE but i don't recommend if http.data[1].company == 'exit' then break end print(http.data[1].company) end
-- in a loop Example while true do http:getData() if http:getCompany() then http2:getData() end end
dependenciesJson to lua - https://gist.github.com/tylerneylon/59f4bcf316be525b30abLuaLib ankulua.boards.net/thread/481/lib-lualib-ankulua-scriptsJson data example - github.com/mercobots/ankulua_http_test/blob/main/json_datastructure- root/ -- httpRequest.lua -- Json.lua -- luaLib.lua -- main.lua I just ran a few simple tests, you may need to re-work httpRequest.lua returned values or add some new conditions to avoid issues! Hope this helps you Best Regards
|
|
1001z
Junior Member
Posts: 51
|
Post by 1001z on Feb 6, 2021 1:18:16 GMT
Thanks!, I will try!
|
|