Skip to content

Snippets

ascob edited this page Aug 12, 2016 · 1 revision

Display all entries of type rule in /etc/config/dynapoint

require "uci"
local uci_cursor = uci.cursor()
uci_cursor:foreach("dynapoint", "rule", function(s)
  print('------------------')
  for key, value in pairs(s) do
    print(key .. ': ' .. tostring(value))
  end
end)

Print content of nested tables:

function print_r ( t )
  local print_r_cache={}
  local function sub_print_r(t,indent)
    if (print_r_cache[tostring(t)]) then
      print(indent.."*"..tostring(t))
    else
      print_r_cache[tostring(t)]=true
      if (type(t)=="table") then
        for pos,val in pairs(t) do
          if (type(val)=="table") then
            print(indent.."["..pos.."] => "..tostring(t).." {")
            sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
            print(indent..string.rep(" ",string.len(pos)+6).."}")
          elseif (type(val)=="string") then
            print(indent.."["..pos..'] => "'..val..'"')
          else
            print(indent.."["..pos.."] => "..tostring(val))
          end
        end
      else
        print(indent..tostring(t))
      end
    end
  end
  if (type(t)=="table") then
    print(tostring(t).." {")
    sub_print_r(t,"  ")
    print("}")
  else
    sub_print_r(t,"  ")
  end
  print()
end

Provide ubus-server-function:

require "ubus"
conn = ubus.connect()
local my_method = {
  dynapoint = {
    online = {
      function(req, msg)
        conn:reply(req, {status="online",interface=msg.interface});
        print("Call to function 'online'")
        print("Interface=" .. msg.interface)

        uci_cursor = uci.cursor()
        uci_cursor:set("wireless", table_name_0 , "disabled", "1")
        uci_cursor:set("wireless", table_name_1 , "disabled", "0")
        conn:call("network", "reload", {})

      end, {interface = ubus.STRING }
    },
    offline = {
      function(req, msg)
        conn:reply(req, {status="offline",interface=msg.interface});
        print("Call to function 'offline'")
        print("Interface=" .. msg.interface)
        uci_cursor = uci.cursor()
        uci_cursor:set("wireless", table_name_0, "disabled", "0")
        uci_cursor:set("wireless", table_name_1, "disabled", "1")
        conn:call("network", "reload", {})
      end, {interface = ubus.STRING }
    }
  }
}

conn:add(my_method)
Clone this wiki locally