Need help adding a button/script


 

Paul Nederveen

TVWBB Member
This is a question for those who understand lua, js, etc. (Bryan, Steve...)

I'm experimenting with adding functionality along a specific tangent. Specifically I've added the mosquitto MQTT package into my build tree. I have access to some IoT tech that could possibly become available to the community if I can get things working.

Anyway, I want to add a button somewhere that fires a "Start Cook" script. For now it would be good enough if there was something I could click that I could attach a script to. Likely it would start a cron job during the initial experiments.

I've looked at ./linkmeter/luasrc/view/linkmeter/index.htm. Think an easy spot would be here:

Code:
write("<li><a href=%q>Alarms</a></li>" % build_url("admin/lm/alarm"))
write("<li><a href=%q>Archive</a></li>" % build_url("admin/lm/archive"))
write("<li><a href=%q>Configuration</a></li>" % build_url("admin/lm"))

Unfortunately I'm not very strong in LUA or JS so I'm not sure where/what code to add to fork&exec a script. It seems like ./controller/linkmeter/lmadmin.lua is a likely spot.

Would someone be willing to help me out? I plan on this eventually leading to some neat IoT application capabilities...

Thanks,
Paul
 
What you need are two things, one is where you want to display a link to click, the other is the code that runs when you click it and executes your script. The code you've linked will work for the UI portion but you'll need a target URL like admin/lm/mynewthing.

Then add a controller. I'd suggest adding a new file to the linkmeter directory rather than adding to an existing file, this makes it easier to maintain rather than merging your code into linkmeter all the time. All the files in the linkmeter directory will be loaded by the indexer. Here's all you need
Code:
module("luci.controller.linkmeter.mynewmodule", package.seeall)

function index()
  entry({"admin", "lm", "newthing"}, call("myaction"))
end

function myaction()
    luci.http.prepare_content("text/plain")
    local pipe = require "luci.controller.admin.system".ltn12_popen("/path/to/my/script")
    return luci.ltn12.pump.all(pipe, luci.http.write)
end

Note that this redirects all the stdout from the script to a text/plain HTTP result. If you just want to run the script and return nothing all the code of myaction is
Code:
function myaction()
    os.execute("/path/to/my/script")
end
 

 

Back
Top