{"id":387,"date":"2015-07-09T21:54:45","date_gmt":"2015-07-09T20:54:45","guid":{"rendered":"http:\/\/tricker.cz\/?p=387"},"modified":"2016-01-22T09:42:28","modified_gmt":"2016-01-22T08:42:28","slug":"adding-missed-measurements-from-th2e-log-csv-file-into-tmep-database","status":"publish","type":"post","link":"https:\/\/tricker.cz\/?p=387","title":{"rendered":"Adding missed measurements from TH2E LOG csv file into TMEP database"},"content":{"rendered":"<pre class=\"toolbar:1 lang:php decode:true   \" title=\"TH2Eimport.php\">&lt;?php\r\n\r\n  \/*\r\n     Simple way how to insert missing data into TMEP database from TH2E LOG\r\n\r\n     Copy this script to root folder of TMEP application along with \"data.csv\" (from TH2E),\r\n     then open it in browser (http:\/\/linktoapp.com\/TH2Eimport.php)\r\n\r\n     Repeat as needed :-)\r\n\r\n     Content of data.csv should look like this:\r\n     tmp;     31.70;06\/13\/2015 19:24:00\r\n     hum;     43.00;06\/13\/2015 19:24:00\r\n   *\/\r\n\r\n  \/\/ Connect to DB\r\n  require \".\/config.php\";\r\n  require \".\/scripts\/db.php\";\r\n\r\n  \/\/ Get data for import\r\n  $data = file_get_contents(\"data.csv\");\r\n\r\n  \/\/ Process data\r\n  $toImport = Array();\r\n  $lines = explode(\"\\n\", $data);\r\n\r\n  foreach($lines as $line)\r\n  {\r\n\r\n    $line = trim($line);\r\n    $values = explode(\";\", $line);\r\n\r\n    \/\/ Looks like we have something to process?\r\n    if(count($values) == 3)\r\n    {\r\n\r\n      \/\/ Trim whitespaces\r\n      $values[0] = trim($values[0]);\r\n      $values[1] = trim($values[1]);\r\n      $values[2] = trim($values[2]);\r\n\r\n      \/\/ Check some simple conditions\r\n      if(($values[0] == \"tmp\" OR $values[0] == \"hum\")\r\n        AND is_numeric($values[1])\r\n        AND strtotime($values[2]) !== false)\r\n      {\r\n\r\n        $date = date(\"Y-m-d H:i:s\", strtotime($values[2]));\r\n        $toImport[$date][$values[0]] = round($values[1], 1);\r\n\r\n      }\r\n\r\n    }\r\n\r\n  }\r\n\r\n  \/\/ Go through gathered data\r\n  foreach($toImport as $date =&gt; $values)\r\n  {\r\n\r\n    \/\/ We need at least temperature\r\n    if(is_numeric($values[\"tmp\"]))\r\n    {\r\n\r\n      $qExists = mysqli_query($GLOBALS[\"DBC\"], \"SELECT id\r\n                                                FROM tme\r\n                                                WHERE kdy &gt;= CAST('\".substr($date, 0, 17).\"00' AS datetime)\r\n                                                  AND kdy &lt;= CAST('\".substr($date, 0, 17).\"59' AS datetime)\");\r\n\r\n      if(mysqli_num_rows($qExists) &gt; 0)\r\n      {\r\n        echo \"&lt;span style='color: darkgreen;'&gt;{$date} - already have&lt;\/span&gt;&lt;br&gt;\";\r\n      }\r\n      else\r\n      {\r\n\r\n        echo \"&lt;span style='color: darkred;'&gt;{$date} - missed, adding&lt;\/span&gt;&lt;br&gt;\";\r\n\r\n        \/\/ Add\r\n        mysqli_query($GLOBALS[\"DBC\"], \"INSERT INTO tme(kdy, teplota, vlhkost)\r\n                                       VALUES('{$date}', '{$values[\"tmp\"]}', '{$values[\"hum\"]}');\");\r\n\r\n        echo mysqli_error($GLOBALS[\"DBC\"]);\r\n\r\n      }\r\n\r\n    }\r\n\r\n  }\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php \/* Simple way how to insert missing data into TMEP database from TH2E LOG Copy this script to root folder of TMEP application along with &#8222;data.csv&#8220; (from TH2E), then open it in browser (http:\/\/linktoapp.com\/TH2Eimport.php) Repeat as needed \ud83d\ude42 Content&#8230; <a class=\"more-link\" href=\"https:\/\/tricker.cz\/?p=387\">Pokra\u010dovat ve \u010dten\u00ed &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/posts\/387"}],"collection":[{"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tricker.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=387"}],"version-history":[{"count":1,"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/posts\/387\/revisions"}],"predecessor-version":[{"id":388,"href":"https:\/\/tricker.cz\/index.php?rest_route=\/wp\/v2\/posts\/387\/revisions\/388"}],"wp:attachment":[{"href":"https:\/\/tricker.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tricker.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tricker.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}