[luci] expand the sensor page with type and function fields + validation

This commit is contained in:
Bart Van Der Meerssche 2011-04-23 00:03:18 +02:00
parent f55b344ec4
commit 3c5dacaf56
1 changed files with 97 additions and 29 deletions

View File

@ -1,19 +1,25 @@
--[[
LuCI - Lua Configuration Interface
sensor.lua - LuCI cbi model for the Flukso sensor page
Copyright 2008 Steven Barth <steven@midlink.org>
Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
Copyright 2011 Bart Van Der Meerssche <bart.vandermeerssche@flukso.net>
Copyright (C) 2011 Bart Van Der Meerssche <bart.vandermeerssche@flukso.net>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
http://www.apache.org/licenses/LICENSE-2.0
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
$Id: network.lua 5949 2010-03-27 14:56:35Z jow $
]]--
local uci = require "luci.model.uci".cursor()
local FLUKSO = uci:get_all("flukso")
@ -22,18 +28,12 @@ local MAX_SENSORS = tonumber(FLUKSO.main.max_sensors)
m = Map("flukso", translate("Sensor"), translate("Fluksometer sensor configuration"))
-- sync with the sensor board after committing to the uci file
m.on_after_commit = function(self)
luci.util.exec("fsync")
end
s = m:section(NamedSection, "main", "settings", "general settings")
phase = s:option(ListValue, "phase", translate("phase"))
phase:value("1")
phase:value("3")
-- (ab)use phase validation callback to assign port numbers to sensors
-- (ab)use phase validation callback to assgn port numbers to sensors
function phase:validate(value, section)
if value == "1" then
uci:set_list("flukso", "1", "port", "1")
@ -53,30 +53,98 @@ function phase:validate(value, section)
return value
end
s = {}
for i = 1, MAX_SENSORS do
s[i] = m:section(NamedSection, tostring(i), "sensor", "sensor #" .. i)
s[i].addremove = false
s = m:section(NamedSection, tostring(i), "sensor", "sensor #" .. i)
s.addremove = false
s[i]:option(DummyValue, "id", translate("identifier"))
s[i]:option(DummyValue, "class", translate("class"))
enable = s:option(Flag, "enable", translate("enable"))
enable.rmempty = false
s:option(DummyValue, "id", translate("identifier"))
s:option(DummyValue, "class", translate("class"))
if FLUKSO[tostring(i)].class == "analog" then
s[i]:option(Value, "voltage", translate("voltage"))
current = s[i]:option(ListValue, "current", translate("current"))
typ = s:option(DummyValue, "type", translate("type"))
func = s:option(Value, "function", translate("function"))
func:depends("enable", "1")
func.rmempty = true
voltage = s:option(Value, "voltage", translate("voltage"))
voltage.rmempty = false
current = s:option(ListValue, "current", translate("current"))
current:value("50")
current:value("100")
current:value("250")
current:value("500")
current.rmempty = false
elseif FLUKSO[tostring(i)].class == "pulse" then
s[i]:option(Value, "constant", translate("constant"))
end
typ = s:option(Value, "type", translate("type"))
typ.rmempty = true
enable = s[i]:option(Flag, "enable", translate("enable"))
enable.rmempty = false
func = s:option(Value, "function", translate("function"))
func:depends("enable", "1")
func.rmempty = true
constant = s:option(Value, "constant", translate("constant"))
constant.rmempty = false
end
end
-- section-level validation does not seem to work with NamedSections
-- so we're resorting to hackery page-level validation instead
function m:parse(...)
-- insert custom processing
for _, sobj in ipairs(self.children) do
local sid = sobj.section
-- only process the 'sensor' sections
if tonumber(sid) then
local err = {}
local field = {}
for _, fld in ipairs(sobj.children) do
field[fld.option] = fld
end
-- an enabled sensor requires a non-empty function/name
if field["class"]:cfgvalue(sid) ~= "uart"
and field["enable"]:formvalue(sid) == "1"
and field["function"]:formvalue(sid) == "" then
err[#err + 1] = "an enabled sensor must have a name"
end
-- an enabled sensor requires a non-empty type
if field["class"]:cfgvalue(sid) == "pulse"
and field["enable"]:formvalue(sid) == "1"
and field["type"]:formvalue(sid) == "" then
err[#err + 1] = "an enabled sensor must have a type"
end
if next(err) then
self.save = false
field["enable"].section.error = {
[sid] = err
}
end
end
end
-- and now call the authentic parser
Map.parse(self, ...)
end
-- sync with the sensor board after committing changes to the uci file
m.on_after_commit = function(self)
luci.util.exec("fsync")
end
return m