From 3c5dacaf5609a276b6132d08ad18a0047edf0142 Mon Sep 17 00:00:00 2001 From: Bart Van Der Meerssche Date: Sat, 23 Apr 2011 00:03:18 +0200 Subject: [PATCH] [luci] expand the sensor page with type and function fields + validation --- .../luasrc/model/cbi/mini/sensor.lua | 126 ++++++++++++++---- 1 file changed, 97 insertions(+), 29 deletions(-) diff --git a/mote/v2/openwrt/package/luci/modules/admin-mini/luasrc/model/cbi/mini/sensor.lua b/mote/v2/openwrt/package/luci/modules/admin-mini/luasrc/model/cbi/mini/sensor.lua index 54c6022..bf4c947 100644 --- a/mote/v2/openwrt/package/luci/modules/admin-mini/luasrc/model/cbi/mini/sensor.lua +++ b/mote/v2/openwrt/package/luci/modules/admin-mini/luasrc/model/cbi/mini/sensor.lua @@ -1,19 +1,25 @@ --[[ -LuCI - Lua Configuration Interface + + sensor.lua - LuCI cbi model for the Flukso sensor page -Copyright 2008 Steven Barth -Copyright 2008 Jo-Philipp Wich -Copyright 2011 Bart Van Der Meerssche + Copyright (C) 2011 Bart Van Der Meerssche -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 . -$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