|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Auth; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Validator; |
|
8
|
|
|
use App\Device; |
|
9
|
|
|
use App\Site; |
|
10
|
|
|
use App\Location; |
|
11
|
|
|
use Carbon\Carbon; |
|
12
|
|
|
|
|
13
|
|
|
class DashboardController extends Controller |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Create a new controller instance. |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->middleware('auth'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Show the application dashboard. |
|
26
|
|
|
* |
|
27
|
|
|
* @return \Illuminate\Http\Response |
|
28
|
|
|
*/ |
|
29
|
|
View Code Duplication |
public function index() |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
//Todo use the users preferred device |
|
32
|
|
|
//Get the very first device |
|
33
|
|
|
$device = Device::publicDashData()->first(); |
|
|
|
|
|
|
34
|
|
|
//Get the devices location |
|
35
|
|
|
$location = $device->location()->select('id', 'name', 'site_id')->firstOrFail(); |
|
36
|
|
|
//Get the devices site |
|
37
|
|
|
$site = $location->site()->select('id', 'name')->firstOrFail(); |
|
38
|
|
|
|
|
39
|
|
|
$data = $this->dashData($site, $location, $device); |
|
40
|
|
|
|
|
41
|
|
|
return view('dashboard.index', [ 'active_device' => $data[0], 'devices' => $data[1], 'locations' => $data[2], 'sites' => $data[3] ]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Show the development layouts for dashboard. |
|
46
|
|
|
* |
|
47
|
|
|
* @return \Illuminate\Http\Response |
|
48
|
|
|
*/ |
|
49
|
|
|
public function dev_layout() |
|
50
|
|
|
{ |
|
51
|
|
|
return view('dashboard.dev_layout'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Refresh all the data on the dashboard except the image and sensor graphs |
|
56
|
|
|
* |
|
57
|
|
|
* @param Request $request |
|
58
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
59
|
|
|
*/ |
|
60
|
|
|
public function ajaxRefreshAll(Request $request) |
|
61
|
|
|
{ |
|
62
|
|
|
Validator::make($request->all(), [ |
|
63
|
|
|
'site_id' => 'required|int|max:255', |
|
64
|
|
|
'location_id' => 'required|int|max:255', |
|
65
|
|
|
'device_id' => 'required|int|max:255', |
|
66
|
|
|
])->validate(); |
|
67
|
|
|
|
|
68
|
|
|
//Todo limit the amount of queries based on if there is changes |
|
69
|
|
|
//Check for changes in database since last update |
|
70
|
|
|
|
|
71
|
|
|
//Get the given site |
|
72
|
|
|
$site = Site::select('id', 'name')->find($request->site_id); |
|
73
|
|
|
//Get the given location |
|
74
|
|
|
$location = Location::select('id', 'name', 'site_id')->find($request->location_id); |
|
75
|
|
|
//Get the given device if it is still at the same location |
|
76
|
|
|
$device = Device::publicDashData()->where('location_id', '=', $request->location_id)->find($request->device_id); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
if (empty($site)) //Check if the site still exists |
|
79
|
|
|
{ |
|
80
|
|
|
//Get the first site and if no sites exist throw an error |
|
81
|
|
|
$site = Site::select('id', 'name')->firstOrFail(); |
|
82
|
|
|
//Get the first location from the site |
|
83
|
|
|
$location = $site->locations()->select('id', 'name', 'site_id')->firstOrFail(); |
|
84
|
|
|
//Get the first device from the location |
|
85
|
|
|
$device = $location->devices()->publicDashData()->firstOrFail(); |
|
86
|
|
|
} |
|
87
|
|
|
else if (empty($location)) //Check if the location still exists |
|
88
|
|
|
{ |
|
89
|
|
|
//Get the first location from the given site |
|
90
|
|
|
$location = $site->locations()->select('id', 'name', 'site_id')->firstOrFail(); |
|
91
|
|
|
//Get the first device from the location |
|
92
|
|
|
$device = $location->devices()->publicDashData()->firstOrFail(); |
|
93
|
|
|
} |
|
94
|
|
|
else if (empty($device)) //Check if the device still exists at the given location |
|
95
|
|
|
{ |
|
96
|
|
|
//Get the first device from the location |
|
97
|
|
|
$device = $location->devices()->publicDashData()->firstOrFail(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$data = $this->dashData($site, $location, $device); |
|
101
|
|
|
|
|
102
|
|
|
return response()->json([ 'active_device' => $data[0], 'devices' => $data[1], 'locations' => $data[2], 'sites' => $data[3] ]); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Return database data about devices, sites, and locations |
|
107
|
|
|
* |
|
108
|
|
|
* @param int $site_id |
|
109
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
110
|
|
|
*/ |
|
111
|
|
|
public function siteChange($site_id) |
|
112
|
|
|
{ |
|
113
|
|
|
//Get the given site |
|
114
|
|
|
$site = Site::select('id', 'name')->findOrFail($site_id); |
|
115
|
|
|
//Get the given location |
|
116
|
|
|
$location = Location::select('id', 'name', 'site_id')->where('site_id', '=', $site->id)->firstOrFail(); |
|
117
|
|
|
//Get the given device if it is still at the same location |
|
118
|
|
|
$device = Device::publicDashData()->where('location_id', '=', $location->id)->firstOrFail(); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
$data = $this->dashData($site, $location, $device); |
|
121
|
|
|
|
|
122
|
|
|
return response()->json([ 'active_device' => $data[0], 'devices' => $data[1], 'locations' => $data[2], 'sites' => $data[3] ]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Return database data about devices, sites, and locations |
|
127
|
|
|
* |
|
128
|
|
|
* @param int $location_id |
|
129
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
130
|
|
|
*/ |
|
131
|
|
View Code Duplication |
public function locationChange($location_id) |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
//Get the given location |
|
134
|
|
|
$location = Location::select('id', 'name', 'site_id')->findOrFail($location_id); |
|
135
|
|
|
//Get the given site |
|
136
|
|
|
$site = $location->site()->select('id', 'name')->firstOrFail(); |
|
137
|
|
|
//Get the given device if it is still at the same location |
|
138
|
|
|
$device = Device::publicDashData()->where('location_id', '=', $location_id)->firstOrFail(); |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
$data = $this->dashData($site, $location, $device); |
|
141
|
|
|
|
|
142
|
|
|
return response()->json([ 'active_device' => $data[0], 'devices' => $data[1], 'locations' => $data[2], 'sites' => $data[3] ]); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Return database data about devices, sites, and locations |
|
147
|
|
|
* |
|
148
|
|
|
* @param \Illuminate\Database\Eloquent\Model $site |
|
149
|
|
|
* @param \Illuminate\Database\Eloquent\Model $location |
|
150
|
|
|
* @param \Illuminate\Database\Eloquent\Model $device |
|
151
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
152
|
|
|
*/ |
|
153
|
|
|
public function dashData($site, $location, $device) |
|
154
|
|
|
{ |
|
155
|
|
|
//Get all the sites except for the current site ordered by name |
|
156
|
|
|
$sites = Site::select('id', 'name')->orderBy('name', 'ASC')->get()->except($site->id); |
|
157
|
|
|
|
|
158
|
|
|
//Get all the locations for the given site except for the new current location ordered by name |
|
159
|
|
|
$locations = $site->locations()->select('id', 'name', 'site_id')->orderBy('name', 'ASC')->get()->except($location->id); |
|
160
|
|
|
|
|
161
|
|
|
//Get all the devices that belong to the given location ordered by name |
|
162
|
|
|
$devices = $location->devices()->publicDashData()->orderBy('name', 'ASC')->get(); |
|
163
|
|
|
|
|
164
|
|
|
$active_device = collect([$device, $location, $site]); |
|
165
|
|
|
|
|
166
|
|
|
return collect([$active_device, $devices, $locations, $sites]); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Open or close the given device |
|
171
|
|
|
* |
|
172
|
|
|
* @param Request $request |
|
173
|
|
|
* @param Device $device |
|
174
|
|
|
* @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection |
|
175
|
|
|
*/ |
|
176
|
|
|
public function updateCommand(Request $request, Device $device) |
|
177
|
|
|
{ |
|
178
|
|
|
Validator::make($request->all(), [ |
|
179
|
|
|
'command' => 'required|string|max:1', |
|
180
|
|
|
])->validate(); |
|
181
|
|
|
|
|
182
|
|
|
//Check that device isn't currently in use or has an error |
|
183
|
|
|
if ($device->cover_status === 'opening' || $device->cover_status === 'closing' || $device->cover_status === 'error') |
|
|
|
|
|
|
184
|
|
|
return response()->json("Device is currently in use.", 403); |
|
185
|
|
|
|
|
186
|
|
|
//1 = open, 2 = close, 3 = lock |
|
187
|
|
|
switch($request->command) |
|
188
|
|
|
{ |
|
189
|
|
|
case 1: |
|
190
|
|
|
$device->cover_command = 'open'; |
|
|
|
|
|
|
191
|
|
|
break; |
|
192
|
|
|
case 2: |
|
193
|
|
|
$device->cover_command = 'close'; |
|
|
|
|
|
|
194
|
|
|
break; |
|
195
|
|
|
case 3: |
|
196
|
|
|
$device->cover_command = $this->disableCommand($device); |
|
|
|
|
|
|
197
|
|
|
break; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$device->save(); |
|
201
|
|
|
|
|
202
|
|
|
return response()->json("Success"); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Get the devices command based on the device being locked or unlocked |
|
207
|
|
|
* |
|
208
|
|
|
* @param Device $device |
|
209
|
|
|
* @return string |
|
210
|
|
|
*/ |
|
211
|
|
|
public function disableCommand(Device $device) |
|
212
|
|
|
{ |
|
213
|
|
|
//Check if the device is already locked or not |
|
214
|
|
|
if ($device->cover_command === 'lock') |
|
|
|
|
|
|
215
|
|
|
{ |
|
216
|
|
|
//Get the open, close, and current time in the users timezone |
|
217
|
|
|
$open_time = new Carbon($device->open_time, Auth::user()->timezone); |
|
|
|
|
|
|
218
|
|
|
$close_time = new Carbon($device->close_time, Auth::user()->timezone); |
|
|
|
|
|
|
219
|
|
|
$time_now = Carbon::now(Auth::user()->timezone); |
|
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
//Check if the current time is during the open schedule or not |
|
222
|
|
|
if (($time_now > $open_time) && ($time_now < $close_time)) |
|
223
|
|
|
$command = 'open'; |
|
224
|
|
|
else |
|
225
|
|
|
$command = 'close'; |
|
226
|
|
|
} |
|
227
|
|
|
else |
|
228
|
|
|
$command = 'lock'; |
|
229
|
|
|
|
|
230
|
|
|
return $command; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.