-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp-exporter.fqa
1 lines (1 loc) · 5.01 KB
/
http-exporter.fqa
1
{"name":"Eksport danych","type":"com.fibaro.genericDevice","apiVersion":"1.2","initialProperties":{"viewLayout":{"$jason":{"body":{"header":{"style":{"height":"0"},"title":"quickApp_device_140"},"sections":{"items":[{"components":[{"name":"label1","style":{"weight":"1.2"},"text":"Hello world","type":"label"},{"style":{"weight":"0.5"},"type":"space"}],"style":{"weight":"1.2"},"type":"vertical"},{"components":[{"name":"button1","style":{"weight":"1.2"},"text":"Send data","type":"button"},{"style":{"weight":"0.5"},"type":"space"}],"style":{"weight":"1.2"},"type":"vertical"},{"components":[{"max":"100","min":"0","name":"slider1","style":{"weight":"1.2"},"text":"","type":"slider"},{"style":{"weight":"0.5"},"type":"space"}],"style":{"weight":"1.2"},"type":"vertical"}]}},"head":{"title":"quickApp_device_140"}}},"uiCallbacks":[{"callback":"button1Event","eventType":"onReleased","name":"button1"},{"callback":"slider1Event","eventType":"onChanged","name":"slider1"}],"quickAppVariables":[{"name":"URL","value":"-"}],"typeTemplateInitialized":true},"files":[{"name":"main","isMain":true,"isOpen":true,"content":"-- ------------------------------------------------------\n-- Fibaro device exporter quick app\n-- Author: Irek Kubicki <[email protected]>\n-- v.0.1.0\n-- GIT: https://github.com/ikubicki/fibaro-exporter\n-- ------------------------------------------------------\n\n\nfunction QuickApp:onInit()\n \n self.http = net.HTTPClient({timeout=3000})\n self.remoteUrl = self:getVariable(\"URL\")\n self:trace(\"Exporter initialized\")\n self:debug(\"URL: \" .. self.remoteUrl)\n self.interval = 5\n\n self:updateView(\"label1\", \"text\", \"Updates will be send every \" .. self.interval .. \" minutes.\")\n self:updateView(\"button1\", \"text\", \"Send data\")\n self:updateView(\"slider1\", \"value\", tostring(self.interval))\n if (string.sub(self.remoteUrl, 0, 4) ~= 'http') then\n self:updateView(\"label1\", \"text\", \"Invalid configuration!\")\n else\n self:loop()\n end\nend\n\nfunction QuickApp:loop()\n self:exportDevices()\n if (self.interval > 0) then\n fibaro.setTimeout(self.interval * 1000 * 60, function() \n self:loop()\n end)\n end\nend\n\nfunction QuickApp:slider1Event(event)\n self.interval = event['values'][1]\n self:updateView(\"label1\", \"text\", \"Updates will be send every \" .. self.interval .. \" minutes.\")\nend\n\nfunction QuickApp:button1Event()\n self:exportDevices()\nend\n\nfunction QuickApp:exportDevices()\n if (string.sub(self.remoteUrl, 0, 4) ~= 'http') then\n return\n end\n self:updateView(\"label1\", \"text\", \"Please wait...\")\n self:updateView(\"button1\", \"text\", \"Sending data\")\n\n local devices = api.get('/devices')\n local devicesPayload = {}\n for _, device in pairs(devices) do\n if (self.id == device.id) then\n goto continue\n end\n if (not device.enabled) then\n goto continue\n end\n if (not device.visible) then\n goto continue\n end\n if (string.sub(device.type, 1, 11) ~= \"com.fibaro.\") then\n goto continue\n end\n if (device.dead or device.properties.dead) then\n goto continue\n end\n local devicePayload = {\n id = device.id,\n name = device.name,\n type = device.type,\n basetype = device.baseType,\n properties = {\n userDescription = device.properties.userDescription,\n quickAppVariables = device.properties.quickAppVariables,\n value = device.properties.value,\n manufacturer = device.properties.manufacturer,\n model = device.properties.model,\n categories = device.properties.categories,\n zwaveInfo = device.properties.zwaveInfo,\n dead = device.properties.dead,\n unit = device.properties.unit\n },\n dead = device.dead\n }\n devicesPayload[#devicesPayload + 1] = devicePayload\n ::continue::\n end\n self.http:request(self.remoteUrl, {\n options = {\n checkCertificate = false,\n method = 'POST',\n data = json.encode(devicesPayload)\n },\n success = function(response)\n if (response.status >= 300) then\n self:updateView(\"button1\", \"text\", \"Send data\")\n self:updateView('label1', 'text', 'Unable to export data')\n else\n self:updateView(\"button1\", \"text\", \"Send data\")\n self:updateView('label1', 'text', 'Updated ' .. tostring(#devicesPayload) .. ' devices at ' .. os.date(\"%Y-%m-%d %H:%M:%S\"))\n end\n end,\n error = function(error)\n self:error('HTTP Error', json.encode(error))\n self:updateView('label1', 'text', 'ERROR: ' .. json.encode(error))\n self:updateView(\"button1\", \"text\", \"Send data\")\n end\n }) \nend\n"}]}