Changed default view to node-list
[feed/routing.git] / bmx6-luci / 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 -- getting position of menu
49 local uci_position = uci:get("luci-bmx6","luci","position")
50
51 ---------------------------
52 -- Starting with the pages
53 ---------------------------
54
55 --- status (default)
56 entry(place,call("action_nodes_j"),place[#place],tonumber(uci_position))
57
58 table.insert(place,"Status")
59 entry(place,call("action_status_j"),"Status",0)
60 table.remove(place)
61
62 -- not visible
63 table.insert(place,"nodes_nojs")
64 entry(place, call("action_nodes"), nil)
65 table.remove(place)
66
67 --- nodes
68 table.insert(place,"Nodes")
69 entry(place,call("action_nodes_j"),"Nodes",1)
70 table.remove(place)
71
72 --- links
73 table.insert(place,"Links")
74 entry(place,call("action_links"),"Links",2).leaf = true
75 table.remove(place)
76
77 -- Tunnels
78 table.insert(place,"Tunnels")
79 entry(place,call("action_tunnels_j"), "Tunnels", 3).leaf = true
80 table.remove(place)
81
82 -- Gateways (deprecated)
83 --table.insert(place,"Gateways")
84 --entry(place,call("action_gateways_j"),"Gateways").leaf = true
85 --table.remove(place)
86
87 --- Chat
88 table.insert(place,"Chat")
89 entry(place,call("action_chat"),"Chat",5)
90 table.remove(place)
91
92 --- Graph
93 table.insert(place,"Graph")
94 entry(place, template("bmx6/graph"), "Graph",4)
95 table.remove(place)
96
97 --- Topology (hidden)
98 table.insert(place,"topology")
99 entry(place, call("action_topology"), nil)
100 table.remove(place)
101
102 --- configuration (CBI)
103 table.insert(place,"Configuration")
104 entry(place, cbi("bmx6/main"), "Configuration",6).dependent=false
105
106 table.insert(place,"General")
107 entry(place, cbi("bmx6/main"), "General",1)
108 table.remove(place)
109
110 table.insert(place,"Advanced")
111 entry(place, cbi("bmx6/advanced"), "Advanced",5)
112 table.remove(place)
113
114 table.insert(place,"Interfaces")
115 entry(place, cbi("bmx6/interfaces"), "Interfaces",2)
116 table.remove(place)
117
118 table.insert(place,"Tunnels")
119 entry(place, cbi("bmx6/tunnels"), "Tunnels",3)
120 table.remove(place)
121
122 table.insert(place,"Plugins")
123 entry(place, cbi("bmx6/plugins"), "Plugins",6)
124 table.remove(place)
125
126 table.insert(place,"HNAv6")
127 entry(place, cbi("bmx6/hna"), "HNAv6",4)
128 table.remove(place)
129
130 table.remove(place)
131
132 end
133
134 function action_status()
135 local status = bmx6json.get("status").status or nil
136 local interfaces = bmx6json.get("interfaces").interfaces or nil
137
138 if status == nil or interfaces == nil then
139 luci.template.render("bmx6/error", {txt="Cannot fetch data from bmx6 json"})
140 else
141 luci.template.render("bmx6/status", {status=status,interfaces=interfaces})
142 end
143 end
144
145 function action_status_j()
146 luci.template.render("bmx6/status_j", {})
147 end
148
149
150 function action_nodes()
151 local orig_list = bmx6json.get("originators").originators or nil
152
153 if orig_list == nil then
154 luci.template.render("bmx6/error", {txt="Cannot fetch data from bmx6 json"})
155 return nil
156 end
157
158 local originators = {}
159 local desc = nil
160 local orig = nil
161 local name = ""
162 local ipv4 = ""
163
164 for _,o in ipairs(orig_list) do
165 orig = bmx6json.get("originators/"..o.name) or {}
166 desc = bmx6json.get("descriptions/"..o.name) or {}
167
168 if string.find(o.name,'.') then
169 name = luci.util.split(o.name,'.')[1]
170 else
171 name = o.name
172 end
173
174 table.insert(originators,{name=name,orig=orig,desc=desc})
175 end
176
177 luci.template.render("bmx6/nodes", {originators=originators})
178 end
179
180 function action_nodes_j()
181 local http = require "luci.http"
182 local link_non_js = "/cgi-bin/luci" .. http.getenv("PATH_INFO") .. '/nodes_nojs'
183
184 luci.template.render("bmx6/nodes_j", {link_non_js=link_non_js})
185 end
186
187 function action_gateways_j()
188 luci.template.render("bmx6/gateways_j", {})
189 end
190
191 function action_tunnels_j()
192 luci.template.render("bmx6/tunnels_j", {})
193 end
194
195
196 function action_links(host)
197 local links = bmx6json.get("links", host)
198 local devlinks = {}
199 local _,l
200
201 if links ~= nil then
202 links = links.links
203 for _,l in ipairs(links) do
204 devlinks[l.viaDev] = {}
205 end
206 for _,l in ipairs(links) do
207 l.globalId = luci.util.split(l.globalId,'.')[1]
208 table.insert(devlinks[l.viaDev],l)
209 end
210 end
211
212 luci.template.render("bmx6/links", {links=devlinks})
213 end
214
215 function action_topology()
216 local originators = bmx6json.get("originators/all")
217 local o,i,l,i2
218 local first = true
219 local topology = '[ '
220 local cache = '/tmp/bmx6-topology.json'
221 local offset = 60
222
223 local cachefd = io.open(cache,r)
224 local update = false
225
226 if cachefd ~= nil then
227 local lastupdate = tonumber(cachefd:read("*line")) or 0
228 if os.time() >= lastupdate + offset then
229 update = true
230 else
231 topology = cachefd:read("*all")
232 end
233 cachefd:close()
234 end
235
236 if cachefd == nil or update then
237 for i,o in ipairs(originators) do
238 local links = bmx6json.get("links",o.primaryIp)
239 if links then
240 if first then
241 first = false
242 else
243 topology = topology .. ', '
244 end
245
246 topology = topology .. '{ "globalId": "%s", "links": [' %o.globalId:match("^[^%.]+")
247
248 local first2 = true
249
250 for i2,l in ipairs(links.links) do
251 if first2 then
252 first2 = false
253 else
254 topology = topology .. ', '
255 end
256
257 topology = topology .. '{ "globalId": "%s", "rxRate": %s, "txRate": %s }'
258 %{ l.globalId:match("^[^%.]+"), l.rxRate, l.txRate }
259
260 end
261
262 topology = topology .. ']}'
263 end
264
265 end
266
267 topology = topology .. ' ]'
268
269 -- Upgrading the content of the cache file
270 cachefd = io.open(cache,'w+')
271 cachefd:write(os.time()..'\n')
272 cachefd:write(topology)
273 cachefd:close()
274 end
275
276 luci.http.prepare_content("application/json")
277 luci.http.write(topology)
278 end
279
280
281 function action_chat()
282 local sms_dir = "/var/run/bmx6/sms"
283 local rcvd_dir = sms_dir .. "/rcvdSms"
284 local send_file = sms_dir .. "/sendSms/chat"
285 local sms_list = bmx6json.get("rcvdSms")
286 local sender = ""
287 local sms_file = ""
288 local chat = {}
289 local to_send = nil
290 local sent = ""
291 local fd = nil
292
293 if luci.sys.call("test -d " .. sms_dir) ~= 0 then
294 luci.template.render("bmx6/error", {txt="sms plugin disabled or some problem with directory " .. sms_dir})
295 return nil
296 end
297
298 sms_list = luci.util.split(luci.util.exec("ls "..rcvd_dir.."/*:chat"))
299
300 for _,sms_path in ipairs(sms_list) do
301 if #sms_path > #rcvd_dir then
302 sms_file = luci.util.split(sms_path,'/')
303 sms_file = sms_file[#sms_file]
304 sender = luci.util.split(sms_file,':')[1]
305
306 -- Trying to clean the name
307 if string.find(sender,".") ~= nil then
308 sender = luci.util.split(sender,".")[1]
309 end
310
311 fd = io.open(sms_path,"r")
312 chat[sender] = fd:read()
313 fd:close()
314 end
315 end
316
317 to_send = luci.http.formvalue("toSend")
318 if to_send ~= nil and #to_send > 1 then
319 fd = io.open(send_file,"w")
320 fd:write(to_send)
321 fd:close()
322 sent = to_send
323 else
324 sent = luci.util.exec("cat "..send_file)
325 end
326
327 luci.template.render("bmx6/chat", {chat=chat,sent=sent})
328 end
329