Projekt

Obecné

Profil

Stáhnout (4.61 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

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

    
27

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

    
35
        if ($request->has(self::SHOW_DIRECTION_PARAM) && $request->input(self::SHOW_DIRECTION_PARAM) == '1') {
36
            $showDirection = 1;
37
        }
38

    
39
        $device = Zarizeni::findByAddressJoinAddress($address, $showDirection);
40
        if ($device == null || count($device) == 0) {
41
            return response('Not found.', 404);
42
        }
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 getDeviceById(Request $request, $id) {
61

    
62
        // nacti parametry
63
        $params = $this->loadDateTimeDirectionConstraints($request);
64
        $dateFrom = $params[self::DATE_FROM_PARAM];
65
        $dateTo = $params[self::DATE_TO_PARAM];
66
        $timeFrom = $params[self::TIME_FROM_PARAM];
67
        $timeTo = $params[self::TIME_TO_PARAM];
68
        $direction = $params[self::DIRECTION_PARAM];
69

    
70
        $device = Zarizeni::findByIdJoinAddress($id);
71
        if ($device != null) {
72
            $device[0]->traffic = Zaznam::findByDevice($id, $dateFrom, $dateTo, $timeFrom, $timeTo, $direction);
73
        } else if ($device == null || count($device) == 0) {
74
            return response('Not found.', 404);
75
        }
76

    
77
        return $device;
78
    }
79

    
80
    public function getTrafficAverageByDevice(Request $request, $id) {
81
        // nacti parametry
82
        $params = $this->loadDateTimeDirectionConstraints($request);
83
        $dateFrom = $params[self::DATE_FROM_PARAM];
84
        $dateTo = $params[self::DATE_TO_PARAM];
85
        $timeFrom = $params[self::TIME_FROM_PARAM];
86
        $timeTo = $params[self::TIME_TO_PARAM];
87
        $direction = $params[self::DIRECTION_PARAM];
88

    
89
        $device = Zarizeni::findByIdJoinAddress($id);
90
        if ($device != null) {
91
            $device[0]->traffic = Zaznam::averageByDevice($id, $dateFrom, $dateTo, $timeFrom, $timeTo, $direction);
92
        } else if ($device == null || count($device) == 0) {
93
            return response('Not found.', 404);
94
        }
95

    
96
        return $device;
97
    }
98

    
99
    public function getAll() {
100
        return Zarizeni::getAllJoinAddress();
101
    }
102

    
103
    public function lastDay() {
104
        return Zaznam::lastInsertedDate();
105
    }
106

    
107
    public function headerTest(Request $request) {
108
        $authHeader = $request->header("jwt");
109

    
110
        if($authHeader != null) {
111
            return $authHeader;
112
        } else {
113
            return $request->header("jwt");
114
        }
115
    }
116

    
117
    /**
118
     * Funkce nacte z requestu url parametry dateFrom, dateTo, timeFrom, timeTo, direciton a vrati je jako pole.
119
     * Defaultni hodnoty jsou null.
120
     *
121
     * @param Request $request Request ze ktere budou nacitany parametry.
122
     */
123
    private function loadDateTimeDirectionConstraints(Request $request) {
124
        $params = array();
125
        $params[self::DATE_FROM_PARAM] = null;
126
        $params[self::DATE_TO_PARAM] = null;
127
        $params[self::TIME_FROM_PARAM] = null;
128
        $params[self::TIME_TO_PARAM] = null;
129
        $params[self::DIRECTION_PARAM] = null;
130

    
131
        if ($request->has(self::DATE_FROM_PARAM)) {
132
            $params[self::DATE_FROM_PARAM] = $request->input(self::DATE_FROM_PARAM);
133
        }
134
        if ($request->has(self::DATE_TO_PARAM)) {
135
            $params[self::DATE_TO_PARAM] = $request->input(self::DATE_TO_PARAM);
136
        }
137
        if ($request->has(self::TIME_FROM_PARAM)) {
138
            $params[self::TIME_FROM_PARAM] = $request->input(self::TIME_FROM_PARAM);
139
        }
140
        if ($request->has(self::TIME_TO_PARAM)) {
141
            $params[self::TIME_TO_PARAM] = $request->input(self::TIME_TO_PARAM);
142
        }
143
        if ($request->has(self::DIRECTION_PARAM)) {
144
            $params[self::DIRECTION_PARAM] = $request->input(self::DIRECTION_PARAM);
145
        }
146

    
147
        return $params;
148
    }
149
}
(2-2/6)