luci-app-travelmate: small controller changes
[oweals/luci.git] / applications / luci-app-travelmate / luasrc / controller / travelmate.lua
1 -- Copyright 2017-2019 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
3
4 module("luci.controller.travelmate", package.seeall)
5
6 local sys  = require("luci.sys")
7 local util = require("luci.util")
8 local http = require("luci.http")
9 local i18n = require("luci.i18n")
10 local json = require("luci.jsonc")
11 local uci  = require("luci.model.uci").cursor()
12
13 function index()
14         if not nixio.fs.access("/etc/config/travelmate") then
15                 return
16         end
17         entry({"admin", "services", "travelmate"}, firstchild(), _("Travelmate"), 40).dependent = false
18         entry({"admin", "services", "travelmate", "tab_from_cbi"}, cbi("travelmate/overview_tab", {hideresetbtn=true, hidesavebtn=true}), _("Overview"), 10).leaf = true
19         entry({"admin", "services", "travelmate", "stations"}, template("travelmate/stations"), _("Wireless Stations"), 20).leaf = true
20         entry({"admin", "services", "travelmate", "log"}, template("travelmate/logread"), _("View Logfile"), 30).leaf = true
21         entry({"admin", "services", "travelmate", "advanced"}, firstchild(), _("Advanced"), 100)
22         entry({"admin", "services", "travelmate", "advanced", "configuration"}, form("travelmate/configuration_tab"), _("Edit Travelmate Configuration"), 110).leaf = true
23         entry({"admin", "services", "travelmate", "advanced", "cfg_wireless"}, form("travelmate/cfg_wireless_tab"), _("Edit Wireless Configuration"), 120).leaf = true
24         entry({"admin", "services", "travelmate", "advanced", "cfg_network"}, form("travelmate/cfg_network_tab"), _("Edit Network Configuration"), 130).leaf = true
25         entry({"admin", "services", "travelmate", "advanced", "cfg_firewall"}, form("travelmate/cfg_firewall_tab"), _("Edit Firewall Configuration"), 140).leaf = true
26
27         entry({"admin", "services", "travelmate", "logread"}, call("logread"), nil).leaf = true
28         entry({"admin", "services", "travelmate", "status"}, call("status_update"), nil).leaf = true
29         entry({"admin", "services", "travelmate", "action"}, call("trm_action"), nil).leaf = true
30         entry({"admin", "services", "travelmate", "wifiscan"}, template("travelmate/wifi_scan")).leaf = true
31         entry({"admin", "services", "travelmate", "wifiadd"}, form("travelmate/wifi_add", {hideresetbtn=true, hidesavebtn=true})).leaf = true
32         entry({"admin", "services", "travelmate", "wifiedit"}, form("travelmate/wifi_edit", {hideresetbtn=true, hidesavebtn=true})).leaf = true
33         entry({"admin", "services", "travelmate", "wifidelete"}, form("travelmate/wifi_delete", {hideresetbtn=true, hidesavebtn=true})).leaf = true
34         entry({"admin", "services", "travelmate", "wifiorder"}, form("travelmate/wifi_order", {hideresetbtn=true, hidesavebtn=true})).leaf = true
35 end
36
37 function trm_action(name)
38         if name == "do_restart" then
39                 luci.sys.call("/etc/init.d/travelmate restart >/dev/null 2>&1")
40         end
41         luci.http.prepare_content("text/plain")
42         luci.http.write("0")
43 end
44
45 function status_update()
46         local rt_file
47         local content
48
49         rt_file = uci:get("travelmate", "global", "trm_rtfile") or "/tmp/trm_runtime.json"
50
51         if nixio.fs.access(rt_file) then
52                 content = json.parse(nixio.fs.readfile(rt_file) or "")
53                 http.prepare_content("application/json")
54                 http.write_json(content)
55         end
56 end
57
58 function logread()
59         local content = util.trim(util.exec("logread -e 'travelmate-'")) or ""
60
61         if content == "" then
62                 content = "No travelmate related logs yet!"
63         end
64         http.write(content)
65 end