luci-app-travelmate: sync with travelmate 1.2.1
[oweals/luci.git] / applications / luci-app-travelmate / luasrc / controller / travelmate.lua
1 -- Copyright 2017-2018 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", "apqr"}, template("travelmate/ap_qr")).leaf = true
31         entry({"admin", "services", "travelmate", "wifiscan"}, template("travelmate/wifi_scan")).leaf = true
32         entry({"admin", "services", "travelmate", "wifiadd"}, form("travelmate/wifi_add", {hideresetbtn=true, hidesavebtn=true})).leaf = true
33         entry({"admin", "services", "travelmate", "wifiedit"}, form("travelmate/wifi_edit", {hideresetbtn=true, hidesavebtn=true})).leaf = true
34         entry({"admin", "services", "travelmate", "wifidelete"}, form("travelmate/wifi_delete", {hideresetbtn=true, hidesavebtn=true})).leaf = true
35         entry({"admin", "services", "travelmate", "wifiorder"}, form("travelmate/wifi_order", {hideresetbtn=true, hidesavebtn=true})).leaf = true
36 end
37
38 function trm_action(name)
39         if name == "do_restart" then
40                 luci.sys.call("/etc/init.d/travelmate restart >/dev/null 2>&1")
41         end
42         luci.http.prepare_content("text/plain") 
43         luci.http.write("0")
44 end
45
46 function status_update()
47         local rt_file
48         local content
49
50         rt_file = uci:get("travelmate", "global", "trm_rtfile") or "/tmp/trm_runtime.json"
51
52         if nixio.fs.access(rt_file) then
53                 content = json.parse(nixio.fs.readfile(rt_file) or "")
54                 http.prepare_content("application/json")
55                 http.write_json(content)
56         end
57 end
58
59 function logread()
60         local content
61
62         if nixio.fs.access("/var/log/messages") then
63                 content = util.trim(util.exec("grep -F 'travelmate-' /var/log/messages"))
64         else
65                 content = util.trim(util.exec("logread -e 'travelmate-'"))
66         end
67         
68         if content == "" then
69                 content = "No travelmate related logs yet!"
70         end
71         http.write(content)
72 end