Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function my_send_cosm (feed, datas)
- require('ltn12')
- local apikey = "<<-- YOUR_API_KEY -->>"
- local base_url = "http://api.cosm.com/v2/feeds/"
- local json_datas = {}
- for i,v in pairs(datas) do
- table.insert(json_datas, '{"id":"' .. i .. '", "current_value":"' .. v .. '"}')
- end
- local json_data_str = '{ "version":"1.0.0","datastreams":[' .. table.concat(json_datas, ',') .. ']}'
- local socket = require("socket")
- local http = require("socket.http")
- local response_body = {}
- local response, status, header = http.request{
- method = "PUT",
- url = base_url .. feed,
- headers = {
- ["Content-Type"] = "application/json",
- ["Content-Length"] = string.len(json_data_str),
- ["X-ApiKey"] = apikey
- },
- source = ltn12.source.string(json_data_str),
- sink = ltn12.sink.table(response_body)
- }
- if status == 200 then
- return true
- end
- end
- -- Exemple d'utilisation :
- local datas = {
- -- EnergyMetering : Watts & KHH
- ["HomeEnergyMeter"] = luup.variable_get("urn:micasaverde-com:serviceId:EnergyMetering1", "Watts", 45),
- ["HomeEnergyMeter_KWh"] = uup.variable_get("urn:micasaverde-com:serviceId:EnergyMetering1", "KWH", 45),
- -- SwitchPower
- ["Prise_TV"] = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 23),
- ["Lampe_Bureau"] = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", 29),
- -- PilotWire
- ["Chauffage_Salon"] = luup.variable_get("urn:antor-fr:serviceId:PilotWire1", "Status", 32),
- -- SecuritySensor
- ["Porte_Terrasse"] = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", 31)
- -- TemperatureSensor & HumiditySensor
- ["Temperature_Salon"] = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", 16),
- ["Humidite_Salon"] = luup.variable_get("urn:micasaverde-com:serviceId:HumiditySensor1", "CurrentLevel", 18)
- }
- return my_send_cosm("<<-- YOUR_FEED_ID -->>", datas)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement