-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathRPC_Lib_example.lua
43 lines (36 loc) · 1.83 KB
/
RPC_Lib_example.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
--[[
--]]
-- Librarys functions
function test1(a,b) return a+b end
function test2(a,b) return a*b end
-------------------------------- Library support --------------------------------------------------------
if fibaro:getSourceTrigger().type=='other' then
function urldecode(str) return str:gsub('%%(%x%x)',function (x) return string.char(tonumber(x,16)) end) end
local function encodeRemoteEvent(e) return {urlencode(json.encode(e)),'%%ER%%'} end
local function decodeRemoteEvent(e) return (json.decode((urldecode(e[1])))) end
local function postRemote(sceneID, e) -- Post event to other scenes
e._from = _EMULATED and -__fibaroSceneId or __fibaroSceneId
local payload = encodeRemoteEvent(e)
if not _EMULATED then -- On HC2
if sceneID < 0 then -- call emulator
if not _emulator.adress then return end
local HTTP = net.HTTPClient()
HTTP:request(_emulator.adress.."trigger/"..sceneID,{options = {
headers = {['Accept']='application/json',['Content-Type']='application/json'},
data = json.encode(payload), timeout=2000, method = 'POST'},
error = function(status) if status~="End of file" then Log(LOG.ERROR,"Emulator error:%s, (%s)",status,tojson(e)) end end,
success = function(status) end,
})
else fibaro:startScene(sceneID,payload) end -- call other scene on HC2
else fibaro:startScene(math.abs(sceneID),payload) end -- on emulator
end
local args = fibaro:args()
if args then
local e = decodeRemoteEvent(args)
if e.type=='%%RPCCALL%%' then
local status, res = pcall(function() return {_ENV[e.fun](table.unpack(e.args))} end)
if status then postRemote(e._from,{type='%%RPCRESP%%',value=res,tag=e.tag})
else postRemote(e._from,{type='%%RPCRESP%%',name=e.fun,error=res,tag=e.tag}) end
end
end
end