1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\EditDevice; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use App\DataTables\DevicesDataTable; |
8
|
|
|
use App\Device; |
9
|
|
|
use App\Site; |
10
|
|
|
use App\Location; |
11
|
|
|
use Charts; |
|
|
|
|
12
|
|
|
use Illuminate\Support\Facades\Auth; |
13
|
|
|
|
14
|
|
|
class DeviceController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Create a new controller instance. |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
$this->middleware('auth'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Display index page and process dataTable ajax request. |
27
|
|
|
* |
28
|
|
|
* @param \App\DataTables\DevicesDataTable $dataTable |
29
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
30
|
|
|
*/ |
31
|
|
|
public function index(DevicesDataTable $dataTable) |
32
|
|
|
{ |
33
|
|
|
$this->authorize('index', Device::class); |
34
|
|
|
|
35
|
|
|
$trashed = Device::onlyTrashed()->get(); |
36
|
|
|
return $dataTable->render('device.index', compact('trashed')); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Show the given device. |
41
|
|
|
* |
42
|
|
|
* @param string $id |
43
|
|
|
* @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
|
|
|
44
|
|
|
*/ |
45
|
|
|
public function show($id) |
46
|
|
|
{ |
47
|
|
|
$this->authorize('show', Device::class); |
48
|
|
|
|
49
|
|
|
$device = Device::findOrFail($id); |
50
|
|
|
|
51
|
|
|
$charts = [ ]; |
52
|
|
|
foreach ($device->sensors as $sensor) { |
53
|
|
|
$data = $sensor->last_month_daily_avg_data; |
54
|
|
|
$charts[ $sensor->id ] = Charts::create('line', 'highcharts') |
55
|
|
|
->title($sensor->name) |
56
|
|
|
->elementLabel($sensor->type) |
57
|
|
|
->labels($data->pluck('date')) |
58
|
|
|
->values($data->pluck('value')) |
59
|
|
|
->responsive(true); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return view('device.show', [ 'device' => $device, 'charts' => $charts ]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* View the edit device page |
67
|
|
|
* |
68
|
|
|
* @param string $id |
69
|
|
|
* @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View |
70
|
|
|
*/ |
71
|
|
|
public function edit($id) |
72
|
|
|
{ |
73
|
|
|
$this->authorize('edit', Device::class); |
74
|
|
|
|
75
|
|
|
//Get the device with the given id |
76
|
|
|
$device = Device::findOrFail($id); |
77
|
|
|
//Get the devices location |
78
|
|
|
$location = $device->location()->select('id', 'name', 'site_id')->first(); |
79
|
|
|
|
80
|
|
|
//Get the site id and location id if they exist and if not assign 0 |
81
|
|
|
$site_id = $location->site_id ?? 0; |
82
|
|
|
$location_id = $location->id ?? 0; |
83
|
|
|
|
84
|
|
|
//Get all sites with the current site first |
85
|
|
|
$sites = Site::select('id', 'name')->orderByRaw("id = ? DESC", $site_id) |
|
|
|
|
86
|
|
|
->orderBy('name', 'ASC')->get(); |
87
|
|
|
//Get all locations for the selected site with the selected location first |
88
|
|
|
$locations = Location::select('id', 'name')->where('site_id', '=', $sites[ 0 ]->id ?? 0) |
89
|
|
|
->orderByRaw("id = ? DESC", $location_id)->orderBy('name', 'ASC')->get(); |
90
|
|
|
|
91
|
|
|
return view('device.edit', [ 'device' => $device, 'locations' => $locations, 'sites' => $sites ]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Update the given device. |
96
|
|
|
* |
97
|
|
|
* @param EditDevice $request |
98
|
|
|
* @param Device $device |
99
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse |
100
|
|
|
*/ |
101
|
|
|
public function update(EditDevice $request, Device $device) |
102
|
|
|
{ |
103
|
|
|
//Update the device if the user us authorized to |
104
|
|
|
if ($request->input('name') != null) |
105
|
|
|
{ |
106
|
|
|
$this->authorize('update', Device::class); |
107
|
|
|
|
108
|
|
|
//Get the site id of the old or newly created site |
109
|
|
|
if (!empty($request->input('new_site_name'))) |
110
|
|
|
{ |
111
|
|
|
//Create a new site |
112
|
|
|
$site = Site::create([ 'name' => $request->input('new_site_name') ]); |
113
|
|
|
$site_id = $site->id; |
114
|
|
|
} |
115
|
|
|
else |
116
|
|
|
$site_id = $request->input('site_id'); |
117
|
|
|
|
118
|
|
|
//Get the location id of the old or newly created location |
119
|
|
|
if (!empty($request->input('new_location_name'))) |
120
|
|
|
{ |
121
|
|
|
//Create a new location |
122
|
|
|
$location = Location::create([ 'name' => $request->input('new_location_name'), 'site_id' => $site_id ]); |
123
|
|
|
$location_id = $location->id; |
124
|
|
|
} |
125
|
|
|
else |
126
|
|
|
$location_id = $request->input('location_id'); |
127
|
|
|
|
128
|
|
|
//Update the device |
129
|
|
|
$device->location_id = $location_id; |
130
|
|
|
|
131
|
|
|
$device->name = $request->input('name'); |
132
|
|
|
$device->open_time = $request->input('open_time'); |
133
|
|
|
$device->close_time = $request->input('close_time'); |
134
|
|
|
$device->update_rate = $request->input('update_rate'); |
135
|
|
|
$device->image_rate = $request->input('image_rate'); |
136
|
|
|
$device->sensor_rate = $request->input('sensor_rate'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
//Check if the cover_command needs to be updated |
140
|
|
|
if ($request->input('command') != null) |
141
|
|
|
{ |
142
|
|
|
$this->authorize('updateCommand', Device::class); |
143
|
|
|
//If device is currently opening, closing or in an error state don't update command |
144
|
|
|
if (!$device->isReadyForCommand()) |
145
|
|
|
return response()->json("Device is currently in use.", 503); |
146
|
|
|
|
147
|
|
|
$command = $request->input('command'); |
148
|
|
|
|
149
|
|
|
//If command is to unlock the device then check if the device should be open or closed based on the schedule |
150
|
|
|
if ($request->command === 'unlock') |
151
|
|
|
{ |
152
|
|
|
if ($device->isDuringScheduleOpen()) |
153
|
|
|
$command = 'open'; |
154
|
|
|
else |
155
|
|
|
$command = 'close'; |
156
|
|
|
} |
157
|
|
|
$device->cover_command = $command; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$device->save(); |
161
|
|
|
|
162
|
|
View Code Duplication |
if (\Request::ajax()) |
|
|
|
|
163
|
|
|
return response()->json([ 'success' => 'Device updated successfully' ]); |
164
|
|
|
else |
165
|
|
|
return redirect()->route('device.show', $device->id) |
|
|
|
|
166
|
|
|
->with('success', 'Device updated successfully'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Deletes a device. |
171
|
|
|
* |
172
|
|
|
* @param string $id |
173
|
|
|
* @return \Illuminate\Http\RedirectResponse |
174
|
|
|
*/ |
175
|
|
|
public function destroy($id) |
176
|
|
|
{ |
177
|
|
|
$this->authorize('destroy', Device::class); |
178
|
|
|
|
179
|
|
|
$device = Device::withTrashed()->findOrFail($id); |
180
|
|
|
|
181
|
|
|
if ($device->trashed()) |
182
|
|
|
{ |
183
|
|
|
//If the device was already deleted then permanently delete it |
184
|
|
|
$device->forceDelete($device->id); |
|
|
|
|
185
|
|
|
} else |
186
|
|
|
{ |
187
|
|
|
//Remove the location from the device |
188
|
|
|
$device->location_id = null; |
189
|
|
|
$device->save(); |
190
|
|
|
|
191
|
|
|
//Soft delete the device the first time |
192
|
|
|
$device->delete(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return redirect()->route('device.index') |
196
|
|
|
->with('success', 'Device deleted successfully'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Restores a device. |
201
|
|
|
* |
202
|
|
|
* @param string $id |
203
|
|
|
* @return \Illuminate\Http\RedirectResponse |
204
|
|
|
*/ |
205
|
|
View Code Duplication |
public function restore($id) |
206
|
|
|
{ |
207
|
|
|
$this->authorize('restore', Device::class); |
208
|
|
|
|
209
|
|
|
$device = Device::onlyTrashed()->findOrFail($id); |
210
|
|
|
|
211
|
|
|
$device->restore(); |
212
|
|
|
|
213
|
|
|
return redirect()->route('device.show', $device->id) |
|
|
|
|
214
|
|
|
->with('success', 'Device restored successfully'); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths