-
Notifications
You must be signed in to change notification settings - Fork 0
/
Toon_Water-02.fqa
1 lines (1 loc) · 8.35 KB
/
Toon_Water-02.fqa
1
{"name":"Toon Water","type":"com.fibaro.multilevelSensor","apiVersion":"1.2","initialProperties":{"viewLayout":{"$jason":{"body":{"header":{"style":{"height":"0"},"title":"quickApp_device_1154"},"sections":{"items":[{"components":[{"name":"label1","style":{"weight":"1.2"},"text":"Label1","type":"label","visible":true},{"style":{"weight":"0.5"},"type":"space"}],"style":{"weight":"1.2"},"type":"vertical"}]}},"head":{"title":"quickApp_device_1154"}}},"uiCallbacks":[],"quickAppVariables":[{"name":"IPaddress","type":"string","value":"192.168.1.112"},{"name":"Interval","type":"string","value":"10"},{"name":"debugLevel","type":"string","value":"1"}],"typeTemplateInitialized":true},"files":[{"name":"main","isMain":true,"isOpen":true,"content":"-- QUICKAPP TOON WATER\n\n-- This Quickapp retrieves water consumption from the Toon Watermeter\n-- The main devices containes the Water Flow (liters/minute)\n-- This QuickApp has Child Device for Water Total (m³)\n\n-- See http://www.toonwater.nl for the device and more info\n\n\n-- Version 0.2 (20th February 2022)\n-- Better check for bad responses Toon Water\n\n-- Version 0.1 (1st January 2022)\n-- Initial version\n\n\n-- Variables (mandatory): \n-- IPaddress = IP address of your Toonwater meter\n-- Interval = Number in seconds \n-- debugLevel = Number (1=some, 2=few, 3=all, 4=simulation mode) (default = 1)\n \n \n-- No editing of this code is needed \n\n\nclass 'waterquantity'(QuickAppChild)\nfunction waterquantity:__init(dev)\n QuickAppChild.__init(self,dev)\nend\nfunction waterquantity:updateValue(data) \n self:updateProperty(\"value\", tonumber(data.waterquantity))\n self:updateProperty(\"unit\", \"m³\")\n self:updateProperty(\"log\", \" \")\nend\n\n\nlocal function getChildVariable(child,varName)\n for _,v in ipairs(child.properties.quickAppVariables or {}) do\n if v.name==varName then return v.value end\n end\n return \"\"\nend\n\n\n-- QuickApp Functions\n\n\nfunction QuickApp:updateChildDevices() -- Update Child Devices\n for id,child in pairs(self.childDevices) do \n child:updateValue(data) \n end\nend\n\n\nfunction QuickApp:logging(level,text) -- Logging function for debug messages\n if tonumber(debugLevel) >= tonumber(level) then \n self:debug(text)\n end\nend\n\n\nfunction QuickApp:updateProperties() -- Update the properties\n self:logging(3,\"QuickApp:updateProperties\")\n self:updateProperty(\"value\", tonumber(data.waterflow))\n self:updateProperty(\"unit\", \"l/m\")\n self:updateProperty(\"log\", os.date(\"%d-%m-%Y %T\"))\nend\n\n\nfunction QuickApp:updateLabels() -- Update the labels\n self:logging(3,\"QuickApp:updateLabels\")\n\n local labelText = \"\"\n if debugLevel == 4 then\n labelText = labelText ..\"SIMULATION MODE\" ..\"\\n\\n\"\n end\n \n labelText = labelText ..\"Water Flow: \" ..data.waterflow ..\" liters/minute\" ..\"\\n\"\n labelText = labelText ..\"Total Water: \" ..data.waterquantity ..\" m³\" ..\"\\n\\n\"\n labelText = labelText ..os.date(\"%d-%m-%Y %T\")\n\n\n self:updateView(\"label1\", \"text\", labelText)\n self:logging(2,labelText)\nend\n\n\nfunction QuickApp:valuesToon() -- Get the values from json file\n self:logging(3,\"QuickApp:valuesToon\")\n data.waterflow = jsonTable.waterflow\n data.waterquantity = tonumber(jsonTable.waterquantity)/1000\nend\n\n\nfunction QuickApp:simData() -- Simulate Toon Water\n self:logging(3,\"simData\")\n apiResult = '{\"waterflow\":\"4\",\"waterquantity\":\"635281\"}'\n jsonTable = json.decode(apiResult) -- Decode the json string from api to lua-table \n\n self:valuesToon() -- Get the values for Toon Water\n self:updateLabels() -- Update the labels\n self:updateProperties() -- Update the properties\n self:updateChildDevices() -- Update the Child Devices\n \n self:logging(3,\"SetTimeout \" ..Interval ..\" seconds\")\n fibaro.setTimeout(Interval*1000, function() \n self:simData()\n end)\nend\n\n\nfunction QuickApp:getData() -- Get data from Toon Water\n self:logging(3,\"getData\")\n local url = \"http://\" ..IPaddress ..\"/json.html\"\n self:logging(3,\"url: \" ..url)\n self.http:request(url, {\n options = {\n headers = {Accept = \"application/json\"}, method = 'GET'},\n success = function(response)\n self:logging(3,\"Response status: \" ..response.status)\n self:logging(3,\"Response data: \" ..response.data)\n\n if response.data == nil or response.data == \"\" or response.data == \"[]\" or response.status > 200 then -- Check for empty result\n self:warning(\"Temporarily no production data from Toon Water\")\n return\n end\n\n jsonTable = json.decode(response.data) -- Decode the json string from api to lua-table\n\n self:valuesToon() -- Get the values for Toon Water\n self:updateLabels() -- Update the labels\n self:updateProperties() -- Update the properties\n self:updateChildDevices() -- Update the Child Devices\n\n end,\n error = function(error)\n self:error(\"error: \" ..json.encode(error))\n self:updateProperty(\"log\", \"error: \" ..json.encode(error))\n end\n }) \n self:logging(3,\"SetTimeout \" ..Interval ..\" seconds\")\n fibaro.setTimeout(Interval*1000, function() -- Checks every [Interval] seconds for new data\n self:getData()\n end)\nend \n\n\nfunction QuickApp:createVariables() -- Create all Variables\n self:logging(3,\"Start createVariables\")\n data = {}\n data.waterflow = \"\"\n data.waterquantity = \"\"\nend\n\n\nfunction QuickApp:getQuickappVariables() -- Get all Quickapp Variables or create them\n IPaddress = self:getVariable(\"IPaddress\")\n Interval = tonumber(self:getVariable(\"Interval\")) \n debugLevel = tonumber(self:getVariable(\"debugLevel\"))\n\n -- Check existence of the mandatory variables, if not, create them with default values \n if IPaddress == \"\" or IPaddress == nil then \n IPaddress = \"192.168.1.112\" -- Default IPaddress \n self:setVariable(\"IPaddress\", IPaddress)\n self:trace(\"Added QuickApp variable IPaddress\")\n end\n if Interval == \"\" or Interval == nil then\n Interval = \"10\" -- Default interval in seconds\n self:setVariable(\"Interval\", Interval)\n self:trace(\"Added QuickApp variable Interval\")\n Interval = tonumber(Interval)\n end\n if debugLevel == \"\" or debugLevel == nil then\n debugLevel = \"1\" -- Default value for debugLevel\n self:setVariable(\"debugLevel\",debugLevel)\n self:trace(\"Added QuickApp variable debugLevel\")\n debugLevel = tonumber(debugLevel)\n end\n self:logging(3,\"Interval: \" ..Interval)\nend\n\n\nfunction QuickApp:setupChildDevices() -- Setup Child Devices\n local cdevs = api.get(\"/devices?parentId=\"..self.id) or {} -- Pick up all Child Devices\n function self:initChildDevices() end -- Null function, else Fibaro calls it after onInit()...\n\n if #cdevs == 0 then -- If no Child Devices, create them\n local initChildData = { \n {className=\"waterquantity\", name=\"Water Total\", type=\"com.fibaro.multilevelSensor\", value=0}, \n }\n for _,c in ipairs(initChildData) do\n local child = self:createChildDevice(\n {name = c.name,\n type=c.type,\n value=c.value,\n unit=c.unit,\n initialInterfaces = {},\n },\n _G[c.className] -- Fetch class constructor from class name\n )\n child:setVariable(\"className\",c.className) -- Save class name so we know when we load it next time\n end \n else \n for _,child in ipairs(cdevs) do\n local className = getChildVariable(child,\"className\") -- Fetch child class name\n local childObject = _G[className](child) -- Create child object from the constructor name\n self.childDevices[child.id]=childObject\n childObject.parent = self -- Setup parent link to device controller \n end\n end\nend\n\n\nfunction QuickApp:onInit()\n __TAG = fibaro.getName(plugin.mainDeviceId) ..\" ID:\" ..plugin.mainDeviceId\n self.http = net.HTTPClient({timeout=3000})\n self:debug(\"onInit\")\n self:setupChildDevices() -- Setup the Child Devices \n \n if not api.get(\"/devices/\"..self.id).enabled then\n self:warning(\"Device\", fibaro.getName(plugin.mainDeviceId), \"is disabled\")\n return\n end\n \n self:getQuickappVariables() -- Get Quickapp Variables or create them\n self:createVariables() -- Create Variables\n \n if tonumber(debugLevel) >= 4 then \n self:simData() -- Go in simulation\n else\n self:getData() -- Get data\n end\n \nend\n\n-- EOF "}]}