commit a257bb078ff090aa8bdc3d45b78748c981e833e7
Author: horkym <37276521+horkym@users.noreply.github.com>
Date:   Tue Apr 17 20:48:42 2018 +0200

    #6350: Parsovani dat
    
    Pridano nacteni dat z extrahovanych souboru a jejich ulozeni do vhodne struktury. K tomuto ucelu pridana trida pro zaznam o doprave a trida pro zaznam o lokaci.

diff --git a/cron/location.php b/cron/location.php
new file mode 100644
index 0000000..7e20202
--- /dev/null
+++ b/cron/location.php
@@ -0,0 +1,24 @@
+<?php
+
+class Location {
+
+    // Pouzivane atributy.
+    public $name;
+    public $town;
+    public $street;
+    public $device;
+    
+    // Nepouzivane.
+    // public $area;
+    
+    public function __construct($data) {
+        $this->name = $data[0];
+        $this->town = $data[1];
+        $this->street = $data[2];
+        $this->device = substr($data[3], 2);
+        // $this->area = $data[4];
+    }
+    
+}
+
+?>
diff --git a/cron/parser.php b/cron/parser.php
index 4128059..eb4dac6 100644
--- a/cron/parser.php
+++ b/cron/parser.php
@@ -1,13 +1,38 @@
 <?php
 
+require_once "traffic.php";
+require_once "location.php";
+
 class Parser {
     
+    // Pro data o doprave - na kolik intervalu se ma rozdelit den (96 = 4 * 24 => intervaly po ctvrt hodine).
+    private $HOW_MANY_INTERVALS = 96;
+    
+    // Pro data o doprave - jak dlouho v milisekundach trva jeden interval.
+    private $intervalMilli;
+    
     private $name;
     private $path;
     
+    // Pole informaci o lokacich.
+    private $locations;
+    
+    // Pole informaci o doprave - peti-rozmerne pole s nasledujici strukturou:
+    // Zaklad tvori pole zarizeni. Pro kazde zarizeni je pak pole pro casove intervaly.
+    // Kazdy casovy interval ma pole o dvou prvcich (dva smery). Pro kazdy smer je vytvoreno pole o jedenacti
+    // prvcich (jedenact typu vozidel). Pro kazde vozidlo je vytvoreno pole o dvou prvcich (pocet vozidel, suma rychlosti).
+    private $traffic;
+    
     public function __construct() {
         $this->name = "DOPR_D_";
         $this->path = "http://doprava.plzensky-kraj.cz/opendata/doprava/den/".$this->name;
+        
+        // Pro lokace se data predzpracovavat nemusi.
+        $this->locations = array();
+        
+        // Naopak u zaznamu je prilis zbytecnych informaci - k predzpracovani dojit musi.
+        $this->intervalMilli = (int) (24 * 3600000 / $this->HOW_MANY_INTERVALS);
+        $this->traffic = array();
     }
     
     public function doWork($date) {
@@ -25,7 +50,8 @@ class Parser {
             }
             
             if ($ok == 0 || $result == 1) {
-                // TODO zpracovani souboru.
+                $this->parse($dir.$this->name.$date.".csv", TRUE);
+                $this->parse($dir."Locations.csv", FALSE);
                 // return; odkomentovat v pripade, ze extrahovana data nemaji byt odstranena.
             }
             
@@ -34,6 +60,49 @@ class Parser {
         }
     }
     
+    private function parse($fileName, $traffic) {
+        if (($file = fopen($fileName, "r"))) {
+            while (($row = fgetcsv($file, 1000, "|"))) {
+                if ($traffic) {
+                    $this->saveVehicleInfo(new Traffic($row));
+                } else {
+                    $this->locations[] = new Location($row);
+                }
+            }
+            fclose($file);
+        }
+    }
+    
+    private function saveVehicleInfo($t) {
+        // Kontrola, jestli je pro dane zarizeni vytvorene pole casu.
+        if (!isSet($this->traffic[$t->device])) {
+            $this->traffic[$t->device] = array();
+            for ($i = 0; $i < $this->HOW_MANY_INTERVALS; $i++) {
+                $this->traffic[$t->device][$i] = NULL;
+            }
+        }
+        
+        // Zjisteni, do jakeho casoveho intervalu patri zaznam.
+        list($date, $time) = explode(" ", $t->dateTime->format("Y-m-d H:i:s.u"), 2);
+        list($hours, $minutes, $seconds) = explode(":", $time, 3);
+        $interval = (int) (($hours * 3600000 + $minutes * 60000 + $seconds * 1000) / $this->intervalMilli);
+        
+        // Kontrola, jestli je pro dany casovy interval vytvorene pole pro smery.
+        if ($this->traffic[$t->device][$interval] == NULL) {
+            $this->traffic[$t->device][$interval] = array();
+            for ($i = 0; $i < 2; $i++) {
+                $this->traffic[$t->device][$interval][$i] = array();
+                for ($j = 0; $j < 11; $j++) {
+                    $this->traffic[$t->device][$interval][$i][$j] = array(0, 0); // Pocet danych vozidel, suma jejich rychlosti.
+                }
+            }
+        }
+        
+        // Ulozeni dulezitych informaci o danem zaznamu.
+        $this->traffic[$t->device][$interval][$t->direction][$t->type10][0]++;
+        $this->traffic[$t->device][$interval][$t->direction][$t->type10][1] += $t->speed;
+    }
+    
     private function download($date, $zipUrl, $dir, $downloaded) {
         if (strpos(get_headers($zipUrl, 1)[0], "404") === FALSE) {
             if (!file_exists($dir)) {
@@ -90,6 +159,14 @@ class Parser {
         }
     }
     
+    public function getTraffic() {
+        return $this->traffic;
+    }
+    
+    public function getLocations() {
+        return $this->locations;
+    }
+    
 }
 
 ?>
\ No newline at end of file
diff --git a/cron/traffic.php b/cron/traffic.php
new file mode 100644
index 0000000..e64810b
--- /dev/null
+++ b/cron/traffic.php
@@ -0,0 +1,40 @@
+<?php
+
+class Traffic {
+    
+    // Pouzivane atributy.
+    public $device;
+    public $direction;
+    public $dateTime;
+    public $speed;
+    public $type10;
+    
+    // TODO pouziti.
+    public $state;
+    
+    // Nepouzivane.
+    // public $intensity;
+    // public $intensityN;
+    // public $occupancy;
+    // public $type;
+    // public $duration;
+    // public $history;
+    
+    public function __construct($data) {
+        $this->device = substr($data[0], 2, 3);
+        $this->direction = substr($data[0], 7) - 1; // Misto hodnot 1, 2 - hodnoty 0, 1 (kvuli poli).
+        $this->dateTime = new DateTime($data[1]);
+        // $this->intensity = $data[2];
+        // $this->intensityN = $data[3];
+        // $this->occupancy = $data[4];
+        $this->speed = $data[5];
+        $this->state = $data[6];
+        // $this->type = $data[7];
+        // $this->duration = $data[8];
+        // $this->history = $data[9];
+        $this->type10 = $data[10];
+    }
+    
+}
+
+?>
