--[[ Monitor a temperature sensor and output the maximum and minimum values --]] --module(...,package.seeall) require("xap") require("xap.bsc") info={ version="2.0", description="Max-Min Monitor" } -- setup some variables MinTemp = 999 MaxTemp = 999 function TempChangeTrigger(frame) -- We only get to this routine if a temperature change has been triggered -- Create a local variable and put the current temperature in it local CurrTemp = frame:getValue("input.state","text") -- Initialise the min and max temps if this is the first run if (tonumber(MinTemp) == 999) then MinTemp = CurrTemp end if (tonumber(MaxTemp) == 999) then MaxTemp = CurrTemp end -- Do we have a new minimum temperature if (tonumber(CurrTemp) <= tonumber(MinTemp)) then MinTemp = CurrTemp bscMinTemp:setText( tostring(MinTemp) ) bscMinTemp:sendEvent() end -- Do we have a new maximum temperature if (tonumber(CurrTemp) >= tonumber(MaxTemp)) then MaxTemp = CurrTemp bscMaxTemp:setText( tostring(MaxTemp) ) bscMaxTemp:sendEvent() end -- Debug lines -- print("MinTemp : " .. MinTemp) -- print("MaxTemp : " .. MaxTemp) -- Prepare an xap message to be broadcast Msg = " Min: " .. MinTemp .. " - Max: " .. MaxTemp displayLCD(Msg) end function displayLCD(msg) -- Send a message to the LCD local msg=string.format([[ xap-header { class=xAPBSC.cmd target=dbzoo.livebox.Controller:lcd } output.state.1 { id=* state=on text=%s }]], msg) xap.sendShort(msg) end function init() -- print("Max-Min Monitor - init") -- Create the new xAP Endpoints bscMinTemp = bsc.Endpoint{source="dbzoo.livebox.Plugboard:mintemp", direction=bsc.INPUT, type=bsc.STREAM} bscMaxTemp = bsc.Endpoint{source="dbzoo.livebox.Plugboard:maxtemp", direction=bsc.INPUT, type=bsc.STREAM} -- Create a filter, the conditions needed to call our "action" routine f = xap.Filter() -- We want a temperature change to trigger our routine f:add("xap-header","source", "dbzoo.livebox.dev.Controller:1wire.1") -- This is the function we call when a match is made to the filters f:callback(TempChangeTrigger) end xap.init("dbzoo.lua.maxmin","FF01DE00") init() xap.process()