--[[ xAP Caching and Thingspeak interface --]] module(...,package.seeall) require("xap") require("pl.stringx").import() require("pl.pretty") socket = require("socket") info={ version="1.0", description="xAP to Thingspeak applet" } local fields = { {source = "dbzoo.livebox2.jeenode:util.temp",class = "xAPBSC.event",section = "input.state",value = "text"}, {source = "dbzoo.livebox2.jeenode:util.light",class = "xAPBSC.event",section = "input.state",value = "text"}, {source = "dbzoo.livebox2.jeenode:test2.light",class = "xAPBSC.event",section = "input.state",value = "text"}, {source = "dbzoo.livebox2.jeenode:mark.light",class = "xAPBSC.event",section = "input.state",value = "text"}, {source = "dbzoo.livebox2.jeenode:test.light",class = "xAPBSC.event",section = "input.state",value = "text"}, {source = "dbzoo.livebox2.jeenode:test.moved",class = "xAPBSC.event",section = "input.state",value = "state"}, {source = "dbzoo.livebox2.jeenode:util.moved",class = "xAPBSC.event",section = "input.state",value = "state"}, {source = "dbzoo.livebox2.jeenode:mark.moved",class = "xAPBSC.event",section = "input.state",value = "state"} } local APIKey = "AQT7JTOC7CO036EK" local vfs = { filter={}, data={} } for i,k in ipairs(fields) do vfs.filter[i] = xap.Filter{["xap-header"] = { source=fields[i]["source"], class=fields[i]["class"] }} end function updateCache(frame) for i,k in ipairs(fields) do if frame:getValue("xap-header","source") == fields[i]["source"] then vfs.data[i] = frame:getValue(fields[i]["section"],fields[i]["value"]) if vfs.data[i] == "on" then vfs.data[i] = "1" end if vfs.data[i] == "off" then vfs.data[i] = "1" end end end end function send() local host, port = "api.thingspeak.com", 80 client = socket.tcp() client:connect(host, port) local fieldtext = "" for i,k in ipairs(fields) do if vfs.data[i] then fieldtext = fieldtext.."&field"..i.."="..vfs.data[i] else end end client:send("GET /update?api_key="..APIKey..fieldtext .. " HTTP/1.1\r\n" .. "Host: api.thingspeak.com\r\n" .. "Connection: close\r\n" .. "Accept: */*\r\n" .. "User-Agent: Mozilla/4.0 (compatible; Lua; Windows NT 5.1)\r\n" .. "\r\n") end function init() for _,k in pairs(vfs.filter) do k:callback(updateCache) end xap.Timer(send, 60):start(false) end