luci-app-travelmate: sync with travelmate 0.9.0
[project/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / wifi_edit.lua
1 -- Copyright 2017 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
3
4 local fs = require("nixio.fs")
5 local uci = require("luci.model.uci").cursor()
6 local http = require("luci.http")
7
8 m = SimpleForm("edit", translate("Edit Wireless Uplink Configuration"))
9 m.cancel = translate("Back to overview")
10 m.reset = false
11
12 function m.on_cancel()
13         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
14 end
15
16 m.hidden = {
17         cfg = http.formvalue("cfg")
18 }
19
20 local s = uci:get_all("wireless", m.hidden.cfg)
21 if s ~= nil then
22         wssid = m:field(Value, "ssid", translate("SSID"))
23         wssid.default = s.ssid
24         
25         if s.encryption and s.key then
26                 wkey = m:field(Value, "key", translatef("Passphrase (%s)", s.encryption))
27                 wkey.password = true
28                 wkey.default = s.key
29                 if s.encryption == "wep" then
30                         wkey.datatype = "wepkey"
31                 else
32                         wkey.datatype = "wpakey"
33                 end
34         end
35 else
36         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
37 end
38
39 function wssid.write(self, section, value)
40         uci:set("wireless", m.hidden.cfg, "ssid", wssid:formvalue(section))
41         if s.encryption and s.key then
42                 uci:set("wireless", m.hidden.cfg, "key", wkey:formvalue(section))
43         end
44         uci:save("wireless")
45         uci:commit("wireless")
46         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
47 end
48
49 return m