2 Copyright (C) 2011 Pau Escrich <pau@dabax.net>
3 Contributors Jo-Philipp Wich <xm@subsignal.org>
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.
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.
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.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
23 local bmx6json = require("luci.model.bmx6json")
25 module("luci.controller.bmx6", package.seeall)
29 local ucim = require "luci.model.uci"
30 local uci = ucim.cursor()
31 -- checking if ignore is on
32 if uci:get("luci-bmx6","luci","ignore") == "1" then
36 -- getting value from uci database
37 local uci_place = uci:get("luci-bmx6","luci","place")
40 if uci_place == nil then
43 local util = require "luci.util"
44 place = util.split(uci_place," ")
46 ---------------------------
47 -- Starting with the pages
48 ---------------------------
50 --- neighbours/descriptions (default)
51 entry(place,call("action_neighbours_j"),place[#place])
53 table.insert(place,"neighbours_nojs")
54 entry(place, call("action_neighbours"), nil)
57 --- status (this is default one)
58 table.insert(place,"Status")
59 entry(place,call("action_status"),"Status")
63 table.insert(place,"Links")
64 entry(place,call("action_links"),"Links").leaf = true
68 table.insert(place,"Gateways")
69 entry(place,call("action_gateways_j"),"Gateways").leaf = true
73 table.insert(place,"Chat")
74 entry(place,call("action_chat"),"Chat")
78 table.insert(place,"Graph")
79 entry(place, template("bmx6/graph"), "Graph")
83 table.insert(place,"topology")
84 entry(place, call("action_topology"), nil)
87 --- configuration (CBI)
88 table.insert(place,"Configuration")
89 entry(place, cbi("bmx6/main"), "Configuration").dependent=false
91 table.insert(place,"Advanced")
92 entry(place, cbi("bmx6/advanced"), "Advanced")
95 table.insert(place,"Interfaces")
96 entry(place, cbi("bmx6/interfaces"), "Interfaces")
99 table.insert(place,"Plugins")
100 entry(place, cbi("bmx6/plugins"), "Plugins")
103 table.insert(place,"HNA")
104 entry(place, cbi("bmx6/hna"), "HNA")
111 function action_status()
112 local status = bmx6json.get("status").status or nil
113 local interfaces = bmx6json.get("interfaces").interfaces or nil
115 if status == nil or interfaces == nil then
116 luci.template.render("bmx6/error", {txt="Cannot fetch data from bmx6 json"})
118 luci.template.render("bmx6/status", {status=status,interfaces=interfaces})
122 function action_neighbours()
123 local orig_list = bmx6json.get("originators").originators or nil
125 if orig_list == nil then
126 luci.template.render("bmx6/error", {txt="Cannot fetch data from bmx6 json"})
130 local originators = {}
136 for _,o in ipairs(orig_list) do
137 orig = bmx6json.get("originators/"..o.name) or {}
138 desc = bmx6json.get("descriptions/"..o.name) or {}
140 if string.find(o.name,'.') then
141 name = luci.util.split(o.name,'.')[1]
146 --Not sure about that, but trying to find main ipv4 from HNA6 published by each node
147 if desc.DESC_ADV ~= nil then
148 for _,h in ipairs(desc.DESC_ADV.extensions[2].HNA6_EXTENSION) do
150 if h ~= nil and string.find(h.address,"::ffff:") then
151 ipv4=string.gsub(h.address,"::ffff:","")
161 table.insert(originators,{name=name,ipv4=ipv4,orig=orig,desc=desc})
164 luci.template.render("bmx6/neighbours", {originators=originators})
167 function action_neighbours_j()
168 local http = require "luci.http"
169 local link_non_js = "/cgi-bin/luci" .. http.getenv("PATH_INFO") .. '/neighbours_nojs'
171 luci.template.render("bmx6/neighbours_j", {link_non_js=link_non_js})
174 function action_gateways_j()
175 luci.template.render("bmx6/gateways_j", {})
179 function action_links(host)
180 local links = bmx6json.get("links", host)
186 for _,l in ipairs(links) do
187 devlinks[l.viaDev] = {}
189 for _,l in ipairs(links) do
190 l.globalId = luci.util.split(l.globalId,'.')[1]
191 table.insert(devlinks[l.viaDev],l)
195 luci.template.render("bmx6/links", {links=devlinks})
198 function action_topology()
199 local originators = bmx6json.get("originators/all")
202 luci.http.prepare_content("application/json")
203 luci.http.write('[ ')
205 for i,o in ipairs(originators) do
206 local links = bmx6json.get("links",o.primaryIp)
211 luci.http.write(', ')
214 luci.http.write('{ "globalId": "%s", "links": [' %o.globalId:match("^[^%.]+"))
218 for i2,l in ipairs(links.links) do
222 luci.http.write(', ')
225 luci.http.write('{ "globalId": "%s", "rxRate": %s, "txRate": %s }'
226 %{ l.globalId:match("^[^%.]+"), l.rxRate, l.txRate })
230 luci.http.write(']}')
234 luci.http.write(' ]')
238 function action_chat()
239 local sms_dir = "/var/run/bmx6/sms"
240 local rcvd_dir = sms_dir .. "/rcvdSms"
241 local send_file = sms_dir .. "/sendSms/chat"
242 local sms_list = bmx6json.get("rcvdSms")
250 if luci.sys.call("test -d " .. sms_dir) ~= 0 then
251 luci.template.render("bmx6/error", {txt="sms plugin disabled or some problem with directory " .. sms_dir})
255 sms_list = luci.util.split(luci.util.exec("ls "..rcvd_dir.."/*:chat"))
257 for _,sms_path in ipairs(sms_list) do
258 if #sms_path > #rcvd_dir then
259 sms_file = luci.util.split(sms_path,'/')
260 sms_file = sms_file[#sms_file]
261 sender = luci.util.split(sms_file,':')[1]
263 -- Trying to clean the name
264 if string.find(sender,".") ~= nil then
265 sender = luci.util.split(sender,".")[1]
268 fd = io.open(sms_path,"r")
269 chat[sender] = fd:read()
274 to_send = luci.http.formvalue("toSend")
275 if to_send ~= nil and #to_send > 1 then
276 fd = io.open(send_file,"w")
281 sent = luci.util.exec("cat "..send_file)
284 luci.template.render("bmx6/chat", {chat=chat,sent=sent})