How to implement a button with a backend action in OpenWRT LUCI without UCI side effects

StackOverflow https://stackoverflow.com/questions/20779228

  •  21-09-2022
  •  | 
  •  

Domanda

I am trying to to implement a button in LuCI which, when clicked , runs a shell script in the backend. This is the model code for this:

field_var_36 = section_var_7:option(Button,"buttonkk36",translate("ButtonKK")) 
field_var_36.inputstyle = "apply" 
field_var_36.rmempty = true 
function field_var_36.write(self, section)
    luci.sys.call('echo "ABCDEFG123" >/dev/null')
end

Though this is working it has some unwanted side-effects. All the unsaved modifications in the page are getting saved and I get a "n unsaved changes" notifications at the top. My guess is the the button if of type "submit" and all the fields inside the html "form" are getting "sumbitted". I dont want this to happen. The button needs to be standalone. Can this be done?

One other option I tried was using a template with button implemented in html. But I dont know how to connect this to the backend script. Is there a javascript API function in LuCI which takes the script as argument?

Thanks in advance for any help.

È stato utile?

Soluzione

I did that using a view template.

You must set up an "entry(...)" in an "index()" function in the "controller/" directory.

The entry associates a path (last part of the LuCI URL's) with either a template view, cbi or LUA function, which is just what you want.

Pay attention that when you change by hand a controller file on the target openwrt device, you must delete /tmp/luci-indexcache so that the LuCI dispatcher does not use the older version.

Then in the template you can use "luci.dispatcher.build_url()" to create the URL that will make the controller call the function, and you put this as the target URL for your button (through either or or onClick="document.location=..."> and so on)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top