Merge pull request #1298 from aparcar/update_info
[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.submit = translate("Save")
10 m.cancel = translate("Back to overview")
11 m.reset = false
12
13 function m.on_cancel()
14         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
15 end
16
17 m.hidden = {
18         cfg = http.formvalue("cfg")
19 }
20
21 local s = uci:get_all("wireless", m.hidden.cfg)
22 if s ~= nil then
23         wssid = m:field(Value, "ssid", translate("SSID"))
24         wssid.default = s.ssid
25         wssid.datatype = "rangelength(1,32)"
26         if s.encryption and s.key then
27                 wkey = m:field(Value, "key", translatef("Passphrase (%s)", s.encryption))
28         elseif s.encryption and s.password then
29                 wkey = m:field(Value, "password", translatef("Passphrase (%s)", s.encryption))
30         end
31         if s.encryption and (s.key or s.password) then
32                 wkey.password = true
33                 wkey.default = s.key or s.password
34                 if s.encryption == "wep" then
35                         wkey.datatype = "wepkey"
36                 else
37                         wkey.datatype = "wpakey"
38                 end
39         end
40 else
41         m.on_cancel()
42 end
43
44 function wssid.write(self, section, value)
45         uci:set("wireless", m.hidden.cfg, "ssid", wssid:formvalue(section))
46         if s.encryption and s.key then
47                 uci:set("wireless", m.hidden.cfg, "key", wkey:formvalue(section))
48         elseif s.encryption and s.password then
49                 uci:set("wireless", m.hidden.cfg, "password", wkey:formvalue(section))
50         end
51         uci:save("wireless")
52         uci:commit("wireless")
53         m.on_cancel()
54 end
55
56 return m