{"name":"Меркурий","type":"virtual_device","properties":{"deviceIcon":1025,"currentIcon":"1025","log":"","logTemp":"","mainLoop":"local value, json_table;\nlocal thisDevice = fibaro:getSelfId();\nlocal deviceIP = fibaro:getValue(thisDevice, 'IPAddress');\nlocal devicePort = fibaro:getValue(thisDevice, 'TCPPort');\nlocal passHash = \"21232f297a57a5a743894a0e4a801fc3\"\n\nlocal data_from_parse = {};\nlocal data_to_parse = {};\nlocal data_delta = {};\nlocal month = {\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"};\n\n\n\nfunction hasbit(x, p)\n return x % (p + p) >= p;\nend\n\nfunction BitAND(a,b)\n\tlocal p,c=1,0\n while a>0 and b>0 do\n local ra,rb=a%2,b%2\n if ra+rb>1 then c=c+p end\n a,b,p=(a-ra)/2,(b-rb)/2,p*2\n end\n return c\nend\n\n--\n-- Message function\n--\nfunction logbug(color, message)\n\tif color and color ~= \"\" then\n\t\tfibaro:debug(''..(message or '')..'')\n\telse\n\t\tfibaro:debug(message or '')\n\tend\nend\n\n\nfunction data_request(inputType) \n\tlogbug(\"lightgreen\", \"Request to \" .. inputType);\n\turl = \"/get/m2m/inputs?type=\"..inputType..\"&number=All\"..\"&hash=\"..passHash\n\tHC2 = Net.FHttp(deviceIP, devicePort);\n\trequest = HC2:GET(url);\n\treturn request; \nend\n\n\nfunction mercury_read()\n\tlocal jsonData = nil\n\tresponse = data_request(\"specific\");\n\tif response ~= nil and response ~=\"\" then\n \tlogbug(\"gray\", response);\n \tjson_table = json.decode(response);\n\t\t\n\t\tif (type(tonumber(json_table.inputs.data[1].phaseQnty)) == 'number') then\n\t\t\tif (tonumber(json_table.inputs.data[1].phaseQnty) == 3) then\n\t\t\t\tshowlabel(thisDevice, 1, \"text\", \"Меркурий, 3ф\")\n\t\t\tend\n\t\tend\n\t\tif (type(tonumber(json_table.inputs.data[1].connect)) == 'number') then\n\t\t\tif (tonumber(json_table.inputs.data[1].connect) == 1) then\n\t\t\t\tshowlabel(thisDevice, 2, \"text\", \"Подключен\")\n\n\t\t\t\tif (type(tonumber(json_table.inputs.data[1].sn)) == 'number' and type(tonumber(json_table.inputs.data[1].addr)) == 'number') then\n\t\t\t\t\tshowlabel(thisDevice, 3, \"text\", tonumber(json_table.inputs.data[1].addr) .. \" / \" .. tonumber(json_table.inputs.data[1].sn))\n\t\t\t\tend\n\t\t\n\t\t\t\tif (type(tonumber(json_table.inputs.data[1].values[\"F\"])) == 'number') then\n\t\t\t\t\tshowlabel(thisDevice, 4, \"text\", string.format(\"%.2f\", tonumber(json_table.inputs.data[1].values[\"F\"])))\n\t\t\t\tend\n\t\t\n\n\t\t\t\tshowlabel(thisDevice, 5, \"text\", json_table.inputs.data[1].values[\"U\"])\n\t\t\t\tshowlabel(thisDevice, 6, \"text\", json_table.inputs.data[1].values[\"I\"])\n\t\t\t\tshowlabel(thisDevice, 7, \"text\", json_table.inputs.data[1].values[\"W\"])\n\t\t\n\t\t\t\tshowlabel(thisDevice, 8, \"text\", string.format(\"%.1f\", tonumber(json_table.inputs.data[1].values[\"energy\"][\"T1\"])) .. \"/\" .. string.format(\"%.1f\", tonumber(json_table.inputs.data[1].values[\"energy\"][\"T2\"])) )\n\t\t\t\tshowlabel(thisDevice, 9, \"text\", string.format(\"%.1f\", tonumber(json_table.inputs.data[1].values[\"energy\"][\"sum\"])))\n\t\t\n\n\t\t\t\tcurrent_month = tonumber(os.date(\"%m\",os.time()));\n\t\t\t\tcurrent_energy_to = json_table.inputs.data[1].values[\"energy\"][\"T1\"] .. \"/\" .. json_table.inputs.data[1].values[\"energy\"][\"T2\"] .. \"/\" .. json_table.inputs.data[1].values[\"energy\"][\"T3\"] .. \"/\" .. json_table.inputs.data[1].values[\"energy\"][\"T4\"] .. \"/\" .. json_table.inputs.data[1].values[\"energy\"][\"sum\"];\n\t\t\t\t\n\t\t\t\tshowlabel(thisDevice, 10, \"text\", month[current_month] .. \": \" .. dataHandler(json_table.inputs.data[1].values[\"energyBeginMonth\"][month[current_month]], current_energy_to))\n\t\t\n\t\t\t\tfor n = 1, 12 do\n\t\t\t\t\tif ( n ~= 12) then showlabel(thisDevice, 10+n, \"text\", dataHandler(json_table.inputs.data[1].values[\"energyBeginMonth\"][month[n]], json_table.inputs.data[1].values[\"energyBeginMonth\"][month[n+1]]))\n\t\t\t\t\telse showlabel(thisDevice, 10+n, \"text\", dataHandler(json_table.inputs.data[1].values[\"energyBeginMonth\"][month[n]], json_table.inputs.data[1].values[\"energyBeginMonth\"][month[1]]))\n\t\t\t\t\tend\n\t\t\t\tend\n\t\t\t\t\n\t\t\t\t\n\t\t\t\t\n\t\t\telse\n\t\t\t\tshowlabel(thisDevice, 2, \"text\", \"Не подключен\")\n\t\t\t\tfor n = 3, 22 do showlabel(thisDevice, n, \"text\", \"-\") end\n\t\t\tend\n\t\tend\n else\n\t\tfor n = 1, 22 do showlabel(thisDevice, n, \"text\", \"-\") end\n\tend\n\t\nend\n\nfunction dataHandler(data_from, data_to)\n \n\tlocal semicolonPointer;\n\t\n\tdata = data_from;\n\tdata = data .. \"/\";\n\tfor i = 1, 5 do\n\t\tsemicolonPointer = string.find(data, '/');\n\t\tdata_from_parse[i] = tonumber(string.sub(data,1, semicolonPointer-1));\n\t\tdata = string.sub(data,semicolonPointer+1, #data);\n\tend\n\t\n\tdata = data_to;\n\tdata = data .. \"/\";\n\tfor i = 1, 5 do\n\t\tsemicolonPointer = string.find(data, '/');\n\t\tdata_to_parse[i] = tonumber(string.sub(data,1, semicolonPointer-1));\n\t\tdata = string.sub(data,semicolonPointer+1, #data);\n\t\t\n\t\tif (data_to_parse[i] >= data_from_parse[i]) then data_delta[i] = data_to_parse[i] - data_from_parse[i];\n\t\telse data_delta[i] = 0;\n\t\tend\n\tend\n\treturn string.format(\"%.1f\", data_delta[1])..\"/\" .. string.format(\"%.1f\", data_delta[2]) .. \"/\" .. string.format(\"%.1f\", data_delta[5]);\nend\n\n\nfunction showlabel(id_vd, label, type, value)\n \tif (type == \"text\") then fibaro:call(id_vd, \"setProperty\", \"ui.Label\" .. label .. \".value\", value);\n elseif (type == \"temp\") then fibaro:call(id_vd, \"setProperty\", \"ui.Label\" .. label .. \".value\", string.format(\"%.2f\", value) .. \" °C\");\n elseif (type == \"time\") then\n \tif (math.floor(value/60) < 10) then hour = \"0\".. math.floor(value/60); end\n \tif (value%60 < 10) then min = \"0\".. value%60; end\n \tfibaro:call(id_vd, \"setProperty\", \"ui.Label\" .. label .. \".value\", hour .. \":\" .. min);\n end\nend\n\nmercury_read();\nfibaro:sleep(5000);","ui.Label1.value":"Меркурий, 3ф","ui.Label10.value":"May: 22.8/0.0/22.8","ui.Label11.value":"0.0/0.0/0.0","ui.Label12.value":"0.0/0.0/0.0","ui.Label13.value":"0.0/0.0/0.0","ui.Label14.value":"19.9/0.0/19.9","ui.Label15.value":"0.0/0.0/0.0","ui.Label16.value":"779.3/0.0/779.3","ui.Label17.value":"795.5/0.0/795.5","ui.Label18.value":"773.7/0.0/773.7","ui.Label19.value":"871.0/0.0/871.0","ui.Label2.value":"Подключен","ui.Label20.value":"1159.4/0.0/1159.4","ui.Label21.value":"976.2/0.0/976.2","ui.Label22.value":"0.0/0.0/0.0","ui.Label3.value":"65 / 33272665","ui.Label4.value":"49.99","ui.Label5.value":"237.33/237.76/234.40","ui.Label6.value":"0.06/0.00/0.13","ui.Label7.value":"8.57/0.00/29.47/38.04","ui.Label8.value":"9884.8/76.4","ui.Label9.value":"9961.3","visible":"true","rows":[{"type":"label","elements":[{"id":1,"lua":false,"waitForResponse":false,"caption":"Тип счетчика","name":"Label1","favourite":false,"main":false}]},{"type":"label","elements":[{"id":2,"lua":false,"waitForResponse":false,"caption":"Статус","name":"Label2","favourite":false,"main":false}]},{"type":"label","elements":[{"id":3,"lua":false,"waitForResponse":false,"caption":"Адрес/SN","name":"Label3","favourite":false,"main":false}]},{"type":"label","elements":[{"id":4,"lua":false,"waitForResponse":false,"caption":"Частота сети","name":"Label4","favourite":false,"main":false}]},{"type":"label","elements":[{"id":5,"lua":false,"waitForResponse":false,"caption":"U, В","name":"Label5","favourite":false,"main":false}]},{"type":"label","elements":[{"id":6,"lua":false,"waitForResponse":false,"caption":"I, А","name":"Label6","favourite":false,"main":false}]},{"type":"label","elements":[{"id":7,"lua":false,"waitForResponse":false,"caption":"W, Вт","name":"Label7","favourite":false,"main":false}]},{"type":"label","elements":[{"id":8,"lua":false,"waitForResponse":false,"caption":"Энергия Д/Н","name":"Label8","favourite":false,"main":false}]},{"type":"label","elements":[{"id":9,"lua":false,"waitForResponse":false,"caption":"Энергия общ.","name":"Label9","favourite":false,"main":false}]},{"type":"label","elements":[{"id":10,"lua":false,"waitForResponse":false,"caption":"Этот месяц","name":"Label10","favourite":false,"main":true}]},{"type":"label","elements":[{"id":11,"lua":false,"waitForResponse":false,"caption":"Январь","name":"Label11","favourite":false,"main":false}]},{"type":"label","elements":[{"id":12,"lua":false,"waitForResponse":false,"caption":"Февраль","name":"Label12","favourite":false,"main":false}]},{"type":"label","elements":[{"id":13,"lua":false,"waitForResponse":false,"caption":"Март","name":"Label13","favourite":false,"main":false}]},{"type":"label","elements":[{"id":14,"lua":false,"waitForResponse":false,"caption":"Апрель","name":"Label14","favourite":false,"main":false}]},{"type":"label","elements":[{"id":15,"lua":false,"waitForResponse":false,"caption":"Май","name":"Label15","favourite":false,"main":false}]},{"type":"label","elements":[{"id":16,"lua":false,"waitForResponse":false,"caption":"Июнь","name":"Label16","favourite":false,"main":false}]},{"type":"label","elements":[{"id":17,"lua":false,"waitForResponse":false,"caption":"Июль","name":"Label17","favourite":false,"main":false}]},{"type":"label","elements":[{"id":18,"lua":false,"waitForResponse":false,"caption":"Август","name":"Label18","favourite":false,"main":false}]},{"type":"label","elements":[{"id":19,"lua":false,"waitForResponse":false,"caption":"Сентябрь","name":"Label19","favourite":false,"main":false}]},{"type":"label","elements":[{"id":20,"lua":false,"waitForResponse":false,"caption":"Октябрь","name":"Label20","favourite":false,"main":false}]},{"type":"label","elements":[{"id":21,"lua":false,"waitForResponse":false,"caption":"Ноябрь","name":"Label21","favourite":false,"main":false}]},{"type":"label","elements":[{"id":22,"lua":false,"waitForResponse":false,"caption":"Декабрь","name":"Label22","favourite":false,"main":false}]}]},"actions":{"pressButton":1,"setSlider":2,"setProperty":2}}