Developing lightweight computation at the DSG edge

bmx6json.lua 3.98 KB
Newer Older
1
--[[
2
3
    Copyright (C) 2011 Pau Escrich <pau@dabax.net>
    Contributors Jo-Philipp Wich <xm@subsignal.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

    The full GNU General Public License is included in this distribution in
    the file called "COPYING".
--]]

23
24
25
26
27
28
29
local ltn12 = require("luci.ltn12")
local json = require("luci.json")
local util = require("luci.util")
local uci = require("luci.model.uci")
local sys = require("luci.sys")
local template = require("luci.template")
local http = require("luci.http")
p4u's avatar
p4u committed
30
31
32
33
34
local string = require("string")
local table = require("table")
local nixio = require("nixio")
local nixiofs = require("nixio.fs")
local ipairs = ipairs
35
36
37
38
39

module "luci.model.bmx6json"

-- Returns a LUA object from bmx6 JSON daemon

p4u's avatar
p4u committed
40
41
42
43
44
45
46
47
48
49
50
function get(field, host)
	local url
	if host ~= nil then
		if host:match(":") then
			url = 'http://[%s]/cgi-bin/bmx6-info?' % host
		else
			url = 'http://%s/cgi-bin/bmx6-info?' % host
		end
	else
		url = uci.cursor():get("luci-bmx6","luci","json")
	end
51
52
53
54
55
56
57
58
59
60

	if url == nil then
		 print_error("bmx6 json url not configured, cannot fetch bmx6 daemon data",true)
		 return nil
	 end

	 local json_url = util.split(url,":")
	 local raw = ""

	if json_url[1] == "http"  then
p4u's avatar
p4u committed
61
		raw,err = wget(url..field,1000)
62
	else
63
64
65
66
67
68
69
70
71
72
73
74

		if json_url[1] == "exec" then
			raw = sys.exec(json_url[2]..' '..field)
		else
			print_error("bmx6 json url not recognized, cannot fetch bmx6 daemon data. Use http: or exec:",true)
			return nil
		end

	end

	local data = nil

p4u's avatar
p4u committed
75
	if raw and raw:len() > 10 then
76
77
78
		local decoder = json.Decoder()
		ltn12.pump.all(ltn12.source.string(raw), decoder:sink())
		data = decoder:get()
p4u's avatar
p4u committed
79
80
--	else
--		print_error("Cannot get data from bmx6 daemon",true)
81
--		return nil
82
83
84
	end

	return data
85
end
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

function print_error(txt,popup)
	util.perror(txt)
	sys.call("logger -t bmx6json " .. txt)

	if popup then
		http.write('<script type="text/javascript">alert("Some error detected, please check it: '..txt..'");</script>')
	else
		http.write("<h1>Dammit! some error detected</h1>")
		http.write("bmx6-luci: " .. txt)
		http.write('<p><FORM><INPUT TYPE="BUTTON" VALUE="Go Back" ONCLICK="history.go(-1)"></FORM></p>')
	end

end

function text2html(txt)
	txt = string.gsub(txt,"<","{")
	txt = string.gsub(txt,">","}")
	txt = util.striptags(txt)
	return txt
end

p4u's avatar
p4u committed
108
109
110
111
112
113
114

function wget(url, timeout)
	local rfd, wfd = nixio.pipe()
	local pid = nixio.fork()
	if pid == 0 then
		rfd:close()
		nixio.dup(wfd, nixio.stdout)
115

p4u's avatar
p4u committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
		local candidates = { "/usr/bin/wget", "/bin/wget" }
		local _, bin
		for _, bin in ipairs(candidates) do
			if nixiofs.access(bin, "x") then
				nixio.exec(bin, "-q", "-O", "-", url)
			end
		end
		return
	else
		wfd:close()
		rfd:setblocking(false)

		local buffer = { }
		local err1, err2

		while true do
			local ready = nixio.poll({{ fd = rfd, events = nixio.poll_flags("in") }}, timeout)
			if not ready then
				nixio.kill(pid, nixio.const.SIGKILL)
				err1 = "timeout"
				break
			end
138

p4u's avatar
p4u committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
			local rv = rfd:read(4096)
			if rv then
				-- eof
				if #rv == 0 then
					break
				end

				buffer[#buffer+1] = rv
			else
				-- error
				if nixio.errno() ~= nixio.const.EAGAIN and
				   nixio.errno() ~= nixio.const.EWOULDBLOCK then
				   	err1 = "error"
				   	err2 = nixio.errno()
				end
			end
		end

		nixio.waitpid(pid, "nohang")
		if not err1 then
			return table.concat(buffer)
		else
			return nil, err1, err2
		end
	end
end