luci-app-travelmate: small bugfixes & optimizations
[project/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / wifi_order.lua
index 5734841..5b6141e 100644 (file)
@@ -1,22 +1,37 @@
 -- Copyright 2017 Dirk Brenken (dev@brenken.org)
 -- This is free software, licensed under the Apache License, Version 2.0
 
-local uci = require("luci.model.uci").cursor()
 local http = require("luci.http")
 local cfg = http.formvalue("cfg")
-local pos = http.formvalue("pos")
 local dir = http.formvalue("dir")
+local uci = require("luci.model.uci").cursor()
+local trmiface = uci:get("travelmate", "global", "trm_iface") or "trm_wwan"
 
 if cfg ~= nil then
-       if dir == "up" then
-               pos = pos - 1
-               uci:reorder("wireless", cfg, pos)
-       elseif dir == "down" then
-               pos = pos + 1
-               uci:reorder("wireless", cfg, pos)
+       local iface = ""
+       local section = ""
+       local idx = ""
+       local idx_change = ""
+       local changed = ""
+       uci:foreach("wireless", "wifi-iface", function(s)
+               iface = s.network
+               if iface == trmiface then
+                       section = s['.name']
+                       if cfg == section then
+                               idx = s['.index']
+                       else
+                               idx_change = s['.index']
+                       end
+                       if (dir == "up" and idx ~= "" and idx_change ~= "" and idx_change < idx) or
+                               (dir == "down" and idx ~= "" and idx_change ~= "" and idx_change > idx) then
+                               changed = uci:reorder("wireless", cfg, idx_change)
+                               idx = ""
+                       end
+               end
+       end)
+       if changed ~= "" then
+               uci:save("wireless")
+               uci:commit("wireless")
        end
-       uci:save("wireless")
-       uci:commit("wireless")
 end
-
 http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))