Advertisement
boomx

SmartLiving - Autostart

Nov 4th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. --[[
  2. %% properties
  3. %% autostart
  4. %% events
  5. %% globals
  6. --]]
  7.  
  8.  
  9. -- Scene : Smart Living Test
  10. -- Version : 1.1
  11. -- Date Created : 09 June 2016
  12. -- Last Changed : 19 June 2016
  13. -- HC Version : Home Center 2 v4.080
  14. -- Created By : Dave Harrison
  15.  
  16. -- Purpose : To test sending data to SmartLiving.io
  17. -- Trigger : Triggered by the Timer Scene updating the fifteenMinuteTimer global variable
  18.  
  19. --====================================================
  20. -------------------- Declaration: Local Variables
  21. --====================================================
  22. local debug = true
  23.  
  24. local sourceTrigger = fibaro:getSourceTrigger()
  25.  
  26.  
  27. local devices =
  28. {
  29. loungeTemperature = {deviceId = 28, assetId = "46259176a77ed70b83138702"},
  30. loungeThermostat = {deviceId = 27, assetId = "464b8ba0e8ced21cf391b773"}
  31. }
  32. --====================================================
  33. -------------------- Functions
  34. --====================================================
  35. local function log(str) if debug then fibaro:debug(str); end; end
  36. local function errorlog(str) fibaro:debug("<font color='red'>"..str.."</font>"); end
  37.  
  38. local function sendData (assetId, requestBody)
  39. local clientID = "xxxxxxxxx" -- Replace xxxxxxxxx with your Client Id
  40. local clientKey = "yyyyyyyyyyy" -- Replace yyyyyyyyyyy with your Client Key
  41. local url = "https://api.smartliving.io/asset/" .. assetId .. "/state"
  42. local httpClient = net.HTTPClient({timeout=5000})
  43.  
  44. local httpHeaders =
  45. {
  46. ["Auth-ClientId"] = clientID,
  47. ["Auth-ClientKey"] = clientKey,
  48. ["Content-Type"] = "application/json",
  49. }
  50.  
  51. httpClient:request(url, {
  52. options={
  53. headers = httpHeaders,
  54. data = requestBody,
  55. method = 'PUT',
  56. timeout = 5000
  57. },
  58. success = function(response)
  59. if (response.status >= 200 and response.status < 300) then
  60. log(assetId .. ": " .. response.status .. " - successful")
  61. else
  62. errorlog(assetId .. ": " .. "ERROR")
  63. end
  64. end,
  65. error = function(error)
  66. errorlog(assetId .. ": " .. "ERROR")
  67. log(error)
  68. end
  69. })
  70. end
  71.  
  72. --====================================================
  73. -------------------- Main
  74. --====================================================
  75.  
  76. -- Send data to SmartLiving
  77. -- ------------------------
  78. if (sourceTrigger["type"] == "autostart") then
  79. fibaro:debug('Autostart')
  80. local requestData = {}
  81. local currentDateISO = os.date("%Y-%m-%dT%XZ")
  82. local currentDateTime = os.time()
  83.  
  84. -- Check each device and format the request data
  85. for i, device in pairs(devices) do
  86.  
  87. requestData = {value = tonumber(fibaro:getValue(device.deviceId, "value")), at = currentDateISO}
  88.  
  89. local requestBody = json.encode(requestData)
  90. log(i .. ": " .. requestBody)
  91.  
  92. -- Send the data
  93. sendData(device.assetId, requestBody)
  94. setTimeout(sendData, 300*1000)
  95. end
  96. else
  97. fibaro:debug('Trigger')
  98. local requestData = {}
  99. local currentDateISO = os.date("%Y-%m-%dT%XZ")
  100. local currentDateTime = os.time()
  101.  
  102. -- Check each device and format the request data
  103. for i, device in pairs(devices) do
  104.  
  105. requestData = {value = tonumber(fibaro:getValue(device.deviceId, "value")), at = currentDateISO}
  106.  
  107. local requestBody = json.encode(requestData)
  108. log(i .. ": " .. requestBody)
  109.  
  110. -- Send the data
  111. sendData(device.assetId, requestBody)
  112. end
  113. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement