Projekt

Obecné

Profil

Stáhnout (11.1 KB) Statistiky
| Větev: | Revize:
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Zdenda
5
 * Date: 20.4.2018
6
 * Time: 20:09
7
 */
8

    
9
namespace App\Http\Controllers;
10

    
11
use App\Model\Device;
12
use App\Model\Zarizeni;
13
use App\Model\Zaznam;
14
use Illuminate\Http\Request;
15
use phpDocumentor\Reflection\Types\Array_;
16
use phpDocumentor\Reflection\Types\Mixed_;
17

    
18
class DeviceController extends Controller
19
{
20
    // nazvy moznych url parametru
21
    const ADDRESS_PARAM = 'address';
22
    const SHOW_DIRECTION_PARAM = 'showDirection';
23
    const DATE_FROM_PARAM = 'dateFrom';
24
    const DATE_TO_PARAM = 'dateTo';
25
    const TIME_FROM_PARAM = 'timeFrom';
26
    const TIME_TO_PARAM = 'timeTo';
27
    const DIRECTION_PARAM = 'direction';
28

    
29

    
30
    public function getDevice(Request $request)
31
    {
32
        $address = null;
33
        $showDirection = 0;
34
        if ($request->has(self::ADDRESS_PARAM)) {
35
            $address = $request->input(self::ADDRESS_PARAM);
36
        }
37

    
38
        if ($request->has(self::SHOW_DIRECTION_PARAM) && $request->input(self::SHOW_DIRECTION_PARAM) == '1') {
39
            $showDirection = 1;
40
        }
41

    
42
        $device = Zarizeni::findByAddressJoinAddress($address, $showDirection);
43

    
44
        return $device;
45
    }
46

    
47
    /**
48
     * Vrati zarizeni podle id.
49
     * Url parametry:
50
     * dateFrom
51
     * dateTo
52
     * timeFrom
53
     * timeTo
54
     * direction
55
     *
56
     * @param Request $request
57
     * @param $id
58
     * @return \Symfony\Component\HttpFoundation\Response
59
     */
60
    public function getDeviceByIdWithTraffic(Request $request, $id)
61
    {
62
        $device = $this->getDeviceById($id);
63
        if ($device != null) {
64
            $device->traffics = $this->findTrafficByDevice($request, $id);
65
            return json_encode($device);
66
        } else {
67
            return response('Not found.', 404);
68
        }
69
    }
70

    
71
    /**
72
     * Vrati zarizeni podle id, nebo null, pokud neni nalezeno.
73
     *
74
     * @param $id
75
     * @return Zarizeni
76
     */
77
    public function getDeviceById($id)
78
    {
79
        return Zarizeni::findByIdJoinAddress($id);
80
    }
81

    
82
    /**
83
     * Vrati zaznamy o doprave pro dane zarizeni. Request obsahuje dodatecne url parametry.
84
     *
85
     * @param Request $request
86
     * @param $deviceId
87
     */
88
    public function findTrafficByDevice(Request $request, $deviceId)
89
    {
90
        // nacti parametry
91
        $params = $this->loadDateTimeDirectionConstraints($request);
92
        $dateFrom = $params[self::DATE_FROM_PARAM];
93
        $dateTo = $params[self::DATE_TO_PARAM];
94
        $timeFrom = $params[self::TIME_FROM_PARAM];
95
        $timeTo = $params[self::TIME_TO_PARAM];
96
        $direction = $params[self::DIRECTION_PARAM];
97

    
98
        return Zaznam::findByDevice($deviceId, $dateFrom, $dateTo, $timeFrom, $timeTo, $direction);
99
    }
100

    
101
    public function findDayAverageTrafficByDevice(Request $request, $deviceId) {
102
        // nacti parametry
103
        $params = $this->loadDateTimeDirectionConstraints($request);
104
        $dateFrom = $params[self::DATE_FROM_PARAM];
105
        $dateTo = $params[self::DATE_TO_PARAM];
106
        $direction = $params[self::DIRECTION_PARAM];
107

    
108
        return Zaznam::averageByDay($deviceId, $dateFrom, $dateTo, $direction);
109
    }
110

    
111
    /**
112
     * Nacte zarizeni spolecne se vsemi jeho zaznamy (podle url parametru) a vrati je jako stahnutelny csv soubor.
113
     * Csv bude obsahovat udaje o zarizeni na prvni radce, udaje o doprave na nasledujicich.
114
     *
115
     * Pokud zarizeni nebylo nalezeno, je vracena 404.
116
     *
117
     * @param Request $request
118
     * @param $id
119
     * @return Mixed_
120
     */
121
    public function getDeviceByIdAsCsv(Request $request, $id)
122
    {
123
        return $this->createCsvFileForDeviceData(
124
            $this->findDeviceByIdSetDates($request, $id),
125
            $this->findTrafficByDevice($request, $id),
126
            'doprava-export-'
127
        );
128
    }
129

    
130
    public function getTrafficAverageByDevice(Request $request, $id)
131
    {
132
        $device = $this->findDeviceByIdSetDates($request, $id);
133
        if ($device != null) {
134

    
135
            $device->traffics = $this->findTrafficAverageByDevice($request, $id);
136
            return json_encode($device);
137
        } else {
138
            return response('Not found.', 404);
139
        }
140
    }
141

    
142
    /**
143
     * Vrati prumerna data o doprave pro dane zarizeni jako stahnutelny csv soubor.
144
     *
145
     * @param Request $request
146
     * @param integer $id Id zarizeni.
147
     * @return Mixed_
148
     */
149
    public function getTrafficAverageByDeviceCsv(Request $request, $id) {
150
        return $this->createCsvFileForDeviceData(
151
            $this->findDeviceByIdSetDates($request, $id),
152
            $this->findDayAverageTrafficByDevice($request, $id),
153
            'doprava-prumery-export-'
154
            );
155
    }
156

    
157
    /**
158
     * Vrati prumerna data o doprave (pouze data o doprave) pro dane zarizeni.
159
     *
160
     * @param Request $request Request s parametry.
161
     * @param $deviceId Id zarizeni.
162
     * @return Mixed_
163
     */
164
    public function findTrafficAverageByDevice(Request $request, $deviceId) {
165
        // nacti parametry
166
        $params = $this->loadDateTimeDirectionConstraints($request);
167
        $dateFrom = $params[self::DATE_FROM_PARAM];
168
        $dateTo = $params[self::DATE_TO_PARAM];
169
        $timeFrom = $params[self::TIME_FROM_PARAM];
170
        $timeTo = $params[self::TIME_TO_PARAM];
171
        $direction = $params[self::DIRECTION_PARAM];
172

    
173
        return Zaznam::averageByDevice($deviceId, $dateFrom, $dateTo, $timeFrom, $timeTo, $direction);
174
    }
175

    
176
    /**
177
     * Vrati denni prumery pro jednotlive typy vozidel.
178
     *
179
     * @param Request $request Request s parametry.
180
     * @param $id Id zarizeni.
181
     * @return Mixed_
182
     */
183
    public function getTrafficDayAverage(Request $request, $id)
184
    {
185
        $device = $this->findDeviceByIdSetDates($request, $id);
186

    
187
        if ($device != null) {
188
            $device->traffics = $this->findDayAverageTrafficByDevice($request, $id);
189
            return json_encode($device);
190
        } else {
191
            return response('Not found.', 404);
192
        }
193
    }
194

    
195

    
196
    /**
197
     * Vrati denni prumery pro jendotlive typy vozidel jako stahnutelny csv soubor.
198
     *
199
     * @param Request $request Request.
200
     * @param $id Id zarizeni.
201
     * @return Mixed_
202
     */
203
    public function getTrafficDayAverageCsv(Request $request, $id)
204
    {
205
        return $this->createCsvFileForDeviceData(
206
            $this->findDeviceByIdSetDates($request, $id),
207
            $this->findDayAverageTrafficByDevice($request, $id),
208
            'doprava-denni-prumery-export-'
209
        );
210
    }
211

    
212
    public function getAll()
213
    {
214
        return Zarizeni::getAllJoinAddress();
215
    }
216

    
217
    public function lastDay()
218
    {
219
        return Zaznam::lastInsertedDate();
220
    }
221

    
222
    public function headerTest(Request $request)
223
    {
224
        $authHeader = $request->header("jwt");
225

    
226
        if ($authHeader != null) {
227
            return $authHeader;
228
        } else {
229
            return $request->header("jwt");
230
        }
231
    }
232

    
233
    /**
234
     * Funkce nacte z requestu url parametry dateFrom, dateTo, timeFrom, timeTo, direciton a vrati je jako pole.
235
     * Defaultni hodnoty jsou null.
236
     *
237
     * @param Request $request Request ze ktere budou nacitany parametry.
238
     * @return array Pole s nactenymi parametry.
239
     */
240
    private function loadDateTimeDirectionConstraints(Request $request)
241
    {
242
        $params = array();
243
        $params[self::DATE_FROM_PARAM] = null;
244
        $params[self::DATE_TO_PARAM] = null;
245
        $params[self::TIME_FROM_PARAM] = null;
246
        $params[self::TIME_TO_PARAM] = null;
247
        $params[self::DIRECTION_PARAM] = null;
248

    
249
        if ($request->has(self::DATE_FROM_PARAM)) {
250
            $params[self::DATE_FROM_PARAM] = $request->input(self::DATE_FROM_PARAM);
251
        }
252
        if ($request->has(self::DATE_TO_PARAM)) {
253
            $params[self::DATE_TO_PARAM] = $request->input(self::DATE_TO_PARAM);
254
        }
255
        if ($request->has(self::TIME_FROM_PARAM)) {
256
            $params[self::TIME_FROM_PARAM] = $request->input(self::TIME_FROM_PARAM);
257
        }
258
        if ($request->has(self::TIME_TO_PARAM)) {
259
            $params[self::TIME_TO_PARAM] = $request->input(self::TIME_TO_PARAM);
260
        }
261
        if ($request->has(self::DIRECTION_PARAM)) {
262
            $params[self::DIRECTION_PARAM] = $request->input(self::DIRECTION_PARAM);
263
        }
264

    
265
        return $params;
266
    }
267

    
268
    /**
269
     * Konvertuje objekt typu stdClass na pole pomoci funkci json_decode(), json_encode().
270
     *
271
     * @param $object
272
     * @return Array_
273
     */
274
    private function stdClassToArray($object)
275
    {
276
        return json_decode(json_encode($object), true);
277
    }
278

    
279
    /**
280
     * Prida hlavicku pole a data z pole do csv souboru.
281
     *
282
     * @param $df Ukazatel na soubor.
283
     * @param $array Pole.
284
     */
285
    private function addArrayToCsvFile($df, $array) {
286
        if ($array != null && count($array) > 0) {
287
            $row = $this->stdClassToArray($array[0]);
288
            fputcsv($df, array_keys($row));
289

    
290
            foreach ($array as $row) {
291
                fputcsv($df, $row);
292
            }
293
        }
294
    }
295

    
296
    /**
297
     * Funkce se pokusi nalezt zarizeni (s adresou) podle id a pokud najde, pokusi se z requestu dostat hodnoty parametru DATE_FROM,
298
     * DATE_TO a DIRECTION, ktere pak prida do nalezeneho zarizeni, ktere vrati. Pokud request uvedene parametry neobsahuje, budou
299
     * v zarizeni nastaveny na null.
300
     *
301
     * Pokud zarizeni nenajde, vrati null.
302
     *
303
     * @param Request $request Request s parametry.
304
     * @param integer $id id zarizeni.
305
     * @return Mixed_
306
     */
307
    private function findDeviceByIdSetDates(Request $request, $id) {
308
        // nacteni parametru
309
        $params = $this->loadDateTimeDirectionConstraints($request);
310
        $dateFrom = $params[self::DATE_FROM_PARAM];
311
        $dateTo = $params[self::DATE_TO_PARAM];
312
        $direction = $params[self::DIRECTION_PARAM];
313

    
314
        $device = Zarizeni::findByIdJoinAddress($id);
315
        if ($device != null) {
316
            $device->dateFrom = $dateFrom;
317
            $device->dateTo = $dateTo;
318

    
319
            if ($direction != null) {
320
                $device->direction = intval($direction);
321
            }
322
        }
323

    
324
        return $device;
325
    }
326

    
327
    /**
328
     * Vytvoří csv soubor pro zarizeni a souvisejici data o doprave a vynuti jeho stazeni.
329
     *
330
     * @param Mixed_ $device Metadata zarizeni (tj. zarizeni bez dat o doprave). Pokud null, je vracena response 404.
331
     * @param Mixed_ $traffic Data o doprave.
332
     * @param string $namePrefix Prefix jmena stahovaneho souboru.
333
     * @return Mixed_
334
     */
335
    private function createCsvFileForDeviceData($device, $traffic, $namePrefix) {
336

    
337
        if ($device != null) {
338
            $devArray = json_decode(json_encode($device), true);
339
            // tmp file
340
            $tmpFileName = $namePrefix . time() . '.csv';
341
            $tmpFilePath = tempnam(sys_get_temp_dir(), $tmpFileName);
342
            $df = fopen($tmpFilePath, 'w');
343

    
344
            // hlavicka pro zarizeni
345
            fputcsv($df, array_keys($devArray));
346
            fputcsv($df, $devArray);
347

    
348
            // zaznamy o doprave
349
            $traffic = $this->stdClassToArray($traffic);
350
            $this->addArrayToCsvFile($df, $traffic);
351
            fclose($df);
352
            return response()->download($tmpFilePath, $tmpFileName, array())->deleteFileAfterSend(true);
353
        } else {
354
            return response('Not found.', 404);
355
        }
356
    }
357
}
(2-2/6)