luci-mod-system: leds.js: display default state as On/Off
[project/luci.git] / applications / luci-app-ddns / luasrc / model / cbi / ddns / overview.lua
1 -- Copyright 2014-2018 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local DISP = require "luci.dispatcher"
5 local HTTP = require "luci.http"
6 local SYS = require "luci.sys"
7 local CTRL = require "luci.controller.ddns" -- this application's controller
8 local DDNS = require "luci.tools.ddns" -- ddns multiused functions
9
10 local show_hints = not (DDNS.env_info("has_ipv6") -- IPv6 support
11 and DDNS.env_info("has_ssl") -- HTTPS support
12 and DDNS.env_info("has_proxy") -- Proxy support
13 and DDNS.env_info("has_bindhost") -- DNS TCP support
14 and DDNS.env_info("has_forceip") -- Force IP version
15 and DDNS.env_info("has_dnsserver") -- DNS server support
16 and DDNS.env_info("has_bindnet") -- Bind to network/interface
17 and DDNS.env_info("has_cacerts") -- certificates installed at /etc/ssl/certs
18 )
19 local not_enabled = not SYS.init.enabled("ddns")
20 local need_update = not CTRL.service_ok()
21
22 -- html constants
23 font_red = [[<font color="red">]]
24 font_off = [[</font>]]
25 bold_on = [[<strong>]]
26 bold_off = [[</strong>]]
27
28 -- cbi-map definition -- #######################################################
29 m = Map("ddns")
30 m.title = CTRL.app_title_main()
31 m.description = CTRL.app_description()
32
33 m.on_after_commit = function(self)
34 if self.changed then -- changes ?
35 local command = CTRL.luci_helper
36 if SYS.init.enabled("ddns") then -- ddns service enabled, restart all
37 command = command .. " -- restart"
38 os.execute(command)
39 else -- ddns service disabled, send SIGHUP to running
40 command = command .. " -- reload"
41 os.execute(command)
42 end
43 end
44 end
45
46 -- SimpleSection definition -- ##################################################
47 -- with all the JavaScripts we need for "a good Show"
48 a = m:section( SimpleSection )
49 a.template = "ddns/overview_status"
50
51 -- SimpleSection definition -- #################################################
52 -- show Hints to optimize installation and script usage
53 if show_hints or need_update or not_enabled then
54
55 s = m:section( SimpleSection, translate("Hints") )
56
57 -- ddns-scripts needs to be updated for full functionality
58 if need_update then
59 local dv = s:option(DummyValue, "_update_needed")
60 dv.titleref = DISP.build_url("admin", "system", "packages")
61 dv.rawhtml = true
62 dv.title = font_red .. bold_on ..
63 translate("Software update required") .. bold_off .. font_off
64 dv.value = translate("The currently installed 'ddns-scripts' package did not support all available settings.") ..
65 "<br />" ..
66 translate("Please update to the current version!")
67 end
68
69 -- DDNS Service disabled
70 if not_enabled then
71 local dv = s:option(DummyValue, "_not_enabled")
72 dv.titleref = DISP.build_url("admin", "system", "startup")
73 dv.rawhtml = true
74 dv.title = bold_on ..
75 translate("DDNS Autostart disabled") .. bold_off
76 dv.value = translate("Currently DDNS updates are not started at boot or on interface events." .. "<br />" ..
77 "You can start/stop each configuration here. It will run until next reboot.")
78 end
79
80 -- Show more hints on a separate page
81 if show_hints then
82 local dv = s:option(DummyValue, "_separate")
83 dv.titleref = DISP.build_url("admin", "services", "ddns", "hints")
84 dv.rawhtml = true
85 dv.title = bold_on ..
86 translate("Show more") .. bold_off
87 dv.value = translate("Follow this link" .. "<br />" ..
88 "You will find more hints to optimize your system to run DDNS scripts with all options")
89 end
90 end
91
92 -- TableSection definition -- ##################################################
93 ts = m:section( TypedSection, "service",
94 translate("Overview"),
95 translate("Below is a list of configured DDNS configurations and their current state.")
96 .. "<br />"
97 .. translate("If you want to send updates for IPv4 and IPv6 you need to define two separate Configurations "
98 .. "i.e. 'myddns_ipv4' and 'myddns_ipv6'")
99 .. "<br />"
100 .. [[<a href="]] .. DISP.build_url("admin", "services", "ddns", "global") .. [[">]]
101 .. translate("To change global settings click here") .. [[</a>]] )
102 ts.sectionhead = translate("Configuration")
103 ts.template = "cbi/tblsection"
104 ts.addremove = true
105 ts.extedit = DISP.build_url("admin", "services", "ddns", "detail", "%s")
106 function ts.create(self, name)
107 AbstractSection.create(self, name)
108 HTTP.redirect( self.extedit:format(name) )
109 end
110
111 -- Lookup_Host and registered IP -- #################################################
112 dom = ts:option(DummyValue, "_lookupIP",
113 translate("Lookup Hostname") .. "<br />" .. translate("Registered IP") )
114 dom.template = "ddns/overview_doubleline"
115 function dom.set_one(self, section)
116 local lookup = self.map:get(section, "lookup_host") or ""
117 if lookup ~= "" then
118 return lookup
119 else
120 return [[<em>]] .. translate("config error") .. [[</em>]]
121 end
122 end
123 function dom.set_two(self, section)
124 local chk_sec = DDNS.calc_seconds(
125 tonumber(self.map:get(section, "check_interval")) or 10,
126 self.map:get(section, "check_unit") or "minutes" )
127 local ip = DDNS.get_regip(section, chk_sec)
128 if ip == "NOFILE" then
129 local lookup_host = self.map:get(section, "lookup_host") or ""
130 if lookup_host == "" then return "" end
131 local dnsserver = self.map:get(section, "dnsserver") or ""
132 local use_ipv6 = tonumber(self.map:get(section, "use_ipv6") or 0)
133 local force_ipversion = tonumber(self.map:get(section, "force_ipversion") or 0)
134 local force_dnstcp = tonumber(self.map:get(section, "force_dnstcp") or 0)
135 local is_glue = tonumber(self.map:get(section, "is_glue") or 0)
136 local command = CTRL.luci_helper .. [[ -]]
137 if (use_ipv6 == 1) then command = command .. [[6]] end
138 if (force_ipversion == 1) then command = command .. [[f]] end
139 if (force_dnstcp == 1) then command = command .. [[t]] end
140 if (is_glue == 1) then command = command .. [[g]] end
141 command = command .. [[l ]] .. lookup_host
142 command = command .. [[ -S ]] .. section
143 if (#dnsserver > 0) then command = command .. [[ -d ]] .. dnsserver end
144 command = command .. [[ -- get_registered_ip]]
145 ip = SYS.exec(command)
146 end
147 if ip == "" then ip = translate("no data") end
148 return ip
149 end
150
151 -- enabled
152 ena = ts:option( Flag, "enabled",
153 translate("Enabled"))
154 ena.template = "ddns/overview_enabled"
155 ena.rmempty = false
156
157 -- show PID and next update
158 upd = ts:option( DummyValue, "_update",
159 translate("Last Update") .. "<br />" .. translate("Next Update"))
160 upd.template = "ddns/overview_doubleline"
161 function upd.set_one(self, section) -- fill Last Update
162 -- get/validate last update
163 local uptime = SYS.uptime()
164 local lasttime = DDNS.get_lastupd(section)
165 if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
166 lasttime = 0
167 end
168
169 -- no last update happen
170 if lasttime == 0 then
171 return translate("never")
172
173 -- we read last update
174 else
175 -- calc last update
176 -- os.epoch - sys.uptime + lastupdate(uptime)
177 local epoch = os.time() - uptime + lasttime
178 -- use linux date to convert epoch
179 return DDNS.epoch2date(epoch)
180 end
181 end
182 function upd.set_two(self, section) -- fill Next Update
183 -- get enabled state
184 local enabled = tonumber(self.map:get(section, "enabled") or 0)
185 local datenext = translate("unknown error") -- formatted date of next update
186
187 -- get force seconds
188 local force_interval = tonumber(self.map:get(section, "force_interval") or 72)
189 local force_unit = self.map:get(section, "force_unit") or "hours"
190 local force_seconds = DDNS.calc_seconds(force_interval, force_unit)
191
192 -- get last update and get/validate PID
193 local uptime = SYS.uptime()
194 local lasttime = DDNS.get_lastupd(section)
195 if lasttime > uptime then -- /var might not be linked to /tmp and cleared on reboot
196 lasttime = 0
197 end
198 local pid = DDNS.get_pid(section)
199
200 -- calc next update
201 if lasttime > 0 then
202 local epoch = os.time() - uptime + lasttime + force_seconds
203 -- use linux date to convert epoch
204 datelast = DDNS.epoch2date(epoch)
205 end
206
207 -- process running but update needs to happen
208 if pid > 0 and ( lasttime + force_seconds - uptime ) < 0 then
209 datenext = translate("Verify")
210
211 -- run once
212 elseif force_seconds == 0 then
213 datenext = translate("Run once")
214
215 -- no process running and NOT enabled
216 elseif pid == 0 and enabled == 0 then
217 datenext = translate("Disabled")
218
219 -- no process running and NOT
220 elseif pid == 0 and enabled ~= 0 then
221 datenext = translate("Stopped")
222 end
223
224 return datenext
225 end
226
227 -- start/stop button
228 btn = ts:option( Button, "_startstop",
229 translate("Process ID") .. "<br />" .. translate("Start / Stop") )
230 btn.template = "ddns/overview_startstop"
231 function btn.cfgvalue(self, section)
232 local pid = DDNS.get_pid(section)
233 if pid > 0 then
234 btn.inputtitle = "PID: " .. pid
235 btn.inputstyle = "reset"
236 btn.disabled = false
237 elseif (self.map:get(section, "enabled") or "0") ~= "0" then
238 btn.inputtitle = translate("Start")
239 btn.inputstyle = "apply"
240 btn.disabled = false
241 else
242 btn.inputtitle = "----------"
243 btn.inputstyle = "button"
244 btn.disabled = true
245 end
246 return true
247 end
248
249 return m