From 78202d5bd8a8d09a55bb71b9df6d3f54b18184b8 Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Wed, 20 Sep 2017 16:54:14 +0200 Subject: Make LUA works - Added LUA library at link - Make app-template crash build if a checked file fails - Fix old defined variable CONTROL_LUA_PATH to a default one in the binding data directory - Add an example with unit conversion made in LUA Change-Id: I1b4712bde32a38044ad6b14ebd38b6782fd01fe9 Signed-off-by: Romain Forlot --- conf.d/project/lua.d/00-viwi-low-can.lua | 27 ----- conf.d/project/lua.d/doscript-helloworld.lua | 36 ------- conf.d/project/lua.d/init-daemon-00-utils.lua | 86 +++++++++++++++ conf.d/project/lua.d/init-daemon-01-init.lua | 47 +++++++++ conf.d/project/lua.d/init-daemon-03-controls.lua | 48 +++++++++ conf.d/project/lua.d/init-daemon-04-oncall.lua | 117 +++++++++++++++++++++ conf.d/project/lua.d/init-daemon-10-event.lua | 74 +++++++++++++ conf.d/project/lua.d/onload-daemon-00-utils.lua | 86 --------------- conf.d/project/lua.d/onload-daemon-01-init.lua | 48 --------- conf.d/project/lua.d/onload-daemon-03-controls.lua | 48 --------- conf.d/project/lua.d/onload-daemon-04-oncall.lua | 70 ------------ conf.d/project/lua.d/onload-daemon-10-event.lua | 74 ------------- 12 files changed, 372 insertions(+), 389 deletions(-) delete mode 100644 conf.d/project/lua.d/00-viwi-low-can.lua delete mode 100644 conf.d/project/lua.d/doscript-helloworld.lua create mode 100644 conf.d/project/lua.d/init-daemon-00-utils.lua create mode 100644 conf.d/project/lua.d/init-daemon-01-init.lua create mode 100644 conf.d/project/lua.d/init-daemon-03-controls.lua create mode 100644 conf.d/project/lua.d/init-daemon-04-oncall.lua create mode 100644 conf.d/project/lua.d/init-daemon-10-event.lua delete mode 100644 conf.d/project/lua.d/onload-daemon-00-utils.lua delete mode 100644 conf.d/project/lua.d/onload-daemon-01-init.lua delete mode 100644 conf.d/project/lua.d/onload-daemon-03-controls.lua delete mode 100644 conf.d/project/lua.d/onload-daemon-04-oncall.lua delete mode 100644 conf.d/project/lua.d/onload-daemon-10-event.lua (limited to 'conf.d/project/lua.d') diff --git a/conf.d/project/lua.d/00-viwi-low-can.lua b/conf.d/project/lua.d/00-viwi-low-can.lua deleted file mode 100644 index b13863a..0000000 --- a/conf.d/project/lua.d/00-viwi-low-can.lua +++ /dev/null @@ -1,27 +0,0 @@ ---[[ - Copyright (C) 2017 "IoT.bzh" - Author Romain Forlot - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. ---]] - -function _SubscribeLowCan (request, args) - - AFB:notice ("_We gonna subscribe to %s", args) - local err, result= AFB:servsync ("low-can","subscribe", args) - if (err) then - AFB:fail ("AFB:service_call_sync fail"); - else - AFB:success (request, result["response"]) - end -end diff --git a/conf.d/project/lua.d/doscript-helloworld.lua b/conf.d/project/lua.d/doscript-helloworld.lua deleted file mode 100644 index e722db7..0000000 --- a/conf.d/project/lua.d/doscript-helloworld.lua +++ /dev/null @@ -1,36 +0,0 @@ ---[[ - Copyright (C) 2016 "IoT.bzh" - Author Fulup Ar Foll - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - - Simple API script to be use with AGL control LuaDoCall API - - After the script is loaded by lua_docall - - Controller start function=xxxx where xxxx is taken from script filename doscript-xxxx-anything - ---]] - -function _helloworld (request, query) - - AFB:notice ("LUA HelloWorld: Simple test query=%s", query); - - if (query == nil) then - AFB:notice ("LUA HelloWorld:FX query should not be empty"); - AFB:fail (request, "LUA HelloWorld: query should not be empty"); - else - AFB:notice ("LUA HelloWorld:OK query=%s", query); - AFB:success (request, {arg0="Demat", arg1="Bonjours", arg2="Gootentag", arg3="Morning"}); - end - -end diff --git a/conf.d/project/lua.d/init-daemon-00-utils.lua b/conf.d/project/lua.d/init-daemon-00-utils.lua new file mode 100644 index 0000000..29d2c70 --- /dev/null +++ b/conf.d/project/lua.d/init-daemon-00-utils.lua @@ -0,0 +1,86 @@ +--[[ + Copyright (C) 2016 "IoT.bzh" + Author Fulup Ar Foll + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + Note: this file should be called before any other to assert declare function + is loaded before anything else. + + References: + http://lua-users.org/wiki/DetectingUndefinedVariables + +--]] + + +--=================================================== +--= Niklas Frykholm +-- basically if user tries to create global variable +-- the system will not let them!! +-- call GLOBAL_lock(_G) +-- +--=================================================== +function GLOBAL_lock(t) + local mt = getmetatable(t) or {} + mt.__newindex = lock_new_index + setmetatable(t, mt) +end + +--=================================================== +-- call GLOBAL_unlock(_G) +-- to change things back to normal. +--=================================================== +function GLOBAL_unlock(t) + local mt = getmetatable(t) or {} + mt.__newindex = unlock_new_index + setmetatable(t, mt) +end + +function lock_new_index(t, k, v) + if (string.sub(k,1,1) ~= "_") then + GLOBAL_unlock(_G) + error("GLOBALS are locked -- " .. k .. + " must be declared local or prefix with '_' for globals.", 2) + else + rawset(t, k, v) + end +end + +function unlock_new_index(t, k, v) + rawset(t, k, v) +end + +-- return serialised version of printable table +function Dump_Table(o) + if type(o) == 'table' then + local s = '{ ' + for k,v in pairs(o) do + if type(k) ~= 'number' then k = '"'..k..'"' end + s = s .. '['..k..'] = ' .. Dump_Table(v) .. ',' + end + return s .. '} ' + else + return tostring(o) + end +end + + +-- simulate C prinf function +printf = function(s,...) + io.write(s:format(...)) + io.write("\n") + return +end + +-- lock global variable +GLOBAL_lock(_G) diff --git a/conf.d/project/lua.d/init-daemon-01-init.lua b/conf.d/project/lua.d/init-daemon-01-init.lua new file mode 100644 index 0000000..0b8a923 --- /dev/null +++ b/conf.d/project/lua.d/init-daemon-01-init.lua @@ -0,0 +1,47 @@ +--[[ + Copyright (C) 2016 "IoT.bzh" + Author Fulup Ar Foll + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--]] + +-- Global variable SHOULD start with _ +_Global_Context={} + +--[[ + This function is call during controller init phase as describe in onload-daemon-sample.json + It receives two argument 1st one is the source (here on load) second one is the arguments + as expose in config file. + + In this sample we create an event that take the name of args["zzzz"], the resulting handle + is save into _Global_Context for further use. + + Note: init functions are not call from a client and thus do not receive query +--]] + +function _Sample_Controller_Init(source, control) + + printf ("[-- Sample_Controller_Init --] source=%d control=%s", source, Dump_Table(control)) + + -- if no argument return now + if (control==nil or control["zzzz"]==nil) then + printf ("[-- Sample_Controller_Init --] no event name given") + return + end + + -- set a count to make more visible each call + _Global_Context["counter"]=0 + + -- just for fun create an event + _Global_Context["event"]=AFB:evtmake(control["zzzz"]) +end diff --git a/conf.d/project/lua.d/init-daemon-03-controls.lua b/conf.d/project/lua.d/init-daemon-03-controls.lua new file mode 100644 index 0000000..cc13e0f --- /dev/null +++ b/conf.d/project/lua.d/init-daemon-03-controls.lua @@ -0,0 +1,48 @@ +--[[ + Copyright (C) 2016 "IoT.bzh" + Author Fulup Ar Foll + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + Following function are called when a client activate a control with + controller api -> APi=control VERB=dispatch + arguments are + - source (0) when requesting the control (-1) when releasing + - control comme from config given with 'args' in onload-middlename-xxxxx.json + - client is the argument part of the query as providing by client requesting the control. + +--]] + + + +-- Simple Happy(granted) Control +function _Button_Happy(source, control, client) + + -- print argument to make sure we understant what we get + printf ("[-- _Button_Happy --] source=%d control=%s client=%s", source, Dump_Table(control), Dump_Table(client)) + + AFB:notice ("[-- _Button_Happy --] To Be Done") + + return 0 -- control granted +end + + +-- Simple UnHappy(debu) Control +function _Button_UnHappy(source, control, client) + + -- print argument to make sure we understant what we get + printf ("[-- _Button_UnHappy --] source=%d control=%s client=%s", source, Dump_Table(client), Dump_Table(control)) + + AFB:error ("[-- _Button_UnHappy --] To Be Done") + return 1 -- control is refused +end diff --git a/conf.d/project/lua.d/init-daemon-04-oncall.lua b/conf.d/project/lua.d/init-daemon-04-oncall.lua new file mode 100644 index 0000000..ae16aa7 --- /dev/null +++ b/conf.d/project/lua.d/init-daemon-04-oncall.lua @@ -0,0 +1,117 @@ +--[[ + Copyright (C) 2016 "IoT.bzh" + Author Fulup Ar Foll + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + Provide sample LUA routines to be used with AGL control "lua_docall" API +--]] + +--global counter to keep track of calls +_count=0 + + +_interval={ + {"km/h",1}, --the "1" should never really get used but + {"mi/h",0.62137119223733}, + {"m/s",0.27777777777778}, +} + +_positions={} +for i=1,3 do + _positions[_interval[i][1]]=i +end + +--function _Unit_Converter(value, sourceunits, targetunits) +function _Unit_Converter(source, args, event) + local value = event["value"] + local sourceunits = args["from"] + local targetunits = args["to"] + local sourcei, targeti = _positions[sourceunits], _positions[targetunits] + assert(sourcei and targeti) + + AFB:notice("LUA OnCall Echo Args source=%s args=%s event=%s", source, args, event) + + if sourceitargeti then + + local base=1 + for i=targeti+1,sourcei do + base=base*interval[i][2] + end + + print("Value in ", targetunits, "is", value*base) + + else + print("No conversion") + end +end + +-- Display receive arguments and echo them to caller +function _Simple_Echo_Args (source, args, event) + _count=_count+1 + AFB:notice("LUA OnCall Echo Args count=%d args=%s event=%s", count, args, event) + + print ("--inlua-- source=", Dump_Table(source)) + print ("--inlua-- args=", Dump_Table(args)) + print ("--inlua-- event=", Dump_Table(event)) + + local response={ + ["count"]=_count, + ["args"]=args, + } + + -- fulup Embdeded table ToeDone AFB:success (request, response) + -- AFB:success (request, response) +end + +local function Test_Async_CB (request, result, context) + response={ + ["result"]=result, + ["context"]=context, + } + + AFB:notice ("Test_Async_CB result=%s context=%s", result, context) + AFB:success (request, response) +end + +function _Test_Call_Async (request, args) + local context={ + ["value1"]="abcd", + ["value2"]=1234 + } + + AFB:notice ("Test_Call_Async args=%s cb=Test_Async_CB", args) + AFB:service("monitor","ping", "Test_Async_CB", context) +end + +function _Simple_Monitor_Call (request, args) + + AFB:notice ("_Simple_Server_Call args=%s", args) + local err, result= AFB:servsync ("monitor","get", args) + if (err) then + AFB:fail ("AFB:service_call_sync fail"); + else + AFB:success (request, result["response"]) + end +end + diff --git a/conf.d/project/lua.d/init-daemon-10-event.lua b/conf.d/project/lua.d/init-daemon-10-event.lua new file mode 100644 index 0000000..474ebe0 --- /dev/null +++ b/conf.d/project/lua.d/init-daemon-10-event.lua @@ -0,0 +1,74 @@ +--[[ + Copyright (C) 2016 "IoT.bzh" + Author Fulup Ar Foll + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + Provide Sample Timer Handing to push event from LUA +--]] + +-- Create event on Lua script load +_MyContext={} + +-- WARNING: call back are global and should start with '_' +function _Timer_Test_CB (timer, context) + + local evtinfo= AFB:timerget(timer) + printf ("[-- _Timer_Test_C --] evtInfo=%s", Dump_Table(evtinfo)) + + --send an event an event with count as value + AFB:evtpush (_MyContext["event"], {["label"]= evtinfo["label"], ["count"]=evtinfo["count"], ["info"]=context["info"]}) + + -- note when timerCB return!=0 timer is kill + return 0 + +end + +-- sendback event depending on count and delay +function _Simple_Timer_Test (request, client) + + local context = { + ["info"]="My 1st private Event", + } + + -- if event does not exit create it now. + if (_MyContext["event"] == nil) then + _MyContext["event"]= AFB:evtmake(client["label"]) + end + + -- if delay not defined default is 5s + if (client["delay"]==nil) then client["delay"]=5000 end + + -- if count is not defined default is 10 + if (client["count"]==nil) then client["count"]=10 end + + -- we could use directly client but it is a sample + local myTimer = { + ["label"]=client["label"], + ["delay"]=client["delay"], + ["count"]=client["count"], + } + AFB:notice ("Test_Timer myTimer=%s", myTimer) + + -- subscribe to event + AFB:subscribe (request, _MyContext["event"]) + + -- settimer take a table with delay+count as input (count==0 means infinite) + AFB:timerset (myTimer, "_Timer_Test_CB", context) + + -- nothing special to return send back + AFB:success (request, myTimer) + + return 0 +end diff --git a/conf.d/project/lua.d/onload-daemon-00-utils.lua b/conf.d/project/lua.d/onload-daemon-00-utils.lua deleted file mode 100644 index 29d2c70..0000000 --- a/conf.d/project/lua.d/onload-daemon-00-utils.lua +++ /dev/null @@ -1,86 +0,0 @@ ---[[ - Copyright (C) 2016 "IoT.bzh" - Author Fulup Ar Foll - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - Note: this file should be called before any other to assert declare function - is loaded before anything else. - - References: - http://lua-users.org/wiki/DetectingUndefinedVariables - ---]] - - ---=================================================== ---= Niklas Frykholm --- basically if user tries to create global variable --- the system will not let them!! --- call GLOBAL_lock(_G) --- ---=================================================== -function GLOBAL_lock(t) - local mt = getmetatable(t) or {} - mt.__newindex = lock_new_index - setmetatable(t, mt) -end - ---=================================================== --- call GLOBAL_unlock(_G) --- to change things back to normal. ---=================================================== -function GLOBAL_unlock(t) - local mt = getmetatable(t) or {} - mt.__newindex = unlock_new_index - setmetatable(t, mt) -end - -function lock_new_index(t, k, v) - if (string.sub(k,1,1) ~= "_") then - GLOBAL_unlock(_G) - error("GLOBALS are locked -- " .. k .. - " must be declared local or prefix with '_' for globals.", 2) - else - rawset(t, k, v) - end -end - -function unlock_new_index(t, k, v) - rawset(t, k, v) -end - --- return serialised version of printable table -function Dump_Table(o) - if type(o) == 'table' then - local s = '{ ' - for k,v in pairs(o) do - if type(k) ~= 'number' then k = '"'..k..'"' end - s = s .. '['..k..'] = ' .. Dump_Table(v) .. ',' - end - return s .. '} ' - else - return tostring(o) - end -end - - --- simulate C prinf function -printf = function(s,...) - io.write(s:format(...)) - io.write("\n") - return -end - --- lock global variable -GLOBAL_lock(_G) diff --git a/conf.d/project/lua.d/onload-daemon-01-init.lua b/conf.d/project/lua.d/onload-daemon-01-init.lua deleted file mode 100644 index 26ff08a..0000000 --- a/conf.d/project/lua.d/onload-daemon-01-init.lua +++ /dev/null @@ -1,48 +0,0 @@ ---[[ - Copyright (C) 2016 "IoT.bzh" - Author Fulup Ar Foll - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. ---]] - --- Global variable SHOULD start with _ -_Global_Context={} - ---[[ - This function is call during controller init phase as describe in onload-daemon-sample.json - It receives two argument 1st one is the source (here on load) second one is the arguments - as expose in config file. - - In this sample we create an event that take the name of args["zzzz"], the resulting handle - is save into _Global_Context for further use. - - Note: init functions are not call from a client and thus do not receive query - ---]] -function _Sample_Controller_Init(source, control) - - printf ("[-- Sample_Controller_Init --] source=%d control=%s", source, Dump_Table(control)) - - -- if no argument return now - if (control==nil or control["zzzz"]==nil) then - printf ("[-- Sample_Controller_Init --] no event name given") - return - end - - -- set a count to make more visible each call - _Global_Context["counter"]=0 - - -- just for fun create an event - _Global_Context["event"]=AFB:evtmake(control["zzzz"]) - -end diff --git a/conf.d/project/lua.d/onload-daemon-03-controls.lua b/conf.d/project/lua.d/onload-daemon-03-controls.lua deleted file mode 100644 index cc13e0f..0000000 --- a/conf.d/project/lua.d/onload-daemon-03-controls.lua +++ /dev/null @@ -1,48 +0,0 @@ ---[[ - Copyright (C) 2016 "IoT.bzh" - Author Fulup Ar Foll - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - Following function are called when a client activate a control with - controller api -> APi=control VERB=dispatch - arguments are - - source (0) when requesting the control (-1) when releasing - - control comme from config given with 'args' in onload-middlename-xxxxx.json - - client is the argument part of the query as providing by client requesting the control. - ---]] - - - --- Simple Happy(granted) Control -function _Button_Happy(source, control, client) - - -- print argument to make sure we understant what we get - printf ("[-- _Button_Happy --] source=%d control=%s client=%s", source, Dump_Table(control), Dump_Table(client)) - - AFB:notice ("[-- _Button_Happy --] To Be Done") - - return 0 -- control granted -end - - --- Simple UnHappy(debu) Control -function _Button_UnHappy(source, control, client) - - -- print argument to make sure we understant what we get - printf ("[-- _Button_UnHappy --] source=%d control=%s client=%s", source, Dump_Table(client), Dump_Table(control)) - - AFB:error ("[-- _Button_UnHappy --] To Be Done") - return 1 -- control is refused -end diff --git a/conf.d/project/lua.d/onload-daemon-04-oncall.lua b/conf.d/project/lua.d/onload-daemon-04-oncall.lua deleted file mode 100644 index 7c0e9bc..0000000 --- a/conf.d/project/lua.d/onload-daemon-04-oncall.lua +++ /dev/null @@ -1,70 +0,0 @@ ---[[ - Copyright (C) 2016 "IoT.bzh" - Author Fulup Ar Foll - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - - Provide sample LUA routines to be used with AGL control "lua_docall" API ---]] - ---global counter to keep track of calls -_count=0 - --- Display receive arguments and echo them to caller -function _LUA_Simple_Echo_Args (request, args) - _count=_count+1 - AFB:notice("LUA OnCall Echo Args count=%d args=%s", count, args) - - print ("--inlua-- args=", Dump_Table(args)) - - local response={ - ["count"]=_count, - ["args"]=args, - } - - -- fulup Embdeded table ToeDone AFB:success (request, response) - AFB:success (request, {["func"]="Simple_Echo_Args", ["ret1"]=5678, ["ret2"]="abcd"}) -end - -local function Test_Async_CB (request, result, context) - response={ - ["result"]=result, - ["context"]=context, - } - - AFB:notice ("Test_Async_CB result=%s context=%s", result, context) - AFB:success (request, response) -end - -function _Test_Call_Async (request, args) - local context={ - ["value1"]="abcd", - ["value2"]=1234 - } - - AFB:notice ("Test_Call_Async args=%s cb=Test_Async_CB", args) - AFB:service("monitor","ping", "Test_Async_CB", context) -end - -function _Simple_Monitor_Call (request, args) - - AFB:notice ("_Simple_Server_Call args=%s", args) - local err, result= AFB:servsync ("monitor","get", args) - if (err) then - AFB:fail ("AFB:service_call_sync fail"); - else - AFB:success (request, result["response"]) - end -end - diff --git a/conf.d/project/lua.d/onload-daemon-10-event.lua b/conf.d/project/lua.d/onload-daemon-10-event.lua deleted file mode 100644 index 474ebe0..0000000 --- a/conf.d/project/lua.d/onload-daemon-10-event.lua +++ /dev/null @@ -1,74 +0,0 @@ ---[[ - Copyright (C) 2016 "IoT.bzh" - Author Fulup Ar Foll - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - - Provide Sample Timer Handing to push event from LUA ---]] - --- Create event on Lua script load -_MyContext={} - --- WARNING: call back are global and should start with '_' -function _Timer_Test_CB (timer, context) - - local evtinfo= AFB:timerget(timer) - printf ("[-- _Timer_Test_C --] evtInfo=%s", Dump_Table(evtinfo)) - - --send an event an event with count as value - AFB:evtpush (_MyContext["event"], {["label"]= evtinfo["label"], ["count"]=evtinfo["count"], ["info"]=context["info"]}) - - -- note when timerCB return!=0 timer is kill - return 0 - -end - --- sendback event depending on count and delay -function _Simple_Timer_Test (request, client) - - local context = { - ["info"]="My 1st private Event", - } - - -- if event does not exit create it now. - if (_MyContext["event"] == nil) then - _MyContext["event"]= AFB:evtmake(client["label"]) - end - - -- if delay not defined default is 5s - if (client["delay"]==nil) then client["delay"]=5000 end - - -- if count is not defined default is 10 - if (client["count"]==nil) then client["count"]=10 end - - -- we could use directly client but it is a sample - local myTimer = { - ["label"]=client["label"], - ["delay"]=client["delay"], - ["count"]=client["count"], - } - AFB:notice ("Test_Timer myTimer=%s", myTimer) - - -- subscribe to event - AFB:subscribe (request, _MyContext["event"]) - - -- settimer take a table with delay+count as input (count==0 means infinite) - AFB:timerset (myTimer, "_Timer_Test_CB", context) - - -- nothing special to return send back - AFB:success (request, myTimer) - - return 0 -end -- cgit 1.2.3-korg