local display_names = { valve_1 = "Irrigation Sector 1", valve_2 = "Irrigation Sector 2", valve_3 = "Irrigation Sector 3 or USB load output", irrigation_timer = "Irrigation timer" } local function gpio_device(pin, state_var) return function(cmd) if cmd == 'turn_OFF' then gpio.wakeup(pin, gpio.INTR_LOW) gpio.write(pin, 0) _G[state_var] = false end if cmd == 'turn_ON' and low_voltage_disconnect_state == 1 then gpio.wakeup(pin, gpio.INTR_HIGH) gpio.write(pin, 1) _G[state_var] = true end if cmd == 'status' then return (_G[state_var] == true) and (low_voltage_disconnect_state == 1) end end end local valve_1 = gpio_device(26, "valve_1_state") local valve_2 = gpio_device(27, "valve_2_state") local valve_3 = gpio_device(12, "valve_3_state") local items = { valve_1 = valve_1, valve_2 = valve_2, valve_3 = valve_3, -- The timer itself is accessed dynamically through irr_watchdog. -- This avoids storing nil here if the timer is created later. irrigation_timer = true } local item_order = { 'valve_1', 'valve_2', 'valve_3', 'irrigation_timer' } -- nil means that this page has not yet established whether the watchdog -- is intentionally enabled. On first use, its current running state is used. local irrigation_timer_enabled = nil local function irrigation_timer_running() if type(irr_watchdog) ~= 'userdata' then return false end local running = irr_watchdog:state() return running == true end local function remember_irrigation_timer_state() if irrigation_timer_enabled == nil then irrigation_timer_enabled = irrigation_timer_running() end end local function stop_irrigation_timer() if type(irr_watchdog) ~= 'userdata' then return false end local running = irr_watchdog:state() if running == true then irr_watchdog:stop() end return false end local function start_irrigation_timer() if type(irr_watchdog) ~= 'userdata' then print("Cannot start irr_watchdog: timer does not exist") return false end local running = irr_watchdog:state() if running == nil then print("Cannot start irr_watchdog: timer is not registered") return false end if running == false then irr_watchdog:start() end return irrigation_timer_running() end local function any_valve_is_on() return valve_1('status') or valve_2('status') or valve_3('status') end local function control_irrigation_timer(cmd) if cmd == 'status' then return irrigation_timer_running() end if cmd == 'turn_OFF' then -- Remember that this was an explicit user command. Closing the -- final manually opened valve must not restart the watchdog. irrigation_timer_enabled = false return stop_irrigation_timer() end if cmd == 'turn_ON' then -- Starting the watchdog while a manually controlled valve is open -- could allow it to terminate the manual irrigation. if any_valve_is_on() then print( "Cannot start irr_watchdog: " .. "one or more irrigation valves are open" ) return false end irrigation_timer_enabled = true return start_irrigation_timer() end return false end local function control_valve(cmd, item) if cmd == 'status' then return item('status') end if cmd == 'turn_ON' then -- Infer the original watchdog state before pausing it. This makes -- the code work even if irr_watchdog was created after this page. remember_irrigation_timer_state() -- The watchdog must not stop a valve opened manually from this page. stop_irrigation_timer() item('turn_ON') -- If the valve could not be opened, for example because low-voltage -- disconnect is active, restore the watchdog when appropriate. if not item('status') and not any_valve_is_on() and irrigation_timer_enabled == true then start_irrigation_timer() end return item('status') end if cmd == 'turn_OFF' then item('turn_OFF') -- Resume automatic irrigation only after the last manually opened -- valve has been closed, and only if the timer was not explicitly -- disabled by the user. if not any_valve_is_on() and irrigation_timer_enabled == true then start_irrigation_timer() end return item('status') end return false end local function startstopstatus(cmd, what) local item = items[what] if item == nil then return false end if what == 'irrigation_timer' then return control_irrigation_timer(cmd) end if type(item) == 'function' then return control_valve(cmd, item) end return false end -- MAIN REQUEST HANDLER return function(info) if not authenticated() then return end local p = info.headers.path -- Routing logic if p:match('^/icontrol/turn_ON/') then startstopstatus('turn_ON', p:sub(19)) elseif p:match('^/icontrol/turn_OFF/') then startstopstatus('turn_OFF', p:sub(20)) end -- Send Preamble and Styles send_buffered(info.http_preamble) send_buffered( "" ) for _, k in ipairs(item_order) do local status = 'OFF' local command = 'turn_ON' local status_class = 'status-OFF' if startstopstatus('status', k) then status = 'ON' command = 'turn_OFF' status_class = 'status-ON' end local label = display_names[k] or k send_buffered([[