ed02a842aada7bec7bab507f19651d027f920a6b
[feed/routing.git] / files / usr / lib / lua / luci / controller / bmx6.lua
1 --[[
2 Copyright (C) 2011 Pau Escrich <pau@dabax.net>
3 Contributors Jo-Philipp Wich <xm@subsignal.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21 --]]
22
23 local bmx6json = require("luci.model.bmx6json")
24
25 module("luci.controller.bmx6", package.seeall)
26
27 function index()
28 local place = {}
29 local ucim = require "luci.model.uci"
30 local uci = ucim.cursor()
31
32 -- checking if ignore is on
33 if uci:get("luci-bmx6","luci","ignore") == "1" then
34 return nil
35 end
36
37 -- getting value from uci database
38 local uci_place = uci:get("luci-bmx6","luci","place")
39
40 -- default values
41 if uci_place == nil then
42 place = {"bmx6"}
43 else
44 local util = require "luci.util"
45 place = util.split(uci_place," ")
46 end
47 ---------------------------
48 -- Starting with the pages
49 ---------------------------
50
51 --- status (default)
52 entry(place,call("action_status_j"),place[#place])
53
54 -- not visible
55 table.insert(place,"nodes_nojs")
56 entry(place, call("action_nodes"), nil)
57 table.remove(place)
58
59 --- nodes
60 table.insert(place,"Nodes")
61 entry(place,call("action_nodes_j"),"Nodes",1)
62 table.remove(place)
63
64 --- links
65 table.insert(place,"Links")
66 entry(place,call("action_links"),"Links",2).leaf = true
67 table.remove(place)
68
69 -- Tunnels
70 table.insert(place,"Tunnels")
71 entry(place,call("action_tunnels_j"), "Tunnels", 3).leaf = true
72 table.remove(place)
73
74 -- Gateways (deprecated)
75 --table.insert(place,"Gateways")
76 --entry(place,call("action_gateways_j"),"Gateways").leaf = true
77 --table.remove(place)
78
79 --- chat
80 table.insert(place,"Chat")
81 entry(place,call("action_chat"),"Chat")
82 table.remove(place)
83
84 --- Graph
85 table.insert(place,"Graph")
86 entry(place, template("bmx6/graph",4), "Graph")
87 table.remove(place)
88
89 --- Topology (hidden)
90 table.insert(place,"topology")
91 entry(place, call("action_topology"), nil)
92 table.remove(place)
93
94 --- configuration (CBI)
95 table.insert(place,"Configuration")
96 entry(place, cbi("bmx6/main"), "Configuration",6).dependent=false
97
98 table.insert(place,"General")
99 entry(place, cbi("bmx6/main"), "General",1)
100 table.remove(place)
101
102 table.insert(place,"Advanced")
103 entry(place, cbi("bmx6/advanced"), "Advanced",5)
104 table.remove(place)
105
106 table.insert(place,"Interfaces")
107 entry(place, cbi("bmx6/interfaces"), "Interfaces",2)
108 table.remove(place)
109
110 table.insert(place,"Tunnels")
111 entry(place, cbi("bmx6/tunnels"), "Tunnels",3)
112 table.remove(place)
113
114 table.insert(place,"Plugins")
115 entry(place, cbi("bmx6/plugins"), "Plugins",6)
116 table.remove(place)
117
118 table.insert(place,"HNAv6")
119 entry(place, cbi("bmx6/hna"), "HNAv6",4)
120 table.remove(place)
121
122 table.remove(place)
123
124 end
125
126 function action_status()
127 local status = bmx6json.get("status").status or nil
128 local interfaces = bmx6json.get("interfaces").interfaces or nil
129
130 if status == nil or interfaces == nil then
131 luci.template.render("bmx6/error", {txt="Cannot fetch data from bmx6 json"})
132 else
133 luci.template.render("bmx6/status", {status=status,interfaces=interfaces})
134 end
135 end
136
137 function action_status_j()
138 luci.template.render("bmx6/status_j", {})
139 end
140
141
142 function action_nodes()
143 local orig_list = bmx6json.get("originators").originators or nil
144
145 if orig_list == nil then
146 luci.template.render("bmx6/error", {txt="Cannot fetch data from bmx6 json"})
147 return nil
148 end
149
150 local originators = {}
151 local desc = nil
152 local orig = nil
153 local name = ""
154 local ipv4 = ""
155
156 for _,o in ipairs(orig_list) do
157 orig = bmx6json.get("originators/"..o.name) or {}
158 desc = bmx6json.get("descriptions/"..o.name) or {}
159
160 if string.find(o.name,'.') then
161 name = luci.util.split(o.name,'.')[1]
162 else
163 name = o.name
164 end
165
166 table.insert(originators,{name=name,orig=orig,desc=desc})
167 end
168
169 luci.template.render("bmx6/nodes", {originators=originators})
170 end
171
172 function action_nodes_j()
173 local http = require "luci.http"
174 local link_non_js = "/cgi-bin/luci" .. http.getenv("PATH_INFO") .. '/nodes_nojs'
175
176 luci.template.render("bmx6/nodes_j", {link_non_js=link_non_js})
177 end
178
179 function action_gateways_j()
180 luci.template.render("bmx6/gateways_j", {})
181 end
182
183 function action_tunnels_j()
184 luci.template.render("bmx6/tunnels_j", {})
185 end
186
187
188 function action_links(host)
189 local links = bmx6json.get("links", host)
190 local devlinks = {}
191 local _,l
192
193 if links ~= nil then
194 links = links.links
195 for _,l in ipairs(links) do
196 devlinks[l.viaDev] = {}
197 end
198 for _,l in ipairs(links) do
199 l.globalId = luci.util.split(l.globalId,'.')[1]
200 table.insert(devlinks[l.viaDev],l)
201 end
202 end
203
204 luci.template.render("bmx6/links", {links=devlinks})
205 end
206
207 function action_topology()
208 local originators = bmx6json.get("originators/all")
209 local o,i,l,i2
210 local first = true
211 local topology = '[ '
212 local cache = '/tmp/bmx6-topology.json'
213 local offset = 60
214
215 local cachefd = io.open(cache,r)
216 local update = false
217
218 if cachefd ~= nil then
219 local lastupdate = tonumber(cachefd:read("*line")) or 0
220 if os.time() >= lastupdate + offset then
221 update = true
222 else
223 topology = cachefd:read("*all")
224 end
225 cachefd:close()
226 end
227
228 if cachefd == nil or update then
229 for i,o in ipairs(originators) do
230 local links = bmx6json.get("links",o.primaryIp)
231 if links then
232 if first then
233 first = false
234 else
235 topology = topology .. ', '
236 end
237
238 topology = topology .. '{ "globalId": "%s", "links": [' %o.globalId:match("^[^%.]+")
239
240 local first2 = true
241
242 for i2,l in ipairs(links.links) do
243 if first2 then
244 first2 = false
245 else
246 topology = topology .. ', '
247 end
248
249 topology = topology .. '{ "globalId": "%s", "rxRate": %s, "txRate": %s }'
250 %{ l.globalId:match("^[^%.]+"), l.rxRate, l.txRate }
251
252 end
253
254 topology = topology .. ']}'
255 end
256
257 end
258
259 topology = topology .. ' ]'
260
261 -- Upgrading the content of the cache file
262 cachefd = io.open(cache,'w+')
263 cachefd:write(os.time()..'\n')
264 cachefd:write(topology)
265 cachefd:close()
266 end
267
268 luci.http.prepare_content("application/json")
269 luci.http.write(topology)
270 end
271
272
273 function action_chat()
274 local sms_dir = "/var/run/bmx6/sms"
275 local rcvd_dir = sms_dir .. "/rcvdSms"
276 local send_file = sms_dir .. "/sendSms/chat"
277 local sms_list = bmx6json.get("rcvdSms")
278 local sender = ""
279 local sms_file = ""
280 local chat = {}
281 local to_send = nil
282 local sent = ""
283 local fd = nil
284
285 if luci.sys.call("test -d " .. sms_dir) ~= 0 then
286 luci.template.render("bmx6/error", {txt="sms plugin disabled or some problem with directory " .. sms_dir})
287 return nil
288 end
289
290 sms_list = luci.util.split(luci.util.exec("ls "..rcvd_dir.."/*:chat"))
291
292 for _,sms_path in ipairs(sms_list) do
293 if #sms_path > #rcvd_dir then
294 sms_file = luci.util.split(sms_path,'/')
295 sms_file = sms_file[#sms_file]
296 sender = luci.util.split(sms_file,':')[1]
297
298 -- Trying to clean the name
299 if string.find(sender,".") ~= nil then
300 sender = luci.util.split(sender,".")[1]
301 end
302
303 fd = io.open(sms_path,"r")
304 chat[sender] = fd:read()
305 fd:close()
306 end
307 end
308
309 to_send = luci.http.formvalue("toSend")
310 if to_send ~= nil and #to_send > 1 then
311 fd = io.open(send_file,"w")
312 fd:write(to_send)
313 fd:close()
314 sent = to_send
315 else
316 sent = luci.util.exec("cat "..send_file)
317 end
318
319 luci.template.render("bmx6/chat", {chat=chat,sent=sent})
320 end
321