5b6141e52d39b246f500f8ac5cf8a2522027f559
[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         local changed = ""
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 (dir == "up" and idx ~= "" and idx_change ~= "" and idx_change < idx) or
26                                 (dir == "down" and idx ~= "" and idx_change ~= "" and idx_change > idx) then
27                                 changed = uci:reorder("wireless", cfg, idx_change)
28                                 idx = ""
29                         end
30                 end
31         end)
32         if changed ~= "" then
33                 uci:save("wireless")
34                 uci:commit("wireless")
35         end
36 end
37 http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))