Pachube applet
More of a proof of concept that anything useful.
--[[
Pachube push via LUA
--]]
module(...,package.seeall)
require("xap")
require('ltn12')
local socket = require("socket")
local http = require("socket.http")
info={
version="1.0", description="XAP LUA to Pachube"
}
local pachubeID = 13058
local pachubeKey = "xxxxx"
local pachubeURL = "http://api.pachube.com/v2/feeds/"
local pachubeTbl = {
["dbzoo.cheese.jeenode:3.temp"] = 1,
["dbzoo.cheese.jeenode:4.temp"] = 2
}
function pachubeDispatch(frame)
local id = pachubeTbl[frame:getValue("xap-header","source")]
local value = frame:getValue("input.state","text")
local json_data = string.format(
[[{
"version":"1.0.0",
"datastreams": [ {"id":"%s", "current_value":"%s"}]
}]], id, value)
local response_body = {}
local response, status, header = http.request{
method = "PUT",
url = pachubeURL .. pachubeID,
headers = {
["Content-Type"] = "application/json",
["Content-Length"] = string.len(json_data),
["X-PachubeApiKey"] = pachubeKey
},
source = ltn12.source.string(json_data),
sink = ltn12.sink.table(response_body)
}
end
function init()
local f
for k in pairs(pachubeTbl) do
f = xap.Filter()
f:add("xap-header","source",k)
f:add("xap-header","class","xAPBSC.event")
f:callback(pachubeDispatch)
end
end
Hi Brett
Just took a look at this and tried to mod it for a couple of my ID's on the new Xively site....I appear to have not understood the mechanics and I can't get anything to show on the site....I've attached my moded version for you to hopefully point out where I'm going wrong...
Hope all is well with you and yours in sunny Auz....
regards
Elliot
Attachment | Size |
---|---|
CXPushtoXively.lua | 1.48 KB |
Hi Brett...
Thanks for responding - and yes, I do realise that you've already put Xively posting to multiple Feeds into the HAH...but, for whatever reason it takes an inordinate amount of time just to add one more line to the Xively table - and I keep getting corruption of my backups, so I wanted a programatic means of setting up alternate Feed posting.
As an example, I've got 7 lines being posted to in my Xively table and to add one more line takes upto 8 mins!(from hitting the save button to getting control back)...
Many times (at least one in three) when I do a backup the data gets corupted and I lose most of the Xively table - fortunately I have a 'Master Backup' which is good, so I revert back to that and have to re-enter my additons again!...
Also - the piece of code looked tempting and I need more practice in lua so I thought I'd have a go!
Thanks...
Elliot
Hi Brett
My other problem is that I'm posting to several Xively Feeds, each with their own API key and ID layout. I couldn't see how to attach an alternative API key in the HAH so I was looking to this bit of code to perform my multi-feed requirement....if I can get my head round the lua script! Still struggling, I'm afraid...any help greatly appreciated...
EJ
Hi Brett
I'd realised the first bit (and am happily posting to my main Xively Feed from within the HAH - although I still have problems with scrambled backup ini files whenever I try and add new data to the tables and then save a backup!!).... and that's why the lua looked so useful, 'cos I have three other Xively Feeds, each with their own API Key and ID structure, that I'd like to post to.
My original question stands - I've moded the original lua for my own criteria, but nothing is being posted...can you see if you can spot what I'm doing wrong (the code is attached to my first post).
Thanks for all your efforts - the HAH and LUA are an absolute beaut!
Cheers....EJ
Also - as a follow on from the PUT lua, and as a bit of a preemptive strike - a GET lua structure might be very useful as well...I'll have a look at the code supplied for the recovery of graphs from Xively, that'll give me a starting point unless you have a more structured code segment that you'd prefer we use?
EJ
Such a simple thing.....I'm an idiot!...I forgot to rename the lua with Applet at the end....it now seems to be working...sorry to have been a pain!
Anyway, I can concentrate on the GET side now.....deep joy!
Thanks (from a very red-faced EJ)
Why cheese? :)