3 local socket = require "socket"
5 function startswith(String,Start)
7 return string.sub(String,1,string.len(Start))==Start
14 return (s:gsub("^%s*(.-)%s*$", "%1"))
17 function url_decode(str)
18 if not str then return nil end
19 str = string.gsub (str, "+", " ")
20 str = string.gsub (str, "%%(%x%x)", function(h) return
21 string.char(tonumber(h,16)) end)
22 str = string.gsub (str, "\r\n", "\n")
26 function url_encode(str)
28 str = string.gsub (str, "\n", "\r\n")
29 str = string.gsub (str, "([^%w %-%_%.%~])",
30 function (c) return string.format ("%%%02X", string.byte(c)) end)
31 str = string.gsub (str, " ", "+")
36 function capture(cmd, raw)
37 local f = assert(io.popen(cmd, 'r'))
38 local s = assert(f:read('*a'))
40 if raw then return s end
41 s = string.gsub(s, '^%s+', '')
42 s = string.gsub(s, '%s+$', '')
43 s = string.gsub(s, '[\n\r]+', ' ')
47 function mqtt_encode(str)
49 str = string.gsub (str, "\n", "")
50 str = string.gsub (str, "/", "-")
56 local file = io.open(name, 'a')
60 function write_file(name,data)
62 local f = io.open(name,"w")
69 function file_exists(name)
70 local f=io.open(name,"r")
71 if f~=nil then io.close(f) return true else return false end
74 function get_file_content(name)
75 local f = io.open(name,"r")
77 local content = trim(f:read("*all"))
89 function split(s, delimiter)
91 for match in (s..delimiter):gmatch("(.-)"..delimiter) do
92 result[#result+1] = match
97 function list_dir(name)
98 local lfs = require "lfs"
100 for name in lfs.dir(name) do
101 if not startswith(name,".") then
102 result[#result+1] = name
108 function get_devid(config)
110 local uci = require "uci"
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()