Advertisement
sebastien_warin

Vera : Send to Cosm.com (ex-Pachube)

May 17th, 2012
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.90 KB | None | 0 0
  1. function my_send_cosm (feed, datas)
  2.  
  3.     require('ltn12')
  4.    
  5.     local apikey = "<<-- YOUR_API_KEY -->>"
  6.     local base_url = "http://api.cosm.com/v2/feeds/"
  7.    
  8.     local json_datas = {}  
  9.     for i,v in pairs(datas) do
  10.         table.insert(json_datas, '{"id":"' .. i .. '", "current_value":"' .. v .. '"}')
  11.     end
  12.    
  13.     local json_data_str = '{ "version":"1.0.0","datastreams":[' .. table.concat(json_datas, ',') .. ']}'
  14.    
  15.     local socket = require("socket")
  16.     local http = require("socket.http")
  17.     local response_body = {}
  18.     local response, status, header = http.request{
  19.         method = "PUT",
  20.         url = base_url .. feed,
  21.         headers = {
  22.             ["Content-Type"] = "application/json",
  23.             ["Content-Length"] = string.len(json_data_str),
  24.             ["X-ApiKey"] = apikey
  25.         },
  26.         source = ltn12.source.string(json_data_str),
  27.         sink = ltn12.sink.table(response_body)
  28.     }
  29.    
  30.     if status == 200 then
  31.         return true
  32.     end
  33.  
  34. end
  35.  
  36. -- Exemple d'utilisation :
  37.  
  38. local datas = {
  39.     -- EnergyMetering : Watts & KHH
  40.     ["HomeEnergyMeter"] =  luup.variable_get("urn:micasaverde-com:serviceId:EnergyMetering1", "Watts", 45),
  41.     ["HomeEnergyMeter_KWh"] = uup.variable_get("urn:micasaverde-com:serviceId:EnergyMetering1", "KWH", 45),
  42.     -- SwitchPower
  43.     ["Prise_TV"] =  luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 23),
  44.     ["Lampe_Bureau"] = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 29),
  45.     -- PilotWire
  46.     ["Chauffage_Salon"] =   luup.variable_get("urn:antor-fr:serviceId:PilotWire1", "Status", 32),
  47.     -- SecuritySensor
  48.     ["Porte_Terrasse"] = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", 31)
  49.     -- TemperatureSensor & HumiditySensor
  50.     ["Temperature_Salon"] = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", 16),
  51.     ["Humidite_Salon"] = luup.variable_get("urn:micasaverde-com:serviceId:HumiditySensor1", "CurrentLevel", 18)
  52. }
  53. return my_send_cosm("<<-- YOUR_FEED_ID -->>", datas)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement