Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- %% properties
- %% autostart
- %% events
- %% globals
- --]]
- -- Scene : Smart Living Test
- -- Version : 1.1
- -- Date Created : 09 June 2016
- -- Last Changed : 19 June 2016
- -- HC Version : Home Center 2 v4.080
- -- Created By : Dave Harrison
- -- Purpose : To test sending data to SmartLiving.io
- -- Trigger : Triggered by the Timer Scene updating the fifteenMinuteTimer global variable
- --====================================================
- -------------------- Declaration: Local Variables
- --====================================================
- local debug = true
- local sourceTrigger = fibaro:getSourceTrigger()
- local devices =
- {
- loungeTemperature = {deviceId = 28, assetId = "46259176a77ed70b83138702"},
- loungeThermostat = {deviceId = 27, assetId = "464b8ba0e8ced21cf391b773"}
- }
- --====================================================
- -------------------- Functions
- --====================================================
- local function log(str) if debug then fibaro:debug(str); end; end
- local function errorlog(str) fibaro:debug("<font color='red'>"..str.."</font>"); end
- local function sendData (assetId, requestBody)
- local clientID = "xxxxxxxxx" -- Replace xxxxxxxxx with your Client Id
- local clientKey = "yyyyyyyyyyy" -- Replace yyyyyyyyyyy with your Client Key
- local url = "https://api.smartliving.io/asset/" .. assetId .. "/state"
- local httpClient = net.HTTPClient({timeout=5000})
- local httpHeaders =
- {
- ["Auth-ClientId"] = clientID,
- ["Auth-ClientKey"] = clientKey,
- ["Content-Type"] = "application/json",
- }
- httpClient:request(url, {
- options={
- headers = httpHeaders,
- data = requestBody,
- method = 'PUT',
- timeout = 5000
- },
- success = function(response)
- if (response.status >= 200 and response.status < 300) then
- log(assetId .. ": " .. response.status .. " - successful")
- else
- errorlog(assetId .. ": " .. "ERROR")
- end
- end,
- error = function(error)
- errorlog(assetId .. ": " .. "ERROR")
- log(error)
- end
- })
- end
- --====================================================
- -------------------- Main
- --====================================================
- -- Send data to SmartLiving
- -- ------------------------
- if (sourceTrigger["type"] == "autostart") then
- fibaro:debug('Autostart')
- local requestData = {}
- local currentDateISO = os.date("%Y-%m-%dT%XZ")
- local currentDateTime = os.time()
- -- Check each device and format the request data
- for i, device in pairs(devices) do
- requestData = {value = tonumber(fibaro:getValue(device.deviceId, "value")), at = currentDateISO}
- local requestBody = json.encode(requestData)
- log(i .. ": " .. requestBody)
- -- Send the data
- sendData(device.assetId, requestBody)
- setTimeout(sendData, 300*1000)
- end
- else
- fibaro:debug('Trigger')
- local requestData = {}
- local currentDateISO = os.date("%Y-%m-%dT%XZ")
- local currentDateTime = os.time()
- -- Check each device and format the request data
- for i, device in pairs(devices) do
- requestData = {value = tonumber(fibaro:getValue(device.deviceId, "value")), at = currentDateISO}
- local requestBody = json.encode(requestData)
- log(i .. ": " .. requestBody)
- -- Send the data
- sendData(device.assetId, requestBody)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement