d53e1f55e59c82d380f5be9d5011289f2675e6d5
[project/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / wifi_order.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 http = require("luci.http")
5 local cfg = http.formvalue("cfg")
6 local dir = http.formvalue("dir")
7 local uci = require("luci.model.uci").cursor()
8 local trmiface = uci:get("travelmate", "global", "trm_iface") or "trm_wwan"
9
10 if cfg ~= nil then
11         local iface = ""
12         local section = ""
13         local idx = ""
14         local idx_change = ""
15         if dir == "up" then
16                 uci:foreach("wireless", "wifi-iface", function(s)
17                         iface = s.network
18                         if iface == trmiface then
19                                 section = s['.name']
20                                 if cfg == section then
21                                         idx = s['.index']
22                                 else
23                                         idx_change = s['.index']
24                                 end
25                                 if idx ~= "" and idx_change ~= "" and idx_change < idx then
26                                         uci:reorder("wireless", cfg, idx_change)
27                                         idx = ""
28                                 end
29                         end
30                 end)
31         elseif dir == "down" then
32                 uci:foreach("wireless", "wifi-iface", function(s)
33                         iface = s.network
34                         if iface == trmiface then
35                                 section = s['.name']
36                                 if cfg == section then
37                                         idx = s['.index']
38                                 else
39                                         idx_change = s['.index']
40                                 end
41                                 if idx ~= "" and idx_change ~= "" and idx_change > idx then
42                                         uci:reorder("wireless", cfg, idx_change)
43                                         idx = ""
44                                 end
45                         end
46                 end)
47         end
48         uci:save("wireless")
49         uci:commit("wireless")
50 end
51 http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))