3 local socket = require "socket"
4 local lfs = require "lfs"
5 local uci = require "uci"
6 local posix = require "posix"
8 function startswith(String,Start)
10 return string.sub(String,1,string.len(Start))==Start
17 return (s:gsub("^%s*(.-)%s*$", "%1"))
20 function url_decode(str)
21 if not str then return nil end
22 str = string.gsub (str, "+", " ")
23 str = string.gsub (str, "%%(%x%x)", function(h) return
24 string.char(tonumber(h,16)) end)
25 str = string.gsub (str, "\r\n", "\n")
29 function url_encode(str)
31 str = string.gsub (str, "\n", "\r\n")
32 str = string.gsub (str, "([^%w %-%_%.%~])",
33 function (c) return string.format ("%%%02X", string.byte(c)) end)
34 str = string.gsub (str, " ", "+")
39 function capture(cmd, raw)
40 local f = assert(io.popen(cmd, 'r'))
41 local s = assert(f:read('*a'))
43 if raw then return s end
44 s = string.gsub(s, '^%s+', '')
45 s = string.gsub(s, '%s+$', '')
46 s = string.gsub(s, '[\n\r]+', ' ')
50 function mqtt_encode(str)
52 str = string.gsub (str, "\n", "")
53 str = string.gsub (str, "/", "-")
59 local file = io.open(name, 'a')
63 function write_file(name,data)
65 local f = io.open(name,"w")
72 function file_exists(name)
73 local f=io.open(name,"r")
74 if f~=nil then io.close(f) return true else return false end
77 function get_file_content(name)
78 local f = io.open(name,"r")
80 local content = trim(f:read("*all"))
92 function split(s, delimiter)
94 for match in (s..delimiter):gmatch("(.-)"..delimiter) do
95 result[#result+1] = match
100 function list_dir(name)
102 for name in lfs.dir(name) do
103 if not startswith(name,".") then
104 result[#result+1] = name
110 function get_devid(config)
112 local web_devid = uci.get(config,"web","devid")
118 web_iface = uci.get(config,"web","iface")
120 if not web_iface then
121 web_iface = list_dir('/sys/class/net/')[1]
124 io.input("/sys/class/net/"..web_iface.."/address")
126 local web_devid = io.read("*line")
127 return web_devid:gsub(":",""):upper()
131 function get_children()
133 local pid = posix.getpid()
134 local pidlist = list_dir('/proc')
137 for k,v in pairs(pidlist) do
140 local stats = get_file_content('/proc/'..v..'/stat')
141 local ppid = tonumber(split(stats,' ')[4])
143 pids[#pids+1] = tonumber(v)