{"name":"PluaHelper","type":"com.fibaro.binarySwitch","apiVersion":"1.3","initialProperties":{"viewLayout":{"$jason":{"body":{"header":{"style":{"height":"0"},"title":"quickApp_device_4103"},"sections":{"items":[]}},"head":{"title":"quickApp_device_4103"}}},"uiView":[],"useUiView":true,"uiCallbacks":[],"quickAppVariables":[],"typeTemplateInitialized":true,"userDescription":""},"initialInterfaces":[],"files":[{"name":"main","isMain":true,"isOpen":true,"content":"local HELPER_UUID = \"plua-00-01\"\nlocal HELPER_VERSION = \"1.0.0\"\n\nfunction QuickApp:onInit()\n    self:debug(self.name,self.id)\n    self:updateProperty('quickAppUuid',HELPER_UUID)\n    self:updateProperty('model',HELPER_VERSION)\nend\n\nlocal connections = {}\n\nlocal function newConnection(url)\n    local ws = net.WebSocketClient()\n    \n    ws:addEventListener(\"connected\", function()\n      print(\"Connection from\",url)\n    end)\n    \n    ws:addEventListener(\"dataReceived\", function(data)\n      -- Only close after receiving echo message (not welcome message)\n      print(\"Receieved from\", url, #data)\n      if data == \"ping\" then return ws:send(\"pong\") end\n      local stat,req = pcall(json.decode,data)\n      if stat then\n        local res = {pcall(api[req.method],req.path,req.data)}\n        ws:send(json.encode(res))\n      end\n    end)\n    \n    ws:addEventListener(\"disconnected\", function()\n      print(\"Disconnected\",url)\n      connections[url] = nil\n    end)\n    \n    ws:addEventListener(\"error\", function(err)\n      print(\"Error, disconnected\", url, err)\n      connections[url] = nil\n    end)\n    \n    print(url,\":\",ws:connect(url))\n    return true\nend\n\nfunction QuickApp:connect(url)\n    if not connections[url] then\n       setTimeout(function()\n         connections[url] = newConnection(url)\n         end,0)\n    end\nend\n"}]}